|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +import expect from 'expect'; |
| 4 | +import { OTLPAwsSpanExporter } from '../src/otlp-aws-span-exporter'; |
| 5 | +import * as sinon from 'sinon'; |
| 6 | +import * as proxyquire from 'proxyquire'; |
| 7 | +import nock = require('nock'); |
| 8 | + |
| 9 | +const XRAY_OTLP_ENDPOINT = 'https://xray.us-east-1.amazonaws.com'; |
| 10 | +const AUTHORIZATION_HEADER = 'Authorization'; |
| 11 | +const X_AMZ_DATE_HEADER = 'X-Amz-Date'; |
| 12 | +const X_AMZ_SECURITY_TOKEN_HEADER = 'X-Amz-Security-Token'; |
| 13 | + |
| 14 | +const EXPECTED_AUTH_HEADER = 'AWS4-HMAC-SHA256 Credential=test_key/some_date/us-east-1/xray/aws4_request'; |
| 15 | +const EXPECTED_AUTH_X_AMZ_DATE = 'some_date'; |
| 16 | +const EXPECTED_AUTH_SECURITY_TOKEN = 'test_token'; |
| 17 | + |
| 18 | +describe('OTLPAwsSpanExporter', () => { |
| 19 | + let sandbox: sinon.SinonSandbox; |
| 20 | + let scope: nock.Scope; |
| 21 | + let mockModule: any; |
| 22 | + |
| 23 | + beforeEach(() => { |
| 24 | + scope = nock(XRAY_OTLP_ENDPOINT) |
| 25 | + .post('/v1/traces') |
| 26 | + .reply((uri: any, requestBody: any) => { |
| 27 | + return [200, '']; |
| 28 | + }); |
| 29 | + |
| 30 | + sandbox = sinon.createSandbox(); |
| 31 | + mockModule = proxyquire('../src/otlp-aws-span-exporter', { |
| 32 | + '@smithy/signature-v4': { |
| 33 | + SignatureV4: class MockSignatureV4 { |
| 34 | + sign(req: any) { |
| 35 | + req.headers = { |
| 36 | + ...req.headers, |
| 37 | + [AUTHORIZATION_HEADER]: EXPECTED_AUTH_HEADER, |
| 38 | + [X_AMZ_DATE_HEADER]: EXPECTED_AUTH_X_AMZ_DATE, |
| 39 | + [X_AMZ_SECURITY_TOKEN_HEADER]: EXPECTED_AUTH_SECURITY_TOKEN, |
| 40 | + }; |
| 41 | + |
| 42 | + return req; |
| 43 | + } |
| 44 | + }, |
| 45 | + }, |
| 46 | + '@aws-sdk/credential-provider-node': { |
| 47 | + defaultProvider: () => async () => { |
| 48 | + return { |
| 49 | + accessKeyId: 'test_access_key', |
| 50 | + secretAccessKey: 'test_secret_key', |
| 51 | + }; |
| 52 | + }, |
| 53 | + }, |
| 54 | + }); |
| 55 | + }); |
| 56 | + |
| 57 | + afterEach(() => { |
| 58 | + sandbox.restore(); |
| 59 | + }); |
| 60 | + |
| 61 | + it('Should inject SigV4 Headers successfully', () => { |
| 62 | + const exporter = new mockModule.OTLPAwsSpanExporter(XRAY_OTLP_ENDPOINT + '/v1/traces'); |
| 63 | + |
| 64 | + scope.on('request', (req, interceptor, body) => { |
| 65 | + const headers = req.headers; |
| 66 | + expect(headers).toHaveProperty(AUTHORIZATION_HEADER.toLowerCase()); |
| 67 | + expect(headers).toHaveProperty(X_AMZ_SECURITY_TOKEN_HEADER.toLowerCase()); |
| 68 | + expect(headers).toHaveProperty(X_AMZ_DATE_HEADER.toLowerCase()); |
| 69 | + |
| 70 | + expect(headers[AUTHORIZATION_HEADER.toLowerCase()]).toBe(EXPECTED_AUTH_HEADER); |
| 71 | + expect(headers[X_AMZ_SECURITY_TOKEN_HEADER.toLowerCase()]).toBe(EXPECTED_AUTH_SECURITY_TOKEN); |
| 72 | + expect(headers[X_AMZ_DATE_HEADER.toLowerCase()]).toBe(EXPECTED_AUTH_X_AMZ_DATE); |
| 73 | + |
| 74 | + expect(headers['content-type']).toBe('application/x-protobuf'); |
| 75 | + expect(headers['user-agent']).toMatch(/^OTel-OTLP-Exporter-JavaScript\/\d+\.\d+\.\d+$/); |
| 76 | + }); |
| 77 | + |
| 78 | + exporter.export([], () => {}); |
| 79 | + }); |
| 80 | + |
| 81 | + describe('Should not inject SigV4 headers if dependencies are missing', () => { |
| 82 | + const dependencies = [ |
| 83 | + '@aws-sdk/credential-provider-node', |
| 84 | + '@aws-crypto/sha256-js', |
| 85 | + '@smithy/signature-v4', |
| 86 | + '@smithy/protocol-http', |
| 87 | + ]; |
| 88 | + |
| 89 | + dependencies.forEach(dependency => { |
| 90 | + it(`should not sign headers if missing dependency: ${dependency}`, () => { |
| 91 | + const exporter = new OTLPAwsSpanExporter(XRAY_OTLP_ENDPOINT + '/v1/traces'); |
| 92 | + |
| 93 | + scope.on('request', (req, interceptor, body) => { |
| 94 | + const headers = req.headers; |
| 95 | + expect(headers).not.toHaveProperty(AUTHORIZATION_HEADER); |
| 96 | + expect(headers).not.toHaveProperty(X_AMZ_DATE_HEADER); |
| 97 | + expect(headers).not.toHaveProperty(X_AMZ_SECURITY_TOKEN_HEADER); |
| 98 | + |
| 99 | + expect(headers['content-type']).toBe('application/x-protobuf'); |
| 100 | + expect(headers['user-agent']).toMatch(/^OTel-OTLP-Exporter-JavaScript\/\d+\.\d+\.\d+$/); |
| 101 | + }); |
| 102 | + |
| 103 | + Object.keys(require.cache).forEach(key => { |
| 104 | + delete require.cache[key]; |
| 105 | + }); |
| 106 | + const requireStub = sandbox.stub(require('module'), '_load'); |
| 107 | + requireStub.withArgs(dependency).throws(new Error(`Cannot find module '${dependency}'`)); |
| 108 | + requireStub.callThrough(); |
| 109 | + |
| 110 | + exporter.export([], () => {}); |
| 111 | + }); |
| 112 | + }); |
| 113 | + }); |
| 114 | + |
| 115 | + it('should not inject SigV4 headers if failure to sign headers', async () => { |
| 116 | + scope.on('request', (req, interceptor, body) => { |
| 117 | + const headers = req.headers; |
| 118 | + expect(headers).not.toHaveProperty(AUTHORIZATION_HEADER); |
| 119 | + expect(headers).not.toHaveProperty(X_AMZ_DATE_HEADER); |
| 120 | + expect(headers).not.toHaveProperty(X_AMZ_SECURITY_TOKEN_HEADER); |
| 121 | + |
| 122 | + expect(headers['content-type']).toBe('application/x-protobuf'); |
| 123 | + expect(headers['user-agent']).toMatch(/^OTel-OTLP-Exporter-JavaScript\/\d+\.\d+\.\d+$/); |
| 124 | + }); |
| 125 | + |
| 126 | + const stubbedModule = proxyquire('../src/otlp-aws-span-exporter', { |
| 127 | + '@smithy/signature-v4': { |
| 128 | + SignatureV4: class MockSignatureV4 { |
| 129 | + sign() { |
| 130 | + throw new Error('signing error'); |
| 131 | + } |
| 132 | + }, |
| 133 | + }, |
| 134 | + }); |
| 135 | + |
| 136 | + const exporter = new stubbedModule.OTLPAwsSpanExporter(XRAY_OTLP_ENDPOINT); |
| 137 | + |
| 138 | + exporter.export([], () => {}); |
| 139 | + }); |
| 140 | + |
| 141 | + it('should not inject SigV4 headers if failure to retrieve credentials', async () => { |
| 142 | + scope.on('request', (req, interceptor, body) => { |
| 143 | + const headers = req.headers; |
| 144 | + expect(headers).not.toHaveProperty(AUTHORIZATION_HEADER); |
| 145 | + expect(headers).not.toHaveProperty(X_AMZ_DATE_HEADER); |
| 146 | + expect(headers).not.toHaveProperty(X_AMZ_SECURITY_TOKEN_HEADER); |
| 147 | + |
| 148 | + expect(headers['content-type']).toBe('application/x-protobuf'); |
| 149 | + expect(headers['user-agent']).toMatch(/^OTel-OTLP-Exporter-JavaScript\/\d+\.\d+\.\d+$/); |
| 150 | + }); |
| 151 | + |
| 152 | + const stubbedModule = proxyquire('../src/otlp-aws-span-exporter', { |
| 153 | + '@aws-sdk/credential-provider-node': { |
| 154 | + defaultProvider: () => async () => { |
| 155 | + throw new Error('credentials error'); |
| 156 | + }, |
| 157 | + }, |
| 158 | + }); |
| 159 | + |
| 160 | + const exporter = new stubbedModule.OTLPAwsSpanExporter(XRAY_OTLP_ENDPOINT); |
| 161 | + |
| 162 | + exporter.export([], () => {}); |
| 163 | + }); |
| 164 | +}); |
0 commit comments