|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import { getTestSpans } from '@opentelemetry/contrib-test-utils'; |
| 18 | +import { SpanKind } from '@opentelemetry/api'; |
| 19 | +import { ReadableSpan } from '@opentelemetry/sdk-trace-base'; |
| 20 | +import { ATTR_AWS_SECRETSMANAGER_SECRET_ARN } from '@opentelemetry/semantic-conventions/incubating'; |
| 21 | + |
| 22 | +import { SecretsManager } from '@aws-sdk/client-secrets-manager'; |
| 23 | + |
| 24 | +import { expect } from 'expect'; |
| 25 | +import * as nock from 'nock'; |
| 26 | + |
| 27 | +const region = 'us-east-1'; |
| 28 | + |
| 29 | +describe('SecretsManager', () => { |
| 30 | + let secretsManager: SecretsManager; |
| 31 | + beforeEach(() => { |
| 32 | + secretsManager = new SecretsManager({ |
| 33 | + region: region, |
| 34 | + credentials: { |
| 35 | + accessKeyId: 'abcde', |
| 36 | + secretAccessKey: 'abcde', |
| 37 | + }, |
| 38 | + }); |
| 39 | + }); |
| 40 | + |
| 41 | + describe('DescribeSecret', () => { |
| 42 | + const testParams = [ |
| 43 | + 'testId', |
| 44 | + 'badarn:aws:secretsmanager:us-weast-1:123456789123:secret:testId123456', |
| 45 | + 'arn:aws:secretsmanager:us-east-1:123456789123:secret:testId123456', |
| 46 | + ]; |
| 47 | + |
| 48 | + testParams.forEach(secretId => { |
| 49 | + it('should generate secret arn attribute only if secretId is an valid ARN', async () => { |
| 50 | + nock(`https://secretsmanager.${region}.amazonaws.com/`) |
| 51 | + .post('/') |
| 52 | + .reply(200, 'null'); |
| 53 | + |
| 54 | + await secretsManager |
| 55 | + .describeSecret({ |
| 56 | + SecretId: secretId, |
| 57 | + }) |
| 58 | + .catch((err: any) => {}); |
| 59 | + |
| 60 | + const testSpans: ReadableSpan[] = getTestSpans(); |
| 61 | + const getDescribeSecretSpans: ReadableSpan[] = testSpans.filter( |
| 62 | + (s: ReadableSpan) => { |
| 63 | + return s.name === 'SecretsManager.DescribeSecret'; |
| 64 | + } |
| 65 | + ); |
| 66 | + |
| 67 | + expect(getDescribeSecretSpans.length).toBe(1); |
| 68 | + const describeSecretSpan = getDescribeSecretSpans[0]; |
| 69 | + |
| 70 | + if (secretId.startsWith('arn:aws:secretsmanager:')) { |
| 71 | + expect( |
| 72 | + describeSecretSpan.attributes[ATTR_AWS_SECRETSMANAGER_SECRET_ARN] |
| 73 | + ).toBe(secretId); |
| 74 | + } else { |
| 75 | + expect( |
| 76 | + describeSecretSpan.attributes[ATTR_AWS_SECRETSMANAGER_SECRET_ARN] |
| 77 | + ).toBeUndefined(); |
| 78 | + } |
| 79 | + |
| 80 | + expect(describeSecretSpan.kind).toBe(SpanKind.CLIENT); |
| 81 | + }); |
| 82 | + }); |
| 83 | + }); |
| 84 | + |
| 85 | + describe('GetSecretValue', () => { |
| 86 | + it('secret arn attribute should be populated from the response', async () => { |
| 87 | + const secretIdArn = |
| 88 | + 'arn:aws:secretsmanager:us-east-1:123456789123:secret:testId123456'; |
| 89 | + |
| 90 | + nock(`https://secretsmanager.${region}.amazonaws.com/`) |
| 91 | + .post('/') |
| 92 | + .reply(200, { |
| 93 | + ARN: secretIdArn, |
| 94 | + Name: 'testId', |
| 95 | + }); |
| 96 | + |
| 97 | + await secretsManager |
| 98 | + .getSecretValue({ |
| 99 | + SecretId: 'testSecret', |
| 100 | + }) |
| 101 | + .catch((err: any) => { |
| 102 | + console.log(err); |
| 103 | + }); |
| 104 | + |
| 105 | + const testSpans: ReadableSpan[] = getTestSpans(); |
| 106 | + const getSecretValueSpans: ReadableSpan[] = testSpans.filter( |
| 107 | + (s: ReadableSpan) => { |
| 108 | + return s.name === 'SecretsManager.GetSecretValue'; |
| 109 | + } |
| 110 | + ); |
| 111 | + |
| 112 | + expect(getSecretValueSpans.length).toBe(1); |
| 113 | + |
| 114 | + const secretValueSpan = getSecretValueSpans[0]; |
| 115 | + expect( |
| 116 | + secretValueSpan.attributes[ATTR_AWS_SECRETSMANAGER_SECRET_ARN] |
| 117 | + ).toBe(secretIdArn); |
| 118 | + expect(secretValueSpan.kind).toBe(SpanKind.CLIENT); |
| 119 | + }); |
| 120 | + }); |
| 121 | +}); |
0 commit comments