Skip to content

Commit 29a050e

Browse files
committed
check ssm-fips supported regions
1 parent c5da899 commit 29a050e

File tree

2 files changed

+43
-6
lines changed

2 files changed

+43
-6
lines changed

datadog_lambda/api.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@
55

66
logger = logging.getLogger(__name__)
77
KMS_ENCRYPTION_CONTEXT_KEY = "LambdaFunctionName"
8+
SSM_FIPS_SUPPORTED_REGIONS = {
9+
"us-east-1",
10+
"us-east-2",
11+
"us-west-1",
12+
"us-west-2",
13+
"ca-central-1",
14+
"ca-west-1",
15+
}
816
api_key = None
917

1018

@@ -92,11 +100,16 @@ def get_api_key() -> str:
92100
)["SecretString"]
93101
elif DD_API_KEY_SSM_NAME:
94102
# SSM endpoints: https://docs.aws.amazon.com/general/latest/gr/ssm.html
95-
fips_endpoint = (
96-
f"https://ssm-fips.{LAMBDA_REGION}.amazonaws.com"
97-
if config.fips_mode_enabled and not config.is_gov_region
98-
else None
99-
)
103+
fips_endpoint = None
104+
if config.fips_mode_enabled and LAMBDA_REGION in SSM_FIPS_SUPPORTED_REGIONS:
105+
fips_endpoint = f"https://ssm-fips.{LAMBDA_REGION}.amazonaws.com"
106+
else:
107+
if not config.is_gov_region:
108+
# Log warning if FIPS is enabled for a commercial region that does not support SSM FIPS endpoints
109+
logger.warning(
110+
"FIPS mode is enabled but region '%s' does not support SSM FIPS endpoints. Using standard SSM endpoint.",
111+
LAMBDA_REGION,
112+
)
100113
ssm_client = _boto3_client("ssm", endpoint_url=fips_endpoint)
101114
api_key = ssm_client.get_parameter(
102115
Name=DD_API_KEY_SSM_NAME, WithDecryption=True

tests/test_api.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def test_secrets_manager_different_region_but_still_fips(self, mock_boto3_client
8989

9090
@patch("datadog_lambda.config.Config.fips_mode_enabled", True)
9191
@patch("botocore.session.Session.create_client")
92-
def test_ssm_fips_endpoint(self, mock_boto3_client):
92+
def test_ssm_fips_endpoint_supported_region(self, mock_boto3_client):
9393
mock_client = MagicMock()
9494
mock_client.get_parameter.return_value = {
9595
"Parameter": {"Value": "test-api-key"}
@@ -124,6 +124,30 @@ def test_ssm_gov_endpoint(self, mock_boto3_client):
124124
mock_boto3_client.assert_called_with("ssm", endpoint_url=None)
125125
self.assertEqual(api_key, "test-api-key")
126126

127+
@patch("datadog_lambda.config.Config.fips_mode_enabled", True)
128+
@patch("botocore.session.Session.create_client")
129+
def test_ssm_fips_endpoint_unsupported_region(self, mock_boto3_client):
130+
mock_client = MagicMock()
131+
mock_client.get_parameter.return_value = {
132+
"Parameter": {"Value": "test-api-key"}
133+
}
134+
mock_boto3_client.return_value = mock_client
135+
136+
os.environ["AWS_REGION"] = "eu-west-1"
137+
os.environ["DD_API_KEY_SSM_NAME"] = "test-ssm-param"
138+
139+
with self.assertLogs("datadog_lambda.api", level="WARNING") as log_context:
140+
api_key = api.get_api_key()
141+
142+
mock_boto3_client.assert_called_with("ssm", endpoint_url=None)
143+
self.assertEqual(api_key, "test-api-key")
144+
self.assertTrue(
145+
any(
146+
"does not support SSM FIPS endpoints" in log_msg
147+
for log_msg in log_context.output
148+
)
149+
)
150+
127151
@patch("datadog_lambda.config.Config.fips_mode_enabled", True)
128152
@patch("botocore.session.Session.create_client")
129153
@patch("datadog_lambda.api.decrypt_kms_api_key")

0 commit comments

Comments
 (0)