Skip to content

Commit 809f1cb

Browse files
feat: add new admin vars
1 parent cc03c96 commit 809f1cb

File tree

7 files changed

+52
-10
lines changed

7 files changed

+52
-10
lines changed

src/probe/builder.ts

+7
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ export const buildProbe = async (socket: Socket): Promise<Probe> => {
3333

3434
const uuid = String(socket.handshake.query['uuid']);
3535

36+
const isHardware = socket.handshake.query['isHardware'] === 'true' || socket.handshake.query['isHardware'] === '1';
37+
38+
const hardwareDeviceValue = socket.handshake.query['hardwareDevice'];
39+
const hardwareDevice = (!hardwareDeviceValue || hardwareDeviceValue === 'undefined') ? null : String(hardwareDeviceValue);
40+
3641
const host = process.env['HOSTNAME'] ?? '';
3742

3843
const ip = getProbeIp(socket);
@@ -71,6 +76,8 @@ export const buildProbe = async (socket: Socket): Promise<Probe> => {
7176
version,
7277
nodeVersion,
7378
uuid,
79+
isHardware,
80+
hardwareDevice,
7481
ipAddress: ip,
7582
host,
7683
location,

src/probe/route/get-probes.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const handle = async (ctx: ParameterizedContext<DefaultState, DefaultContext & R
1616
version: socket.data.probe.version,
1717
nodeVersion: isAdmin ? socket.data.probe.nodeVersion : undefined,
1818
uuid: isAdmin ? socket.data.probe.uuid : undefined,
19+
ipAddress: isAdmin ? socket.data.probe.ipAddress : undefined,
1920
location: {
2021
continent: socket.data.probe.location.continent,
2122
region: socket.data.probe.location.region,
@@ -28,8 +29,9 @@ const handle = async (ctx: ParameterizedContext<DefaultState, DefaultContext & R
2829
network: socket.data.probe.location.network,
2930
},
3031
tags: socket.data.probe.tags.map(({ value }) => value),
32+
...(isAdmin && socket.data.probe.isHardware ? { isHardware: socket.data.probe.isHardware } : null),
33+
...(isAdmin && socket.data.probe.hardwareDevice ? { hardwareDevice: socket.data.probe.hardwareDevice } : null),
3134
resolvers: socket.data.probe.resolvers,
32-
ipAddress: isAdmin ? socket.data.probe.ipAddress : undefined,
3335
host: isAdmin ? socket.data.probe.host : undefined,
3436
stats: isAdmin ? socket.data.probe.stats : undefined,
3537
}));

src/probe/types.ts

+2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ export type Probe = {
3737
version: string;
3838
nodeVersion: string;
3939
uuid: string;
40+
isHardware: boolean;
41+
hardwareDevice: string | null;
4042
ipAddress: string;
4143
host: string;
4244
location: ProbeLocation;

test/tests/integration/probes/get-probes.test.ts

+25-4
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ import { client } from '../../../../src/lib/sql/client.js';
1111
describe('Get Probes', () => {
1212
let requestAgent: SuperTest<Test>;
1313
const probes: Socket[] = [];
14-
let addProbe: () => Promise<Socket>;
14+
let addProbe: (events?: object, options?: object) => Promise<Socket>;
1515

1616
before(async () => {
17-
addProbe = async () => {
18-
const probe = await addFakeProbe();
17+
addProbe = async (events = {}, options = {}) => {
18+
const probe = await addFakeProbe(events, options);
1919
probes.push(probe);
2020
return probe;
2121
};
@@ -187,11 +187,12 @@ describe('Get Probes', () => {
187187
.send()
188188
.expect(200)
189189
.expect((response) => {
190-
expect(response.body[0]).to.deep.include({
190+
expect(response.body[0]).to.deep.equal({
191191
version: '0.14.0',
192192
host: '',
193193
ipAddress: '1.2.3.4',
194194
uuid: '1-1-1-1-1',
195+
nodeVersion: 'v18.17.0',
195196
location: {
196197
continent: 'SA',
197198
region: 'South America',
@@ -213,6 +214,26 @@ describe('Get Probes', () => {
213214
});
214215
});
215216

217+
it('should add hardware info if admin key is provided and there is hardware info', async () => {
218+
nockGeoIpProviders({ ip2location: 'argentina', ipmap: 'argentina', maxmind: 'argentina', ipinfo: 'argentina', fastly: 'argentina' });
219+
220+
const probe = await addProbe({}, { query: { isHardware: 'true', hardwareDevice: 'v1' } });
221+
probe.emit('probe:status:update', 'ready');
222+
223+
await requestAgent.get('/v1/probes?adminkey=admin')
224+
.send()
225+
.expect(200)
226+
.expect((response) => {
227+
expect(response.body[0]).to.deep.include({
228+
isHardware: true,
229+
hardwareDevice: 'v1',
230+
});
231+
232+
expect(response.body[0].ipAddress).to.be.a('string');
233+
expect(response).to.matchApiSchema();
234+
});
235+
});
236+
216237
describe('adopted probes', () => {
217238
before(async () => {
218239
await client(ADOPTED_PROBES_TABLE).insert({

test/tests/unit/adopted-probes.test.ts

+2
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ const defaultConnectedProbe: Probe = {
4141
network: 'Amazon.com, Inc.',
4242
normalizedNetwork: 'amazon.com, inc.',
4343
},
44+
isHardware: false,
45+
hardwareDevice: null,
4446
tags: [],
4547
index: [],
4648
client: '',

test/tests/unit/ws/fetch-sockets.test.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { expect } from 'chai';
44

55
import type { LRUOptions } from '../../../../src/lib/ws/helper/throttle.js';
66
import type { RemoteProbeSocket } from '../../../../src/lib/ws/server.js';
7+
import type { Probe } from '../../../../src/probe/types.js';
78

89
const fetchRawSockets = sinon.stub().resolves([]);
910
const getByIp = sinon.stub();
@@ -65,11 +66,13 @@ describe('fetchSockets', () => {
6566
[ 'scaleway s.a.s.' ],
6667
[],
6768
],
69+
isHardware: false,
70+
hardwareDevice: null,
6871
resolvers: [ 'private' ],
6972
tags: [{ type: 'system', value: 'datacenter-network' }],
7073
stats: { cpu: { count: 8, load: [] }, jobs: { count: 0 } },
7174
status: 'ready',
72-
},
75+
} as Probe,
7376
},
7477
}]);
7578

@@ -147,11 +150,13 @@ describe('fetchSockets', () => {
147150
[ 'scaleway s.a.s.' ],
148151
[],
149152
],
153+
isHardware: false,
154+
hardwareDevice: null,
150155
resolvers: [ 'private' ],
151156
tags: [{ type: 'system', value: 'datacenter-network' }, { type: 'user', value: 'u-jimaek-my-tag-1' }],
152157
stats: { cpu: { count: 8, load: [] }, jobs: { count: 0 } },
153158
status: 'ready',
154-
},
159+
} as Probe,
155160
},
156161
},
157162
]);

test/utils/server.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import _ from 'lodash';
12
import type { Server } from 'node:http';
23
import type { AddressInfo } from 'node:net';
34
import { io, type Socket } from 'socket.io-client';
@@ -17,18 +18,20 @@ export const getTestServer = async (): Promise<Server> => {
1718
return app;
1819
};
1920

20-
export const addFakeProbe = async (events: Record<string, any> = {}): Promise<Socket> => {
21+
export const addFakeProbe = async (events: object = {}, options: object = {}): Promise<Socket> => {
2122
const socket = await new Promise<Socket>((resolve) => {
22-
const client = io(url, {
23+
const client = io(url, _.merge({
2324
transports: [ 'websocket' ],
2425
reconnectionDelay: 100,
2526
reconnectionDelayMax: 500,
2627
query: {
2728
version: '0.14.0',
2829
nodeVersion: 'v18.17.0',
2930
uuid: '1-1-1-1-1',
31+
isHardware: 'undefined',
32+
hardwareDevice: 'undefined',
3033
},
31-
});
34+
}, options));
3235

3336
for (const [ event, listener ] of Object.entries(events)) {
3437
client.on(event, listener);

0 commit comments

Comments
 (0)