-
Notifications
You must be signed in to change notification settings - Fork 4.5k
feat(iam): immutable IAM roles #4501
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
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
57562b3
feat(iam): immutable IAM roles
skinny85 021bc24
Implement LazyPolicy.
skinny85 f8d7acc
Implement LazyPolicy in terms of a new 'deleteChild'.
rix0rrr 5590237
Switch to using L2 dependency mechanism
rix0rrr 04b9569
Merge branch 'master' into fix/immutable-role
rix0rrr e843694
Document `deleteChild`.
rix0rrr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| import { ConstructNode, Stack } from "@aws-cdk/core"; | ||
| import { Grant } from './grant'; | ||
| import { IGroup } from './group'; | ||
| import { IManagedPolicy } from './managed-policy'; | ||
| import { IPolicy, PolicyProps } from './policy'; | ||
| import { PolicyStatement } from './policy-statement'; | ||
| import { IPrincipal } from './principals'; | ||
| import { IRole } from './role'; | ||
| import { IUser } from './user'; | ||
|
|
||
| /** | ||
| * An immutable wrapper around an IRole that ignores all mutating operations, | ||
| * like attaching policies or adding policy statements. | ||
| * Useful in cases where you want to turn off CDK's automatic permissions management, | ||
| * and instead have full control over all permissions. | ||
| * Note: if you want to ignore all mutations for an externally defined role | ||
| * which was imported into the CDK with {@link Role.fromRoleArn}, you don't have to use this class - | ||
| * simply pass the property mutable = false when calling {@link Role.fromRoleArn}. | ||
| */ | ||
| export class ImmutableRole implements IRole { | ||
skinny85 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| public readonly assumeRoleAction = this.role.assumeRoleAction; | ||
| public readonly policyFragment = this.role.policyFragment; | ||
| public readonly grantPrincipal = this.role.grantPrincipal; | ||
| public readonly roleArn = this.role.roleArn; | ||
| public readonly roleName = this.role.roleName; | ||
| public readonly node = this.role.node; | ||
| public readonly stack = this.role.stack; | ||
|
|
||
| constructor(private readonly role: IRole) { | ||
| } | ||
|
|
||
| public addPolicy(_id: string, _props?: PolicyProps): IPolicy { | ||
| return new ImmutablePolicy(); | ||
| } | ||
|
|
||
| public attachInlinePolicy(_policy: IPolicy): void { | ||
| // do nothing | ||
| } | ||
|
|
||
| public addManagedPolicy(_policy: IManagedPolicy): void { | ||
| // do nothing | ||
| } | ||
|
|
||
| public addToPolicy(_statement: PolicyStatement): boolean { | ||
| // Not really added, but for the purposes of consumer code pretend that it was. | ||
| return true; | ||
| } | ||
|
|
||
| public grant(grantee: IPrincipal, ...actions: string[]): Grant { | ||
| return this.role.grant(grantee, ...actions); | ||
| } | ||
|
|
||
| public grantPassRole(grantee: IPrincipal): Grant { | ||
| return this.role.grantPassRole(grantee); | ||
| } | ||
| } | ||
|
|
||
| class ImmutablePolicy implements IPolicy { | ||
| public addStatements(..._statements: PolicyStatement[]): void { | ||
| // do nothing | ||
| } | ||
|
|
||
| public attachToGroup(_group: IGroup): void { | ||
| // do nothing | ||
| } | ||
|
|
||
| public attachToRole(_role: IRole): void { | ||
| // do nothing | ||
| } | ||
|
|
||
| public attachToUser(_user: IUser): void { | ||
| // do nothing | ||
| } | ||
|
|
||
| public get node(): ConstructNode { | ||
| throw new Error('IConstruct.node is not implemented for ImmutablePolicy'); | ||
| } | ||
|
|
||
| public get policyName(): string { | ||
| throw new Error('IPolicy.policyName is not implemented for ImmutablePolicy'); | ||
| } | ||
|
|
||
| public get stack(): Stack { | ||
| throw new Error('IResource.stack is not implemented for ImmutablePolicy'); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| import { Lazy } from '@aws-cdk/core'; | ||
| import { IPolicy, Policy } from './policy'; | ||
|
|
||
| export class LazyPolicy extends Policy implements IPolicy { | ||
| private _policyName!: string; | ||
|
|
||
| public get policyName(): string { | ||
| return Lazy.stringValue({ produce: () => { | ||
| if (this.isAttached) { | ||
| return super.policyName; | ||
| } else { | ||
| return this._policyName; | ||
| } | ||
| }}); | ||
| } | ||
|
|
||
| public set policyName(value: string) { | ||
| this._policyName = value; | ||
| } | ||
|
|
||
| public get ref(): string { | ||
| return Lazy.stringValue({ produce: () => { | ||
| if (this.isAttached) { | ||
| return super.policyName; | ||
| } else { | ||
| throw Error('Cannot get ref of unattached/empty LazyPolicy'); | ||
| } | ||
| }}); | ||
| } | ||
|
|
||
| protected prepare() { | ||
| if (!this.isMeaningful) { | ||
| this.node.deleteChild('Resource'); | ||
| } | ||
| } | ||
|
|
||
| protected validate(): string[] { | ||
| // Inherited validate would validate that we are attached and | ||
| // have statements. This version of policy does not validate that, | ||
| // it just won't render. | ||
| return []; | ||
| } | ||
|
|
||
| private get isMeaningful() { | ||
| return this.document.statementCount > 0 && this.isAttached; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This docstring can't be correct anymore.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. I'm actually struggling to write these docs, like I said here: #4501 (comment)