Skip to content

Commit 69cb43a

Browse files
committed
use region from secrets manager arn
1 parent 71b64fa commit 69cb43a

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

datadog_lambda/api.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,30 +64,35 @@ 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 as e:
79+
logger.debug("Invalid secret arn in DD_API_KEY_SECRET_ARN. Unable to get API key.")
80+
return ""
81+
endpoint_url = (
82+
f"https://secretsmanager-fips.{secrets_region}.amazonaws.com"
7883
if is_gov_region
79-
else None
84+
else f"https://secretsmanager.{secrets_region}.amazonaws.com"
8085
)
8186
secrets_manager_client = boto3.client(
82-
"secretsmanager", endpoint_url=fips_endpoint
87+
"secretsmanager", endpoint_url=endpoint_url
8388
)
8489
api_key = secrets_manager_client.get_secret_value(
8590
SecretId=DD_API_KEY_SECRET_ARN
8691
)["SecretString"]
8792
elif DD_API_KEY_SSM_NAME:
8893
# SSM endpoints: https://docs.aws.amazon.com/general/latest/gr/ssm.html
8994
fips_endpoint = (
90-
f"https://ssm-fips.{REGION}.amazonaws.com" if is_gov_region else None
95+
f"https://ssm-fips.{LAMBDA_REGION}.amazonaws.com" if is_gov_region else None
9196
)
9297
ssm_client = boto3.client("ssm", endpoint_url=fips_endpoint)
9398
api_key = ssm_client.get_parameter(
@@ -96,7 +101,7 @@ def get_api_key() -> str:
96101
elif DD_KMS_API_KEY:
97102
# KMS endpoints: https://docs.aws.amazon.com/general/latest/gr/kms.html
98103
fips_endpoint = (
99-
f"https://kms-fips.{REGION}.amazonaws.com" if is_gov_region else None
104+
f"https://kms-fips.{LAMBDA_REGION}.amazonaws.com" if is_gov_region else None
100105
)
101106
kms_client = boto3.client("kms", endpoint_url=fips_endpoint)
102107
api_key = decrypt_kms_api_key(kms_client, DD_KMS_API_KEY)

0 commit comments

Comments
 (0)