-
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
feat(iam): Role.withoutPolicyUpdates()
#5569
Changes from 5 commits
c571295
e017fb6
f81c312
7349f2a
6e0d1e3
fe49f05
10c3eb0
1973409
334cb8c
39de5a1
717d052
8ef7ab5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { Grant } from './grant'; | ||
import { IManagedPolicy } from './managed-policy'; | ||
import { Policy } from './policy'; | ||
import { PolicyStatement } from './policy-statement'; | ||
import { IPrincipal } from './principals'; | ||
import { IRole } from './role'; | ||
|
||
/** | ||
* An immutable wrapper around an IRole | ||
* | ||
* This wrapper 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 { | ||
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 attachInlinePolicy(_policy: Policy): 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); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,6 +56,22 @@ export interface PolicyProps { | |
* @default - No statements. | ||
*/ | ||
readonly statements?: PolicyStatement[]; | ||
|
||
/** | ||
* Whether an `AWS::IAM::Policy` must be created | ||
* | ||
* Unless set to `true`, this `Policy` construct will not materialize to an | ||
* `AWS::IAM::Policy` CloudFormation resource in case it would have no effect | ||
* (for example, if it remains unattached to an IAM identity or if it has no | ||
* statements). This is generally desired behavior, since it prevents | ||
* creating invalid--and hence undeployable--CloudFormation templates. | ||
* | ||
* In cases where you know the policy must be created and it is actually | ||
* an error if no statements have been added to it, you can se this to `true`. | ||
* | ||
* @default false | ||
*/ | ||
readonly mustCreate?: boolean; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not crazy about this name... How about |
||
} | ||
|
||
/** | ||
|
@@ -79,16 +95,12 @@ export class Policy extends Resource implements IPolicy { | |
*/ | ||
public readonly document = new PolicyDocument(); | ||
|
||
/** | ||
* The name of this policy. | ||
* | ||
* @attribute | ||
*/ | ||
public readonly policyName: string; | ||
|
||
private readonly _policyName: string; | ||
private readonly roles = new Array<IRole>(); | ||
private readonly users = new Array<IUser>(); | ||
private readonly groups = new Array<IGroup>(); | ||
private readonly mustCreate: boolean; | ||
private referenceTaken = false; | ||
|
||
constructor(scope: Construct, id: string, props: PolicyProps = {}) { | ||
super(scope, id, { | ||
|
@@ -107,7 +119,8 @@ export class Policy extends Resource implements IPolicy { | |
groups: undefinedIfEmpty(() => this.groups.map(g => g.groupName)), | ||
}); | ||
|
||
this.policyName = this.physicalName!; | ||
this._policyName = this.physicalName!; | ||
this.mustCreate = props.mustCreate !== undefined ? props.mustCreate : false; | ||
|
||
if (props.users) { | ||
props.users.forEach(u => this.attachToUser(u)); | ||
|
@@ -160,19 +173,60 @@ export class Policy extends Resource implements IPolicy { | |
group.attachInlinePolicy(this); | ||
} | ||
|
||
/** | ||
* The name of this policy. | ||
* | ||
* @attribute | ||
*/ | ||
public get policyName(): string { | ||
this.referenceTaken = true; | ||
return this._policyName; | ||
} | ||
|
||
protected validate(): string[] { | ||
const result = new Array<string>(); | ||
|
||
// validate that the policy document is not empty | ||
if (this.document.isEmpty) { | ||
result.push('Policy is empty. You must add statements to the policy'); | ||
if (this.mustCreate) { | ||
result.push('Policy created with mustCreate=true is empty. You must add statements to the policy'); | ||
} | ||
if (!this.mustCreate && this.referenceTaken) { | ||
result.push('Policy name has been read of empty policy. You must add statements to the policy so it can exist.'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can word this a little more clearly... how about: |
||
} | ||
} | ||
|
||
// validate that the policy is attached to at least one principal (role, user or group). | ||
if (this.groups.length + this.users.length + this.roles.length === 0) { | ||
result.push(`Policy must be attached to at least one principal: user, group or role`); | ||
if (!this.isAttached) { | ||
if (this.mustCreate) { | ||
result.push(`Policy created with mustCreate=true must be attached to at least one principal: user, group or role`); | ||
} | ||
if (!this.mustCreate && this.referenceTaken) { | ||
result.push('Policy name has been read of unattached policy. Attach to at least one principal: user, group or role.'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here: |
||
} | ||
} | ||
|
||
return result; | ||
} | ||
|
||
protected prepare() { | ||
// Remove the resource if it shouldn't exist. This will prevent it from being rendered to the template. | ||
if (!this.shouldExist) { | ||
this.node.removeChild('Resource'); | ||
} | ||
} | ||
|
||
/** | ||
* Whether the policy resource has been attached to any identity | ||
*/ | ||
private get isAttached() { | ||
return this.groups.length + this.users.length + this.roles.length > 0; | ||
} | ||
|
||
/** | ||
* Whether the policy resource should be created | ||
*/ | ||
private get shouldExist() { | ||
return this.mustCreate || this.referenceTaken || (!this.document.isEmpty && this.isAttached); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import '@aws-cdk/assert/jest'; | ||
import { Stack } from '@aws-cdk/core'; | ||
import * as iam from '../lib'; | ||
|
||
// tslint:disable:object-literal-key-quotes | ||
|
||
describe('ImmutableRole', () => { | ||
let stack: Stack; | ||
let mutableRole: iam.IRole; | ||
let immutableRole: iam.IRole; | ||
|
||
beforeEach(() => { | ||
stack = new Stack(); | ||
mutableRole = new iam.Role(stack, 'MutableRole', { | ||
assumedBy: new iam.AnyPrincipal(), | ||
}); | ||
immutableRole = new iam.ImmutableRole(mutableRole); | ||
}); | ||
|
||
test('ignores calls to attachInlinePolicy', () => { | ||
const user = new iam.User(stack, 'User'); | ||
const policy = new iam.Policy(stack, 'Policy', { | ||
statements: [new iam.PolicyStatement({ | ||
resources: ['*'], | ||
actions: ['s3:*'], | ||
})], | ||
users: [user], | ||
}); | ||
|
||
immutableRole.attachInlinePolicy(policy); | ||
|
||
expect(stack).toHaveResource('AWS::IAM::Policy', { | ||
"PolicyDocument": { | ||
"Statement": [ | ||
{ | ||
"Action": "s3:*", | ||
"Resource": "*", | ||
"Effect": "Allow", | ||
}, | ||
], | ||
"Version": "2012-10-17", | ||
}, | ||
"PolicyName": "Policy23B91518", | ||
"Users": [ | ||
{ | ||
"Ref": "User00B015A1", | ||
}, | ||
], | ||
}); | ||
}); | ||
|
||
test('ignores calls to addManagedPolicy', () => { | ||
mutableRole.addManagedPolicy({ managedPolicyArn: 'Arn1' }); | ||
|
||
immutableRole.addManagedPolicy({ managedPolicyArn: 'Arn2' }); | ||
|
||
expect(stack).toHaveResourceLike('AWS::IAM::Role', { | ||
"ManagedPolicyArns": [ | ||
'Arn1', | ||
], | ||
}); | ||
}); | ||
|
||
test('ignores calls to addToPolicy', () => { | ||
mutableRole.addToPolicy(new iam.PolicyStatement({ | ||
resources: ['*'], | ||
actions: ['s3:*'], | ||
})); | ||
|
||
immutableRole.addToPolicy(new iam.PolicyStatement({ | ||
resources: ['*'], | ||
actions: ['iam:*'], | ||
})); | ||
|
||
expect(stack).toHaveResourceLike('AWS::IAM::Policy', { | ||
"PolicyDocument": { | ||
"Statement": [ | ||
{ | ||
"Resource": "*", | ||
"Action": "s3:*", | ||
"Effect": "Allow", | ||
}, | ||
], | ||
}, | ||
}); | ||
}); | ||
}); |
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.