Skip to content

Commit

Permalink
feat: use getParameter to obtain ssm parameters in issue lambda
Browse files Browse the repository at this point in the history
  • Loading branch information
marnixdessing committed Mar 13, 2023
1 parent b2e1273 commit 3cb3335
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 69 deletions.
41 changes: 0 additions & 41 deletions src/app/code/AwsUtil.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/app/code/YiviApi.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { AWS } from '@gemeentenijmegen/utils';
import { aws4Interceptor } from 'aws4-axios';
import axios, { Axios } from 'axios';
import { AWS } from '@gemeentenijmegen/utils';

export class YiviApi {

Expand All @@ -27,7 +27,7 @@ export class YiviApi {
}

async init() {
if (!process.env.YIVI_API_ACCESS_KEY_ID_ARN || !process.env.YIVI_API_SECRET_KEY_ARN
if (!process.env.YIVI_API_ACCESS_KEY_ID_ARN || !process.env.YIVI_API_SECRET_KEY_ARN
|| !process.env.YIVI_API_KEY_ARN || !process.env.YIVI_API_HOST) {
throw Error('Clould not initialize YIVI API client');
}
Expand Down
12 changes: 8 additions & 4 deletions src/app/issue/BrpApi.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
import { ApiClient } from '@gemeentenijmegen/apiclient';
import { Bsn } from '@gemeentenijmegen/utils/lib/Bsn';
import { AWS, Bsn } from '@gemeentenijmegen/utils';

export class BrpApi {

private endpoint: string;
private client: ApiClient;

constructor(client: ApiClient) {
this.client = client;
this.endpoint = '';
}

async init() {
if (!process.env.BRP_API_URL) {
throw new Error('Could not initialize brp api as no endpoint is provided in BRP_API_URL');
throw new Error('Could not initialize brp api as no endpoint parameter is provided in BRP_API_URL (should be SSM parameter path)');
}
this.endpoint = process.env.BRP_API_URL;
this.client = client ? client : new ApiClient();
this.endpoint = await AWS.getParameter(process.env.BRP_API_URL);
}

/**
Expand Down
14 changes: 7 additions & 7 deletions src/app/issue/issue.lambda.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import { DynamoDBClient } from '@aws-sdk/client-dynamodb';
import { ApiClient } from '@gemeentenijmegen/apiclient';
import { Response } from '@gemeentenijmegen/apigateway-http';
import { BrpApi } from './BrpApi';
import { issueRequestHandler } from './issueRequestHandler';
import { YiviApi } from '../code/YiviApi';

const dynamoDBClient = new DynamoDBClient({});

const brpClient = new ApiClient();
const yiviApi = new YiviApi();
const brpApi = new BrpApi(brpClient);

async function init() {
console.time('init');
console.timeLog('init', 'start init');
let promiseBrp = brpClient.init();
let promiseYivi = yiviApi.init();
console.timeEnd('init');
return Promise.all([promiseBrp, promiseYivi]);
const promiseBrpClient = brpClient.init();
const promiseYiviApi = yiviApi.init();
const promiseBrpApi = brpApi.init();
return Promise.all([promiseBrpClient, promiseBrpApi, promiseYiviApi]);
}

const initPromise = init();
Expand All @@ -31,7 +31,7 @@ exports.handler = async (event: any) => {
const params = parseEvent(event);
await initPromise;

return await issueRequestHandler(params.cookies, brpClient, yiviApi, dynamoDBClient);
return await issueRequestHandler(params.cookies, brpApi, yiviApi, dynamoDBClient);

} catch (err) {
console.error(err);
Expand Down
8 changes: 3 additions & 5 deletions src/app/issue/issueRequestHandler.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { DynamoDBClient } from '@aws-sdk/client-dynamodb';
import { ApiClient } from '@gemeentenijmegen/apiclient';
import { Response } from '@gemeentenijmegen/apigateway-http';
import { Session } from '@gemeentenijmegen/session';
import { BrpApi } from './BrpApi';
Expand All @@ -10,11 +9,11 @@ import { YiviApi } from '../code/YiviApi';
/**
* Check login and handle request
*/
export async function issueRequestHandler(cookies: string, brpClient: ApiClient, yiviApi: YiviApi, dynamoDBClient: DynamoDBClient) {
export async function issueRequestHandler(cookies: string, brpApi: BrpApi, yiviApi: YiviApi, dynamoDBClient: DynamoDBClient) {
let session = new Session(cookies, dynamoDBClient);
await session.init();
if (session.isLoggedIn() == true) {
return handleLoggedinRequest(session, brpClient, yiviApi);
return handleLoggedinRequest(session, brpApi, yiviApi);
}
return Response.redirect('/login');
}
Expand All @@ -27,7 +26,7 @@ export async function issueRequestHandler(cookies: string, brpClient: ApiClient,
* @param yiviApi
* @returns
*/
async function handleLoggedinRequest(session: Session, brpClient: ApiClient, yiviApi: YiviApi) {
async function handleLoggedinRequest(session: Session, brpApi: BrpApi, yiviApi: YiviApi) {
let error = undefined;

// If issuing already is completed
Expand All @@ -40,7 +39,6 @@ async function handleLoggedinRequest(session: Session, brpClient: ApiClient, yiv
let brpData = undefined;
if (!error) {
const bsn = session.getValue('bsn');
const brpApi = new BrpApi(brpClient);
brpData = await brpApi.getBrpData(bsn);
naam = brpData?.Persoon?.Persoonsgegevens?.Naam ?? naam;
if (brpData.error) {
Expand Down
21 changes: 11 additions & 10 deletions test/app/irma.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import { YiviApi } from '../../src/app/code/YiviApi';
import axios from 'axios';
import MockAdapter from 'axios-mock-adapter';
import { AwsUtil } from '../../src/app/code/AwsUtil';
import {AWS } from '@gemeentenijmegen/utils';
import * as dotenv from 'dotenv'
dotenv.config()

const axiosMock = new MockAdapter(axios);

const getSecretMock = jest.spyOn(AwsUtil.prototype, 'getSecret')
.mockImplementation(async (arn: string) => {
return `secret-${arn}`;
});
const axiosMock = new MockAdapter(axios);
jest.mock('@gemeentenijmegen/utils/lib/AWS', () => ({
AWS: {
getParameter: jest.fn().mockImplementation((name) => `param-${name}`),
getSecret: jest.fn().mockImplementation((arn) => `secret-${arn}`),
}
}));

beforeAll(() => {
console.log = jest.fn();
Expand Down Expand Up @@ -48,13 +50,13 @@ test('Initialization', async () => {
process.env.YIVI_API_ACCESS_KEY_ID_ARN = 'key-id-arn';
process.env.YIVI_API_SECRET_KEY_ARN = 'secret-arn';
process.env.YIVI_API_KEY_ARN = 'key-arn';
process.env.YIVI_API_HOST = 'gw-test.nijmegen.nl';
process.env.YIVI_API_HOST = '/yivi/api/host';
process.env.YIVI_API_DEMO = 'demo';

const api = new YiviApi();
await api.init();
expect(api.getHost()).toBe(process.env.YIVI_API_HOST);
expect(getSecretMock).toHaveBeenCalledTimes(3);
expect(api.getHost()).toBe('param-/yivi/api/host');
expect(AWS.getSecret).toHaveBeenCalledTimes(3);
});

test('Initialization and test', async () => {
Expand All @@ -68,7 +70,6 @@ test('Initialization and test', async () => {

const api = new YiviApi();
await api.init();
expect(getSecretMock).toHaveBeenCalledTimes(3);

const yiviResp = await api.startSession(brpData);

Expand Down

0 comments on commit 3cb3335

Please sign in to comment.