@@ -64,30 +64,37 @@ def get_api_key() -> str:
6464 DD_KMS_API_KEY = os .environ .get ("DD_KMS_API_KEY" , "" )
6565 DD_API_KEY = os .environ .get ("DD_API_KEY" , os .environ .get ("DATADOG_API_KEY" , "" ))
6666
67- REGION = os .environ .get ("AWS_REGION" , "" )
68- is_gov_region = REGION .startswith ("us-gov-" )
67+ LAMBDA_REGION = os .environ .get ("AWS_REGION" , "" )
68+ is_gov_region = LAMBDA_REGION .startswith ("us-gov-" )
6969 if is_gov_region :
7070 logger .debug (
7171 "Govcloud region detected. Using FIPs endpoints for secrets management."
7272 )
7373
7474 if DD_API_KEY_SECRET_ARN :
7575 # Secrets manager endpoints: https://docs.aws.amazon.com/general/latest/gr/asm.html
76- fips_endpoint = (
77- f"https://secretsmanager-fips.{ REGION } .amazonaws.com"
76+ try :
77+ secrets_region = DD_API_KEY_SECRET_ARN .split (":" )[3 ]
78+ except Exception :
79+ logger .debug (
80+ "Invalid secret arn in DD_API_KEY_SECRET_ARN. Unable to get API key."
81+ )
82+ return ""
83+ endpoint_url = (
84+ f"https://secretsmanager-fips.{ secrets_region } .amazonaws.com"
7885 if is_gov_region
7986 else None
8087 )
8188 secrets_manager_client = boto3 .client (
82- "secretsmanager" , endpoint_url = fips_endpoint
89+ "secretsmanager" , endpoint_url = endpoint_url , region_name = secrets_region
8390 )
8491 api_key = secrets_manager_client .get_secret_value (
8592 SecretId = DD_API_KEY_SECRET_ARN
8693 )["SecretString" ]
8794 elif DD_API_KEY_SSM_NAME :
8895 # SSM endpoints: https://docs.aws.amazon.com/general/latest/gr/ssm.html
8996 fips_endpoint = (
90- f"https://ssm-fips.{ REGION } .amazonaws.com" if is_gov_region else None
97+ f"https://ssm-fips.{ LAMBDA_REGION } .amazonaws.com" if is_gov_region else None
9198 )
9299 ssm_client = boto3 .client ("ssm" , endpoint_url = fips_endpoint )
93100 api_key = ssm_client .get_parameter (
@@ -96,7 +103,7 @@ def get_api_key() -> str:
96103 elif DD_KMS_API_KEY :
97104 # KMS endpoints: https://docs.aws.amazon.com/general/latest/gr/kms.html
98105 fips_endpoint = (
99- f"https://kms-fips.{ REGION } .amazonaws.com" if is_gov_region else None
106+ f"https://kms-fips.{ LAMBDA_REGION } .amazonaws.com" if is_gov_region else None
100107 )
101108 kms_client = boto3 .client ("kms" , endpoint_url = fips_endpoint )
102109 api_key = decrypt_kms_api_key (kms_client , DD_KMS_API_KEY )
0 commit comments