-
Notifications
You must be signed in to change notification settings - Fork 4k
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
(dynamodb): grant calls no longer working when passed a ManagedPolicy #32795
Comments
Can you show me how do you define if you use fromXxx() method, you get |
@pahud it's defined within the same CDK stack I'm using the
There are a number of other calls in the stack to add other statements to the policy (many of which are constructed manually via |
Yes I am seeing
The error is thrown from
Reason:
which was fixed in this PR. so it should be a bug fix not a breaking change. |
I don't think that PR is the root cause of this bug? If I'm reading it correctly that PR adds |
If we read the git blame, that PR introduced According to your error message:
I am pretty sure it was thrown from here: I will bring this up to the team for further inputs. |
Cool, thanks @pahud! |
Hey Here are some references AWS IAM Documentation: "Specifying a Principal" AWS IAM Documentation: "Managed Policies and Inline Policies" Please let us know if there still a concern. |
@QuantumNeuralCoder as noted in the original bug description under Additional Information/Context:
If this is the correct behaviour then it should be implemented consistently across the various parts of CDK, which isn't the case at present. |
If you find any existing code of the latest release not following this behavior, that should be a bug we need to get it immediately fixed. Please let us know. Thank you. |
@pahud again, back to the original description:
|
@wilhen01 |
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. |
Hi, folks. It worked well when I made the PR...
aws-cdk/packages/aws-cdk-lib/aws-iam/lib/group.ts Lines 82 to 84 in 2b2443d
Adding aws-cdk/packages/aws-cdk-lib/aws-iam/lib/policy-statement.ts Lines 240 to 244 in 2b2443d
|
@wilhen01 const managedPolicy = new iam.ManagedPolicy(this, 'ManagedPolicy');
const table1 = dynamodb.TableV2.fromTableName(this, 'Table1', 'same-account-table');
table1.grantReadWrite(managedPolicy); // OK
const table2 = dynamodb.TableV2.fromTableArn(this, 'Table2', 'arn:aws:dynamodb:region:other-account:table/cross-account-table');
table2.grantReadWrite(managedPolicy); // Fail This behavior is intended because you cannot specify a ManagedPolicy as a principal of a resource policy. Note: when the stack's account is unspecified (rendered as You can specify account of Stack to avoid above senario: const stack = new cdk.Stack(app, 'Stack', { env: { account: '12345678' } });
const table = dynamodb.TableV2.fromTableArn(stack, 'Table', 'arn:aws:dynamodb:region:12345678:table/same-account-table');
table.grantReadWrite(managedPolicy); // OK |
@Tietew I'm specifying an account number in the stack, I'm referencing the table ARNs via imports, and none of it is cross-account anyway. It fails with the error given in the original bug report at the top of this issue. There's definitely some form of regression here. |
A new table creation works as expected. This is specific to usecase where we import the table using
Below does not
We are looking into revising this behavior. |
Describe the bug
Previously, it's been possible to use
.grant
functions on dynamoDB tables and pass them aManagedPolicy
(which is allowed by the type system sinceManagedPolicy
implementsIGrantable
).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
Last Known Working CDK Version
2.131.0
Expected Behavior
.grant
functions for dynamo tables work when passed aManagedPolicy
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
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
The text was updated successfully, but these errors were encountered: