|
| 1 | +import os |
| 2 | +import unittest |
| 3 | +from unittest.mock import patch, MagicMock |
| 4 | + |
| 5 | +import datadog_lambda.api as api |
| 6 | + |
| 7 | + |
| 8 | +class TestDatadogLambdaAPI(unittest.TestCase): |
| 9 | + def setUp(self): |
| 10 | + api.api_key = None |
| 11 | + self.env_patcher = patch.dict( |
| 12 | + os.environ, |
| 13 | + { |
| 14 | + "DD_API_KEY_SECRET_ARN": "", |
| 15 | + "DD_API_KEY_SSM_NAME": "", |
| 16 | + "DD_KMS_API_KEY": "", |
| 17 | + "DD_API_KEY": "", |
| 18 | + "DATADOG_API_KEY": "", |
| 19 | + "AWS_REGION": "", |
| 20 | + }, |
| 21 | + clear=True, |
| 22 | + ) |
| 23 | + self.env_patcher.start() |
| 24 | + |
| 25 | + @patch("boto3.client") |
| 26 | + def test_secrets_manager_fips_endpoint(self, mock_boto3_client): |
| 27 | + mock_client = MagicMock() |
| 28 | + mock_client.get_secret_value.return_value = {"SecretString": "test-api-key"} |
| 29 | + mock_boto3_client.return_value = mock_client |
| 30 | + |
| 31 | + os.environ["AWS_REGION"] = "us-gov-east-1" |
| 32 | + os.environ["DD_API_KEY_SECRET_ARN"] = "test-secrets-arn" |
| 33 | + |
| 34 | + api_key = api.get_api_key() |
| 35 | + |
| 36 | + mock_boto3_client.assert_called_with( |
| 37 | + "secretsmanager", |
| 38 | + endpoint_url="https://secretsmanager-fips.us-gov-east-1.amazonaws.com", |
| 39 | + ) |
| 40 | + self.assertEqual(api_key, "test-api-key") |
| 41 | + |
| 42 | + @patch("boto3.client") |
| 43 | + def test_ssm_fips_endpoint(self, mock_boto3_client): |
| 44 | + mock_client = MagicMock() |
| 45 | + mock_client.get_parameter.return_value = { |
| 46 | + "Parameter": {"Value": "test-api-key"} |
| 47 | + } |
| 48 | + mock_boto3_client.return_value = mock_client |
| 49 | + |
| 50 | + os.environ["AWS_REGION"] = "us-gov-west-1" |
| 51 | + os.environ["DD_API_KEY_SSM_NAME"] = "test-ssm-param" |
| 52 | + |
| 53 | + api_key = api.get_api_key() |
| 54 | + |
| 55 | + mock_boto3_client.assert_called_with( |
| 56 | + "ssm", endpoint_url="https://ssm-fips.us-gov-west-1.amazonaws.com" |
| 57 | + ) |
| 58 | + self.assertEqual(api_key, "test-api-key") |
| 59 | + |
| 60 | + @patch("boto3.client") |
| 61 | + @patch("datadog_lambda.api.decrypt_kms_api_key") |
| 62 | + def test_kms_fips_endpoint(self, mock_decrypt_kms, mock_boto3_client): |
| 63 | + mock_client = MagicMock() |
| 64 | + mock_boto3_client.return_value = mock_client |
| 65 | + mock_decrypt_kms.return_value = "test-api-key" |
| 66 | + |
| 67 | + os.environ["AWS_REGION"] = "us-gov-west-1" |
| 68 | + os.environ["DD_KMS_API_KEY"] = "encrypted-api-key" |
| 69 | + |
| 70 | + api_key = api.get_api_key() |
| 71 | + |
| 72 | + mock_boto3_client.assert_called_with( |
| 73 | + "kms", endpoint_url="https://kms-fips.us-gov-west-1.amazonaws.com" |
| 74 | + ) |
| 75 | + self.assertEqual(api_key, "test-api-key") |
| 76 | + |
| 77 | + @patch("boto3.client") |
| 78 | + def test_no_fips_for_standard_regions(self, mock_boto3_client): |
| 79 | + mock_client = MagicMock() |
| 80 | + mock_client.get_secret_value.return_value = {"SecretString": "test-api-key"} |
| 81 | + mock_boto3_client.return_value = mock_client |
| 82 | + |
| 83 | + os.environ.clear() |
| 84 | + os.environ["AWS_REGION"] = "us-west-2" |
| 85 | + os.environ["DD_API_KEY_SECRET_ARN"] = "test-arn" |
| 86 | + |
| 87 | + api.get_api_key() |
| 88 | + |
| 89 | + mock_boto3_client.assert_called_with("secretsmanager", endpoint_url=None) |
0 commit comments