Skip to content
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

fix(dynamodb): addtoresourcepolicy fix for table (v1) construct #31516

Closed
wants to merge 6 commits into from

Conversation

LeeroyHannigan
Copy link
Contributor

Issue # (if applicable)

Closes #30793.

Reason for this change

addToResourcePolicy() was not correctly adding a policy statement to the table

Description of changes

Fixed the method to correctly add the policy to the table constructor.

Description of how you validated changes

Tests added

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 September 21, 2024 09:06
@github-actions github-actions bot added beginning-contributor [Pilot] contributed between 0-2 PRs to the CDK bug This issue is a bug. effort/small Small work item – less than a day of effort p2 labels Sep 21, 2024
@LeeroyHannigan LeeroyHannigan changed the title fix(dynamodb): addtoresourcepolicy fix for table (v1) fix(dynamodb): addtoresourcepolicy fix for table (v1) construct Sep 21, 2024
@@ -564,7 +564,7 @@ export abstract class TableBase extends Resource implements ITable, iam.IResourc
* @param actions The set of actions to allow (i.e. "dynamodb:PutItem", "dynamodb:GetItem", ...)
*/
public grant(grantee: iam.IGrantable, ...actions: string[]): iam.Grant {
return iam.Grant.addToPrincipalOrResource({
return iam.Grant.addToPrincipal({

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does this need to become addToPrincipal? We are for example already using the resource policy to grant cross-account access to a table directly. We build the policy in CDK from external account ArnPrincipals.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because addToPrincipalOrResource never adds to the resource, as IAM is missing methods. So I'm putting it back to how it was before I changed it until I have time to implement the missing methods in IAM

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't fully understand what methods are missing. Looking at Grant, it seems to be handling this already, if given the resource in the call: https://github.com/aws/aws-cdk/blob/main/packages/aws-cdk-lib/aws-iam/lib/grant.ts#L145-L157. It's just that Table.addToResourcePolicy is not functional, but instead adds statements to a policy instance not bound with the internal CfnTable.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@LeeroyHannigan I want to check if I follow your line of thoughts here.

I see the iam.Grant.addToPrincipalOrResource is implemented here. It basically does 2 things, call addToPrincipal and a bit further down on line 151, it calls options.resource.addToResourcePolicy, which means the Table.addToResourcePolicy. Since the method is reportedly a no-op (from #30793), you are saying your change here from addToPrincipalOrResource to addToPrincipal will not change any actual behaviour.

I agree with that. But if I understand @everilae correctly, even though Table.addToResourcePolicy was a no-op, there may be CDK users who are currently relying on what iam.Grant.addToPrincipalOrResource returns (it returns the resource policy statement whereas iam.Grant.addToPrincipal returns principal policy statement.). This allows CDK users to use this method to build the resource policy instead of creating their own.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi,

We rely on the "...OrResource" part, or would like to, once all this is fixed. If the call is replaced with just "addToPrincipal", then attempts at granting to external accounts would not work.

@@ -958,11 +941,11 @@ export abstract class TableBase extends Resource implements ITable, iam.IResourc
produce: () => this.hasIndex ? `${arn}/index/*` : Aws.NO_VALUE,
})),
];
const ret = iam.Grant.addToPrincipalOrResource({
const ret = iam.Grant.addToPrincipal({

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above, this would break Table v1 for us.

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

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

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

@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 Oct 12, 2024
@samson-keung samson-keung self-assigned this Dec 5, 2024
@aws-cdk-automation
Copy link
Collaborator

This PR has been in the MERGE CONFLICTS state for 3 weeks, and looks abandoned. To keep this PR from being closed, please continue work on it. If not, it will automatically be closed in a week.

@samson-keung
Copy link
Contributor

Hi @LeeroyHannigan, I see there are some merge conflicts as well as comments about this can be a breaking change. Are you planning to update this PR?

@LeeroyHannigan
Copy link
Contributor Author

Hi @LeeroyHannigan, I see there are some merge conflicts as well as comments about this can be a breaking change. Are you planning to update this PR?

Hi, yes I plan to fix this. Although I don't believe there are any breaking changes, I changed the methods previously, but they never worked as intended and this change just reverts them back to original state.

principals: [new iam.AccountRootPrincipal()],
resources: ['*'],
}));

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the change in this PR is already covered in the unit test. Integ test does not seem necessary to me because we are not using new L1 props. Is there anything the integ test would catch while the unit test cannot?

@@ -564,7 +564,7 @@ export abstract class TableBase extends Resource implements ITable, iam.IResourc
* @param actions The set of actions to allow (i.e. "dynamodb:PutItem", "dynamodb:GetItem", ...)
*/
public grant(grantee: iam.IGrantable, ...actions: string[]): iam.Grant {
return iam.Grant.addToPrincipalOrResource({
return iam.Grant.addToPrincipal({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@LeeroyHannigan I want to check if I follow your line of thoughts here.

I see the iam.Grant.addToPrincipalOrResource is implemented here. It basically does 2 things, call addToPrincipal and a bit further down on line 151, it calls options.resource.addToResourcePolicy, which means the Table.addToResourcePolicy. Since the method is reportedly a no-op (from #30793), you are saying your change here from addToPrincipalOrResource to addToPrincipal will not change any actual behaviour.

I agree with that. But if I understand @everilae correctly, even though Table.addToResourcePolicy was a no-op, there may be CDK users who are currently relying on what iam.Grant.addToPrincipalOrResource returns (it returns the resource policy statement whereas iam.Grant.addToPrincipal returns principal policy statement.). This allows CDK users to use this method to build the resource policy instead of creating their own.

@aws-cdk-automation
Copy link
Collaborator

This PR has been deemed to be abandoned, and will be automatically closed. Please create a new PR for these changes if you think this decision has been made in error.

@aws-cdk-automation aws-cdk-automation added the closed-for-staleness This issue was automatically closed because it hadn't received any attention in a while. label Dec 16, 2024
Copy link

Comments on closed issues and PRs are hard for our team to see.
If you need help, please open a new issue that references this one.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 16, 2024
@aws-cdk-automation aws-cdk-automation removed the pr/needs-community-review This PR needs a review from a Trusted Community Member or Core Team Member. label Dec 16, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
beginning-contributor [Pilot] contributed between 0-2 PRs to the CDK bug This issue is a bug. closed-for-staleness This issue was automatically closed because it hadn't received any attention in a while. effort/small Small work item – less than a day of effort p2
Projects
None yet
Development

Successfully merging this pull request may close these issues.

aws-dynamodb: addToResourcePolicy has no effect
4 participants