Skip to content

Commit a5802cd

Browse files
fix: consider admin location overrides during adoptions sync
1 parent f5cd561 commit a5802cd

File tree

5 files changed

+117
-74
lines changed

5 files changed

+117
-74
lines changed

src/lib/override/adopted-probes.ts

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import Bluebird from 'bluebird';
33
import _ from 'lodash';
44
import config from 'config';
55
import { scopedLogger } from '../logger.js';
6-
import type { fetchProbesWithAdminData as serverFetchProbesWithAdminData } from '../ws/server.js';
6+
import type { getProbesWithAdminData as serverGetProbesWithAdminData } from '../ws/server.js';
77
import type { Probe, ProbeLocation, Tag } from '../../probe/types.js';
88
import { normalizeCoordinate, normalizeFromPublicName } from '../geoip/utils.js';
99
import { getIndex } from '../location/location.js';
@@ -137,22 +137,22 @@ export class AdoptedProbes {
137137

138138
constructor (
139139
private readonly sql: Knex,
140-
private readonly fetchProbesWithAdminData: typeof serverFetchProbesWithAdminData,
140+
private readonly getProbesWithAdminData: typeof serverGetProbesWithAdminData,
141141
) {}
142142

143143
getByIp (ip: string) {
144144
return this.ipToAdoption.get(ip);
145145
}
146146

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

150-
if (!adoption || !adoption.isCustomCity || adoption.countryOfCustomCity !== probe.location.country) {
150+
if (!adoption || !adoption.isCustomCity || adoption.countryOfCustomCity !== location.country) {
151151
return null;
152152
}
153153

154154
return {
155-
...probe.location,
155+
...location,
156156
city: adoption.city!,
157157
normalizedCity: normalizeFromPublicName(adoption.city!),
158158
state: adoption.state,
@@ -189,7 +189,7 @@ export class AdoptedProbes {
189189
return { ...probe, owner: { id: adoption.userId } };
190190
}
191191

192-
const newLocation = this.getUpdatedLocation(probe) || probe.location;
192+
const newLocation = this.getUpdatedLocation(probe.ipAddress, probe.location) || probe.location;
193193

194194
const newTags = this.getUpdatedTags(probe);
195195

@@ -212,15 +212,13 @@ export class AdoptedProbes {
212212
}
213213

214214
async syncDashboardData () {
215-
const [ probes ] = await Promise.all([
216-
this.fetchProbesWithAdminData(),
217-
this.fetchAdoptions(),
218-
]);
215+
await this.fetchAdoptions();
219216

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

221+
const probes = this.getProbesWithAdminData();
224222
const { adoptionsWithProbe, adoptionsWithoutProbe } = this.matchAdoptionsAndProbes(probes);
225223
const { updatedAdoptions, adoptionDataUpdates } = this.generateUpdatedAdoptions(adoptionsWithProbe, adoptionsWithoutProbe);
226224
const { adoptionsToDelete, adoptionAltIpUpdates } = this.findDuplications(updatedAdoptions);

src/lib/override/probe-override.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export class ProbeOverride {
2222

2323
getUpdatedLocation (probe: Probe) {
2424
const adminLocation = this.adminData.getUpdatedLocation(probe);
25-
const adoptedLocation = this.adoptedProbes.getUpdatedLocation(probe);
25+
const adoptedLocation = this.adoptedProbes.getUpdatedLocation(probe.ipAddress, adminLocation || probe.location);
2626
return { ...probe.location, ...adminLocation, ...adoptedLocation };
2727
}
2828

src/lib/ws/server.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,7 @@ export const fetchRawSockets = async () => {
8282
return io.of(PROBES_NAMESPACE).fetchSockets();
8383
};
8484

85-
export const fetchRawProbes = async (): Promise<Probe[]> => {
86-
if (!syncedProbeList) {
87-
throw new Error('WS server not initialized yet');
88-
}
89-
90-
return syncedProbeList.getRawProbes();
91-
};
92-
93-
export const fetchProbesWithAdminData = async (): Promise<Probe[]> => {
85+
export const getProbesWithAdminData = (): Probe[] => {
9486
if (!syncedProbeList) {
9587
throw new Error('WS server not initialized yet');
9688
}
@@ -118,7 +110,7 @@ export const getProbeByIp = async (ip: string, { allowStale = true } = {}): Prom
118110
return syncedProbeList.getProbeByIp(ip);
119111
};
120112

121-
export const adoptedProbes = new AdoptedProbes(client, fetchRawProbes);
113+
export const adoptedProbes = new AdoptedProbes(client, getProbesWithAdminData);
122114

123115
export const adminData = new AdminData(client);
124116

test/tests/integration/measurement/create-measurement.test.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -845,5 +845,58 @@ describe('Create measurement', () => {
845845
});
846846
});
847847
});
848+
849+
describe('adopted probes + admin overrides', () => {
850+
before(async () => {
851+
await client(ADOPTIONS_TABLE).insert({
852+
userId: '89da69bd-a236-4ab7-9c5d-b5f52ce09959',
853+
lastSyncDate: new Date(),
854+
ip: '1.2.3.4',
855+
uuid: '1-1-1-1-1',
856+
isCustomCity: 1,
857+
tags: '[{"prefix":"jsdelivr","value":"Dashboard-Tag"}]',
858+
status: 'ready',
859+
isIPv4Supported: true,
860+
isIPv6Supported: true,
861+
version: '0.26.0',
862+
nodeVersion: 'v18.14.2',
863+
hardwareDevice: null,
864+
country: 'US',
865+
countryOfCustomCity: 'US',
866+
city: 'Oklahoma City',
867+
latitude: 35.47,
868+
longitude: -97.52,
869+
network: 'InterBS S.R.L. (BAEHOST)',
870+
asn: 61004,
871+
});
872+
873+
await client('gp_location_overrides').insert({
874+
id: 5,
875+
ip_range: '1.2.3.4/24',
876+
city: 'Paris',
877+
country: 'FR',
878+
latitude: 48.85,
879+
longitude: 2.35,
880+
});
881+
882+
await probeOverride.fetchDashboardData();
883+
await waitForProbesUpdate();
884+
});
885+
886+
after(async () => {
887+
await client(ADOPTIONS_TABLE).where({ city: 'Oklahoma City' }).delete();
888+
await client('gp_location_overrides').where({ city: 'Paris' }).delete();
889+
});
890+
891+
it.only('should ignore adopted custom city if admin data says it is another country', async () => {
892+
await requestAgent.post('/v1/measurements')
893+
.send({
894+
type: 'ping',
895+
target: 'example.com',
896+
locations: [{ city: 'Oklahoma City', limit: 2 }],
897+
})
898+
.expect(422);
899+
});
900+
});
848901
});
849902
});

0 commit comments

Comments
 (0)