Skip to content

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.

Reference: 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).

Issue # (if applicable)

Closes #34819

Reason for this change

CDK build allows users to write invalid statement ids containing dashes when building policy statements, then failed on deployment. To reduce time to a successful deployment, I believe the CDK should throw an exception at build time to help user make a fix locally before a failed deployment.

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
Resolves aws#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
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 aws-cdk-automation added the pr/needs-community-review This PR needs a review from a Trusted Community Member or Core Team Member. label Aug 1, 2025
@github-actions github-actions bot added the effort/medium Medium work item – several days of effort label Aug 1, 2025
@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildv2Project1C6BFA3F-wQm2hXv2jqQv
  • Commit ID: 411bbd2
  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

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

@pahud
Copy link
Contributor

pahud commented Nov 11, 2025

Hi,

This PR has conflicts that need to be resolved before it can be reviewed.

@pahud pahud removed the pr/needs-community-review This PR needs a review from a Trusted Community Member or Core Team Member. label Nov 11, 2025
@aws-cdk-automation
Copy link
Collaborator

This PR has been in the MERGE CONFLICTS state for 3 weeks, and looks abandoned. Note that PRs with failing linting check or builds are not reviewed, please ensure your build is passing

To prevent automatic closure:

  • Resume work on the PR
  • OR request an exemption by adding a comment containing 'Exemption Request' with justification e.x "Exemption Request: "
  • OR request clarification by adding a comment containing 'Clarification Request' with a question e.x "Clarification Request: "

This PR will automatically close in 14 days if no action is taken.

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. effort/medium Medium work item – several days of effort 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

3 participants