Commit 1a166d2
committed
fix(s3,kms): use tags for granting cross-account
When a bucket or key grant targets a CDK-owned principal in another
account, the resource policy referenced the role by its ARN. S3 and KMS
validate that every principal in a resource policy exists at the time
the policy is deployed, so unless the principal's stack happens to
deploy first, the first deployment fails with "Invalid principal in
policy" (S3) or "Policy contains a statement with one or more invalid
principals" (KMS). The most common way to hit this is a cross-account +
cross-region CodePipeline: the action role lives in a support stack
with no deploy-ordering guarantee relative to the bucket/key stack, so
there is no deploy order that works on the first attempt.
ecr.Repository.grant() already solves this exact problem: when the
grantee is a CDK-owned resource in another account and the resource's
stack does not depend on the grantee's stack, tag the grantee
(aws-cdk:id) and trust the grantee's account root scoped by an
aws:PrincipalTag condition instead of the bare role ARN. Account-root
principals and condition values are not existence-checked, so deploy
order no longer matters, while the tag keeps the trust scoped to the
single intended role.
This change extracts the ECR implementation into a shared private
helper (aws-iam/lib/private/cross-account-grants.ts) and applies it to
S3 bucket grants and KMS key grants. ECR is refactored to use the
helper with no behavior change. Imported principals, same-account
grants, grants where the resource stack already depends on the principal
stack, and the KMS trustAccountIdentities are not affected.
The @aws-cdk/aws-iam:crossAccountGrantsViaPrincipalTag flag gates the S3
and KMS behavior and is off by default for existing apps. It acknowledges
that tag-scoped account-root trust survives role recreation and can be
spoofed in-account by principals allowed to tag roles.1 parent 08a2543 commit 1a166d2
43 files changed
Lines changed: 3183 additions & 93 deletions
File tree
- packages
- @aws-cdk-testing/framework-integ/test
- aws-kms/test
- integ.key-cross-account-grant.js.snapshot
- aws-s3/test
- integ.bucket-cross-account-grant.js.snapshot
- asset.44e9c4d7a5d3fd2d677e1a7e416b2b56f6b0104bd5eff9cac5557b4c65a9dc61
- aws-cdk-lib
- aws-codepipeline-actions/test/s3
- aws-ecr
- lib
- test
- aws-iam/lib/private
- aws-kms
- lib
- test
- aws-s3
- lib
- test
- cx-api
- lib
- test
Some content is hidden
Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 91 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 82 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 28 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
0 commit comments