-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathmanaged-policy.ts
350 lines (307 loc) · 11.3 KB
/
managed-policy.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
import { Construct } from 'constructs';
import { IGroup } from './group';
import { CfnManagedPolicy } from './iam.generated';
import { PolicyDocument } from './policy-document';
import { PolicyStatement } from './policy-statement';
import { AddToPrincipalPolicyResult, IGrantable, IPrincipal, PrincipalPolicyFragment } from './principals';
import { undefinedIfEmpty } from './private/util';
import { IRole } from './role';
import { IUser } from './user';
import { ArnFormat, Resource, Stack, Arn, Aws } from '../../core';
import { getCustomizeRolesConfig, PolicySynthesizer } from '../../core/lib/helpers-internal';
/**
* A managed policy
*/
export interface IManagedPolicy {
/**
* The ARN of the managed policy
* @attribute
*/
readonly managedPolicyArn: string;
}
/**
* Properties for defining an IAM managed policy
*/
export interface ManagedPolicyProps {
/**
* The name of the managed policy. If you specify multiple policies for an entity,
* specify unique names. For example, if you specify a list of policies for
* an IAM role, each policy must have a unique name.
*
* @default - A name is automatically generated.
*/
readonly managedPolicyName?: string;
/**
* A description of the managed policy. Typically used to store information about the
* permissions defined in the policy. For example, "Grants access to production DynamoDB tables."
* The policy description is immutable. After a value is assigned, it cannot be changed.
*
* @default - empty
*/
readonly description?: string;
/**
* The path for the policy. This parameter allows (through its regex pattern) a string of characters
* consisting of either a forward slash (/) by itself or a string that must begin and end with forward slashes.
* In addition, it can contain any ASCII character from the ! (\u0021) through the DEL character (\u007F),
* including most punctuation characters, digits, and upper and lowercased letters.
*
* For more information about paths, see IAM Identifiers in the IAM User Guide.
*
* @default - "/"
*/
readonly path?: string;
/**
* Users to attach this policy to.
* You can also use `attachToUser(user)` to attach this policy to a user.
*
* @default - No users.
*/
readonly users?: IUser[];
/**
* Roles to attach this policy to.
* You can also use `attachToRole(role)` to attach this policy to a role.
*
* @default - No roles.
*/
readonly roles?: IRole[];
/**
* Groups to attach this policy to.
* You can also use `attachToGroup(group)` to attach this policy to a group.
*
* @default - No groups.
*/
readonly groups?: IGroup[];
/**
* Initial set of permissions to add to this policy document.
* You can also use `addPermission(statement)` to add permissions later.
*
* @default - No statements.
*/
readonly statements?: PolicyStatement[];
/**
* Initial PolicyDocument to use for this ManagedPolicy. If omited, any
* `PolicyStatement` provided in the `statements` property will be applied
* against the empty default `PolicyDocument`.
*
* @default - An empty policy.
*/
readonly document?: PolicyDocument;
}
/**
* Managed policy
*
*/
export class ManagedPolicy extends Resource implements IManagedPolicy, IGrantable {
/**
* Import a customer managed policy from the managedPolicyName.
*
* For this managed policy, you only need to know the name to be able to use it.
*
*/
public static fromManagedPolicyName(scope: Construct, id: string, managedPolicyName: string): IManagedPolicy {
class Import extends Resource implements IManagedPolicy {
public readonly managedPolicyArn = Stack.of(scope).formatArn({
service: 'iam',
region: '', // no region for managed policy
account: Stack.of(scope).account, // Can this be something the user specifies?
resource: 'policy',
resourceName: managedPolicyName,
});
}
return new Import(scope, id);
}
/**
* Import an external managed policy by ARN.
*
* For this managed policy, you only need to know the ARN to be able to use it.
* This can be useful if you got the ARN from a CloudFormation Export.
*
* If the imported Managed Policy ARN is a Token (such as a
* `CfnParameter.valueAsString` or a `Fn.importValue()`) *and* the referenced
* managed policy has a `path` (like `arn:...:policy/AdminPolicy/AdminAllow`), the
* `managedPolicyName` 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 Managed Policy ARN should be supplied without the
* `path` in order to resolve the correct managed policy resource.
*
* @param scope construct scope
* @param id construct id
* @param managedPolicyArn the ARN of the managed policy to import
*/
public static fromManagedPolicyArn(scope: Construct, id: string, managedPolicyArn: string): IManagedPolicy {
class Import extends Resource implements IManagedPolicy {
public readonly managedPolicyArn = managedPolicyArn;
}
return new Import(scope, id);
}
/**
* Import a managed policy from one of the policies that AWS manages.
*
* For this managed policy, you only need to know the name to be able to use it.
*
* Some managed policy names start with "service-role/", some start with
* "job-function/", and some don't start with anything. Include the
* prefix when constructing this object.
*/
public static fromAwsManagedPolicyName(managedPolicyName: string): IManagedPolicy {
class AwsManagedPolicy implements IManagedPolicy {
public readonly managedPolicyArn = Arn.format({
partition: Aws.PARTITION,
service: 'iam',
region: '', // no region for managed policy
account: 'aws', // the account for a managed policy is 'aws'
resource: 'policy',
resourceName: managedPolicyName,
});
}
return new AwsManagedPolicy();
}
/**
* Returns the ARN of this managed policy.
*
* @attribute
*/
public readonly managedPolicyArn: string;
/**
* The policy document.
*/
public readonly document = new PolicyDocument();
/**
* The name of this policy.
*
* @attribute
*/
public readonly managedPolicyName: string;
/**
* The description of this policy.
*
* @attribute
*/
public readonly description: string;
/**
* The path of this policy.
*
* @attribute
*/
public readonly path: string;
public readonly grantPrincipal: IPrincipal;
private readonly roles = new Array<IRole>();
private readonly users = new Array<IUser>();
private readonly groups = new Array<IGroup>();
private readonly _precreatedPolicy?: IManagedPolicy;
constructor(scope: Construct, id: string, props: ManagedPolicyProps = {}) {
super(scope, id, {
physicalName: props.managedPolicyName,
});
this.description = props.description || '';
this.path = props.path || '/';
if (props.document) {
this.document = props.document;
}
const config = getCustomizeRolesConfig(this);
const _precreatedPolicy = ManagedPolicy.fromManagedPolicyName(this, 'Imported'+id, id);
this.managedPolicyName = id;
this.managedPolicyArn = _precreatedPolicy.managedPolicyArn;
if (config.enabled) {
this._precreatedPolicy = _precreatedPolicy;
}
if (!config.preventSynthesis) {
const resource = new CfnManagedPolicy(this, 'Resource', {
policyDocument: this.document,
managedPolicyName: this.physicalName,
description: this.description,
path: this.path,
roles: undefinedIfEmpty(() => this.roles.map(r => r.roleName)),
users: undefinedIfEmpty(() => this.users.map(u => u.userName)),
groups: undefinedIfEmpty(() => this.groups.map(g => g.groupName)),
});
// arn:aws:iam::123456789012:policy/teststack-CreateTestDBPolicy-16M23YE3CS700
this.managedPolicyName = this.getResourceNameAttribute(Stack.of(this).splitArn(resource.ref, ArnFormat.SLASH_RESOURCE_NAME).resourceName!);
this.managedPolicyArn = this.getResourceArnAttribute(resource.ref, {
region: '', // IAM is global in each partition
service: 'iam',
resource: 'policy',
resourceName: this.physicalName,
});
}
if (props.users) {
props.users.forEach(u => this.attachToUser(u));
}
if (props.groups) {
props.groups.forEach(g => this.attachToGroup(g));
}
if (props.roles) {
props.roles.forEach(r => this.attachToRole(r));
}
if (props.statements) {
props.statements.forEach(p => this.addStatements(p));
}
this.grantPrincipal = new ManagedPolicyGrantPrincipal(this);
this.node.addValidation({ validate: () => this.validateManagedPolicy() });
}
/**
* Adds a statement to the policy document.
*/
public addStatements(...statement: PolicyStatement[]) {
this.document.addStatements(...statement);
}
/**
* Attaches this policy to a user.
*/
public attachToUser(user: IUser) {
if (this.users.find(u => u === user)) { return; }
this.users.push(user);
}
/**
* Attaches this policy to a role.
*/
public attachToRole(role: IRole) {
if (this.roles.find(r => r === role)) { return; }
this.roles.push(role);
}
/**
* Attaches this policy to a group.
*/
public attachToGroup(group: IGroup) {
if (this.groups.find(g => g === group)) { return; }
this.groups.push(group);
}
private validateManagedPolicy(): string[] {
const result = new Array<string>();
// validate that the policy document is not empty
if (this.document.isEmpty) {
result.push('Managed Policy is empty. You must add statements to the policy');
}
result.push(...this.document.validateForIdentityPolicy());
if (result.length === 0 && this._precreatedPolicy) {
PolicySynthesizer.getOrCreate(this).addManagedPolicy(this.node.path, {
policyStatements: this.document.toJSON()?.Statement,
roles: this.roles.map(role => role.node.path),
});
}
return result;
}
}
class ManagedPolicyGrantPrincipal implements IPrincipal {
public readonly assumeRoleAction = 'sts:AssumeRole';
public readonly grantPrincipal: IPrincipal;
public readonly principalAccount?: string;
constructor(private _managedPolicy: ManagedPolicy) {
this.grantPrincipal = this;
this.principalAccount = _managedPolicy.env.account;
}
public get policyFragment(): PrincipalPolicyFragment {
// This property is referenced to add policy statements as a resource-based policy.
// We should fail because a managed policy cannot be used as a principal of a policy document.
// cf. https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_elements_principal.html#Principal_specifying
throw new Error(`Cannot use a ManagedPolicy '${this._managedPolicy.node.path}' as the 'Principal' or 'NotPrincipal' in an IAM Policy`);
}
public addToPolicy(statement: PolicyStatement): boolean {
return this.addToPrincipalPolicy(statement).statementAdded;
}
public addToPrincipalPolicy(statement: PolicyStatement): AddToPrincipalPolicyResult {
this._managedPolicy.addStatements(statement);
return { statementAdded: true, policyDependable: this._managedPolicy };
}
}