fix(logs_monitoring): read the API key from the region in its ARN - #1192
Open
ppulec-make wants to merge 1 commit into
Open
fix(logs_monitoring): read the API key from the region in its ARN#1192ppulec-make wants to merge 1 commit into
ppulec-make wants to merge 1 commit into
Conversation
The Secrets Manager and SSM clients were built without a region, so boto3 resolved one from the environment, which on Lambda is always the region the Forwarder runs in. Neither service redirects a request based on the ARN it carries, so a secret or parameter in another region was unreachable and the lookup failed during module import, taking the whole init down with it. Pass the region parsed out of the ARN to both clients. Values that are not ARNs, such as a plain SSM parameter name, keep the default resolution. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
rtrieu
approved these changes
Aug 4, 2026
Author
|
Hello @rtrieu, thank you for approving this change. Could you please help me to get an approval from the aws-integrations team? Thank you! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
Passes the region parsed out of the ARN to the Secrets Manager and SSM clients that fetch the Datadog API key in
aws/logs_monitoring/settings.py, so a secret or parameter stored in a different region than the Forwarder can be read.get_region_from_arn()returns the region field of an ARN, orNonewhen the value is not an ARN.boto3.client("secretsmanager", region_name=..., ...)forDD_API_KEY_SECRET_ARN.boto3.client("ssm", region_name=..., ...)forDD_API_KEY_SSM_NAME.DdApiKeySsmParameterNameaccepts a full SSM ARN in addition to a parameter path. ItsAllowedPatternrejected colons, so without this the SSM half of the fix is unreachable from CloudFormation. The SSM statement in the execution role already grantsResource: "*", so no IAM change is needed.Passing
region_name=Noneis equivalent to omitting it, so a plain SSM parameter name (/datadog/api-key) keeps the existing default region resolution. Both clients are unchanged for same-region configurations.Motivation
Both clients were created without a region, so botocore resolved one from the environment — on Lambda that is
AWS_REGION, always the region the Forwarder runs in. Neither Secrets Manager nor SSM redirects a request based on the ARN it carries: thesecretsmanagerendpoint ruleset takes onlyRegion,UseDualStack,UseFIPSandEndpoint, and the service model defines nocontextParambindingSecretIdto the endpoint. A cross-region ARN was therefore sent to the local region's endpoint and came backResourceNotFoundException.The failure mode is severe: the lookup runs at
settingsimport time and onlyjson.JSONDecodeErroris caught, so theClientErrorpropagates out of the import and fails Lambda initialization rather than degrading. It also reads as a permissions problem when it is not — the role'ssecretsmanager:GetSecretValuestatement is scoped to!Sub "${DdApiKeySecretArn}*", which does grant the cross-region secret.This affects a single Secrets Manager secret shared by Forwarders in several regions, which is otherwise a supported way to hold one API key.
Not changed
DD_KMS_API_KEYstill requires a KMS key in the Forwarder's region. Its input is a base64 ciphertext blob with no ARN to parse, so supporting a cross-region key would mean adding a new parameter for the key's region. That is left out to keep this a bug fix; the constraint is now stated in the README.Testing Guidelines
New cases in
aws/logs_monitoring/tests/test_settings.py:get_region_from_arn()over a full secret ARN, a partial secret ARN, an SSM parameter ARN, a non-commercial partition ARN, a plain parameter name, a secret friendly name, an ARN with an empty region field, and a truncated ARN.settingswith a patchedboto3asserts the client is built withregion_name="eu-west-1"for a cross-region secret ARN,region_name="us-west-2"for a cross-region SSM ARN, andregion_name=Nonefor a parameter name.Verified locally on Python 3.14 with the CI commands:
python -m unittest discover ./aws/logs_monitoring/(190 tests, OK),flake8 --select=E9,F --ignore=F824,black --check, andcfn-lint -i E3030 -t aws/logs_monitoring/template.yamlall clean. TheAllowedPatternregex was checked against parameter paths andaws,aws-us-govandaws-cnSSM ARNs, and against values it must keep rejecting.Additional Notes
Unrelated latent bug noticed while writing the tests, not addressed here: when a plaintext secret parses as non-object JSON — an all-digit string, which a 32-character hex API key can be —
json.loadsreturns anintand"DD_API_KEY" in secret_jsonraisesTypeError: argument of type 'int' is not a container or iterable. Onlyjson.JSONDecodeErroris caught. Happy to send a separate PR guarding onisinstance(secret_json, dict).Types of changes
Check all that apply
🤖 Generated with Claude Code