Skip to content

Commit

Permalink
fix: adopted probes list should consider admin location overrides (#594)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexey-yarmosh authored Dec 23, 2024
1 parent f5cd561 commit 675e9b7
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 77 deletions.
20 changes: 9 additions & 11 deletions src/lib/override/adopted-probes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Bluebird from 'bluebird';
import _ from 'lodash';
import config from 'config';
import { scopedLogger } from '../logger.js';
import type { fetchProbesWithAdminData as serverFetchProbesWithAdminData } from '../ws/server.js';
import type { getProbesWithAdminData as serverGetProbesWithAdminData } from '../ws/server.js';
import type { Probe, ProbeLocation, Tag } from '../../probe/types.js';
import { normalizeCoordinate, normalizeFromPublicName } from '../geoip/utils.js';
import { getIndex } from '../location/location.js';
Expand Down Expand Up @@ -137,22 +137,22 @@ export class AdoptedProbes {

constructor (
private readonly sql: Knex,
private readonly fetchProbesWithAdminData: typeof serverFetchProbesWithAdminData,
private readonly getProbesWithAdminData: typeof serverGetProbesWithAdminData,
) {}

getByIp (ip: string) {
return this.ipToAdoption.get(ip);
}

getUpdatedLocation (probe: Probe): ProbeLocation | null {
const adoption = this.getByIp(probe.ipAddress);
getUpdatedLocation (ip: string, location: ProbeLocation): ProbeLocation | null {
const adoption = this.getByIp(ip);

if (!adoption || !adoption.isCustomCity || adoption.countryOfCustomCity !== probe.location.country) {
if (!adoption || !adoption.isCustomCity || adoption.countryOfCustomCity !== location.country) {
return null;
}

return {
...probe.location,
...location,
city: adoption.city!,
normalizedCity: normalizeFromPublicName(adoption.city!),
state: adoption.state,
Expand Down Expand Up @@ -189,7 +189,7 @@ export class AdoptedProbes {
return { ...probe, owner: { id: adoption.userId } };
}

const newLocation = this.getUpdatedLocation(probe) || probe.location;
const newLocation = this.getUpdatedLocation(probe.ipAddress, probe.location) || probe.location;

const newTags = this.getUpdatedTags(probe);

Expand All @@ -212,15 +212,13 @@ export class AdoptedProbes {
}

async syncDashboardData () {
const [ probes ] = await Promise.all([
this.fetchProbesWithAdminData(),
this.fetchAdoptions(),
]);
await this.fetchAdoptions();

if (process.env['SHOULD_SYNC_ADOPTIONS'] !== 'true') {
return;
}

const probes = this.getProbesWithAdminData();
const { adoptionsWithProbe, adoptionsWithoutProbe } = this.matchAdoptionsAndProbes(probes);
const { updatedAdoptions, adoptionDataUpdates } = this.generateUpdatedAdoptions(adoptionsWithProbe, adoptionsWithoutProbe);
const { adoptionsToDelete, adoptionAltIpUpdates } = this.findDuplications(updatedAdoptions);
Expand Down
2 changes: 1 addition & 1 deletion src/lib/override/probe-override.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class ProbeOverride {

getUpdatedLocation (probe: Probe) {
const adminLocation = this.adminData.getUpdatedLocation(probe);
const adoptedLocation = this.adoptedProbes.getUpdatedLocation(probe);
const adoptedLocation = this.adoptedProbes.getUpdatedLocation(probe.ipAddress, adminLocation || probe.location);
return { ...probe.location, ...adminLocation, ...adoptedLocation };
}

Expand Down
6 changes: 3 additions & 3 deletions src/lib/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ export const createServer = async (): Promise<Server> => {
await populateIpWhiteList();
// Populate cities info
await populateCitiesList();

await initWsServer();

// Populate Dashboard override data before using it during initWsServer()
await probeOverride.fetchDashboardData();
probeOverride.scheduleSync();

await initWsServer();

await auth.syncTokens();
auth.scheduleSync();

Expand Down
12 changes: 2 additions & 10 deletions src/lib/ws/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,7 @@ export const fetchRawSockets = async () => {
return io.of(PROBES_NAMESPACE).fetchSockets();
};

export const fetchRawProbes = async (): Promise<Probe[]> => {
if (!syncedProbeList) {
throw new Error('WS server not initialized yet');
}

return syncedProbeList.getRawProbes();
};

export const fetchProbesWithAdminData = async (): Promise<Probe[]> => {
export const getProbesWithAdminData = (): Probe[] => {
if (!syncedProbeList) {
throw new Error('WS server not initialized yet');
}
Expand Down Expand Up @@ -118,7 +110,7 @@ export const getProbeByIp = async (ip: string, { allowStale = true } = {}): Prom
return syncedProbeList.getProbeByIp(ip);
};

export const adoptedProbes = new AdoptedProbes(client, fetchRawProbes);
export const adoptedProbes = new AdoptedProbes(client, getProbesWithAdminData);

export const adminData = new AdminData(client);

Expand Down
53 changes: 53 additions & 0 deletions test/tests/integration/measurement/create-measurement.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -845,5 +845,58 @@ describe('Create measurement', () => {
});
});
});

describe('adopted probes + admin overrides', () => {
before(async () => {
await client(ADOPTIONS_TABLE).insert({
userId: '89da69bd-a236-4ab7-9c5d-b5f52ce09959',
lastSyncDate: new Date(),
ip: '1.2.3.4',
uuid: '1-1-1-1-1',
isCustomCity: 1,
tags: '[{"prefix":"jsdelivr","value":"Dashboard-Tag"}]',
status: 'ready',
isIPv4Supported: true,
isIPv6Supported: true,
version: '0.26.0',
nodeVersion: 'v18.14.2',
hardwareDevice: null,
country: 'US',
countryOfCustomCity: 'US',
city: 'Oklahoma City',
latitude: 35.47,
longitude: -97.52,
network: 'InterBS S.R.L. (BAEHOST)',
asn: 61004,
});

await client('gp_location_overrides').insert({
id: 5,
ip_range: '1.2.3.4/24',
city: 'Paris',
country: 'FR',
latitude: 48.85,
longitude: 2.35,
});

await probeOverride.fetchDashboardData();
await waitForProbesUpdate();
});

after(async () => {
await client(ADOPTIONS_TABLE).where({ city: 'Oklahoma City' }).delete();
await client('gp_location_overrides').where({ city: 'Paris' }).delete();
});

it('should ignore adopted custom city if admin data says it is another country', async () => {
await requestAgent.post('/v1/measurements')
.send({
type: 'ping',
target: 'example.com',
locations: [{ city: 'Oklahoma City', limit: 2 }],
})
.expect(422);
});
});
});
});
Loading

0 comments on commit 675e9b7

Please sign in to comment.