aws-cdk: (dynamodb): grant calls no longer working when passed a ManagedPolicy
Describe the bug
Previously, it’s been possible to use .grant functions on dynamoDB tables and pass them a ManagedPolicy (which is allowed by the type system since ManagedPolicy implements IGrantable).
Under 2.174.0, that results in an error:
Cannot use a ManagedPolicy as the 'Principal' or 'NotPrincipal' in an IAM Policy.
This is a change in behaviour, and it’s also inconsistent with e.g. S3 where it’s still possible to call bucket.grantReadWrite(managedPolicy);
Regression Issue
- Select this option if this issue appears to be a regression.
Last Known Working CDK Version
2.131.0
Expected Behavior
.grant functions for dynamo tables work when passed a ManagedPolicy and add the appropriate policy statements.
Current Behavior
Error thrown: Cannot use a ManagedPolicy as the ‘Principal’ or ‘NotPrincipal’ in an IAM Policy
Reproduction Steps
const table = TableV2.fromTableArn(
this,
'Table',
arnForTable,
);
table.grantReadWriteData(managedPolicy);
Error is visible in CDK unit tests which verify the managed policy composition.
Possible Solution
No response
Additional Information/Context
I’m not sure if the new behaviour is correct in that a ManagedPolicy is not in fact a Principal, and hence the previous behaviour shouldn’t have been allowed, but it should be consistent and obvious from documentation where you can and can’t use the convenience grant functions. At present they work in this scenario for other library areas e.g. S3.
CDK CLI Version
2.174.0
Framework Version
No response
Node.js Version
18
OS
Mac OS
Language
TypeScript
Language Version
5.x
Other information
No response
About this issue
- Original URL
- State: open
- Created 2 years ago
- Comments: 16 (16 by maintainers)
A new table creation works as expected. This is specific to usecase where we import the table using
fromTableArn. Because we resolve the account the addToPrincipalOrResource proceeds beyond the check (grant.js:141) into creating new PolicyStatement and fails as we explicitly throw the error for ManagedPolicy. So for a Managed Policy, Below works…Below does not
We are looking into revising this behavior.
We discussed this internally. This is the original source of this feature. Here the grant statements effectively mutate the PolicyDocument itself. Its effective only after a principal is specified or until you attach the policy to an identity.
We will bring TableV2 to parity with this.
@wilhen01 Your point seems to be valid. Let me discuss internally and circle back. But blocking granting of permissions to managed policies which are essentially container of permissions is the right behavior.
@pahud again, back to the original description:
If we read the git blame, that PR introduced
ManagedPolicyGrantPrincipalthat implementsIPrincipal.According to your error message:
I am pretty sure it was thrown from here:
https://github.com/aws/aws-cdk/blame/bbdd42c8f45916d5c6945f3429916f6199d2ec66/packages/aws-cdk-lib/aws-iam/lib/managed-policy.ts#L335C14-L340
I will bring this up to the team for further inputs.
I don’t think that PR is the root cause of this bug? If I’m reading it correctly that PR adds
IGrantabletoManagedPolicy, but my code was working as of2.131.0andManagedPolicyimplementsIGrantablein that version of the library so I think something else has changed between 2.131.0 and the current version to cause this issue…