Skip to content

Commit ed75966

Browse files
Fix SSM endpoint for Gov regions (#693)
* only override ssm endpoint url in commercial fips * lint * check ssm-fips supported regions * fix
1 parent df6df8e commit ed75966

File tree

2 files changed

+65
-8
lines changed

2 files changed

+65
-8
lines changed

datadog_lambda/api.py

Lines changed: 20 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,18 @@ 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
98-
else None
99-
)
103+
fips_endpoint = None
104+
if config.fips_mode_enabled:
105+
if LAMBDA_REGION in SSM_FIPS_SUPPORTED_REGIONS:
106+
fips_endpoint = f"https://ssm-fips.{LAMBDA_REGION}.amazonaws.com"
107+
else:
108+
# Log warning if SSM FIPS endpoint is not supported for commercial region
109+
if not config.is_gov_region:
110+
logger.warning(
111+
"FIPS mode is enabled, but '%s' does not support SSM FIPS endpoints. "
112+
"Using standard SSM endpoint.",
113+
LAMBDA_REGION,
114+
)
100115
ssm_client = _boto3_client("ssm", endpoint_url=fips_endpoint)
101116
api_key = ssm_client.get_parameter(
102117
Name=DD_API_KEY_SSM_NAME, WithDecryption=True

tests/test_api.py

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,23 +89,65 @@ 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"}
9696
}
9797
mock_boto3_client.return_value = mock_client
9898

99-
os.environ["AWS_REGION"] = "us-gov-west-1"
99+
os.environ["AWS_REGION"] = "us-east-1"
100100
os.environ["DD_API_KEY_SSM_NAME"] = "test-ssm-param"
101101

102102
api_key = api.get_api_key()
103103

104104
mock_boto3_client.assert_called_with(
105-
"ssm", endpoint_url="https://ssm-fips.us-gov-west-1.amazonaws.com"
105+
"ssm", endpoint_url="https://ssm-fips.us-east-1.amazonaws.com"
106106
)
107107
self.assertEqual(api_key, "test-api-key")
108108

109+
@patch("datadog_lambda.config.Config.fips_mode_enabled", True)
110+
@patch("datadog_lambda.config.Config.is_gov_region", True)
111+
@patch("botocore.session.Session.create_client")
112+
def test_ssm_gov_endpoint(self, mock_boto3_client):
113+
mock_client = MagicMock()
114+
mock_client.get_parameter.return_value = {
115+
"Parameter": {"Value": "test-api-key"}
116+
}
117+
mock_boto3_client.return_value = mock_client
118+
119+
os.environ["AWS_REGION"] = "us-gov-west-1"
120+
os.environ["DD_API_KEY_SSM_NAME"] = "test-ssm-param"
121+
122+
api_key = api.get_api_key()
123+
124+
mock_boto3_client.assert_called_with("ssm", endpoint_url=None)
125+
self.assertEqual(api_key, "test-api-key")
126+
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+
109151
@patch("datadog_lambda.config.Config.fips_mode_enabled", True)
110152
@patch("botocore.session.Session.create_client")
111153
@patch("datadog_lambda.api.decrypt_kms_api_key")

0 commit comments

Comments
 (0)