|
| 1 | +/* eslint-disable @typescript-eslint/no-empty-function */ |
| 2 | + |
| 3 | +import test from 'ava' |
| 4 | +import { mockClient } from 'aws-sdk-client-mock' |
| 5 | +import { |
| 6 | + SecretsManagerClient, |
| 7 | + GetSecretValueCommand, |
| 8 | +} from '@aws-sdk/client-secrets-manager' |
| 9 | +import { handler, apiKeys } from '../../src/lambdas/pre-hook/index.js' |
| 10 | + |
| 11 | +const DEFAULT_EVENT = { |
| 12 | + body: 'eyJ0ZXN0IjoiYm9keSJ9', |
| 13 | + resource: '/{proxy+}', |
| 14 | + path: '/path/to/resource', |
| 15 | + httpMethod: 'POST', |
| 16 | + isBase64Encoded: true, |
| 17 | + queryStringParameters: { |
| 18 | + foo: 'bar', |
| 19 | + }, |
| 20 | + pathParameters: { |
| 21 | + proxy: '/path/to/resource', |
| 22 | + }, |
| 23 | + headers: {}, |
| 24 | + multiValueHeaders: {}, |
| 25 | + multiValueQueryStringParameters: null, |
| 26 | + stageVariables: null, |
| 27 | + requestContext: { |
| 28 | + authorizer: null, |
| 29 | + accountId: '123456789012', |
| 30 | + resourceId: '123456', |
| 31 | + stage: 'prod', |
| 32 | + requestId: 'c6af9ac6-7b61-11e6-9a41-93e8deadbeef', |
| 33 | + requestTime: '09/Apr/2015:12:34:56 +0000', |
| 34 | + requestTimeEpoch: 1428582896000, |
| 35 | + identity: { |
| 36 | + cognitoIdentityPoolId: null, |
| 37 | + accountId: null, |
| 38 | + cognitoIdentityId: null, |
| 39 | + caller: null, |
| 40 | + accessKey: null, |
| 41 | + sourceIp: '127.0.0.1', |
| 42 | + cognitoAuthenticationType: null, |
| 43 | + cognitoAuthenticationProvider: null, |
| 44 | + userArn: null, |
| 45 | + userAgent: 'Custom User Agent String', |
| 46 | + user: null, |
| 47 | + apiKey: null, |
| 48 | + apiKeyId: null, |
| 49 | + clientCert: null, |
| 50 | + principalOrgId: null, |
| 51 | + }, |
| 52 | + path: '/prod/path/to/resource', |
| 53 | + resourcePath: '/{proxy+}', |
| 54 | + httpMethod: 'POST', |
| 55 | + apiId: '1234567890', |
| 56 | + protocol: 'HTTP/1.1', |
| 57 | + }, |
| 58 | +} |
| 59 | +const DEFAULT_CONTEXT = { |
| 60 | + callbackWaitsForEmptyEventLoop: false, |
| 61 | + functionName: '', |
| 62 | + functionVersion: '', |
| 63 | + invokedFunctionArn: '', |
| 64 | + memoryLimitInMB: '', |
| 65 | + awsRequestId: '', |
| 66 | + logGroupName: '', |
| 67 | + logStreamName: '', |
| 68 | + getRemainingTimeInMillis: () => 0, |
| 69 | + done: () => {}, |
| 70 | + fail: () => {}, |
| 71 | + succeed: () => {}, |
| 72 | +} |
| 73 | + |
| 74 | +// @ts-ignore |
| 75 | +const secretsManagerMock = mockClient(SecretsManagerClient) |
| 76 | + |
| 77 | +const response401 = { |
| 78 | + statusCode: 401, |
| 79 | + body: '', |
| 80 | + headers: { 'access-control-allow-origin': '*' }, |
| 81 | +} |
| 82 | + |
| 83 | +test.beforeEach(() => { |
| 84 | + secretsManagerMock.reset() |
| 85 | + apiKeys.clear() |
| 86 | +}) |
| 87 | + |
| 88 | +test.serial('authenticate cases', async (t) => { |
| 89 | + secretsManagerMock |
| 90 | + // @ts-ignore |
| 91 | + .on(GetSecretValueCommand) |
| 92 | + // @ts-ignore |
| 93 | + .resolves({ SecretString: JSON.stringify({ ABC: 'read', DEF: 'other' }) }) |
| 94 | + |
| 95 | + const event = { ...DEFAULT_EVENT } |
| 96 | + const context = { ...DEFAULT_CONTEXT } |
| 97 | + |
| 98 | + // no credentials |
| 99 | + t.deepEqual(await handler(event, context), response401) |
| 100 | + |
| 101 | + // invalid credentials |
| 102 | + event.headers['Authorization'] = 'Bearer invalid' |
| 103 | + t.deepEqual(await handler(event, context), response401) |
| 104 | + |
| 105 | + // valid credentials |
| 106 | + event.headers['Authorization'] = 'Bearer ABC' |
| 107 | + t.deepEqual(await handler(event, context), event) |
| 108 | + |
| 109 | + delete event.headers['Authorization'] |
| 110 | + |
| 111 | + // credentials don't have read permissions |
| 112 | + event.headers['Authorization'] = 'Bearer DEF' |
| 113 | + t.deepEqual(await handler(event, context), response401) |
| 114 | + |
| 115 | + delete event.headers['Authorization'] |
| 116 | + |
| 117 | + // invalid credentials |
| 118 | + event.queryStringParameters['auth_token'] = 'invalid' |
| 119 | + t.deepEqual(await handler(event, context), response401) |
| 120 | + |
| 121 | + // valid credentials |
| 122 | + event.queryStringParameters['auth_token'] = 'ABC' |
| 123 | + t.deepEqual(await handler(event, context), event) |
| 124 | +}) |
| 125 | + |
| 126 | +test.serial('authenticate failure with retrieving keys', async (t) => { |
| 127 | + // @ts-ignore |
| 128 | + secretsManagerMock.on(GetSecretValueCommand).rejectsOnce('mocked rejection') |
| 129 | + |
| 130 | + const event = { ...DEFAULT_EVENT } |
| 131 | + const context = { ...DEFAULT_CONTEXT } |
| 132 | + |
| 133 | + t.deepEqual(await handler(event, context), response401) |
| 134 | +}) |
0 commit comments