-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
secret.ts
532 lines (452 loc) · 15.3 KB
/
secret.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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
import * as iam from '@aws-cdk/aws-iam';
import * as kms from '@aws-cdk/aws-kms';
import { Construct, IResource, Resource, SecretValue, Stack } from '@aws-cdk/core';
import { ResourcePolicy } from './policy';
import { RotationSchedule, RotationScheduleOptions } from './rotation-schedule';
import * as secretsmanager from './secretsmanager.generated';
/**
* A secret in AWS Secrets Manager.
*/
export interface ISecret extends IResource {
/**
* The customer-managed encryption key that is used to encrypt this secret, if any. When not specified, the default
* KMS key for the account and region is being used.
*/
readonly encryptionKey?: kms.IKey;
/**
* The ARN of the secret in AWS Secrets Manager.
* @attribute
*/
readonly secretArn: string;
/**
* Retrieve the value of the stored secret as a `SecretValue`.
* @attribute
*/
readonly secretValue: SecretValue;
/**
* Interpret the secret as a JSON object and return a field's value from it as a `SecretValue`.
*/
secretValueFromJson(key: string): SecretValue;
/**
* Grants reading the secret value to some role.
*
* @param grantee the principal being granted permission.
* @param versionStages the version stages the grant is limited to. If not specified, no restriction on the version
* stages is applied.
*/
grantRead(grantee: iam.IGrantable, versionStages?: string[]): iam.Grant;
/**
* Grants writing the secret value to some role.
*
* @param grantee the principal being granted permission.
*/
grantWrite(grantee: iam.IGrantable): iam.Grant;
/**
* Adds a rotation schedule to the secret.
*/
addRotationSchedule(id: string, options: RotationScheduleOptions): RotationSchedule;
/**
* Adds a statement to the IAM resource policy associated with this secret.
*
* If this secret was created in this stack, a resource policy will be
* automatically created upon the first call to `addToResourcePolicy`. If
* the secret is imported, then this is a no-op.
*/
addToResourcePolicy(statement: iam.PolicyStatement): iam.AddToResourcePolicyResult;
/**
* Denies the `DeleteSecret` action to all principals within the current
* account.
*/
denyAccountRootDelete(): void;
}
/**
* The properties required to create a new secret in AWS Secrets Manager.
*/
export interface SecretProps {
/**
* An optional, human-friendly description of the secret.
*
* @default - No description.
*/
readonly description?: string;
/**
* The customer-managed encryption key to use for encrypting the secret value.
*
* @default - A default KMS key for the account and region is used.
*/
readonly encryptionKey?: kms.IKey;
/**
* Configuration for how to generate a secret value.
*
* @default - 32 characters with upper-case letters, lower-case letters, punctuation and numbers (at least one from each
* category), per the default values of ``SecretStringGenerator``.
*/
readonly generateSecretString?: SecretStringGenerator;
/**
* A name for the secret. Note that deleting secrets from SecretsManager does not happen immediately, but after a 7 to
* 30 days blackout period. During that period, it is not possible to create another secret that shares the same name.
*
* @default - A name is generated by CloudFormation.
*/
readonly secretName?: string;
}
/**
* Attributes required to import an existing secret into the Stack.
*/
export interface SecretAttributes {
/**
* The encryption key that is used to encrypt the secret, unless the default SecretsManager key is used.
*/
readonly encryptionKey?: kms.IKey;
/**
* The ARN of the secret in SecretsManager.
*/
readonly secretArn: string;
}
/**
* The common behavior of Secrets. Users should not use this class directly, and instead use ``Secret``.
*/
abstract class SecretBase extends Resource implements ISecret {
public abstract readonly encryptionKey?: kms.IKey;
public abstract readonly secretArn: string;
protected abstract readonly autoCreatePolicy: boolean;
private policy?: ResourcePolicy;
public grantRead(grantee: iam.IGrantable, versionStages?: string[]): iam.Grant {
// @see https://docs.aws.amazon.com/fr_fr/secretsmanager/latest/userguide/auth-and-access_identity-based-policies.html
const result = iam.Grant.addToPrincipal({
grantee,
actions: ['secretsmanager:GetSecretValue'],
resourceArns: [this.secretArn],
scope: this,
});
if (versionStages != null && result.principalStatement) {
result.principalStatement.addCondition('ForAnyValue:StringEquals', {
'secretsmanager:VersionStage': versionStages,
});
}
if (this.encryptionKey) {
// @see https://docs.aws.amazon.com/fr_fr/kms/latest/developerguide/services-secrets-manager.html
this.encryptionKey.grantDecrypt(
new kms.ViaServicePrincipal(`secretsmanager.${Stack.of(this).region}.amazonaws.com`, grantee.grantPrincipal),
);
}
return result;
}
public grantWrite(grantee: iam.IGrantable): iam.Grant {
// See https://docs.aws.amazon.com/secretsmanager/latest/userguide/auth-and-access_identity-based-policies.html
const result = iam.Grant.addToPrincipal({
grantee,
actions: ['secretsmanager:PutSecretValue'],
resourceArns: [this.secretArn],
scope: this,
});
if (this.encryptionKey) {
// See https://docs.aws.amazon.com/kms/latest/developerguide/services-secrets-manager.html
this.encryptionKey.grantEncrypt(
new kms.ViaServicePrincipal(`secretsmanager.${Stack.of(this).region}.amazonaws.com`, grantee.grantPrincipal),
);
}
return result;
}
public get secretValue() {
return this.secretValueFromJson('');
}
public secretValueFromJson(jsonField: string) {
return SecretValue.secretsManager(this.secretArn, { jsonField });
}
public addRotationSchedule(id: string, options: RotationScheduleOptions): RotationSchedule {
return new RotationSchedule(this, id, {
secret: this,
...options,
});
}
public addToResourcePolicy(statement: iam.PolicyStatement): iam.AddToResourcePolicyResult {
if (!this.policy && this.autoCreatePolicy) {
this.policy = new ResourcePolicy(this, 'Policy', { secret: this });
}
if (this.policy) {
this.policy.document.addStatements(statement);
return { statementAdded: true, policyDependable: this.policy };
}
return { statementAdded: false };
}
public denyAccountRootDelete() {
this.addToResourcePolicy(new iam.PolicyStatement({
actions: ['secretsmanager:DeleteSecret'],
effect: iam.Effect.DENY,
resources: ['*'],
principals: [new iam.AccountRootPrincipal()],
}));
}
}
/**
* Creates a new secret in AWS SecretsManager.
*/
export class Secret extends SecretBase {
public static fromSecretArn(scope: Construct, id: string, secretArn: string): ISecret {
return Secret.fromSecretAttributes(scope, id, { secretArn });
}
/**
* Import an existing secret into the Stack.
*
* @param scope the scope of the import.
* @param id the ID of the imported Secret in the construct tree.
* @param attrs the attributes of the imported secret.
*/
public static fromSecretAttributes(scope: Construct, id: string, attrs: SecretAttributes): ISecret {
class Import extends SecretBase {
public readonly encryptionKey = attrs.encryptionKey;
public readonly secretArn = attrs.secretArn;
protected readonly autoCreatePolicy = false;
}
return new Import(scope, id);
}
public readonly encryptionKey?: kms.IKey;
public readonly secretArn: string;
protected readonly autoCreatePolicy = true;
constructor(scope: Construct, id: string, props: SecretProps = {}) {
super(scope, id, {
physicalName: props.secretName,
});
if (props.generateSecretString &&
(props.generateSecretString.secretStringTemplate || props.generateSecretString.generateStringKey) &&
!(props.generateSecretString.secretStringTemplate && props.generateSecretString.generateStringKey)) {
throw new Error('`secretStringTemplate` and `generateStringKey` must be specified together.');
}
const resource = new secretsmanager.CfnSecret(this, 'Resource', {
description: props.description,
kmsKeyId: props.encryptionKey && props.encryptionKey.keyArn,
generateSecretString: props.generateSecretString || {},
name: this.physicalName,
});
this.secretArn = this.getResourceArnAttribute(resource.ref, {
service: 'secretsmanager',
resource: 'secret',
resourceName: this.physicalName,
sep: ':',
});
this.encryptionKey = props.encryptionKey;
// @see https://docs.aws.amazon.com/kms/latest/developerguide/services-secrets-manager.html#asm-authz
const principle =
new kms.ViaServicePrincipal(`secretsmanager.${Stack.of(this).region}.amazonaws.com`, new iam.AccountPrincipal(Stack.of(this).account));
this.encryptionKey?.grantEncryptDecrypt(principle);
this.encryptionKey?.grant(principle, 'kms:CreateGrant', 'kms:DescribeKey');
}
/**
* Adds a target attachment to the secret.
*
* @returns an AttachedSecret
*
* @deprecated use `attach()` instead
*/
public addTargetAttachment(id: string, options: AttachedSecretOptions): SecretTargetAttachment {
return new SecretTargetAttachment(this, id, {
secret: this,
...options,
});
}
/**
* Attach a target to this secret
*
* @param target The target to attach
* @returns An attached secret
*/
public attach(target: ISecretAttachmentTarget): ISecret {
const id = 'Attachment';
const existing = this.node.tryFindChild(id);
if (existing) {
throw new Error('Secret is already attached to a target.');
}
return new SecretTargetAttachment(this, id, {
secret: this,
target,
});
}
}
/**
* A secret attachment target.
*/
export interface ISecretAttachmentTarget {
/**
* Renders the target specifications.
*/
asSecretAttachmentTarget(): SecretAttachmentTargetProps;
}
/**
* The type of service or database that's being associated with the secret.
*/
export enum AttachmentTargetType {
/**
* A database instance
*
* @deprecated use RDS_DB_INSTANCE instead
*/
INSTANCE = 'AWS::RDS::DBInstance',
/**
* A database cluster
*
* @deprecated use RDS_DB_CLUSTER instead
*/
CLUSTER = 'AWS::RDS::DBCluster',
/**
* AWS::RDS::DBInstance
*/
RDS_DB_INSTANCE = 'AWS::RDS::DBInstance',
/**
* AWS::RDS::DBCluster
*/
RDS_DB_CLUSTER = 'AWS::RDS::DBCluster',
/**
* AWS::Redshift::Cluster
*/
REDSHIFT_CLUSTER = 'AWS::Redshift::Cluster',
/**
* AWS::DocDB::DBInstance
*/
DOCDB_DB_INSTANCE = 'AWS::DocDB::DBInstance',
/**
* AWS::DocDB::DBCluster
*/
DOCDB_DB_CLUSTER = 'AWS::DocDB::DBCluster'
}
/**
* Attachment target specifications.
*/
export interface SecretAttachmentTargetProps {
/**
* The id of the target to attach the secret to.
*/
readonly targetId: string;
/**
* The type of the target to attach the secret to.
*/
readonly targetType: AttachmentTargetType;
}
/**
* Options to add a secret attachment to a secret.
*
* @deprecated use `secret.attach()` instead
*/
export interface AttachedSecretOptions {
/**
* The target to attach the secret to.
*/
readonly target: ISecretAttachmentTarget;
}
/**
* Construction properties for an AttachedSecret.
*/
export interface SecretTargetAttachmentProps extends AttachedSecretOptions {
/**
* The secret to attach to the target.
*/
readonly secret: ISecret;
}
export interface ISecretTargetAttachment extends ISecret {
/**
* Same as `secretArn`
*
* @attribute
*/
readonly secretTargetAttachmentSecretArn: string;
}
/**
* An attached secret.
*/
export class SecretTargetAttachment extends SecretBase implements ISecretTargetAttachment {
public static fromSecretTargetAttachmentSecretArn(scope: Construct, id: string, secretTargetAttachmentSecretArn: string): ISecretTargetAttachment {
class Import extends SecretBase implements ISecretTargetAttachment {
public encryptionKey?: kms.IKey | undefined;
public secretArn = secretTargetAttachmentSecretArn;
public secretTargetAttachmentSecretArn = secretTargetAttachmentSecretArn;
protected readonly autoCreatePolicy = false;
}
return new Import(scope, id);
}
public readonly encryptionKey?: kms.IKey;
public readonly secretArn: string;
/**
* @attribute
*/
public readonly secretTargetAttachmentSecretArn: string;
protected readonly autoCreatePolicy = true;
constructor(scope: Construct, id: string, props: SecretTargetAttachmentProps) {
super(scope, id);
const attachment = new secretsmanager.CfnSecretTargetAttachment(this, 'Resource', {
secretId: props.secret.secretArn,
targetId: props.target.asSecretAttachmentTarget().targetId,
targetType: props.target.asSecretAttachmentTarget().targetType,
});
this.encryptionKey = props.secret.encryptionKey;
// This allows to reference the secret after attachment (dependency).
this.secretArn = attachment.ref;
this.secretTargetAttachmentSecretArn = attachment.ref;
}
}
/**
* Configuration to generate secrets such as passwords automatically.
*/
export interface SecretStringGenerator {
/**
* Specifies that the generated password shouldn't include uppercase letters.
*
* @default false
*/
readonly excludeUppercase?: boolean;
/**
* Specifies whether the generated password must include at least one of every allowed character type.
*
* @default true
*/
readonly requireEachIncludedType?: boolean;
/**
* Specifies that the generated password can include the space character.
*
* @default false
*/
readonly includeSpace?: boolean;
/**
* A string that includes characters that shouldn't be included in the generated password. The string can be a minimum
* of ``0`` and a maximum of ``4096`` characters long.
*
* @default no exclusions
*/
readonly excludeCharacters?: string;
/**
* The desired length of the generated password.
*
* @default 32
*/
readonly passwordLength?: number;
/**
* Specifies that the generated password shouldn't include punctuation characters.
*
* @default false
*/
readonly excludePunctuation?: boolean;
/**
* Specifies that the generated password shouldn't include lowercase letters.
*
* @default false
*/
readonly excludeLowercase?: boolean;
/**
* Specifies that the generated password shouldn't include digits.
*
* @default false
*/
readonly excludeNumbers?: boolean;
/**
* A properly structured JSON string that the generated password can be added to. The ``generateStringKey`` is
* combined with the generated random string and inserted into the JSON structure that's specified by this parameter.
* The merged JSON string is returned as the completed SecretString of the secret. If you specify ``secretStringTemplate``
* then ``generateStringKey`` must be also be specified.
*/
readonly secretStringTemplate?: string;
/**
* The JSON key name that's used to add the generated password to the JSON structure specified by the
* ``secretStringTemplate`` parameter. If you specify ``generateStringKey`` then ``secretStringTemplate``
* must be also be specified.
*/
readonly generateStringKey?: string;
}