Skip to content

Commit

Permalink
implement conn params
Browse files Browse the repository at this point in the history
  • Loading branch information
gnarea committed Jan 18, 2024
1 parent 9782303 commit 6460c73
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 15 deletions.
3 changes: 3 additions & 0 deletions src/services/poweb/PowebRouteOptions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface PowebRouteOptions {
readonly internetAddress: string;
}
1 change: 0 additions & 1 deletion src/services/poweb/RouteOptions.ts

This file was deleted.

35 changes: 35 additions & 0 deletions src/services/poweb/connectionParams.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { NodeConnectionParams } from '@relaycorp/relaynet-core';

import { testDisallowedMethods } from '../../testUtils/fastify';
import { makePoWebTestServer } from './_test_utils';
import { CONTENT_TYPES } from './contentTypes';
import { InternetGatewayManager } from '../../node/InternetGatewayManager';
import { GATEWAY_INTERNET_ADDRESS } from '../../testUtils/awala';

jest.mock('../../utilities/exitHandling');

const ENDPOINT_URL = '/connection-params.der';

describe('connectionParams', () => {
const getFixtures = makePoWebTestServer();

testDisallowedMethods(['HEAD', 'GET'], ENDPOINT_URL, async () => getFixtures().server);

test('Should respond with connection parameters', async () => {
const { server, dbConnection } = getFixtures();
const gatewayManager = await InternetGatewayManager.init(dbConnection);
const gateway = await gatewayManager.getCurrent();
await gateway.makeInitialSessionKeyIfMissing();

const response = await server.inject({ method: 'GET', url: ENDPOINT_URL });

expect(response).toHaveProperty('statusCode', 200);
expect(response).toHaveProperty('headers.content-type', CONTENT_TYPES.CONNECTION_PARAMS);
const expectedParameters = new NodeConnectionParams(
GATEWAY_INTERNET_ADDRESS,
gateway.identityKeyPair.publicKey,
(await gateway.keyStores.privateKeyStore.retrieveUnboundSessionPublicKey(gateway.id))!,
);
expect(response.rawPayload).toMatchObject(Buffer.from(await expectedParameters.serialize()));
});
});
32 changes: 32 additions & 0 deletions src/services/poweb/connectionParams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { NodeConnectionParams } from '@relaycorp/relaynet-core';
import { FastifyInstance } from 'fastify';

import { registerDisallowedMethods } from '../../utilities/fastify/server';
import { InternetGatewayManager } from '../../node/InternetGatewayManager';
import type { PowebRouteOptions } from './PowebRouteOptions';
import { CONTENT_TYPES } from './contentTypes';

const ENDPOINT_URL = '/connection-params.der';

export default async function registerRoutes(
fastify: FastifyInstance,
options: PowebRouteOptions,
): Promise<void> {
registerDisallowedMethods(['HEAD', 'GET'], ENDPOINT_URL, fastify);

fastify.route({
method: ['HEAD', 'GET'],
url: ENDPOINT_URL,
async handler(_req, reply): Promise<void> {
const gatewayManager = await InternetGatewayManager.init(fastify.mongoose);
const gateway = await gatewayManager.getCurrent();
const params = new NodeConnectionParams(
options.internetAddress,
gateway.identityKeyPair.publicKey,
(await gateway.keyStores.privateKeyStore.retrieveUnboundSessionPublicKey(gateway.id))!,
);
const paramsSerialised = await params.serialize();
reply.type(CONTENT_TYPES.CONNECTION_PARAMS).send(Buffer.from(paramsSerialised));
},
});
}
1 change: 1 addition & 0 deletions src/services/poweb/contentTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ export const CONTENT_TYPES = {
REQUEST: 'application/vnd.awala.node-registration.request',
},
PARCEL: 'application/vnd.awala.parcel',
CONNECTION_PARAMS: 'application/vnd.etsi.tsl.der',
};
6 changes: 1 addition & 5 deletions src/services/poweb/parcelDelivery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,12 @@ import { ParcelStore } from '../../parcelStore';
import { retrieveOwnCertificates } from '../../pki';
import { registerDisallowedMethods } from '../../utilities/fastify/server';
import { CONTENT_TYPES } from './contentTypes';
import RouteOptions from './RouteOptions';
import { RedisPubSubClient } from '../../backingServices/RedisPubSubClient';
import { QueueEmitter } from '../../utilities/backgroundQueue/QueueEmitter';

const ENDPOINT_URL = '/v1/parcels';

export default async function registerRoutes(
fastify: FastifyInstance,
_options: RouteOptions,
): Promise<void> {
export default async function registerRoutes(fastify: FastifyInstance): Promise<void> {
const parcelStore = ParcelStore.initFromEnv();

registerDisallowedMethods(['POST'], ENDPOINT_URL, fastify);
Expand Down
11 changes: 6 additions & 5 deletions src/services/poweb/registration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
} from '@relaycorp/relaynet-core';
import { MongoCertificateStore } from '@relaycorp/awala-keystore-mongodb';
import bufferToArray from 'buffer-to-arraybuffer';
import { get as getEnvVar } from 'env-var';
import { FastifyInstance, FastifyReply } from 'fastify';

import { initPrivateKeyStore } from '../../backingServices/keystore';
Expand All @@ -16,10 +15,14 @@ import { Config, ConfigKey } from '../../utilities/config';
import { sha256 } from '../../utilities/crypto';
import { registerDisallowedMethods } from '../../utilities/fastify/server';
import { CONTENT_TYPES } from './contentTypes';
import { PowebRouteOptions } from './PowebRouteOptions';

const ENDPOINT_URL = '/v1/nodes';

export default async function registerRoutes(fastify: FastifyInstance): Promise<void> {
export default async function registerRoutes(
fastify: FastifyInstance,
options: PowebRouteOptions,
): Promise<void> {
registerDisallowedMethods(['POST'], ENDPOINT_URL, fastify);

fastify.addContentTypeParser(
Expand All @@ -30,8 +33,6 @@ export default async function registerRoutes(fastify: FastifyInstance): Promise<

const privateKeyStore = initPrivateKeyStore((fastify as any).mongoose);

const internetAddress = getEnvVar('INTERNET_ADDRESS').required().asString();

fastify.route<{ readonly Body: Buffer }>({
method: ['POST'],
url: ENDPOINT_URL,
Expand Down Expand Up @@ -102,7 +103,7 @@ export default async function registerRoutes(fastify: FastifyInstance): Promise<
const registration = new PrivateNodeRegistration(
privateGatewayCertificate,
internetGatewayCertPath!.leafCertificate,
internetAddress,
options.internetAddress,
sessionKeyPair.sessionKey,
);
return reply
Expand Down
10 changes: 9 additions & 1 deletion src/services/poweb/server.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import envVar from 'env-var';
import pino from 'pino';

import { mockSpy } from '../../testUtils/jest';
Expand All @@ -7,7 +8,7 @@ import { makeServer } from './server';

jest.mock('../../utilities/exitHandling');

makePoWebTestServer();
const getFixtures = makePoWebTestServer();

const mockFastifyInstance = { close: jest.fn() };
const mockConfigureFastify = mockSpy(
Expand All @@ -30,6 +31,13 @@ describe('makeServer', () => {
expect(mockConfigureFastify).toBeCalledWith(expect.anything(), expect.anything(), logger);
});

test('Env var INTERNET_ADDRESS should be defined', async () => {
const { envVarMocker } = getFixtures();
envVarMocker({});

await expect(makeServer()).rejects.toThrowWithMessage(envVar.EnvVarError, /INTERNET_ADDRESS/);
});

test('Fastify instance should be returned', async () => {
await expect(makeServer()).resolves.toEqual(mockFastifyInstance);
});
Expand Down
10 changes: 7 additions & 3 deletions src/services/poweb/server.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import { get as getEnvVar } from 'env-var';
import { FastifyInstance, FastifyPluginCallback } from 'fastify';
import type { BaseLogger } from 'pino';

import { configureFastify } from '../../utilities/fastify/server';
import type { PowebRouteOptions } from './PowebRouteOptions';
import healthcheck from './healthcheck';
import connectionParams from './connectionParams';
import parcelCollection from './parcelCollection';
import parcelDelivery from './parcelDelivery';
import preRegistrationRoutes from './preRegistration';
import registrationRoutes from './registration';
import RouteOptions from './RouteOptions';

const ROUTES: ReadonlyArray<FastifyPluginCallback<RouteOptions>> = [
const ROUTES: ReadonlyArray<FastifyPluginCallback<PowebRouteOptions>> = [
healthcheck,
connectionParams,
parcelCollection,
parcelDelivery,
preRegistrationRoutes,
Expand All @@ -23,5 +26,6 @@ const ROUTES: ReadonlyArray<FastifyPluginCallback<RouteOptions>> = [
* This function doesn't call .listen() so we can use .inject() for testing purposes.
*/
export async function makeServer(logger?: BaseLogger): Promise<FastifyInstance> {
return configureFastify(ROUTES, {}, logger);
const internetAddress = getEnvVar('INTERNET_ADDRESS').required().asString();
return configureFastify(ROUTES, { internetAddress }, logger);
}

0 comments on commit 6460c73

Please sign in to comment.