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(iam): Role.addManagedPolicy() does not work for imported roles IRole #8307 #31212

Merged
merged 11 commits into from
Dec 6, 2024

Conversation

stm29
Copy link
Contributor

@stm29 stm29 commented Aug 24, 2024

Issue #8307

Closes #8307

Reason for this change

  • This addManagedPolicy() just does nothing, neither throws warning nor fails.

Description of changes

  • This implements addManagedPolicy() for ImportedRole
  • This throws Error, when IRole is used with IManagedPolicy, due to constrains

Approaches decided

  • we can't change addManagedPolicy(policy: IManagedPolicy) to addManagedPolicy(policy: ManagedPolicy) (i.e., to ManagedPolicy) , like how attachInlinePolicy(policy: Policy), it will break for almost many customers.
  • We can't use IRole & IManagedPolicy to do the work at the same time, so we need to allow ManagedPolicy in params, and throw Error when IManagedPolicy is being sent, because of how attachToRole() will be used by addManagedPolicy()
  • This PR Implements follows,

    • enables addManagedPolicy()
    • Accepts ManagedPolicy
    • Throws understandable Error, when IRole is used with IManagedPolicy.

Description of how you validated changes

  • unit & integration tests

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 August 24, 2024 19:01
@github-actions github-actions bot added bug This issue is a bug. effort/small Small work item – less than a day of effort p2 repeat-contributor [Pilot] contributed between 3-5 PRs to the CDK labels Aug 24, 2024
…ill result in IRole used with IManagedPolicy
@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 Aug 25, 2024
This was referenced Aug 26, 2024
Copy link

@epoctic epoctic left a comment

Choose a reason for hiding this comment

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

Changes seem reasonable to me.

@stm29
Copy link
Contributor Author

stm29 commented Oct 15, 2024

Can I get one more community review?

@stm29
Copy link
Contributor Author

stm29 commented Oct 20, 2024

Communicated in #contributing channel. waiting for one more community review.

Copy link
Contributor

@tmokmss tmokmss left a comment

Choose a reason for hiding this comment

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

Thank you for the fix! I left some comments :)

policy.attachToRole(this);
} catch (e) {
if (e instanceof Error && e.message === 'policy.attachToRole is not a function') {
throw new Error('Can\'t Combine IRole with IManagedPolicy. use ManagedPolicy directly.');
Copy link
Contributor

Choose a reason for hiding this comment

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

Throwing an error from this function is also a breaking change. Can we just add a warning as the previous behavior?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@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 Oct 20, 2024
@stm29 stm29 changed the title fix(iam): Role.addManagedPolicy() does not work for imported roles #8307 fix(iam): Role.addManagedPolicy() does not work for imported roles IRole #8307 Oct 21, 2024
} else {
Annotations.of(this).addWarningV2(
'@aws-cdk/aws-iam:IRoleCantBeUsedWithIManagedPolicy',
'Can\'t combine IRole with IManagedPolicy. Use ManagedPolicy directly.',
Copy link
Contributor

@tmokmss tmokmss Oct 22, 2024

Choose a reason for hiding this comment

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

We can add more information like ${policy.managedPolicyArn} to imported role: ${this.roleName} as in the original code to make it clearer why and where the warning is emitted. So that we do not have to add another warning from a caller.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Corrected with One Common message.

Copy link
Contributor

@tmokmss tmokmss left a comment

Choose a reason for hiding this comment

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

LGTM, thanks!

@Leo10Gama Leo10Gama self-assigned this Oct 25, 2024
@aws-cdk-automation aws-cdk-automation added the pr/needs-maintainer-review This PR needs a review from a Core Team Member label Oct 29, 2024
@Leo10Gama
Copy link
Member

Hi @stm29 thanks for your contribution! Reading through the changes, I'm trying to understand the nature of the changes being made. The PR description mentions you're adding functionality to IRole, but it looks like you're adding it to ImportedRole. Could you please update the description to better reflect this?

I also did some digging through the rest of the codebase, and it seems like some of the other extensions of IRole have other implementations of addManagedPolicy(), like here:

  public addManagedPolicy(policy: IManagedPolicy): void {
    this.managedPolicies.push(policy);
  }

Would a change like this work instead of the more complex logic?

It also looks like the changes in the S3 notifications resource are minor, so I would agree that moving that change to another PR and opening a separate issue would be best, as long as the tests that currently exist for it still pass with this change. Though I'm not quite sure what the issue is in this case. It seems like a few other libraries already call on the addManagedPolicy() method, so is there something specific to S3 in this regard?

@Leo10Gama Leo10Gama removed the pr/needs-maintainer-review This PR needs a review from a Core Team Member label Nov 14, 2024
@aws-cdk-automation aws-cdk-automation added the pr/needs-maintainer-review This PR needs a review from a Core Team Member label Nov 14, 2024
Copy link

codecov bot commented Nov 27, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 78.67%. Comparing base (f4c19c7) to head (36deb38).
Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main   #31212   +/-   ##
=======================================
  Coverage   78.67%   78.67%           
=======================================
  Files         107      107           
  Lines        7237     7237           
  Branches     1329     1329           
=======================================
  Hits         5694     5694           
  Misses       1357     1357           
  Partials      186      186           
Flag Coverage Δ
suite.unit 78.67% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Components Coverage Δ
packages/aws-cdk 78.67% <ø> (ø)

@stm29
Copy link
Contributor Author

stm29 commented Nov 27, 2024

@Leo10Gama , Thanks for the review.

I'm trying to understand the nature of the changes being made. The PR description mentions you're adding functionality to IRole, but it looks like you're adding it to ImportedRole. Could you please update the description to better reflect this?

Updated this with correct ImportedRole


Would a change like this work instead of the more complex logic?

Indeed Logic is simple, following is the working Logic which adds Policy to the Role. and this is same as how other Implementations are already in place.

policy.attachToRole(this);

Other if ... else and type predicate is to make sure Implementation is working as expected.


It seems like a few other libraries already call on the addManagedPolicy() method, so is there something specific to S3 in this regard?

Sorry for the confusion, There is no change in notifications-resource-handler.ts file, I have reverted to main.

Quick Question: Should we need to add Unit Tests in all the possible occurrence of IManagedPolicy with IRole, or Just UnitTests in packages/aws-cdk-lib/aws-iam/test/imported-role.test.ts is sufficient?
If UnitTests in imported-role.test.ts is sufficient, then notification.test.ts can also be reverted to main


GavinZZ
GavinZZ previously approved these changes Dec 4, 2024
Copy link
Contributor

mergify bot commented Dec 4, 2024

Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@aws-cdk-automation aws-cdk-automation removed the pr/needs-maintainer-review This PR needs a review from a Core Team Member label Dec 4, 2024
Copy link
Contributor

mergify bot commented Dec 4, 2024

Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

Copy link
Contributor

mergify bot commented Dec 5, 2024

This pull request has been removed from the queue for the following reason: pull request branch update failed.

The pull request can't be updated.

You should look at the reason for the failure and decide if the pull request needs to be fixed or if you want to requeue it.

If you want to requeue this pull request, you need to post a comment with the text: @mergifyio requeue

@mergify mergify bot dismissed GavinZZ’s stale review December 5, 2024 22:37

Pull request has been modified.

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

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

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

Copy link
Contributor

mergify bot commented Dec 5, 2024

Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@GavinZZ GavinZZ merged commit c78ef1b into aws:main Dec 6, 2024
16 of 18 checks passed
Copy link

github-actions bot commented Dec 6, 2024

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 6, 2024
@stm29 stm29 deleted the issue_8307 branch December 6, 2024 03:41
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug This issue is a bug. effort/small Small work item – less than a day of effort p2 repeat-contributor [Pilot] contributed between 3-5 PRs to the CDK
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Role.addManagedPolicy does not work for imported roles
7 participants