chore(iam): add resource validation to PolicyStatement for resource-based policies #35300
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.
Issue # (if applicable)
Closes #35296.
Reason for this change
CDK currently allows creating resource-based policies (such as S3 bucket policies, SNS topic policies, KMS key policies) with PolicyStatements that lack the required
resourcesproperty. This causes deployment-time CloudFormation failures instead of providing immediate synthesis-time validation errors.This creates a poor developer experience where:
The root cause is that
PolicyStatement.validateForResourcePolicy()validates principals but not resources, despite both being required for resource-based policies according to AWS IAM documentation.Description of changes
This change enhances the existing validation infrastructure to catch missing resources at synthesis time while handling the nuanced differences between different types of resource-based policies:
Enhanced PolicyStatement.validateForResourcePolicy():
ResourcePolicyValidationOptionsparameter with JSII-compatible interface for services with implicit resource contextsNew ResourcePolicyValidationOptions interface:
skipResourceValidation?: booleanproperty to handle services like ECR where resources are implicitNew PolicyStatement.validateForTrustPolicy():
Updated PolicyDocument.validateForTrustPolicy():
Updated Role validation:
ECR Repository policy handling:
{ skipResourceValidation: true }since ECR repository policies have implicit resources (the repository itself)Cross-service impact:
This change automatically benefits 8+ AWS services that use resource-based policies:
skipResourceValidation: trueAPI Changes:
New Interface:
Updated Method Signatures:
Describe any new or updated permissions being added
N/A - This change only adds synthesis-time validation logic and does not modify IAM permissions, resource access patterns, or CloudFormation template generation.
Description of how you validated changes
Unit tests:
Added 7 comprehensive unit tests in
packages/aws-cdk-lib/aws-iam/test/policy-document.test.ts:Integration tests:
Added integration test in
packages/aws-cdk-lib/aws-s3/test/bucket-policy.test.ts:Checklist
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license