Skip to content

Commit

Permalink
feat: add new admin vars (#446)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexey-yarmosh authored Nov 27, 2023
1 parent cc03c96 commit 598f921
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 10 deletions.
7 changes: 7 additions & 0 deletions src/probe/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ export const buildProbe = async (socket: Socket): Promise<Probe> => {

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

const isHardware = socket.handshake.query['isHardware'] === 'true' || socket.handshake.query['isHardware'] === '1';

const hardwareDeviceValue = socket.handshake.query['hardwareDevice'];
const hardwareDevice = (!hardwareDeviceValue || hardwareDeviceValue === 'undefined') ? null : String(hardwareDeviceValue);

const host = process.env['HOSTNAME'] ?? '';

const ip = getProbeIp(socket);
Expand Down Expand Up @@ -71,6 +76,8 @@ export const buildProbe = async (socket: Socket): Promise<Probe> => {
version,
nodeVersion,
uuid,
isHardware,
hardwareDevice,
ipAddress: ip,
host,
location,
Expand Down
4 changes: 3 additions & 1 deletion src/probe/route/get-probes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const handle = async (ctx: ParameterizedContext<DefaultState, DefaultContext & R
version: socket.data.probe.version,
nodeVersion: isAdmin ? socket.data.probe.nodeVersion : undefined,
uuid: isAdmin ? socket.data.probe.uuid : undefined,
ipAddress: isAdmin ? socket.data.probe.ipAddress : undefined,
location: {
continent: socket.data.probe.location.continent,
region: socket.data.probe.location.region,
Expand All @@ -28,8 +29,9 @@ const handle = async (ctx: ParameterizedContext<DefaultState, DefaultContext & R
network: socket.data.probe.location.network,
},
tags: socket.data.probe.tags.map(({ value }) => value),
...(isAdmin && socket.data.probe.isHardware ? { isHardware: socket.data.probe.isHardware } : null),
...(isAdmin && socket.data.probe.hardwareDevice ? { hardwareDevice: socket.data.probe.hardwareDevice } : null),
resolvers: socket.data.probe.resolvers,
ipAddress: isAdmin ? socket.data.probe.ipAddress : undefined,
host: isAdmin ? socket.data.probe.host : undefined,
stats: isAdmin ? socket.data.probe.stats : undefined,
}));
Expand Down
2 changes: 2 additions & 0 deletions src/probe/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ export type Probe = {
version: string;
nodeVersion: string;
uuid: string;
isHardware: boolean;
hardwareDevice: string | null;
ipAddress: string;
host: string;
location: ProbeLocation;
Expand Down
29 changes: 25 additions & 4 deletions test/tests/integration/probes/get-probes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import { client } from '../../../../src/lib/sql/client.js';
describe('Get Probes', () => {
let requestAgent: SuperTest<Test>;
const probes: Socket[] = [];
let addProbe: () => Promise<Socket>;
let addProbe: (events?: object, options?: object) => Promise<Socket>;

before(async () => {
addProbe = async () => {
const probe = await addFakeProbe();
addProbe = async (events = {}, options = {}) => {
const probe = await addFakeProbe(events, options);
probes.push(probe);
return probe;
};
Expand Down Expand Up @@ -187,11 +187,12 @@ describe('Get Probes', () => {
.send()
.expect(200)
.expect((response) => {
expect(response.body[0]).to.deep.include({
expect(response.body[0]).to.deep.equal({
version: '0.14.0',
host: '',
ipAddress: '1.2.3.4',
uuid: '1-1-1-1-1',
nodeVersion: 'v18.17.0',
location: {
continent: 'SA',
region: 'South America',
Expand All @@ -213,6 +214,26 @@ describe('Get Probes', () => {
});
});

it('should add hardware info if admin key is provided and there is hardware info', async () => {
nockGeoIpProviders({ ip2location: 'argentina', ipmap: 'argentina', maxmind: 'argentina', ipinfo: 'argentina', fastly: 'argentina' });

const probe = await addProbe({}, { query: { isHardware: 'true', hardwareDevice: 'v1' } });
probe.emit('probe:status:update', 'ready');

await requestAgent.get('/v1/probes?adminkey=admin')
.send()
.expect(200)
.expect((response) => {
expect(response.body[0]).to.deep.include({
isHardware: true,
hardwareDevice: 'v1',
});

expect(response.body[0].ipAddress).to.be.a('string');
expect(response).to.matchApiSchema();
});
});

describe('adopted probes', () => {
before(async () => {
await client(ADOPTED_PROBES_TABLE).insert({
Expand Down
2 changes: 2 additions & 0 deletions test/tests/unit/adopted-probes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ const defaultConnectedProbe: Probe = {
network: 'Amazon.com, Inc.',
normalizedNetwork: 'amazon.com, inc.',
},
isHardware: false,
hardwareDevice: null,
tags: [],
index: [],
client: '',
Expand Down
9 changes: 7 additions & 2 deletions test/tests/unit/ws/fetch-sockets.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { expect } from 'chai';

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

const fetchRawSockets = sinon.stub().resolves([]);
const getByIp = sinon.stub();
Expand Down Expand Up @@ -65,11 +66,13 @@ describe('fetchSockets', () => {
[ 'scaleway s.a.s.' ],
[],
],
isHardware: false,
hardwareDevice: null,
resolvers: [ 'private' ],
tags: [{ type: 'system', value: 'datacenter-network' }],
stats: { cpu: { count: 8, load: [] }, jobs: { count: 0 } },
status: 'ready',
},
} as Probe,
},
}]);

Expand Down Expand Up @@ -147,11 +150,13 @@ describe('fetchSockets', () => {
[ 'scaleway s.a.s.' ],
[],
],
isHardware: false,
hardwareDevice: null,
resolvers: [ 'private' ],
tags: [{ type: 'system', value: 'datacenter-network' }, { type: 'user', value: 'u-jimaek-my-tag-1' }],
stats: { cpu: { count: 8, load: [] }, jobs: { count: 0 } },
status: 'ready',
},
} as Probe,
},
},
]);
Expand Down
9 changes: 6 additions & 3 deletions test/utils/server.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import _ from 'lodash';
import type { Server } from 'node:http';
import type { AddressInfo } from 'node:net';
import { io, type Socket } from 'socket.io-client';
Expand All @@ -17,18 +18,20 @@ export const getTestServer = async (): Promise<Server> => {
return app;
};

export const addFakeProbe = async (events: Record<string, any> = {}): Promise<Socket> => {
export const addFakeProbe = async (events: object = {}, options: object = {}): Promise<Socket> => {
const socket = await new Promise<Socket>((resolve) => {
const client = io(url, {
const client = io(url, _.merge({
transports: [ 'websocket' ],
reconnectionDelay: 100,
reconnectionDelayMax: 500,
query: {
version: '0.14.0',
nodeVersion: 'v18.17.0',
uuid: '1-1-1-1-1',
isHardware: 'undefined',
hardwareDevice: 'undefined',
},
});
}, options));

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

0 comments on commit 598f921

Please sign in to comment.