|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +import { describe, it, beforeEach } from 'mocha'; |
| 5 | +import * as proxyquire from 'proxyquire'; |
| 6 | +import * as sinon from 'sinon'; |
| 7 | +import expect from 'expect'; |
| 8 | +import { AwsAuthenticator } from '../../../../../src/exporter/otlp/aws/common/aws-authenticator'; |
| 9 | + |
| 10 | +const PATH = '../../../../../src/exporter/otlp/aws/common/aws-authenticator'; |
| 11 | +const SIGNATURE_V4_MODULE = '@smithy/signature-v4'; |
| 12 | +const CREDENTIAL_PROVIDER_MODULE = '@aws-sdk/credential-provider-node'; |
| 13 | +const SHA_256_MODULE = '@aws-crypto/sha256-js'; |
| 14 | +const AWS_HTTP_MODULE = '@smithy/protocol-http'; |
| 15 | + |
| 16 | +const AWS_OTLP_TRACES_ENDPOINT = 'https://xray.us-east-1.amazonaws.com/v1/traces'; |
| 17 | + |
| 18 | +const AUTHORIZATION_HEADER = 'authorization'; |
| 19 | +const X_AMZ_DATE_HEADER = 'x-amz-date'; |
| 20 | +const X_AMZ_SECURITY_TOKEN_HEADER = 'x-amz-security-token'; |
| 21 | + |
| 22 | +const mockCredentials = { |
| 23 | + accessKeyId: 'test_access_key', |
| 24 | + secretAccessKey: 'test_secret_key', |
| 25 | + sessionToken: 'test_session_token', |
| 26 | +}; |
| 27 | + |
| 28 | +describe('AwsAuthenticator', () => { |
| 29 | + let sandbox: sinon.SinonSandbox; |
| 30 | + |
| 31 | + beforeEach(() => { |
| 32 | + sandbox = sinon.createSandbox(); |
| 33 | + }); |
| 34 | + |
| 35 | + afterEach(() => { |
| 36 | + sandbox.restore(); |
| 37 | + }); |
| 38 | + |
| 39 | + describe('should not inject SigV4 Headers if required modules are not available', async () => { |
| 40 | + const dependencies = [SIGNATURE_V4_MODULE, CREDENTIAL_PROVIDER_MODULE, SHA_256_MODULE, AWS_HTTP_MODULE]; |
| 41 | + |
| 42 | + dependencies.forEach(dependency => { |
| 43 | + it(`should not sign headers if missing dependency: ${dependency}`, async () => { |
| 44 | + Object.keys(require.cache).forEach(key => { |
| 45 | + delete require.cache[key]; |
| 46 | + }); |
| 47 | + |
| 48 | + const requireStub = sandbox.stub(require('module'), '_load'); |
| 49 | + requireStub.withArgs(dependency).throws(new Error(`Cannot find module '${dependency}'`)); |
| 50 | + requireStub.callThrough(); |
| 51 | + |
| 52 | + const { AwsAuthenticator: MockThrowableModuleAuthenticator } = require(PATH); |
| 53 | + |
| 54 | + const result = await new MockThrowableModuleAuthenticator('us-east-1', 'xray').authenticate( |
| 55 | + AWS_OTLP_TRACES_ENDPOINT, |
| 56 | + {}, |
| 57 | + new Uint8Array() |
| 58 | + ); |
| 59 | + |
| 60 | + expect(result).not.toHaveProperty(AUTHORIZATION_HEADER); |
| 61 | + expect(result).not.toHaveProperty(X_AMZ_DATE_HEADER); |
| 62 | + expect(result).not.toHaveProperty(X_AMZ_SECURITY_TOKEN_HEADER); |
| 63 | + }); |
| 64 | + }); |
| 65 | + }); |
| 66 | + |
| 67 | + it('should not inject SigV4 Headers if serialized data is undefined', async () => { |
| 68 | + const authenticator = new AwsAuthenticator('us-east-1', 'xray'); |
| 69 | + const result = await authenticator.authenticate(AWS_OTLP_TRACES_ENDPOINT, {}, undefined); |
| 70 | + |
| 71 | + expect(result).not.toHaveProperty(AUTHORIZATION_HEADER); |
| 72 | + expect(result).not.toHaveProperty(X_AMZ_DATE_HEADER); |
| 73 | + expect(result).not.toHaveProperty(X_AMZ_SECURITY_TOKEN_HEADER); |
| 74 | + }); |
| 75 | + |
| 76 | + it('should inject SigV4 Headers', async () => { |
| 77 | + const expected = { |
| 78 | + [AUTHORIZATION_HEADER]: 'testAuth', |
| 79 | + [X_AMZ_DATE_HEADER]: 'testDate', |
| 80 | + [X_AMZ_SECURITY_TOKEN_HEADER]: 'testSecurityToken', |
| 81 | + }; |
| 82 | + |
| 83 | + const AwsAuthenticatorWithMock = proxyquire(PATH, { |
| 84 | + [CREDENTIAL_PROVIDER_MODULE]: { |
| 85 | + defaultProvider: () => Promise.resolve(mockCredentials), |
| 86 | + }, |
| 87 | + [SIGNATURE_V4_MODULE]: { |
| 88 | + SignatureV4: class { |
| 89 | + constructor() {} |
| 90 | + sign(request: any) { |
| 91 | + return Promise.resolve({ |
| 92 | + headers: expected, |
| 93 | + }); |
| 94 | + } |
| 95 | + }, |
| 96 | + }, |
| 97 | + }).AwsAuthenticator; |
| 98 | + |
| 99 | + const result = await new AwsAuthenticatorWithMock('us-east-1', 'xray').authenticate( |
| 100 | + AWS_OTLP_TRACES_ENDPOINT, |
| 101 | + { test: 'test' }, |
| 102 | + new Uint8Array() |
| 103 | + ); |
| 104 | + |
| 105 | + expect(result).toHaveProperty(AUTHORIZATION_HEADER); |
| 106 | + expect(result).toHaveProperty(X_AMZ_DATE_HEADER); |
| 107 | + expect(result).toHaveProperty(X_AMZ_SECURITY_TOKEN_HEADER); |
| 108 | + |
| 109 | + expect(result[AUTHORIZATION_HEADER]).toBe(expected[AUTHORIZATION_HEADER]); |
| 110 | + expect(result[X_AMZ_DATE_HEADER]).toBe(expected[X_AMZ_DATE_HEADER]); |
| 111 | + expect(result[X_AMZ_SECURITY_TOKEN_HEADER]).toBe(expected[X_AMZ_SECURITY_TOKEN_HEADER]); |
| 112 | + }); |
| 113 | + |
| 114 | + it('should clear SigV4 headers if already present ', async () => { |
| 115 | + const notExpected = { |
| 116 | + [AUTHORIZATION_HEADER]: 'notExpectedAuth', |
| 117 | + [X_AMZ_DATE_HEADER]: 'notExpectedDate', |
| 118 | + [X_AMZ_SECURITY_TOKEN_HEADER]: 'notExpectedSecurityToken', |
| 119 | + }; |
| 120 | + |
| 121 | + const expected = { |
| 122 | + [AUTHORIZATION_HEADER]: 'testAuth', |
| 123 | + [X_AMZ_DATE_HEADER]: 'testDate', |
| 124 | + [X_AMZ_SECURITY_TOKEN_HEADER]: 'testSecurityToken', |
| 125 | + }; |
| 126 | + |
| 127 | + const AwsAuthenticatorWithMock = proxyquire(PATH, { |
| 128 | + [CREDENTIAL_PROVIDER_MODULE]: { |
| 129 | + defaultProvider: () => Promise.resolve(mockCredentials), |
| 130 | + }, |
| 131 | + [SIGNATURE_V4_MODULE]: { |
| 132 | + SignatureV4: class { |
| 133 | + constructor() {} |
| 134 | + sign(request: any) { |
| 135 | + return Promise.resolve({ |
| 136 | + headers: expected, |
| 137 | + }); |
| 138 | + } |
| 139 | + }, |
| 140 | + }, |
| 141 | + }).AwsAuthenticator; |
| 142 | + |
| 143 | + const result = await new AwsAuthenticatorWithMock('us-east-1', 'xray').authenticate( |
| 144 | + AWS_OTLP_TRACES_ENDPOINT, |
| 145 | + notExpected, |
| 146 | + new Uint8Array() |
| 147 | + ); |
| 148 | + |
| 149 | + expect(result).toHaveProperty(AUTHORIZATION_HEADER); |
| 150 | + expect(result).toHaveProperty(X_AMZ_DATE_HEADER); |
| 151 | + expect(result).toHaveProperty(X_AMZ_SECURITY_TOKEN_HEADER); |
| 152 | + |
| 153 | + expect(result[AUTHORIZATION_HEADER]).toBe(expected[AUTHORIZATION_HEADER]); |
| 154 | + expect(result[X_AMZ_DATE_HEADER]).toBe(expected[X_AMZ_DATE_HEADER]); |
| 155 | + expect(result[X_AMZ_SECURITY_TOKEN_HEADER]).toBe(expected[X_AMZ_SECURITY_TOKEN_HEADER]); |
| 156 | + }); |
| 157 | +}); |
0 commit comments