Skip to content

fix(iam): throw error when statement id is invalid (#34819) #34828

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

camerondurham
Copy link

@camerondurham camerondurham commented Jun 26, 2025

Resolves #34819 by throwing error when PolicyStatement Statement id contains characters not allowed by the API.

See docs in https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_elements_sid.html

Replacing #34827 since I didn't follow contributing guidelines in last PR.

Issue # (if applicable)

Closes #34819

Reason for this change

CDK build allowed me to write invalid statement ids containing dashes when building policy statements, then failed on deployment. I'd like the CDK to throw an exception on build/synth time to make that feedback cycle faster.

Description of changes

Validate that statement id matches requirements specified in https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_elements_sid.html in the constructor.

Add tests that verify only valid statement ids are set in constructor.

Describe any new or updated permissions being added

n/a

Description of how you validated changes

yes, added unit tests

Checklist


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license

@aws-cdk-automation aws-cdk-automation requested a review from a team June 26, 2025 15:48
@github-actions github-actions bot added bug This issue is a bug. p2 beginning-contributor [Pilot] contributed between 0-2 PRs to the CDK labels Jun 26, 2025
Copy link
Collaborator

@aws-cdk-automation aws-cdk-automation left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(This review is outdated)

@camerondurham
Copy link
Author

Looks like there are existing invalid SIDs defined (DefaultSid-1, etc). Will go and fix them and revise this PR.

@aws-cdk-automation aws-cdk-automation dismissed their stale review June 27, 2025 05:40

✅ Updated pull request passes all PRLinter validations. Dismissing previous PRLinter review.

@camerondurham camerondurham force-pushed the iam-sid-validation branch 3 times, most recently from 10284c4 to d3dd64d Compare June 27, 2025 09:35
@camerondurham
Copy link
Author

camerondurham commented Jun 27, 2025

Current failure is due to replacing a policy within statements:

@aws-cdk-testing/framework-integ: Failed: /codebuild/output/src1576825816/src/github.com/aws/aws-cdk/packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/integ.cluster-encrypt-ephemeral-storage.js
@aws-cdk-testing/framework-integ: Failed: /codebuild/output/src1576825816/src/github.com/aws/aws-cdk/packages/@aws-cdk-testing/framework-integ/test/aws-events/test/integ.eventbus-cross-account-grants.js
@aws-cdk-testing/framework-integ: Failed: /codebuild/output/src1576825816/src/github.com/aws/aws-cdk/packages/@aws-cdk-testing/framework-integ/test/aws-events/test/integ.eventbus.js
@aws-cdk-testing/framework-integ: !!! This test contains destructive changes !!!
@aws-cdk-testing/framework-integ:     Stack: Stack - Resource: BuscdkStatement1D7D87B9D - Impact: WILL_DESTROY
@aws-cdk-testing/framework-integ:     Stack: Stack - Resource: BuscdkStatement2341A5B58 - Impact: WILL_DESTROY
@aws-cdk-testing/framework-integ: !!! If these destructive changes are necessary, please indicate this on the PR !!!
@aws-cdk-testing/framework-integ: Failed: /codebuild/output/src1576825816/src/github.com/aws/aws-cdk/packages/@aws-cdk-testing/framework-integ/test/aws-iam/test/integ.policy-statement-sid.js
@aws-cdk-testing/framework-integ: Failed: /codebuild/output/src1576825816/src/github.com/aws/aws-cdk/packages/@aws-cdk-testing/framework-integ/test/aws-logs/test/integ.log-group.js
@aws-cdk-testing/framework-integ: Error: Some changes were destructive!
@aws-cdk-testing/framework-integ:     at main (/codebuild/output/src1576825816/src/github.com/aws/aws-cdk/node_modules/@aws-cdk/integ-runner/lib/index.js:10274:11)
@aws-cdk-testing/framework-integ: Error: integ-runner exited with error code 1

However, the current CFN stack seems invalid now, with the current Sid containing invalid characters that would be rejected on deployment (https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_elements_sid.html)

The Sid element supports ASCII uppercase letters (A-Z), lowercase letters (a-z), and numbers (0-9).

@aws-cdk-testing/framework-integ: [~] AWS::Logs::LogGroup LogGroupLambdaAC756C5B
@aws-cdk-testing/framework-integ:  └─ [~] DataProtectionPolicy
@aws-cdk-testing/framework-integ:      └─ [~] .Statement:
@aws-cdk-testing/framework-integ:          └─ @@ -1,6 +1,6 @@
@aws-cdk-testing/framework-integ:             [ ] [
@aws-cdk-testing/framework-integ:             [ ]   {
@aws-cdk-testing/framework-integ:             [-]     "Sid": "audit-statement-cdk",
@aws-cdk-testing/framework-integ:             [+]     "Sid": "auditStatementCdk",
@aws-cdk-testing/framework-integ:             [ ]     "DataIdentifier": [

Next Steps

Plan to fix this build error

  • add skipValidation to PolicyStatement and update integ tests to avoid renaming existing Sid's, and add comment explaining why this had to be done

There appear to be other examples of using this approach in the CDK repo (see)

@aws-cdk-automation
Copy link
Collaborator

This PR cannot be merged because it has conflicts. Please resolve them. The PR will be considered stale and closed if it remains in an unmergeable state.

@camerondurham camerondurham force-pushed the iam-sid-validation branch 2 times, most recently from 5b3f98b to 94f3f9f Compare July 5, 2025 00:38
Comment on lines +364 to +372
skipValidation: true,
principals: [new ServicePrincipal('fargate.amazonaws.com')],
resources: ['*'],
actions: ['kms:GenerateDataKeyWithoutPlaintext'],
conditions: clusterConditions,
}));
key.addToResourcePolicy(new PolicyStatement({
sid: 'Allow grant creation permission for Fargate tasks.',
skipValidation: true,
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer to not make these changes and use the "default" skipValidation:false when using the PolicyStatement constructor. However, when I don't it causes build to fail due to destructive CDK changes during the integ test. So this is a workaround until I can get some confirmation whether the destructive integ test changes are "acceptable" or not.

For reference what I'm talking about, see: #34828 (comment)

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildv2Project1C6BFA3F-wQm2hXv2jqQv
  • Commit ID: 4729599
  • Result: FAILED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
beginning-contributor [Pilot] contributed between 0-2 PRs to the CDK bug This issue is a bug. p2
Projects
None yet
Development

Successfully merging this pull request may close these issues.

(aws-iam): Invalid Policy Statement Id strings should fail at build time
2 participants