Skip to content

Commit

Permalink
fix(awala): Send bundle request to background queue, not endpoint (#233)
Browse files Browse the repository at this point in the history
Also pass public key id as CloudEvent data instead of id.
  • Loading branch information
gnarea authored Oct 12, 2023
1 parent 2f2b99c commit 9d356bf
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 37 deletions.
2 changes: 2 additions & 0 deletions k8s/awala-service.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ spec:

- name: CE_CHANNEL_AWALA_OUTGOING_MESSAGES
value: http://mock-awala-middleware.default
- name: CE_CHANNEL_BACKGROUND_QUEUE
value: http://veraid-authority-queue.default
envFrom:
- configMapRef:
name: mongodb
Expand Down
21 changes: 4 additions & 17 deletions src/backgroundQueue/sinks/memberBundleRequest.sink.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,11 @@ describe('memberBundleIssuance', () => {
let dbConnection: Connection;
let memberBundleRequestModel: ReturnModelType<typeof MemberBundleRequestModelSchema>;
const triggerEvent = new CloudEvent<MemberBundleRequestPayload>({
id: MEMBER_PUBLIC_KEY_MONGO_ID,
source: CE_SOURCE,
type: BUNDLE_REQUEST_TYPE,
subject: AWALA_PEER_ID,
datacontenttype: 'text/plain',
data: MEMBER_PUBLIC_KEY_MONGO_ID,
});

beforeEach(() => {
Expand All @@ -78,7 +79,7 @@ describe('memberBundleIssuance', () => {
await postEvent(triggerEvent, server);

expect(logs).toContainEqual(
partialPinoLog('debug', 'Starting member bundle request trigger', {
partialPinoLog('debug', 'Processing member bundle request', {
publicKeyId: MEMBER_PUBLIC_KEY_MONGO_ID,
}),
);
Expand Down Expand Up @@ -126,16 +127,6 @@ describe('memberBundleIssuance', () => {
);
});

test('Id should be member public key id', async () => {
await postEvent(triggerEvent, server);

expect(getEvents(EmitterChannel.AWALA_OUTGOING_MESSAGES)).toContainEqual(
expect.objectContaining({
id: MEMBER_PUBLIC_KEY_MONGO_ID,
}),
);
});

test('Source should be URL identifying Awala Internet Endpoint', async () => {
await postEvent(triggerEvent, server);

Expand Down Expand Up @@ -233,11 +224,7 @@ describe('memberBundleIssuance', () => {
});

test('Event with missing subject should be refused', async () => {
const invalidTriggerEvent = new CloudEvent<MemberBundleRequestPayload>({
id: MEMBER_PUBLIC_KEY_MONGO_ID,
source: CE_SOURCE,
type: BUNDLE_REQUEST_TYPE,
});
const invalidTriggerEvent = triggerEvent.cloneWith({ subject: undefined });

await postEvent(invalidTriggerEvent, server);

Expand Down
7 changes: 3 additions & 4 deletions src/backgroundQueue/sinks/memberBundleRequest.sink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ import { Emitter } from '../../utilities/eventing/Emitter.js';
import { EmitterChannel } from '../../utilities/eventing/EmitterChannel.js';

export default async function memberBundleIssuance(
event: CloudEventV1<unknown>,
event: CloudEventV1<Buffer>,
options: ServiceOptions,
): Promise<void> {
const publicKeyId = event.id;
const publicKeyId = event.data!.toString();
const keyAwareLogger = options.logger.child({ publicKeyId });

keyAwareLogger.debug('Starting member bundle request trigger');
keyAwareLogger.debug('Processing member bundle request');

if (event.subject === undefined) {
keyAwareLogger.info('Refusing member bundle request with missing subject');
Expand All @@ -34,7 +34,6 @@ export default async function memberBundleIssuance(

const now = new Date();
const message = makeOutgoingServiceMessageEvent({
publicKeyId,
peerId: event.subject,
contentType: VeraidContentType.MEMBER_BUNDLE,
content: Buffer.from(memberBundle.result),
Expand Down
2 changes: 1 addition & 1 deletion src/events/bundleRequest.event.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export const BUNDLE_REQUEST_TYPE = 'net.veraid.authority.member-bundle-request';

export type MemberBundleRequestPayload = '';
export type MemberBundleRequestPayload = string;
7 changes: 2 additions & 5 deletions src/events/outgoingServiceMessage.event.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { randomUUID } from 'node:crypto';

import { addMinutes } from 'date-fns';

import { AWALA_PEER_ID } from '../testUtils/stubs.js';
Expand All @@ -17,7 +15,6 @@ describe('makeIncomingServiceMessageEvent', () => {
const options: OutgoingServiceMessageOptions = {
creationDate: new Date(),
expiryDate: addMinutes(new Date(), 5),
publicKeyId: randomUUID(),
contentType: CE_SERVICE_MESSAGE_CONTENT_TYPE,
content: CE_SERVICE_MESSAGE_CONTENT,
peerId: AWALA_PEER_ID,
Expand All @@ -29,10 +26,10 @@ describe('makeIncomingServiceMessageEvent', () => {
expect(version).toBe('1.0');
});

test('Event id should be the parcel id', () => {
test('Event id should be auto-generated', () => {
const { id } = makeOutgoingServiceMessageEvent(options);

expect(id).toBe(options.publicKeyId);
expect(id).toBeString();
});

test('Event type should be outgoing-service-message', () => {
Expand Down
2 changes: 0 additions & 2 deletions src/events/outgoingServiceMessage.event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ export const OUTGOING_MESSAGE_SOURCE = 'https://relaycorp.tech/awala-endpoint-in
export interface OutgoingServiceMessageOptions {
readonly creationDate: Date;
readonly expiryDate: Date;
readonly publicKeyId: string;
readonly peerId: string;
readonly contentType: string;
readonly content: Buffer;
Expand All @@ -20,7 +19,6 @@ export function makeOutgoingServiceMessageEvent(
return new CloudEvent({
specversion: '1.0',
type: OUTGOING_SERVICE_MESSAGE_TYPE,
id: options.publicKeyId,
source: OUTGOING_MESSAGE_SOURCE,
subject: options.peerId,
datacontenttype: options.contentType,
Expand Down
7 changes: 3 additions & 4 deletions src/memberKeyImportToken.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,14 +160,13 @@ describe('member key import token', () => {
);

requireSuccessfulResult(result);
expect(getEvents(EmitterChannel.AWALA_OUTGOING_MESSAGES)).toContainEqual(
expect(getEvents(EmitterChannel.BACKGROUND_QUEUE)).toContainEqual(
expect.objectContaining<Partial<CloudEvent<string>>>({
id: MEMBER_PUBLIC_KEY_MONGO_ID,
source: 'https://veraid.net/authority/awala-member-key-import',
type: BUNDLE_REQUEST_TYPE,
subject: AWALA_PEER_ID,
datacontenttype: 'application/vnd.veraid-authority.member-public-key-import',
data: '',
datacontenttype: 'text/plain',
data: MEMBER_PUBLIC_KEY_MONGO_ID,
}),
);
});
Expand Down
7 changes: 3 additions & 4 deletions src/memberKeyImportToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,13 @@ export async function processMemberKeyImportToken(
}

const event = new CloudEvent<string>({
id: publicKeyCreationResult.result.id,
source: 'https://veraid.net/authority/awala-member-key-import',
type: BUNDLE_REQUEST_TYPE,
subject: peerId,
datacontenttype: 'application/vnd.veraid-authority.member-public-key-import',
data: '',
datacontenttype: 'text/plain',
data: publicKeyCreationResult.result.id,
});
const ceEmitter = await Emitter.init(EmitterChannel.AWALA_OUTGOING_MESSAGES);
const ceEmitter = await Emitter.init(EmitterChannel.BACKGROUND_QUEUE);
await ceEmitter.emit(event);

await memberKeyImportTokenModel.findByIdAndDelete(keyImportRequest.publicKeyImportToken);
Expand Down

0 comments on commit 9d356bf

Please sign in to comment.