-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
secret.ts
1035 lines (902 loc) · 37.6 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
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import * as iam from '../../aws-iam';
import * as kms from '../../aws-kms';
import { ArnFormat, FeatureFlags, Fn, IResource, Lazy, RemovalPolicy, Resource, ResourceProps, SecretValue, Stack, Token, TokenComparison } from '../../core';
import * as cxapi from '../../cx-api';
import { IConstruct, Construct } from 'constructs';
import { ResourcePolicy } from './policy';
import { RotationSchedule, RotationScheduleOptions } from './rotation-schedule';
import * as secretsmanager from './secretsmanager.generated';
const SECRET_SYMBOL = Symbol.for('@aws-cdk/secretsmanager.Secret');
/**
* 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. Will return the full ARN if available, otherwise a partial arn.
* For secrets imported by the deprecated `fromSecretName`, it will return the `secretName`.
* @attribute
*/
readonly secretArn: string;
/**
* The full ARN of the secret in AWS Secrets Manager, which is the ARN including the Secrets Manager-supplied 6-character suffix.
* This is equal to `secretArn` in most cases, but is undefined when a full ARN is not available (e.g., secrets imported by name).
*/
readonly secretFullArn?: string;
/**
* The name of the secret.
*
* For "owned" secrets, this will be the full resource name (secret name + suffix), unless the
* '@aws-cdk/aws-secretsmanager:parseOwnedSecretName' feature flag is set.
*/
readonly secretName: 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 and updating 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;
/**
* Attach a target to this secret.
*
* @param target The target to attach.
* @returns An attached secret
*/
attach(target: ISecretAttachmentTarget): ISecret;
}
/**
* 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.
*
* Only one of `secretString` and `generateSecretString` can be provided.
*
* @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;
/**
* Initial value for the secret
*
* **NOTE:** *It is **highly** encouraged to leave this field undefined and allow SecretsManager to create the secret value.
* The secret string -- if provided -- will be included in the output of the cdk as part of synthesis,
* and will appear in the CloudFormation template in the console. This can be secure(-ish) if that value is merely reference to
* another resource (or one of its attributes), but if the value is a plaintext string, it will be visible to anyone with access
* to the CloudFormation template (via the AWS Console, SDKs, or CLI).
*
* Specifies text data that you want to encrypt and store in this new version of the secret.
* May be a simple string value, or a string representation of a JSON structure.
*
* Only one of `secretStringBeta1`, `secretStringValue`, and `generateSecretString` can be provided.
*
* @default - SecretsManager generates a new secret value.
* @deprecated Use `secretStringValue` instead.
*/
readonly secretStringBeta1?: SecretStringValueBeta1;
/**
* Initial value for the secret
*
* **NOTE:** *It is **highly** encouraged to leave this field undefined and allow SecretsManager to create the secret value.
* The secret string -- if provided -- will be included in the output of the cdk as part of synthesis,
* and will appear in the CloudFormation template in the console. This can be secure(-ish) if that value is merely reference to
* another resource (or one of its attributes), but if the value is a plaintext string, it will be visible to anyone with access
* to the CloudFormation template (via the AWS Console, SDKs, or CLI).
*
* Specifies text data that you want to encrypt and store in this new version of the secret.
* May be a simple string value. To provide a string representation of JSON structure, use `SecretProps.secretObjectValue` instead.
*
* Only one of `secretStringBeta1`, `secretStringValue`, 'secretObjectValue', and `generateSecretString` can be provided.
*
* @default - SecretsManager generates a new secret value.
*/
readonly secretStringValue?: SecretValue;
/**
* Initial value for a JSON secret
*
* **NOTE:** *It is **highly** encouraged to leave this field undefined and allow SecretsManager to create the secret value.
* The secret object -- if provided -- will be included in the output of the cdk as part of synthesis,
* and will appear in the CloudFormation template in the console. This can be secure(-ish) if that value is merely reference to
* another resource (or one of its attributes), but if the value is a plaintext string, it will be visible to anyone with access
* to the CloudFormation template (via the AWS Console, SDKs, or CLI).
*
* Specifies a JSON object that you want to encrypt and store in this new version of the secret.
* To specify a simple string value instead, use `SecretProps.secretStringValue`
*
* Only one of `secretStringBeta1`, `secretStringValue`, 'secretObjectValue', and `generateSecretString` can be provided.
*
* @example
* declare const user: iam.User;
* declare const accessKey: iam.AccessKey;
* declare const stack: Stack;
* new secretsmanager.Secret(stack, 'JSONSecret', {
* secretObjectValue: {
* username: SecretValue.unsafePlainText(user.userName), // intrinsic reference, not exposed as plaintext
* database: SecretValue.unsafePlainText('foo'), // rendered as plain text, but not a secret
* password: accessKey.secretAccessKey, // SecretValue
* },
* });
*
* @default - SecretsManager generates a new secret value.
*/
readonly secretObjectValue?: { [key: string]: SecretValue };
/**
* Policy to apply when the secret is removed from this stack.
*
* @default - Not set.
*/
readonly removalPolicy?: RemovalPolicy;
/**
* A list of regions where to replicate this secret.
*
* @default - Secret is not replicated
*/
readonly replicaRegions?: ReplicaRegion[];
}
/**
* Secret replica region
*/
export interface ReplicaRegion {
/**
* The name of the region
*/
readonly region: 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;
}
/**
* An experimental class used to specify an initial secret value for a Secret.
*
* The class wraps a simple string (or JSON representation) in order to provide some safety checks and warnings
* about the dangers of using plaintext strings as initial secret seed values via CDK/CloudFormation.
*
* @deprecated Use `cdk.SecretValue` instead.
*/
export class SecretStringValueBeta1 {
/**
* Creates a `SecretStringValueBeta1` from a plaintext value.
*
* This approach is inherently unsafe, as the secret value may be visible in your source control repository
* and will also appear in plaintext in the resulting CloudFormation template, including in the AWS Console or APIs.
* Usage of this method is discouraged, especially for production workloads.
*/
public static fromUnsafePlaintext(secretValue: string) { return new SecretStringValueBeta1(secretValue); }
/**
* Creates a `SecretValueValueBeta1` from a string value coming from a Token.
*
* The intent is to enable creating secrets from references (e.g., `Ref`, `Fn::GetAtt`) from other resources.
* This might be the direct output of another Construct, or the output of a Custom Resource.
* This method throws if it determines the input is an unsafe plaintext string.
*
* For example:
*
* ```ts
* // Creates a new IAM user, access and secret keys, and stores the secret access key in a Secret.
* const user = new iam.User(this, 'User');
* const accessKey = new iam.AccessKey(this, 'AccessKey', { user });
* const secret = new secretsmanager.Secret(this, 'Secret', {
* secretStringValue: accessKey.secretAccessKey,
* });
* ```
*
* The secret may also be embedded in a string representation of a JSON structure:
*
* ```ts
* const user = new iam.User(this, 'User');
* const accessKey = new iam.AccessKey(this, 'AccessKey', { user });
* const secretValue = secretsmanager.SecretStringValueBeta1.fromToken(JSON.stringify({
* username: user.userName,
* database: 'foo',
* password: accessKey.secretAccessKey.unsafeUnwrap(),
* }));
* ```
*
* Note that the value being a Token does *not* guarantee safety. For example, a Lazy-evaluated string
* (e.g., `Lazy.string({ produce: () => 'myInsecurePassword' }))`) is a Token, but as the output is
* ultimately a plaintext string, and so insecure.
*
* @param secretValueFromToken a secret value coming from a Construct attribute or Custom Resource output
*/
public static fromToken(secretValueFromToken: string) {
if (!Token.isUnresolved(secretValueFromToken)) {
throw new Error('SecretStringValueBeta1 appears to be plaintext (unsafe) string (or resolved Token); use fromUnsafePlaintext if this is intentional');
}
return new SecretStringValueBeta1(secretValueFromToken);
}
private constructor(private readonly _secretValue: string) { }
/** Returns the secret value */
public secretValue(): string { return this._secretValue; }
}
/**
* Attributes required to import an existing secret into the Stack.
* One ARN format (`secretArn`, `secretCompleteArn`, `secretPartialArn`) must be provided.
*/
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.
* Cannot be used with `secretCompleteArn` or `secretPartialArn`.
* @deprecated use `secretCompleteArn` or `secretPartialArn` instead.
*/
readonly secretArn?: string;
/**
* The complete ARN of the secret in SecretsManager. This is the ARN including the Secrets Manager 6-character suffix.
* Cannot be used with `secretArn` or `secretPartialArn`.
*/
readonly secretCompleteArn?: string;
/**
* The partial ARN of the secret in SecretsManager. This is the ARN without the Secrets Manager 6-character suffix.
* Cannot be used with `secretArn` or `secretCompleteArn`.
*/
readonly secretPartialArn?: 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;
public abstract readonly secretName: string;
protected abstract readonly autoCreatePolicy: boolean;
private policy?: ResourcePolicy;
constructor(scope: Construct, id: string, props: ResourceProps = {}) {
super(scope, id, props);
this.node.addValidation({ validate: () => this.policy?.document.validateForResourcePolicy() ?? [] });
}
public get secretFullArn(): string | undefined { return this.secretArn; }
public grantRead(grantee: iam.IGrantable, versionStages?: string[]): iam.Grant {
// @see https://docs.aws.amazon.com/secretsmanager/latest/userguide/auth-and-access_identity-based-policies.html
const result = iam.Grant.addToPrincipalOrResource({
grantee,
actions: ['secretsmanager:GetSecretValue', 'secretsmanager:DescribeSecret'],
resourceArns: [this.arnForPolicies],
resource: this,
});
const statement = result.principalStatement || result.resourceStatement;
if (versionStages != null && statement) {
statement.addCondition('ForAnyValue:StringEquals', {
'secretsmanager:VersionStage': versionStages,
});
}
if (this.encryptionKey) {
// @see https://docs.aws.amazon.com/kms/latest/developerguide/services-secrets-manager.html
this.encryptionKey.grantDecrypt(
new kms.ViaServicePrincipal(`secretsmanager.${Stack.of(this).region}.amazonaws.com`, grantee.grantPrincipal),
);
}
const crossAccount = Token.compareStrings(Stack.of(this).account, grantee.grantPrincipal.principalAccount || '');
// Throw if secret is not imported and it's shared cross account and no KMS key is provided
if (this instanceof Secret && result.resourceStatement && (!this.encryptionKey && crossAccount === TokenComparison.DIFFERENT)) {
throw new Error('KMS Key must be provided for cross account access to Secret');
}
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.addToPrincipalOrResource({
grantee,
actions: ['secretsmanager:PutSecretValue', 'secretsmanager:UpdateSecret'],
resourceArns: [this.arnForPolicies],
resource: 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),
);
}
// Throw if secret is not imported and it's shared cross account and no KMS key is provided
if (this instanceof Secret && result.resourceStatement && !this.encryptionKey) {
throw new Error('KMS Key must be provided for cross account access to Secret');
}
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()],
}));
}
/**
* Provides an identifier for this secret for use in IAM policies.
* If there is a full ARN, this is just the ARN;
* if we have a partial ARN -- due to either importing by secret name or partial ARN --
* then we need to add a suffix to capture the full ARN's format.
*/
protected get arnForPolicies() {
return this.secretFullArn ? this.secretFullArn : `${this.secretArn}-??????`;
}
/**
* 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,
});
}
}
/**
* Creates a new secret in AWS SecretsManager.
*/
export class Secret extends SecretBase {
/**
* Return whether the given object is a Secret.
*/
public static isSecret(x: any): x is Secret {
return x !== null && typeof(x) === 'object' && SECRET_SYMBOL in x;
}
/** @deprecated use `fromSecretCompleteArn` or `fromSecretPartialArn` */
public static fromSecretArn(scope: Construct, id: string, secretArn: string): ISecret {
const attrs = arnIsComplete(secretArn) ? { secretCompleteArn: secretArn } : { secretPartialArn: secretArn };
return Secret.fromSecretAttributes(scope, id, attrs);
}
/** Imports a secret by complete ARN. The complete ARN is the ARN with the Secrets Manager-supplied suffix. */
public static fromSecretCompleteArn(scope: Construct, id: string, secretCompleteArn: string): ISecret {
return Secret.fromSecretAttributes(scope, id, { secretCompleteArn });
}
/** Imports a secret by partial ARN. The partial ARN is the ARN without the Secrets Manager-supplied suffix. */
public static fromSecretPartialArn(scope: Construct, id: string, secretPartialArn: string): ISecret {
return Secret.fromSecretAttributes(scope, id, { secretPartialArn });
}
/**
* Imports a secret by secret name; the ARN of the Secret will be set to the secret name.
* A secret with this name must exist in the same account & region.
* @deprecated use `fromSecretNameV2`
*/
public static fromSecretName(scope: Construct, id: string, secretName: string): ISecret {
return new class extends SecretBase {
public readonly encryptionKey = undefined;
public readonly secretArn = secretName;
public readonly secretName = secretName;
protected readonly autoCreatePolicy = false;
public get secretFullArn() { return undefined; }
// Overrides the secretArn for grant* methods, where the secretArn must be in ARN format.
// Also adds a wildcard to the resource name to support the SecretsManager-provided suffix.
protected get arnForPolicies() {
return Stack.of(this).formatArn({
service: 'secretsmanager',
resource: 'secret',
resourceName: this.secretName + '*',
arnFormat: ArnFormat.COLON_RESOURCE_NAME,
});
}
}(scope, id);
}
/**
* Imports a secret by secret name.
* A secret with this name must exist in the same account & region.
* Replaces the deprecated `fromSecretName`.
* Please note this method returns ISecret that only contains partial ARN and could lead to AccessDeniedException
* when you pass the partial ARN to CLI or SDK to get the secret value. If your secret name ends with a hyphen and
* 6 characters, you should always use fromSecretCompleteArn() to avoid potential AccessDeniedException.
* @see https://docs.aws.amazon.com/secretsmanager/latest/userguide/troubleshoot.html#ARN_secretnamehyphen
*/
public static fromSecretNameV2(scope: Construct, id: string, secretName: string): ISecret {
return new class extends SecretBase {
public readonly encryptionKey = undefined;
public readonly secretName = secretName;
public readonly secretArn = this.partialArn;
protected readonly autoCreatePolicy = false;
public get secretFullArn() { return undefined; }
// Creates a "partial" ARN from the secret name. The "full" ARN would include the SecretsManager-provided suffix.
private get partialArn(): string {
return Stack.of(this).formatArn({
service: 'secretsmanager',
resource: 'secret',
resourceName: secretName,
arnFormat: ArnFormat.COLON_RESOURCE_NAME,
});
}
}(scope, id);
}
/**
* 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 {
let secretArn: string;
let secretArnIsPartial: boolean;
if (attrs.secretArn) {
if (attrs.secretCompleteArn || attrs.secretPartialArn) {
throw new Error('cannot use `secretArn` with `secretCompleteArn` or `secretPartialArn`');
}
secretArn = attrs.secretArn;
secretArnIsPartial = false;
} else {
if ((attrs.secretCompleteArn && attrs.secretPartialArn) ||
(!attrs.secretCompleteArn && !attrs.secretPartialArn)) {
throw new Error('must use only one of `secretCompleteArn` or `secretPartialArn`');
}
if (attrs.secretCompleteArn && !arnIsComplete(attrs.secretCompleteArn)) {
throw new Error('`secretCompleteArn` does not appear to be complete; missing 6-character suffix');
}
[secretArn, secretArnIsPartial] = attrs.secretCompleteArn ? [attrs.secretCompleteArn, false] : [attrs.secretPartialArn!, true];
}
return new class extends SecretBase {
public readonly encryptionKey = attrs.encryptionKey;
public readonly secretArn = secretArn;
public readonly secretName = parseSecretName(scope, secretArn);
protected readonly autoCreatePolicy = false;
public get secretFullArn() { return secretArnIsPartial ? undefined : secretArn; }
}(scope, id, { environmentFromArn: secretArn });
}
public readonly encryptionKey?: kms.IKey;
public readonly secretArn: string;
public readonly secretName: string;
/**
* The string of the characters that are excluded in this secret
* when it is generated.
*/
public readonly excludeCharacters?: string;
private replicaRegions: secretsmanager.CfnSecret.ReplicaRegionProperty[] = [];
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.');
}
if ((props.generateSecretString ? 1 : 0)
+ (props.secretStringBeta1 ? 1 : 0)
+ (props.secretStringValue ? 1 : 0)
+ (props.secretObjectValue ? 1 : 0)
> 1) {
throw new Error('Cannot specify more than one of `generateSecretString`, `secretStringValue`, `secretObjectValue`, and `secretStringBeta1`.');
}
const secretString = props.secretObjectValue
? this.resolveSecretObjectValue(props.secretObjectValue)
: props.secretStringValue?.unsafeUnwrap() ?? props.secretStringBeta1?.secretValue();
const resource = new secretsmanager.CfnSecret(this, 'Resource', {
description: props.description,
kmsKeyId: props.encryptionKey && props.encryptionKey.keyArn,
generateSecretString: props.generateSecretString ?? (secretString ? undefined : {}),
secretString,
name: this.physicalName,
replicaRegions: Lazy.any({ produce: () => this.replicaRegions }, { omitEmptyArray: true }),
});
resource.applyRemovalPolicy(props.removalPolicy, {
default: RemovalPolicy.DESTROY,
});
this.secretArn = this.getResourceArnAttribute(resource.ref, {
service: 'secretsmanager',
resource: 'secret',
resourceName: this.physicalName,
arnFormat: ArnFormat.COLON_RESOURCE_NAME,
});
this.encryptionKey = props.encryptionKey;
const parseOwnedSecretName = FeatureFlags.of(this).isEnabled(cxapi.SECRETS_MANAGER_PARSE_OWNED_SECRET_NAME);
this.secretName = parseOwnedSecretName
? parseSecretNameForOwnedSecret(this, this.secretArn, props.secretName)
: parseSecretName(this, this.secretArn);
// @see https://docs.aws.amazon.com/kms/latest/developerguide/services-secrets-manager.html#asm-authz
const principal =
new kms.ViaServicePrincipal(`secretsmanager.${Stack.of(this).region}.amazonaws.com`, new iam.AccountPrincipal(Stack.of(this).account));
this.encryptionKey?.grantEncryptDecrypt(principal);
this.encryptionKey?.grant(principal, 'kms:CreateGrant', 'kms:DescribeKey');
for (const replica of props.replicaRegions ?? []) {
this.addReplicaRegion(replica.region, replica.encryptionKey);
}
this.excludeCharacters = props.generateSecretString?.excludeCharacters;
}
private resolveSecretObjectValue(secretObject: { [key: string]: SecretValue }): string {
const resolvedObject: { [key: string]: string } = {};
for (const [key, value] of Object.entries(secretObject)) {
resolvedObject[key] = value.unsafeUnwrap();
}
return JSON.stringify(resolvedObject);
}
/**
* 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,
});
}
/**
* Adds a replica region for the secret
*
* @param region The name of the region
* @param encryptionKey The customer-managed encryption key to use for encrypting the secret value.
*/
public addReplicaRegion(region: string, encryptionKey?: kms.IKey): void {
const stack = Stack.of(this);
if (!Token.isUnresolved(stack.region) && !Token.isUnresolved(region) && region === stack.region) {
throw new Error('Cannot add the region where this stack is deployed as a replica region.');
}
this.replicaRegions.push({
region,
kmsKeyId: encryptionKey?.keyArn,
});
}
}
/**
* 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 {
/**
* AWS::RDS::DBInstance
*/
RDS_DB_INSTANCE = 'AWS::RDS::DBInstance',
/**
* A database instance
*
* @deprecated use RDS_DB_INSTANCE instead
*/
INSTANCE = 'deprecated_AWS::RDS::DBInstance',
/**
* AWS::RDS::DBCluster
*/
RDS_DB_CLUSTER = 'AWS::RDS::DBCluster',
/**
* A database cluster
*
* @deprecated use RDS_DB_CLUSTER instead
*/
CLUSTER = 'deprecated_AWS::RDS::DBCluster',
/**
* AWS::RDS::DBProxy
*/
RDS_DB_PROXY = 'AWS::RDS::DBProxy',
/**
* 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.
*/
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;
public secretName = parseSecretName(scope, secretTargetAttachmentSecretArn);
protected readonly autoCreatePolicy = false;
}
return new Import(scope, id);
}
public readonly encryptionKey?: kms.IKey;
public readonly secretArn: string;
public readonly secretName: string;
/**
* @attribute
*/
public readonly secretTargetAttachmentSecretArn: string;
protected readonly autoCreatePolicy = true;
private readonly attachedSecret: ISecret;
constructor(scope: Construct, id: string, props: SecretTargetAttachmentProps) {
super(scope, id);
this.attachedSecret = props.secret;
const attachment = new secretsmanager.CfnSecretTargetAttachment(this, 'Resource', {
secretId: this.attachedSecret.secretArn,
targetId: props.target.asSecretAttachmentTarget().targetId,
targetType: attachmentTargetTypeToString(props.target.asSecretAttachmentTarget().targetType),
});
this.encryptionKey = this.attachedSecret.encryptionKey;
this.secretName = this.attachedSecret.secretName;
// This allows to reference the secret after attachment (dependency).
this.secretArn = attachment.ref;
this.secretTargetAttachmentSecretArn = attachment.ref;
}
/**
* Forward any additions to the resource policy to the original secret.
* This is required because a secret can only have a single resource policy.
* If we do not forward policy additions, a new policy resource is created using the secret attachment ARN.
* This ends up being rejected by CloudFormation.
*/
public addToResourcePolicy(statement: iam.PolicyStatement): iam.AddToResourcePolicyResult {
if (FeatureFlags.of(this).isEnabled(cxapi.SECRETS_MANAGER_TARGET_ATTACHMENT_RESOURCE_POLICY)) {
return this.attachedSecret.addToResourcePolicy(statement);
}
return super.addToResourcePolicy(statement);
}
}
/**
* 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;
}
/** Parses the secret name from the ARN. */
function parseSecretName(construct: IConstruct, secretArn: string) {
const resourceName = Stack.of(construct).splitArn(secretArn, ArnFormat.COLON_RESOURCE_NAME).resourceName;
if (resourceName) {
// Can't operate on the token to remove the SecretsManager suffix, so just return the full secret name
if (Token.isUnresolved(resourceName)) {
return resourceName;
}
// Secret resource names are in the format `${secretName}-${6-character SecretsManager suffix}`
// If there is no hyphen (or 6-character suffix) assume no suffix was provided, and return the whole name.
const lastHyphenIndex = resourceName.lastIndexOf('-');
const hasSecretsSuffix = lastHyphenIndex !== -1 && resourceName.slice(lastHyphenIndex + 1).length === 6;
return hasSecretsSuffix ? resourceName.slice(0, lastHyphenIndex) : resourceName;
}
throw new Error('invalid ARN format; no secret name provided');
}
/**
* Parses the secret name from the ARN of an owned secret. With owned secrets we know a few things we don't with imported secrets:
* - The ARN is guaranteed to be a full ARN, with suffix.
* - The name -- if provided -- will tell us how many hyphens to expect in the final secret name.
* - If the name is not provided, we know the format used by CloudFormation for auto-generated names.
*
* Note: This is done rather than just returning the secret name passed in by the user to keep the relationship
* explicit between the Secret and wherever the secretName might be used (i.e., using Tokens).
*/
function parseSecretNameForOwnedSecret(construct: Construct, secretArn: string, secretName?: string) {
const resourceName = Stack.of(construct).splitArn(secretArn, ArnFormat.COLON_RESOURCE_NAME).resourceName;
if (!resourceName) {
throw new Error('invalid ARN format; no secret name provided');
}
// Secret name was explicitly provided, but is unresolved; best option is to use it directly.
// If it came from another Secret, it should (hopefully) already be properly formatted.
if (secretName && Token.isUnresolved(secretName)) {
return secretName;
}
// If no secretName was provided, the name will be automatically generated by CloudFormation.
// The autogenerated names have the form of `${logicalID}-${random}`.
// Otherwise, we can use the existing secretName to determine how to parse the resulting resourceName.
const secretNameHyphenatedSegments = secretName ? secretName.split('-').length : 2;
// 2 => [0, 1]
const segmentIndexes = [...new Array(secretNameHyphenatedSegments)].map((_, i) => i);
// Create the secret name from the resource name by joining all the known segments together.
// This should have the effect of stripping the final hyphen and SecretManager suffix.