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): only create policies for imported roles if they're in the same account as the owning stack #3716

Merged
merged 2 commits into from
Sep 6, 2019

Conversation

skinny85
Copy link
Contributor

@skinny85 skinny85 commented Aug 19, 2019

We also allow adding policies to the imported role if the owning stack does not have an account specified.

Fixes #2985
Fixes #3025


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license

@skinny85 skinny85 requested a review from rix0rrr August 19, 2019 23:08
@mergify
Copy link
Contributor

mergify bot commented Aug 19, 2019

Pull Request Checklist

  • Testing
  • Unit test added (prefer to add a new test rather than modify existing tests)
  • CLI change? Re-run/add CLI integration tests
  • Documentation
  • Inline docs: make sure all public APIs are documented (copy & paste from official AWS docs)
  • README: update module README
  • Design: for significant features, follow the design process
  • Title uses the format type(scope): text
  • Type: fix, feat, refactor go into CHANGELOG, chore is hidden
  • Scope: name of the module without the aws- or cdk- prefix or postfix (e.g. s3 instead of aws-s3-deployment)
  • Style: use all lower-case, do not end with a period
  • Description
  • Rationale: describe rationale of change and approach taken
  • Issues: Indicate issues fixed via: fixes #xxx or closes #xxx
  • Breaking?: last paragraph: BREAKING CHANGE: <describe what changed + link for details>
  • Sensitive Modules (requires 2 PR approvers)
  • IAM document library (in @aws-cdk/aws-iam)
  • EC2 security groups and ACLs (in @aws-cdk/aws-ec2)
  • Grant APIs (if not based on official documentation with a reference)

@skinny85
Copy link
Contributor Author

skinny85 commented Aug 20, 2019

I'm actually wondering whether the logic around stacks without account set should not be a little different....

When providing the roleArn to fromRoleArn, you can use the AWS::AccountId pseudo-parameter inside, right? So, we can now say that if your stack does not have account set, we will add new policies to the Role imported in its scope only if the account part of roleArn is equal to AWS::AccountId. If you have a concrete account put there, we will ignore all calls to add a policy.

So:

const stack = new Stack(app, 'S');

// this role will create new policies
iam.Role.fromRoleArn(stack, 'R1',
  `arn:aws:iam::${Aws.ACCOUNT_ID}:role/MyRole`);

// this role will ignore all calls to `addToRolePolicy` and `attachInlinePolicy`
iam.Role.fromRoleArn(stack, 'R2',
  'arn:aws:iam::123456789012:role/MyRole');

Thoughts on this @rix0rrr ?

@skinny85
Copy link
Contributor Author

@rix0rrr can you address my question from above?

@mergify
Copy link
Contributor

mergify bot commented Aug 23, 2019

Pull Request Checklist

  • Testing
  • Unit test added (prefer to add a new test rather than modify existing tests)
  • CLI change? Re-run/add CLI integration tests
  • Documentation
  • Inline docs: make sure all public APIs are documented (copy & paste from official AWS docs)
  • README: update module README
  • Design: for significant features, follow the design process
  • Title uses the format type(scope): text
  • Type: fix, feat, refactor go into CHANGELOG, chore is hidden
  • Scope: name of the module without the aws- or cdk- prefix or postfix (e.g. s3 instead of aws-s3-deployment)
  • Style: use all lower-case, do not end with a period
  • Description
  • Rationale: describe rationale of change and approach taken
  • Issues: Indicate issues fixed via: fixes #xxx or closes #xxx
  • Breaking?: last paragraph: BREAKING CHANGE: <describe what changed + link for details>
  • Sensitive Modules (requires 2 PR approvers)
  • IAM document library (in @aws-cdk/aws-iam)
  • EC2 security groups and ACLs (in @aws-cdk/aws-ec2)
  • Grant APIs (if not based on official documentation with a reference)

@rix0rrr
Copy link
Contributor

rix0rrr commented Aug 23, 2019

Thoughts on this @rix0rrr ?

Don't like it very much, because it can still be the case that the accounts are specified and the same.

All in all--isn't it easier to give control to the user, by providing them a class as described in #3753?

@skinny85
Copy link
Contributor Author

Thoughts on this @rix0rrr ?

Don't like it very much, because it can still be the case that the accounts are specified and the same.

All in all--isn't it easier to give control to the user, by providing them a class as described in #3753?

I don't think the 2 are exclusive. I want to do #3753 as well. This is more about changing the current behavior, which is obviously incorrect for the case where the ARN account and the Stack account are different.

@michaeljfazio
Copy link

Hanging out to see this issue fixed. Code pipeline basically useless in cross-account environments :(

@skinny85 skinny85 force-pushed the feat/imported-role-add-policy branch 2 times, most recently from d551d6e to f9d731d Compare August 30, 2019 01:50
@skinny85
Copy link
Contributor Author

After talking with @rix0rrr offline, submitted a new revision. I've covered a lot more cases, included inline policies added with attachInlinePolicy in the logic, and also added the option of specifying that you want the returned Role to be immutable by passing { mutable: false } to the fromRoleArn function (I called it mutable, as I didn't like that immutable in the default (false) was a double negative).

I would recommend starting the review with looking at the unit tests, as they document what is the behavior we expect for the various cases (and there is a lot of different cases here!).

packages/@aws-cdk/aws-iam/test/test.role.ts Outdated Show resolved Hide resolved
packages/@aws-cdk/aws-iam/test/test.role.ts Outdated Show resolved Hide resolved
packages/@aws-cdk/aws-iam/test/test.role.ts Outdated Show resolved Hide resolved
packages/@aws-cdk/aws-iam/lib/role.ts Outdated Show resolved Hide resolved
packages/@aws-cdk/aws-iam/lib/role.ts Outdated Show resolved Hide resolved
packages/@aws-cdk/aws-iam/lib/role.ts Outdated Show resolved Hide resolved
packages/@aws-cdk/aws-iam/package.json Show resolved Hide resolved
@rix0rrr
Copy link
Contributor

rix0rrr commented Aug 30, 2019

This PR closes #2985, by the way.

@skinny85 skinny85 added the pr/do-not-merge This PR should not be merged at this time. label Aug 30, 2019
@skinny85 skinny85 changed the base branch from master to chore/iam-tests-2-jest August 31, 2019 01:40
@skinny85 skinny85 force-pushed the feat/imported-role-add-policy branch from f9d731d to ee71990 Compare August 31, 2019 01:40
@skinny85
Copy link
Contributor Author

I've re-worked the tests completely; @rix0rrr let me know how you like their structure now.

@rix0rrr
Copy link
Contributor

rix0rrr commented Sep 2, 2019

Love it! Still a shit-ton to read, but this structure is great!

@rix0rrr
Copy link
Contributor

rix0rrr commented Sep 2, 2019

Still prefer that we change the mutable terminology.

@skinny85 skinny85 force-pushed the feat/imported-role-add-policy branch from ee71990 to 65c988f Compare September 4, 2019 01:09
@skinny85 skinny85 changed the base branch from chore/iam-tests-2-jest to master September 4, 2019 01:10
@skinny85
Copy link
Contributor Author

skinny85 commented Sep 4, 2019

Submitted a new revision.

@rix0rrr can you go through the tests, and confirm all of them have (in your mind) the correct behavior right now? I feel like a few could go either way, so I just wanted to confirm the expectations for the behavior are the same.

@skinny85
Copy link
Contributor Author

skinny85 commented Sep 4, 2019

Actually, if you could confirm that as well @michaeljfazio , that would be super helpful :)

Copy link
Contributor

@rix0rrr rix0rrr left a comment

Choose a reason for hiding this comment

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

@rix0rrr can you go through the tests, and confirm all of them have (in your mind) the correct behavior right now?

I believe they do (or at least, the most important ones I looked at). Do you have any particular ones in mind you're concerned about?

We allow attaching policies to the imported role if the account passed in the ARN
matches the account of the stack the policy belongs to,
or if either one of those accounts was not specified (i.e., is env-agnostic),
or if the ARN used for importing was dynamic.

Fixes aws#2985
Fixes aws#3025
@skinny85 skinny85 force-pushed the feat/imported-role-add-policy branch from 65c988f to 792ff10 Compare September 5, 2019 18:26
@skinny85
Copy link
Contributor Author

skinny85 commented Sep 5, 2019

Included last comments and rebased.

@skinny85 skinny85 removed the pr/do-not-merge This PR should not be merged at this time. label Sep 6, 2019
@mergify
Copy link
Contributor

mergify bot commented Sep 6, 2019

Thank you for contributing! Your pull request is now being automatically merged.

@mergify
Copy link
Contributor

mergify bot commented Sep 6, 2019

Thank you for contributing! Your pull request is now being automatically merged.

@mergify mergify bot merged commit 87db7aa into aws:master Sep 6, 2019
@skinny85 skinny85 deleted the feat/imported-role-add-policy branch September 6, 2019 16:12
@NGL321 NGL321 added the contribution/core This is a PR that came from AWS. label Sep 27, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contribution/core This is a PR that came from AWS.
Projects
None yet
4 participants