|
| 1 | +# Copyright The OpenTelemetry Authors |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import botocore.session |
| 16 | +from moto import mock_aws |
| 17 | + |
| 18 | +from opentelemetry.instrumentation.botocore import BotocoreInstrumentor |
| 19 | +from opentelemetry.semconv._incubating.attributes.aws_attributes import ( |
| 20 | + AWS_SECRETSMANAGER_SECRET_ARN, |
| 21 | +) |
| 22 | +from opentelemetry.test.test_base import TestBase |
| 23 | + |
| 24 | + |
| 25 | +class TestSecretsManagerExtension(TestBase): |
| 26 | + def setUp(self): |
| 27 | + super().setUp() |
| 28 | + BotocoreInstrumentor().instrument() |
| 29 | + session = botocore.session.get_session() |
| 30 | + session.set_credentials( |
| 31 | + access_key="access-key", secret_key="secret-key" |
| 32 | + ) |
| 33 | + self.region = "us-west-2" |
| 34 | + self.client = session.create_client( |
| 35 | + "secretsmanager", region_name=self.region |
| 36 | + ) |
| 37 | + |
| 38 | + def tearDown(self): |
| 39 | + super().tearDown() |
| 40 | + BotocoreInstrumentor().uninstrument() |
| 41 | + |
| 42 | + def create_secret_and_get_arn(self, name: str = "test-secret") -> str: |
| 43 | + """ |
| 44 | + Create a secret in mocked Secrets Manager and return its ARN. |
| 45 | + """ |
| 46 | + # Clear spans before creating secret for helper method |
| 47 | + self.memory_exporter.clear() |
| 48 | + response = self.client.create_secret( |
| 49 | + Name=name, SecretString="test-secret-value" |
| 50 | + ) |
| 51 | + return response["ARN"] |
| 52 | + |
| 53 | + @mock_aws |
| 54 | + def test_tag_resource_with_arn(self): |
| 55 | + secret_arn = self.create_secret_and_get_arn() |
| 56 | + |
| 57 | + self.client.tag_resource( |
| 58 | + SecretId=secret_arn, Tags=[{"Key": "Environment", "Value": "Test"}] |
| 59 | + ) |
| 60 | + |
| 61 | + spans = self.memory_exporter.get_finished_spans() |
| 62 | + assert spans |
| 63 | + self.assertEqual(len(spans), 2) |
| 64 | + span = spans[1] # tag_resource span |
| 65 | + self.assertEqual( |
| 66 | + span.attributes[AWS_SECRETSMANAGER_SECRET_ARN], |
| 67 | + secret_arn, |
| 68 | + ) |
| 69 | + |
| 70 | + @mock_aws |
| 71 | + def test_create_secret(self): |
| 72 | + secret_name = "test-secret" |
| 73 | + response = self.client.create_secret( |
| 74 | + Name=secret_name, SecretString="test-secret-value" |
| 75 | + ) |
| 76 | + secret_arn = response["ARN"] |
| 77 | + |
| 78 | + spans = self.memory_exporter.get_finished_spans() |
| 79 | + assert spans |
| 80 | + self.assertEqual(len(spans), 1) |
| 81 | + span = spans[0] # create_secret span |
| 82 | + # Should capture ARN from response |
| 83 | + self.assertEqual( |
| 84 | + span.attributes[AWS_SECRETSMANAGER_SECRET_ARN], |
| 85 | + secret_arn, |
| 86 | + ) |
0 commit comments