Commit 24d2adf
authored
fix(dynamodb): resolve circular dependency with AccountRootPrincipal grants (#35983)
### Issue # (if applicable)
Closes #35967.
### Reason for this change
In CDK 2.222.0, PR #35554 fixed `addToResourcePolicy()` to actually work (it was previously a no-op). This exposed a circular dependency issue when using `grantReadData()` or other grant methods with `AccountRootPrincipal`.
When `AccountRootPrincipal` is used with grant methods, the IAM Grant system adds the policy to the table's resource policy (since it's in the same account). The resource policy statement included the table's ARN (`!GetAtt Table.Arn`), creating a circular dependency: Table → ResourcePolicy → Table.Arn → Table.
This is a regression that breaks existing user code that worked in 2.221.1.
### Description of changes
Applied the established KMS grant pattern to DynamoDB by adding `resourceSelfArns: ['*']` parameter to `Grant.addToPrincipalOrResource()` calls in the `combinedGrant` method.
**How it works:**
- `resourceArns` contains actual table ARNs → used for **principal policies** (IAM user/role policies)
- `resourceSelfArns: ['*']` → used for **resource policies** (table's resource policy)
- IAM Grant system automatically chooses which to use based on context
- No circular dependency because resource policy uses wildcard instead of `!GetAtt Table.Arn`
**Why wildcard is safe:**
- Wildcard is scoped to the table's resource policy (not global)
- Resource policy is attached to specific table resource
- Principal and Action fields still enforce access control
- Same pattern used by KMS for years in production
**Files modified:**
- `packages/aws-cdk-lib/aws-dynamodb/lib/table.ts` - Added `resourceSelfArns: ['*']` to `combinedGrant` method
- `packages/aws-cdk-lib/aws-dynamodb/lib/table-v2-base.ts` - Applied identical change for Table V2
- `packages/aws-cdk-lib/aws-dynamodb/README.md` - Added documentation about grant methods and resource policy interaction
**Before (causes circular dependency):**
```typescript
const table = new dynamodb.Table(this, 'Table', {
partitionKey: { name: 'id', type: dynamodb.AttributeType.STRING },
});
// This caused circular dependency error in 2.222.0
table.grantReadData(new iam.AccountRootPrincipal());
// Error: Circular dependency between resources: [Table]
```
**After (no circular dependency):**
```typescript
const table = new dynamodb.Table(this, 'Table', {
partitionKey: { name: 'id', type: dynamodb.AttributeType.STRING },
});
// This now works correctly
table.grantReadData(new iam.AccountRootPrincipal());
// ✓ Resource policy uses wildcard, no circular dependency
```
**CloudFormation template change:**
```json
{
"Resources": {
"Table": {
"Type": "AWS::DynamoDB::Table",
"Properties": {
"ResourcePolicy": {
"PolicyDocument": {
"Statement": [{
"Action": ["dynamodb:BatchGetItem", "dynamodb:GetItem", "dynamodb:Query", "dynamodb:Scan"],
"Effect": "Allow",
"Principal": { "AWS": "arn:aws:iam::ACCOUNT:root" },
"Resource": "*"
}]
}
}
}
}
}
}
```
### Describe any new or updated permissions being added
N/A - This fix does not add new permissions. It resolves how existing grant methods generate resource policies to avoid circular dependencies.
### Description of how you validated changes
- **Unit tests**: Added 2 new tests validating `AccountRootPrincipal` with grant methods
- `packages/aws-cdk-lib/aws-dynamodb/test/dynamodb.test.ts`: Test for Table V1
- `packages/aws-cdk-lib/aws-dynamodb/test/table-v2.test.ts`: Test for Table V2
- Both tests verify resource policy uses wildcard (`*`) to avoid circular dependency
- All 348 unit tests pass (346 existing + 2 new)
- **Integration tests**: Enhanced existing integration test with grant scenario
- `packages/@aws-cdk-testing/framework-integ/test/aws-dynamodb/test/integ.dynamodb.add-to-resource-policy.ts`
- Added TEST 3: Validates `grantWriteData(new AccountRootPrincipal())` works without circular dependency
- Successfully deployed to AWS (us-east-1)
- CloudFormation synthesis succeeds, no circular dependency errors
- Snapshots updated with GrantTable resource
- **Regression testing**: All 346 existing tests pass
- Grant methods with IAM Users still work
- Grant methods with IAM Roles still work
- Grant methods with Service Principals still work
- Tables with indexes work correctly
- Global tables (Table V2) work correctly
- Encrypted tables work correctly
### Checklist
- [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md)
----
*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*1 parent 771ea13 commit 24d2adf
File tree
10 files changed
+263
-15
lines changed- packages
- @aws-cdk-testing/framework-integ/test/aws-dynamodb/test
- integ.dynamodb.add-to-resource-policy.js.snapshot
- aws-cdk-lib/aws-dynamodb
- lib
- test
10 files changed
+263
-15
lines changedSome generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 59 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
115 | 115 | | |
116 | 116 | | |
117 | 117 | | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
118 | 177 | | |
119 | 178 | | |
120 | 179 | | |
| |||
Lines changed: 65 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 17 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
26 | 26 | | |
27 | 27 | | |
28 | 28 | | |
| 29 | + | |
29 | 30 | | |
30 | 31 | | |
31 | 32 | | |
| |||
66 | 67 | | |
67 | 68 | | |
68 | 69 | | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
69 | 86 | | |
70 | 87 | | |
71 | 88 | | |
| |||
0 commit comments