-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
props.ts
544 lines (487 loc) · 14.8 KB
/
props.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
import * as ec2 from '@aws-cdk/aws-ec2';
import * as kms from '@aws-cdk/aws-kms';
import * as secretsmanager from '@aws-cdk/aws-secretsmanager';
import { Duration, SecretValue } from '@aws-cdk/core';
import { IParameterGroup } from './parameter-group';
/**
* Instance properties for database instances
*/
export interface InstanceProps {
/**
* What type of instance to start for the replicas.
*
* @default - t3.medium (or, more precisely, db.t3.medium)
*/
readonly instanceType?: ec2.InstanceType;
/**
* What subnets to run the RDS instances in.
*
* Must be at least 2 subnets in two different AZs.
*/
readonly vpc: ec2.IVpc;
/**
* Where to place the instances within the VPC
*
* @default - the Vpc default strategy if not specified.
*/
readonly vpcSubnets?: ec2.SubnetSelection;
/**
* Security group.
*
* @default a new security group is created.
*/
readonly securityGroups?: ec2.ISecurityGroup[];
/**
* The DB parameter group to associate with the instance.
*
* @default no parameter group
*/
readonly parameterGroup?: IParameterGroup;
/**
* The parameters in the DBParameterGroup to create automatically
*
* You can only specify parameterGroup or parameters but not both.
* You need to use a versioned engine to auto-generate a DBParameterGroup.
*
* @default - None
*/
readonly parameters?: { [key: string]: string };
/**
* Whether to enable Performance Insights for the DB instance.
*
* @default - false, unless ``performanceInsightRentention`` or ``performanceInsightEncryptionKey`` is set.
*/
readonly enablePerformanceInsights?: boolean;
/**
* The amount of time, in days, to retain Performance Insights data.
*
* @default 7
*/
readonly performanceInsightRetention?: PerformanceInsightRetention;
/**
* The AWS KMS key for encryption of Performance Insights data.
*
* @default - default master key
*/
readonly performanceInsightEncryptionKey?: kms.IKey;
/**
* Whether to enable automatic upgrade of minor version for the DB instance.
*
* @default - true
*/
readonly autoMinorVersionUpgrade?: boolean;
/**
* Whether to allow upgrade of major version for the DB instance.
*
* @default - false
*/
readonly allowMajorVersionUpgrade?: boolean;
/**
* Whether to remove automated backups immediately after the DB instance is deleted for the DB instance.
*
* @default - true
*/
readonly deleteAutomatedBackups?: boolean;
/**
* Indicates whether the DB instance is an internet-facing instance.
*
* @default - `true` if `vpcSubnets` is `subnetType: SubnetType.PUBLIC`, `false` otherwise
*/
readonly publiclyAccessible?: boolean;
}
/**
* Backup configuration for RDS databases
*
* @default - The retention period for automated backups is 1 day.
* The preferred backup window will be a 30-minute window selected at random
* from an 8-hour block of time for each AWS Region.
* @see https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/USER_WorkingWithAutomatedBackups.html#USER_WorkingWithAutomatedBackups.BackupWindow
*/
export interface BackupProps {
/**
* How many days to retain the backup
*/
readonly retention: Duration;
/**
* A daily time range in 24-hours UTC format in which backups preferably execute.
*
* Must be at least 30 minutes long.
*
* Example: '01:00-02:00'
*
* @default - a 30-minute window selected at random from an 8-hour block of
* time for each AWS Region. To see the time blocks available, see
* https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/USER_WorkingWithAutomatedBackups.html#USER_WorkingWithAutomatedBackups.BackupWindow
*/
readonly preferredWindow?: string;
}
/**
* Base options for creating Credentials.
*/
export interface CredentialsBaseOptions {
/**
* The name of the secret.
*
* @default - A name is generated by CloudFormation.
*/
readonly secretName?: string;
/**
* KMS encryption key to encrypt the generated secret.
*
* @default - default master key
*/
readonly encryptionKey?: kms.IKey;
/**
* The characters to exclude from the generated password.
* Has no effect if `password` has been provided.
*
* @default - the DatabaseSecret default exclude character set (" %+~`#$&*()|[]{}:;<>?!'/@\"\\")
*/
readonly excludeCharacters?: string;
/**
* A list of regions where to replicate this secret.
*
* @default - Secret is not replicated
*/
readonly replicaRegions?: secretsmanager.ReplicaRegion[];
}
/**
* Options for creating Credentials from a username.
*/
export interface CredentialsFromUsernameOptions extends CredentialsBaseOptions {
/**
* Password
*
* Do not put passwords in your CDK code directly.
*
* @default - a Secrets Manager generated password
*/
readonly password?: SecretValue;
}
/**
* Username and password combination
*/
export abstract class Credentials {
/**
* Creates Credentials with a password generated and stored in Secrets Manager.
*/
public static fromGeneratedSecret(username: string, options: CredentialsBaseOptions = {}): Credentials {
return {
...options,
username,
usernameAsString: true,
};
}
/**
* Creates Credentials from a password
*
* Do not put passwords in your CDK code directly.
*/
public static fromPassword(username: string, password: SecretValue): Credentials {
return {
username,
password,
usernameAsString: true,
};
}
/**
* Creates Credentials for the given username, and optional password and key.
* If no password is provided, one will be generated and stored in Secrets Manager.
*/
public static fromUsername(username: string, options: CredentialsFromUsernameOptions = {}): Credentials {
return {
...options,
username,
};
}
/**
* Creates Credentials from an existing Secrets Manager ``Secret`` (or ``DatabaseSecret``)
*
* The Secret must be a JSON string with a ``username`` and ``password`` field:
* ```
* {
* ...
* "username": <required: username>,
* "password": <required: password>,
* }
* ```
*
* @param secret The secret where the credentials are stored
* @param username The username defined in the secret. If specified the username
* will be referenced as a string and not a dynamic reference to the username
* field in the secret. This allows to replace the secret without replacing the
* instance or cluster.
*/
public static fromSecret(secret: secretsmanager.ISecret, username?: string): Credentials {
return {
username: username ?? secret.secretValueFromJson('username').unsafeUnwrap(),
password: secret.secretValueFromJson('password'),
encryptionKey: secret.encryptionKey,
secret,
};
}
/**
* Username
*/
public abstract readonly username: string;
/**
* The name to use for the Secret if a new Secret is to be generated in
* SecretsManager for these Credentials.
*
* @default - A name is generated by CloudFormation.
*/
public abstract readonly secretName?: string;
/**
* Whether the username should be referenced as a string and not as a dynamic
* reference to the username in the secret.
*
* @default false
*/
public abstract readonly usernameAsString?: boolean;
/**
* Password
*
* Do not put passwords in your CDK code directly.
*
* @default - a Secrets Manager generated password
*/
public abstract readonly password?: SecretValue;
/**
* KMS encryption key to encrypt the generated secret.
*
* @default - default master key
*/
public abstract readonly encryptionKey?: kms.IKey;
/**
* Secret used to instantiate this Login.
*
* @default - none
*/
public abstract readonly secret?: secretsmanager.ISecret;
/**
* The characters to exclude from the generated password.
* Only used if `password` has not been set.
*
* @default - the DatabaseSecret default exclude character set (" %+~`#$&*()|[]{}:;<>?!'/@\"\\")
*/
public abstract readonly excludeCharacters?: string;
/**
* A list of regions where to replicate the generated secret.
*
* @default - Secret is not replicated
*/
public abstract readonly replicaRegions?: secretsmanager.ReplicaRegion[];
}
/**
* Options used in the `SnapshotCredentials.fromGeneratedPassword` method.
*/
export interface SnapshotCredentialsFromGeneratedPasswordOptions {
/**
* KMS encryption key to encrypt the generated secret.
*
* @default - default master key
*/
readonly encryptionKey?: kms.IKey;
/**
* The characters to exclude from the generated password.
*
* @default - the DatabaseSecret default exclude character set (" %+~`#$&*()|[]{}:;<>?!'/@\"\\")
*/
readonly excludeCharacters?: string;
/**
* A list of regions where to replicate this secret.
*
* @default - Secret is not replicated
*/
readonly replicaRegions?: secretsmanager.ReplicaRegion[];
}
/**
* Credentials to update the password for a ``DatabaseInstanceFromSnapshot``.
*/
export abstract class SnapshotCredentials {
/**
* Generate a new password for the snapshot, using the existing username and an optional encryption key.
* The new credentials are stored in Secrets Manager.
*
* Note - The username must match the existing master username of the snapshot.
*/
public static fromGeneratedSecret(username: string, options: SnapshotCredentialsFromGeneratedPasswordOptions = {}): SnapshotCredentials {
return {
...options,
generatePassword: true,
replaceOnPasswordCriteriaChanges: true,
username,
};
}
/**
* Generate a new password for the snapshot, using the existing username and an optional encryption key.
*
* Note - The username must match the existing master username of the snapshot.
*
* NOTE: use `fromGeneratedSecret()` for new Clusters and Instances. Switching from
* `fromGeneratedPassword()` to `fromGeneratedSecret()` for already deployed Clusters
* or Instances will update their master password.
*/
public static fromGeneratedPassword(username: string, options: SnapshotCredentialsFromGeneratedPasswordOptions = {}): SnapshotCredentials {
return {
...options,
generatePassword: true,
username,
};
}
/**
* Update the snapshot login with an existing password.
*/
public static fromPassword(password: SecretValue): SnapshotCredentials {
return { generatePassword: false, password };
}
/**
* Update the snapshot login with an existing password from a Secret.
*
* The Secret must be a JSON string with a ``password`` field:
* ```
* {
* ...
* "password": <required: password>,
* }
* ```
*/
public static fromSecret(secret: secretsmanager.ISecret): SnapshotCredentials {
return {
generatePassword: false,
password: secret.secretValueFromJson('password'),
secret,
};
}
/**
* The master user name.
*
* Must be the **current** master user name of the snapshot.
* It is not possible to change the master user name of a RDS instance.
*
* @default - the existing username from the snapshot
*/
public abstract readonly username?: string;
/**
* Whether a new password should be generated.
*/
public abstract readonly generatePassword: boolean;
/**
* Whether to replace the generated secret when the criteria for the password change.
*
* @default false
*/
public abstract readonly replaceOnPasswordCriteriaChanges?: boolean;
/**
* The master user password.
*
* Do not put passwords in your CDK code directly.
*
* @default - the existing password from the snapshot
*/
public abstract readonly password?: SecretValue;
/**
* KMS encryption key to encrypt the generated secret.
*
* @default - default master key
*/
public abstract readonly encryptionKey?: kms.IKey;
/**
* Secret used to instantiate this Login.
*
* @default - none
*/
public abstract readonly secret?: secretsmanager.ISecret;
/**
* The characters to exclude from the generated password.
* Only used if `generatePassword` if true.
*
* @default - the DatabaseSecret default exclude character set (" %+~`#$&*()|[]{}:;<>?!'/@\"\\")
*/
public abstract readonly excludeCharacters?: string;
/**
* A list of regions where to replicate the generated secret.
*
* @default - Secret is not replicated
*/
public abstract readonly replicaRegions?: secretsmanager.ReplicaRegion[];
}
/**
* Properties common to single-user and multi-user rotation options.
*/
export interface CommonRotationUserOptions {
/**
* Specifies the number of days after the previous rotation
* before Secrets Manager triggers the next automatic rotation.
*
* @default - 30 days
*/
readonly automaticallyAfter?: Duration;
/**
* Specifies characters to not include in generated passwords.
*
* @default " %+~`#$&*()|[]{}:;<>?!'/@\"\\"
*/
readonly excludeCharacters?: string;
/**
* Where to place the rotation Lambda function
*
* @default - same placement as instance or cluster
*/
readonly vpcSubnets?: ec2.SubnetSelection;
/**
* The VPC interface endpoint to use for the Secrets Manager API
*
* If you enable private DNS hostnames for your VPC private endpoint (the default), you don't
* need to specify an endpoint. The standard Secrets Manager DNS hostname the Secrets Manager
* CLI and SDKs use by default (https://secretsmanager.<region>.amazonaws.com) automatically
* resolves to your VPC endpoint.
*
* @default https://secretsmanager.<region>.amazonaws.com
*/
readonly endpoint?: ec2.IInterfaceVpcEndpoint;
/**
* The security group for the Lambda rotation function
*
* @default - a new security group is created
*/
readonly securityGroup?: ec2.ISecurityGroup;
}
/**
* Options to add the multi user rotation
*/
export interface RotationSingleUserOptions extends CommonRotationUserOptions {
}
/**
* Options to add the multi user rotation
*/
export interface RotationMultiUserOptions extends CommonRotationUserOptions {
/**
* The secret to rotate. It must be a JSON string with the following format:
* ```
* {
* "engine": <required: database engine>,
* "host": <required: instance host name>,
* "username": <required: username>,
* "password": <required: password>,
* "dbname": <optional: database name>,
* "port": <optional: if not specified, default port will be used>,
* "masterarn": <required: the arn of the master secret which will be used to create users/change passwords>
* }
* ```
*/
readonly secret: secretsmanager.ISecret;
}
/**
* The retention period for Performance Insight.
*/
export enum PerformanceInsightRetention {
/**
* Default retention period of 7 days.
*/
DEFAULT = 7,
/**
* Long term retention period of 2 years.
*/
LONG_TERM = 731
}