fix(custom-resource-handler): remove hardcoded partition in s3 auto-delete-objects handler - #37963
fix(custom-resource-handler): remove hardcoded partition in s3 auto-delete-objects handler#37963Khangdang1690 wants to merge 2 commits into
Conversation
…elete-objects handler Derive partition from event.ServiceToken at runtime instead of hardcoding 'aws'. CloudFormation always sets ServiceToken to this Lambda's own ARN, which is in the same partition as the bucket being managed. Fixes silent failure of the pre-delete s3:PutObject deny policy in aws-cn and aws-us-gov partitions. fixes aws#33212
There was a problem hiding this comment.
The pull request linter fails with the following errors:
❌ Fixes must contain a change to an integration test file and the resulting snapshot.
If you believe this pull request should receive an exemption, please comment and provide a justification. A comment requesting an exemption should contain the text Exemption Request. Additionally, if clarification is needed, add Clarification Request to a comment.
✅ A exemption request has been requested. Please wait for a maintainer's review.
|
Exemption Request This is a runtime-only fix in The bug only manifests in The fix is covered by a new unit test in If a reviewer can suggest a way to exercise non- |
Issue # (if applicable)
Closes #33212.
Reason for this change
The auto-delete-objects custom resource Lambda hardcodes
arn:aws:s3:::${bucketName}/*when attaching the pre-deletes3:PutObjectdeny policy. The literalawspartition silently breaks the deny policy inaws-cn(China) andaws-us-gov(GovCloud), where bucket ARNs arearn:aws-cn:.../arn:aws-us-gov:.... Thes3:PutObjectdeny statement is non-fatal (the bucket still gets emptied), so the issue is silent — the race-condition protection against external writers is simply absent in non-default partitions.The CDK team filed this themselves when the
@cdklabs/no-literal-partitionlint rule flagged the line; an inlineeslint-disablewas added with a TODO at the time.Description of changes
The handler now derives the partition at runtime from
event.ServiceToken, which CloudFormation always populates with the Lambda's own ARN. Since the Lambda is always provisioned in the same partition as the bucket it manages, this is the correct partition to use in the deny ARN. The eslint suppression and TODO are removed.Alternative considered and rejected: passing the partition to the handler as a Lambda environment variable populated from
Stack.of(scope).partitionat synth time. That works but adds a newEnvironment.Variablesentry to the provider Lambda in every stack withautoDeleteObjects: true, which would regenerate ~100 integration test snapshots across theframework-integpackage. Theevent.ServiceTokenapproach delivers the same correctness with zero synthesized-template changes.Describe any new or updated permissions being added
None.
Description of how you validated changes
Deleteevents to provide a top-levelServiceTokenARN (the canonical CloudFormation custom-resource field) so they continue to assert on the existingarn:aws:s3:::MyBucket/*deny ARN.'deny policy ARN uses the partition from the ServiceToken (aws-cn)'that fires a Delete event with anarn:aws-cn:...ServiceToken and asserts the resulting deny statement usesarn:aws-cn:s3:::MyBucket/*.yarn testinpackages/@aws-cdk/custom-resource-handlers.Checklist
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license