-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
portfolio.ts
381 lines (330 loc) · 12.9 KB
/
portfolio.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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
import { Construct, IConstruct } from 'constructs';
import { MessageLanguage } from './common';
import {
CloudFormationRuleConstraintOptions, CommonConstraintOptions,
StackSetsConstraintOptions, TagUpdateConstraintOptions,
} from './constraints';
import { AssociationManager } from './private/association-manager';
import { hashValues } from './private/util';
import { InputValidator } from './private/validation';
import { IProduct } from './product';
import { CfnPortfolio, CfnPortfolioPrincipalAssociation, CfnPortfolioShare } from './servicecatalog.generated';
import { TagOptions } from './tag-options';
import * as iam from '../../aws-iam';
import { IBucket } from '../../aws-s3';
import * as sns from '../../aws-sns';
import * as cdk from '../../core';
/**
* Options for portfolio share.
*/
export interface PortfolioShareOptions {
/**
* Whether to share tagOptions as a part of the portfolio share
*
* @default - share not specified
*/
readonly shareTagOptions?: boolean;
/**
* The message language of the share.
* Controls status and error message language for share.
*
* @default - English
*/
readonly messageLanguage?: MessageLanguage;
}
/**
* A Service Catalog portfolio.
*/
export interface IPortfolio extends cdk.IResource {
/**
* The ARN of the portfolio.
* @attribute
*/
readonly portfolioArn: string;
/**
* The ID of the portfolio.
* @attribute
*/
readonly portfolioId: string;
/**
* Associate portfolio with an IAM Role.
* @param role an IAM role
*/
giveAccessToRole(role: iam.IRole): void;
/**
* Associate portfolio with an IAM User.
* @param user an IAM user
*/
giveAccessToUser(user: iam.IUser): void;
/**
* Associate portfolio with an IAM Group.
* @param group an IAM Group
*/
giveAccessToGroup(group: iam.IGroup): void;
/**
* Initiate a portfolio share with another account.
* @param accountId AWS account to share portfolio with
* @param options Options for the initiate share
*/
shareWithAccount(accountId: string, options?: PortfolioShareOptions): void;
/**
* Associate portfolio with the given product.
* @param product A service catalog produt.
*/
addProduct(product: IProduct): void;
/**
* Associate Tag Options.
* A TagOption is a key-value pair managed in AWS Service Catalog.
* It is not an AWS tag, but serves as a template for creating an AWS tag based on the TagOption.
*/
associateTagOptions(tagOptions: TagOptions): void;
/**
* Add a Resource Update Constraint.
*/
constrainTagUpdates(product: IProduct, options?: TagUpdateConstraintOptions): void;
/**
* Add notifications for supplied topics on the provisioned product.
* @param product A service catalog product.
* @param topic A SNS Topic to receive notifications on events related to the provisioned product.
*/
notifyOnStackEvents(product: IProduct, topic: sns.ITopic, options?: CommonConstraintOptions): void;
/**
* Set provisioning rules for the product.
* @param product A service catalog product.
* @param options options for the constraint.
*/
constrainCloudFormationParameters(product: IProduct, options: CloudFormationRuleConstraintOptions): void;
/**
* Force users to assume a certain role when launching a product.
* This sets the launch role using the role arn which is tied to the account this role exists in.
* This is useful if you will be provisioning products from the account where this role exists.
* If you intend to share the portfolio across accounts, use a local launch role.
*
* @param product A service catalog product.
* @param launchRole The IAM role a user must assume when provisioning the product.
* @param options options for the constraint.
*/
setLaunchRole(product: IProduct, launchRole: iam.IRole, options?: CommonConstraintOptions): void;
/**
* Force users to assume a certain role when launching a product.
* The role will be referenced by name in the local account instead of a static role arn.
* A role with this name will automatically be created and assumable by Service Catalog in this account.
* This is useful when sharing the portfolio with multiple accounts.
*
* @param product A service catalog product.
* @param launchRoleName The name of the IAM role a user must assume when provisioning the product. A role with this name must exist in the account where the portolio is created and the accounts it is shared with.
* @param options options for the constraint.
*/
setLocalLaunchRoleName(product: IProduct, launchRoleName: string, options?: CommonConstraintOptions): iam.IRole;
/**
* Force users to assume a certain role when launching a product.
* The role name will be referenced by in the local account and must be set explicitly.
* This is useful when sharing the portfolio with multiple accounts.
*
* @param product A service catalog product.
* @param launchRole The IAM role a user must assume when provisioning the product. A role with this name must exist in the account where the portolio is created and the accounts it is shared with. The role name must be set explicitly.
* @param options options for the constraint.
*/
setLocalLaunchRole(product: IProduct, launchRole: iam.IRole, options?: CommonConstraintOptions): void;
/**
* Configure deployment options using AWS Cloudformation StackSets
*
* @param product A service catalog product.
* @param options Configuration options for the constraint.
*/
deployWithStackSets(product: IProduct, options: StackSetsConstraintOptions): void;
}
abstract class PortfolioBase extends cdk.Resource implements IPortfolio {
public abstract readonly portfolioArn: string;
public abstract readonly portfolioId: string;
private readonly associatedPrincipals: Set<string> = new Set();
private readonly assetBuckets: Set<IBucket> = new Set<IBucket>();
private readonly sharedAccounts: string[] = [];
public giveAccessToRole(role: iam.IRole): void {
this.associatePrincipal(role.roleArn, role.node.addr);
}
public giveAccessToUser(user: iam.IUser): void {
this.associatePrincipal(user.userArn, user.node.addr);
}
public giveAccessToGroup(group: iam.IGroup): void {
this.associatePrincipal(group.groupArn, group.node.addr);
}
public addProduct(product: IProduct): void {
if (product.assetBuckets) {
for (const bucket of product.assetBuckets) {
this.assetBuckets.add(bucket);
}
}
AssociationManager.associateProductWithPortfolio(this, product, undefined);
}
public shareWithAccount(accountId: string, options: PortfolioShareOptions = {}): void {
const hashId = this.generateUniqueHash(accountId);
this.sharedAccounts.push(accountId);
new CfnPortfolioShare(this, `PortfolioShare${hashId}`, {
portfolioId: this.portfolioId,
accountId: accountId,
shareTagOptions: options.shareTagOptions,
acceptLanguage: options.messageLanguage,
});
}
public associateTagOptions(tagOptions: TagOptions) {
AssociationManager.associateTagOptions(this, this.portfolioId, tagOptions);
}
public constrainTagUpdates(product: IProduct, options: TagUpdateConstraintOptions = {}): void {
AssociationManager.constrainTagUpdates(this, product, options);
}
public notifyOnStackEvents(product: IProduct, topic: sns.ITopic, options: CommonConstraintOptions = {}): void {
AssociationManager.notifyOnStackEvents(this, product, topic, options);
}
public constrainCloudFormationParameters(product: IProduct, options: CloudFormationRuleConstraintOptions): void {
AssociationManager.constrainCloudFormationParameters(this, product, options);
}
public setLaunchRole(product: IProduct, launchRole: iam.IRole, options: CommonConstraintOptions = {}): void {
AssociationManager.setLaunchRole(this, product, launchRole, options);
}
public setLocalLaunchRoleName(product: IProduct, launchRoleName: string, options: CommonConstraintOptions = {}): iam.IRole {
const launchRole: iam.IRole = new iam.Role(this, `LaunchRole${launchRoleName}`, {
roleName: launchRoleName,
assumedBy: new iam.ServicePrincipal('servicecatalog.amazonaws.com'),
});
AssociationManager.setLocalLaunchRoleName(this, product, launchRole.roleName, options);
return launchRole;
}
public setLocalLaunchRole(product: IProduct, launchRole: iam.IRole, options: CommonConstraintOptions = {}): void {
InputValidator.validateRoleNameSetForLocalLaunchRole(launchRole);
AssociationManager.setLocalLaunchRoleName(this, product, launchRole.roleName, options);
}
public deployWithStackSets(product: IProduct, options: StackSetsConstraintOptions) {
AssociationManager.deployWithStackSets(this, product, options);
}
/**
* Associate a principal with the portfolio.
* If the principal is already associated, it will skip.
*/
private associatePrincipal(principalArn: string, principalId: string): void {
if (!this.associatedPrincipals.has(principalArn)) {
const hashId = this.generateUniqueHash(principalId);
new CfnPortfolioPrincipalAssociation(this, `PortolioPrincipalAssociation${hashId}`, {
portfolioId: this.portfolioId,
principalArn: principalArn,
principalType: 'IAM',
});
this.associatedPrincipals.add(principalArn);
}
}
/**
* Gives access to Asset Buckets to Shared Accounts.
*
*/
protected addBucketPermissionsToSharedAccounts() {
if (this.sharedAccounts.length > 0) {
for (const bucket of this.assetBuckets) {
bucket.grantRead(new iam.CompositePrincipal(...this.sharedAccounts.map(account => new iam.AccountPrincipal(account))),
);
}
}
}
/**
* Create a unique id based off the L1 CfnPortfolio or the arn of an imported portfolio.
*/
protected abstract generateUniqueHash(value: string): string;
}
/**
* Properties for a Portfolio.
*/
export interface PortfolioProps {
/**
* The name of the portfolio.
*/
readonly displayName: string;
/**
* The provider name.
*/
readonly providerName: string;
/**
* The message language. Controls language for
* status logging and errors.
*
* @default - English
*/
readonly messageLanguage?: MessageLanguage;
/**
* Description for portfolio.
*
* @default - No description provided
*/
readonly description?: string;
/**
* TagOptions associated directly to a portfolio.
*
* @default - No tagOptions provided
*/
readonly tagOptions?: TagOptions;
}
/**
* A Service Catalog portfolio.
*/
export class Portfolio extends PortfolioBase {
/**
* Creates a Portfolio construct that represents an external portfolio.
*
* @param scope The parent creating construct (usually `this`).
* @param id The construct's name.
* @param portfolioArn the Amazon Resource Name of the existing portfolio.
*/
public static fromPortfolioArn(scope: Construct, id: string, portfolioArn: string): IPortfolio {
const arn = cdk.Stack.of(scope).splitArn(portfolioArn, cdk.ArnFormat.SLASH_RESOURCE_NAME);
const portfolioId = arn.resourceName;
if (!portfolioId) {
throw new Error('Missing required Portfolio ID from Portfolio ARN: ' + portfolioArn);
}
class Import extends PortfolioBase {
public readonly portfolioArn = portfolioArn;
public readonly portfolioId = portfolioId!;
protected generateUniqueHash(value: string): string {
return hashValues(this.portfolioArn, value);
}
}
return new Import(scope, id, {
environmentFromArn: portfolioArn,
});
}
public readonly portfolioArn: string;
public readonly portfolioId: string;
private readonly portfolio: CfnPortfolio;
constructor(scope: Construct, id: string, props: PortfolioProps) {
super(scope, id);
this.validatePortfolioProps(props);
this.portfolio = new CfnPortfolio(this, 'Resource', {
displayName: props.displayName,
providerName: props.providerName,
description: props.description,
acceptLanguage: props.messageLanguage,
});
this.portfolioId = this.portfolio.ref;
this.portfolioArn = cdk.Stack.of(this).formatArn({
service: 'catalog',
resource: 'portfolio',
resourceName: this.portfolioId,
});
if (props.tagOptions !== undefined) {
this.associateTagOptions(props.tagOptions);
}
const portfolioNodeId = this.node.id;
cdk.Aspects.of(this).add({
visit(c: IConstruct) {
if (c.node.id === portfolioNodeId) {
(c as Portfolio).addBucketPermissionsToSharedAccounts();
};
},
});
}
protected generateUniqueHash(value: string): string {
return hashValues(cdk.Names.nodeUniqueId(this.portfolio.node), value);
}
private validatePortfolioProps(props: PortfolioProps) {
InputValidator.validateLength(this.node.path, 'portfolio display name', 1, 100, props.displayName);
InputValidator.validateLength(this.node.path, 'portfolio provider name', 1, 50, props.providerName);
InputValidator.validateLength(this.node.path, 'portfolio description', 0, 2000, props.description);
}
}