@@ -64,30 +64,37 @@ def get_api_key() -> str:
64
64
DD_KMS_API_KEY = os .environ .get ("DD_KMS_API_KEY" , "" )
65
65
DD_API_KEY = os .environ .get ("DD_API_KEY" , os .environ .get ("DATADOG_API_KEY" , "" ))
66
66
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-" )
69
69
if is_gov_region :
70
70
logger .debug (
71
71
"Govcloud region detected. Using FIPs endpoints for secrets management."
72
72
)
73
73
74
74
if DD_API_KEY_SECRET_ARN :
75
75
# 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"
78
85
if is_gov_region
79
86
else None
80
87
)
81
88
secrets_manager_client = boto3 .client (
82
- "secretsmanager" , endpoint_url = fips_endpoint
89
+ "secretsmanager" , endpoint_url = endpoint_url , region_name = secrets_region
83
90
)
84
91
api_key = secrets_manager_client .get_secret_value (
85
92
SecretId = DD_API_KEY_SECRET_ARN
86
93
)["SecretString" ]
87
94
elif DD_API_KEY_SSM_NAME :
88
95
# SSM endpoints: https://docs.aws.amazon.com/general/latest/gr/ssm.html
89
96
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
91
98
)
92
99
ssm_client = boto3 .client ("ssm" , endpoint_url = fips_endpoint )
93
100
api_key = ssm_client .get_parameter (
@@ -96,7 +103,7 @@ def get_api_key() -> str:
96
103
elif DD_KMS_API_KEY :
97
104
# KMS endpoints: https://docs.aws.amazon.com/general/latest/gr/kms.html
98
105
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
100
107
)
101
108
kms_client = boto3 .client ("kms" , endpoint_url = fips_endpoint )
102
109
api_key = decrypt_kms_api_key (kms_client , DD_KMS_API_KEY )
0 commit comments