-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
group.ts
213 lines (188 loc) · 6.77 KB
/
group.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import { ArnFormat, Lazy, Resource, Stack } from '@aws-cdk/core';
import { Construct } from 'constructs';
import { CfnGroup } from './iam.generated';
import { IIdentity } from './identity-base';
import { IManagedPolicy } from './managed-policy';
import { Policy } from './policy';
import { PolicyStatement } from './policy-statement';
import { AddToPrincipalPolicyResult, ArnPrincipal, IPrincipal, PrincipalPolicyFragment } from './principals';
import { IUser } from './user';
import { AttachedPolicies } from './util';
/**
* Represents an IAM Group.
*
* @see https://docs.aws.amazon.com/IAM/latest/UserGuide/id_groups.html
*/
export interface IGroup extends IIdentity {
/**
* Returns the IAM Group Name
*
* @attribute
*/
readonly groupName: string;
/**
* Returns the IAM Group ARN
*
* @attribute
*/
readonly groupArn: string;
}
/**
* Properties for defining an IAM group
*/
export interface GroupProps {
/**
* A name for the IAM group. For valid values, see the GroupName parameter
* for the CreateGroup action in the IAM API Reference. If you don't specify
* a name, AWS CloudFormation generates a unique physical ID and uses that
* ID for the group name.
*
* If you specify a name, you must specify the CAPABILITY_NAMED_IAM value to
* acknowledge your template's capabilities. For more information, see
* Acknowledging IAM Resources in AWS CloudFormation Templates.
*
* @default Generated by CloudFormation (recommended)
*/
readonly groupName?: string;
/**
* A list of managed policies associated with this role.
*
* You can add managed policies later using
* `addManagedPolicy(ManagedPolicy.fromAwsManagedPolicyName(policyName))`.
*
* @default - No managed policies.
*/
readonly managedPolicies?: IManagedPolicy[];
/**
* The path to the group. For more information about paths, see [IAM
* Identifiers](http://docs.aws.amazon.com/IAM/latest/UserGuide/index.html?Using_Identifiers.html)
* in the IAM User Guide.
*
* @default /
*/
readonly path?: string;
}
abstract class GroupBase extends Resource implements IGroup {
public abstract readonly groupName: string;
public abstract readonly groupArn: string;
public readonly grantPrincipal: IPrincipal = this;
public readonly principalAccount: string | undefined = this.env.account;
public readonly assumeRoleAction: string = 'sts:AssumeRole';
private readonly attachedPolicies = new AttachedPolicies();
private defaultPolicy?: Policy;
public get policyFragment(): PrincipalPolicyFragment {
return new ArnPrincipal(this.groupArn).policyFragment;
}
/**
* Attaches a policy to this group.
* @param policy The policy to attach.
*/
public attachInlinePolicy(policy: Policy) {
this.attachedPolicies.attach(policy);
policy.attachToGroup(this);
}
public addManagedPolicy(_policy: IManagedPolicy) {
// drop
}
/**
* Adds a user to this group.
*/
public addUser(user: IUser) {
user.addToGroup(this);
}
/**
* Adds an IAM statement to the default policy.
*/
public addToPrincipalPolicy(statement: PolicyStatement): AddToPrincipalPolicyResult {
if (!this.defaultPolicy) {
this.defaultPolicy = new Policy(this, 'DefaultPolicy');
this.defaultPolicy.attachToGroup(this);
}
this.defaultPolicy.addStatements(statement);
return { statementAdded: true, policyDependable: this.defaultPolicy };
}
public addToPolicy(statement: PolicyStatement): boolean {
return this.addToPrincipalPolicy(statement).statementAdded;
}
}
/**
* An IAM Group (collection of IAM users) lets you specify permissions for
* multiple users, which can make it easier to manage permissions for those users.
*
* @see https://docs.aws.amazon.com/IAM/latest/UserGuide/id_groups.html
*/
export class Group extends GroupBase {
/**
* Import an external group by ARN.
*
* If the imported Group ARN is a Token (such as a
* `CfnParameter.valueAsString` or a `Fn.importValue()`) *and* the referenced
* group has a `path` (like `arn:...:group/AdminGroup/NetworkAdmin`), the
* `groupName` property will not resolve to the correct value. Instead it
* will resolve to the first path component. We unfortunately cannot express
* the correct calculation of the full path name as a CloudFormation
* expression. In this scenario the Group ARN should be supplied without the
* `path` in order to resolve the correct group resource.
*
* @param scope construct scope
* @param id construct id
* @param groupArn the ARN of the group to import (e.g. `arn:aws:iam::account-id:group/group-name`)
*/
public static fromGroupArn(scope: Construct, id: string, groupArn: string): IGroup {
const arnComponents = Stack.of(scope).splitArn(groupArn, ArnFormat.SLASH_RESOURCE_NAME);
const groupName = arnComponents.resourceName!;
class Import extends GroupBase {
public groupName = groupName;
public groupArn = groupArn;
public principalAccount = arnComponents.account;
}
return new Import(scope, id);
}
/**
* Import an existing group by given name (with path).
* This method has same caveats of `fromGroupArn`
*
* @param scope construct scope
* @param id construct id
* @param groupName the groupName (path included) of the existing group to import
*/
static fromGroupName(scope: Construct, id: string, groupName: string) {
const groupArn = Stack.of(scope).formatArn({
service: 'iam',
region: '',
resource: 'group',
resourceName: groupName,
});
return Group.fromGroupArn(scope, id, groupArn);
}
public readonly groupName: string;
public readonly groupArn: string;
private readonly managedPolicies: IManagedPolicy[] = [];
constructor(scope: Construct, id: string, props: GroupProps = {}) {
super(scope, id, {
physicalName: props.groupName,
});
this.managedPolicies.push(...props.managedPolicies || []);
const group = new CfnGroup(this, 'Resource', {
groupName: this.physicalName,
managedPolicyArns: Lazy.list({ produce: () => this.managedPolicies.map(p => p.managedPolicyArn) }, { omitEmpty: true }),
path: props.path,
});
this.groupName = this.getResourceNameAttribute(group.ref);
this.groupArn = this.getResourceArnAttribute(group.attrArn, {
region: '', // IAM is global in each partition
service: 'iam',
resource: 'group',
// Removes leading slash from path
resourceName: `${props.path ? props.path.substr(props.path.charAt(0) === '/' ? 1 : 0) : ''}${this.physicalName}`,
});
}
/**
* Attaches a managed policy to this group.
* @param policy The managed policy to attach.
*/
public addManagedPolicy(policy: IManagedPolicy) {
if (this.managedPolicies.find(mp => mp === policy)) { return; }
this.managedPolicies.push(policy);
}
}