-
Notifications
You must be signed in to change notification settings - Fork 4k
/
parameter.ts
584 lines (513 loc) · 18.3 KB
/
parameter.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
import * as iam from '@aws-cdk/aws-iam';
import * as kms from '@aws-cdk/aws-kms';
import * as cxschema from '@aws-cdk/cloud-assembly-schema';
import {
CfnDynamicReference, CfnDynamicReferenceService, CfnParameter,
Construct as CompatConstruct, ContextProvider, Fn, IResource, Resource, Stack, Token,
Tokenization,
} from '@aws-cdk/core';
import { Construct } from 'constructs';
import * as ssm from './ssm.generated';
import { arnForParameterName, AUTOGEN_MARKER } from './util';
/**
* An SSM Parameter reference.
*/
export interface IParameter extends IResource {
/**
* The ARN of the SSM Parameter resource.
* @attribute
*/
readonly parameterArn: string;
/**
* The name of the SSM Parameter resource.
* @attribute
*/
readonly parameterName: string;
/**
* The type of the SSM Parameter resource.
* @attribute
*/
readonly parameterType: string;
/**
* Grants read (DescribeParameter, GetParameter, GetParameterHistory) permissions on the SSM Parameter.
*
* @param grantee the role to be granted read-only access to the parameter.
*/
grantRead(grantee: iam.IGrantable): iam.Grant;
/**
* Grants write (PutParameter) permissions on the SSM Parameter.
*
* @param grantee the role to be granted write access to the parameter.
*/
grantWrite(grantee: iam.IGrantable): iam.Grant;
}
/**
* A String SSM Parameter.
*/
export interface IStringParameter extends IParameter {
/**
* The parameter value. Value must not nest another parameter. Do not use {{}} in the value.
*
* @attribute Value
*/
readonly stringValue: string;
}
/**
* A StringList SSM Parameter.
*/
export interface IStringListParameter extends IParameter {
/**
* The parameter value. Value must not nest another parameter. Do not use {{}} in the value. Values in the array
* cannot contain commas (``,``).
*
* @attribute Value
*/
readonly stringListValue: string[];
}
/**
* Properties needed to create a new SSM Parameter.
*/
export interface ParameterOptions {
/**
* A regular expression used to validate the parameter value. For example, for String types with values restricted to
* numbers, you can specify the following: ``^\d+$``
*
* @default no validation is performed
*/
readonly allowedPattern?: string;
/**
* Information about the parameter that you want to add to the system.
*
* @default none
*/
readonly description?: string;
/**
* The name of the parameter.
*
* @default - a name will be generated by CloudFormation
*/
readonly parameterName?: string;
/**
* Indicates of the parameter name is a simple name (i.e. does not include "/"
* separators).
*
* This is only required only if `parameterName` is a token, which means we
* are unable to detect if the name is simple or "path-like" for the purpose
* of rendering SSM parameter ARNs.
*
* If `parameterName` is not specified, `simpleName` must be `true` (or
* undefined) since the name generated by AWS CloudFormation is always a
* simple name.
*
* @default - auto-detect based on `parameterName`
*/
readonly simpleName?: boolean;
/**
* The tier of the string parameter
*
* @default - undefined
*/
readonly tier?: ParameterTier;
}
/**
* Properties needed to create a String SSM parameter.
*/
export interface StringParameterProps extends ParameterOptions {
/**
* The value of the parameter. It may not reference another parameter and ``{{}}`` cannot be used in the value.
*/
readonly stringValue: string;
/**
* The type of the string parameter
*
* @default ParameterType.STRING
*/
readonly type?: ParameterType;
/**
* The data type of the parameter, such as `text` or `aws:ec2:image`.
*
* @default ParameterDataType.TEXT
*/
readonly dataType?: ParameterDataType;
}
/**
* Properties needed to create a StringList SSM Parameter
*/
export interface StringListParameterProps extends ParameterOptions {
/**
* The values of the parameter. It may not reference another parameter and ``{{}}`` cannot be used in the value.
*/
readonly stringListValue: string[];
}
/**
* Basic features shared across all types of SSM Parameters.
*/
abstract class ParameterBase extends Resource implements IParameter {
public abstract readonly parameterArn: string;
public abstract readonly parameterName: string;
public abstract readonly parameterType: string;
/**
* The encryption key that is used to encrypt this parameter.
*
* * @default - default master key
*/
public readonly encryptionKey?: kms.IKey;
public grantRead(grantee: iam.IGrantable): iam.Grant {
if (this.encryptionKey) {
this.encryptionKey.grantDecrypt(grantee);
}
return iam.Grant.addToPrincipal({
grantee,
actions: [
'ssm:DescribeParameters',
'ssm:GetParameters',
'ssm:GetParameter',
'ssm:GetParameterHistory',
],
resourceArns: [this.parameterArn],
});
}
public grantWrite(grantee: iam.IGrantable): iam.Grant {
if (this.encryptionKey) {
this.encryptionKey.grantEncrypt(grantee);
}
return iam.Grant.addToPrincipal({
grantee,
actions: ['ssm:PutParameter'],
resourceArns: [this.parameterArn],
});
}
}
/**
* SSM parameter type
*/
export enum ParameterType {
/**
* String
*/
STRING = 'String',
/**
* Secure String
*
* Parameter Store uses an AWS Key Management Service (KMS) customer master key (CMK) to encrypt the parameter value.
* Parameters of type SecureString cannot be created directly from a CDK application.
*/
SECURE_STRING = 'SecureString',
/**
* String List
*/
STRING_LIST = 'StringList',
/**
* An Amazon EC2 image ID, such as ami-0ff8a91507f77f867
*/
AWS_EC2_IMAGE_ID = 'AWS::EC2::Image::Id',
}
/**
* SSM parameter data type
*/
export enum ParameterDataType {
/**
* Text
*/
TEXT = 'text',
/**
* Aws Ec2 Image
*/
AWS_EC2_IMAGE = 'aws:ec2:image',
}
/**
* SSM parameter tier
*/
export enum ParameterTier {
/**
* String
*/
ADVANCED = 'Advanced',
/**
* String
*/
INTELLIGENT_TIERING = 'Intelligent-Tiering',
/**
* String
*/
STANDARD = 'Standard',
}
/**
* Common attributes for string parameters.
*/
export interface CommonStringParameterAttributes {
/**
* The name of the parameter store value.
*
* This value can be a token or a concrete string. If it is a concrete string
* and includes "/" it must also be prefixed with a "/" (fully-qualified).
*/
readonly parameterName: string;
/**
* Indicates of the parameter name is a simple name (i.e. does not include "/"
* separators).
*
* This is only required only if `parameterName` is a token, which means we
* are unable to detect if the name is simple or "path-like" for the purpose
* of rendering SSM parameter ARNs.
*
* If `parameterName` is not specified, `simpleName` must be `true` (or
* undefined) since the name generated by AWS CloudFormation is always a
* simple name.
*
* @default - auto-detect based on `parameterName`
*/
readonly simpleName?: boolean;
}
/**
* Attributes for parameters of various types of string.
*
* @see ParameterType
*/
export interface StringParameterAttributes extends CommonStringParameterAttributes {
/**
* The version number of the value you wish to retrieve.
*
* @default The latest version will be retrieved.
*/
readonly version?: number;
/**
* The type of the string parameter
*
* @default ParameterType.STRING
*/
readonly type?: ParameterType;
}
/**
* Attributes for secure string parameters.
*/
export interface SecureStringParameterAttributes extends CommonStringParameterAttributes {
/**
* The version number of the value you wish to retrieve.
*
* @default - AWS CloudFormation uses the latest version of the parameter
*/
readonly version?: number;
/**
* The encryption key that is used to encrypt this parameter
*
* @default - default master key
*/
readonly encryptionKey?: kms.IKey;
}
/**
* Creates a new String SSM Parameter.
* @resource AWS::SSM::Parameter
*/
export class StringParameter extends ParameterBase implements IStringParameter {
/**
* Imports an external string parameter by name.
*/
public static fromStringParameterName(scope: Construct, id: string, stringParameterName: string): IStringParameter {
return this.fromStringParameterAttributes(scope, id, { parameterName: stringParameterName });
}
/**
* Imports an external string parameter with name and optional version.
*/
public static fromStringParameterAttributes(scope: Construct, id: string, attrs: StringParameterAttributes): IStringParameter {
if (!attrs.parameterName) {
throw new Error('parameterName cannot be an empty string');
}
const type = attrs.type || ParameterType.STRING;
const stringValue = attrs.version
? new CfnDynamicReference(CfnDynamicReferenceService.SSM, `${attrs.parameterName}:${Tokenization.stringifyNumber(attrs.version)}`).toString()
: new CfnParameter(scope as CompatConstruct, `${id}.Parameter`, { type: `AWS::SSM::Parameter::Value<${type}>`, default: attrs.parameterName }).valueAsString;
class Import extends ParameterBase {
public readonly parameterName = attrs.parameterName;
public readonly parameterArn = arnForParameterName(this, attrs.parameterName, { simpleName: attrs.simpleName });
public readonly parameterType = type;
public readonly stringValue = stringValue;
}
return new Import(scope, id);
}
/**
* Imports a secure string parameter from the SSM parameter store.
*/
public static fromSecureStringParameterAttributes(scope: Construct, id: string, attrs: SecureStringParameterAttributes): IStringParameter {
const version = attrs.version ? Tokenization.stringifyNumber(attrs.version) : '';
const stringValue = new CfnDynamicReference(CfnDynamicReferenceService.SSM_SECURE, `${attrs.parameterName}:${version}`).toString();
class Import extends ParameterBase {
public readonly parameterName = attrs.parameterName;
public readonly parameterArn = arnForParameterName(this, attrs.parameterName, { simpleName: attrs.simpleName });
public readonly parameterType = ParameterType.SECURE_STRING;
public readonly stringValue = stringValue;
public readonly encryptionKey = attrs.encryptionKey;
}
return new Import(scope, id);
}
/**
* Reads the value of an SSM parameter during synthesis through an
* environmental context provider.
*
* Requires that the stack this scope is defined in will have explicit
* account/region information. Otherwise, it will fail during synthesis.
*/
public static valueFromLookup(scope: CompatConstruct, parameterName: string): string {
const value = ContextProvider.getValue(scope, {
provider: cxschema.ContextProvider.SSM_PARAMETER_PROVIDER,
props: { parameterName },
dummyValue: `dummy-value-for-${parameterName}`,
}).value;
return value;
}
/**
* Returns a token that will resolve (during deployment) to the string value of an SSM string parameter.
* @param scope Some scope within a stack
* @param parameterName The name of the SSM parameter.
* @param version The parameter version (recommended in order to ensure that the value won't change during deployment)
*/
public static valueForStringParameter(scope: Construct, parameterName: string, version?: number): string {
return StringParameter.valueForTypedStringParameter(scope, parameterName, ParameterType.STRING, version);
}
/**
* Returns a token that will resolve (during deployment) to the string value of an SSM string parameter.
* @param scope Some scope within a stack
* @param parameterName The name of the SSM parameter.
* @param type The type of the SSM parameter.
* @param version The parameter version (recommended in order to ensure that the value won't change during deployment)
*/
public static valueForTypedStringParameter(scope: Construct, parameterName: string, type = ParameterType.STRING, version?: number): string {
const stack = Stack.of(scope);
const id = makeIdentityForImportedValue(parameterName);
const exists = stack.node.tryFindChild(id) as IStringParameter;
if (exists) { return exists.stringValue; }
return this.fromStringParameterAttributes(stack, id, { parameterName, version, type }).stringValue;
}
/**
* Returns a token that will resolve (during deployment)
* @param scope Some scope within a stack
* @param parameterName The name of the SSM parameter
* @param version The parameter version (required for secure strings)
* @deprecated Use `SecretValue.ssmSecure()` instead, it will correctly type the imported value as a `SecretValue` and allow importing without version.
*/
public static valueForSecureStringParameter(scope: Construct, parameterName: string, version: number): string {
const stack = Stack.of(scope);
const id = makeIdentityForImportedValue(parameterName);
const exists = stack.node.tryFindChild(id) as IStringParameter;
if (exists) { return exists.stringValue; }
return this.fromSecureStringParameterAttributes(stack, id, { parameterName, version }).stringValue;
}
public readonly parameterArn: string;
public readonly parameterName: string;
public readonly parameterType: string;
public readonly stringValue: string;
constructor(scope: Construct, id: string, props: StringParameterProps) {
super(scope, id, {
physicalName: props.parameterName,
});
if (props.allowedPattern) {
_assertValidValue(props.stringValue, props.allowedPattern);
}
validateParameterName(this.physicalName);
if (props.description && props.description?.length > 1024) {
throw new Error('Description cannot be longer than 1024 characters.');
}
if (props.type && props.type === ParameterType.AWS_EC2_IMAGE_ID) {
throw new Error('The type must either be ParameterType.STRING or ParameterType.STRING_LIST. Did you mean to set dataType: ParameterDataType.AWS_EC2_IMAGE instead?');
}
const resource = new ssm.CfnParameter(this, 'Resource', {
allowedPattern: props.allowedPattern,
description: props.description,
name: this.physicalName,
tier: props.tier,
type: props.type || ParameterType.STRING,
dataType: props.dataType,
value: props.stringValue,
});
this.parameterName = this.getResourceNameAttribute(resource.ref);
this.parameterArn = arnForParameterName(this, this.parameterName, {
physicalName: props.parameterName || AUTOGEN_MARKER,
simpleName: props.simpleName,
});
this.parameterType = resource.attrType;
this.stringValue = resource.attrValue;
}
}
/**
* Creates a new StringList SSM Parameter.
* @resource AWS::SSM::Parameter
*/
export class StringListParameter extends ParameterBase implements IStringListParameter {
/**
* Imports an external parameter of type string list.
* Returns a token and should not be parsed.
*/
public static fromStringListParameterName(scope: Construct, id: string, stringListParameterName: string): IStringListParameter {
class Import extends ParameterBase {
public readonly parameterName = stringListParameterName;
public readonly parameterArn = arnForParameterName(this, this.parameterName);
public readonly parameterType = ParameterType.STRING_LIST;
public readonly stringListValue = Fn.split(',', new CfnDynamicReference(CfnDynamicReferenceService.SSM, stringListParameterName).toString());
}
return new Import(scope, id);
}
public readonly parameterArn: string;
public readonly parameterName: string;
public readonly parameterType: string;
public readonly stringListValue: string[];
constructor(scope: Construct, id: string, props: StringListParameterProps) {
super(scope, id, {
physicalName: props.parameterName,
});
if (props.stringListValue.find(str => !Token.isUnresolved(str) && str.indexOf(',') !== -1)) {
throw new Error('Values of a StringList SSM Parameter cannot contain the \',\' character. Use a string parameter instead.');
}
if (props.allowedPattern && !Token.isUnresolved(props.stringListValue)) {
props.stringListValue.forEach(str => _assertValidValue(str, props.allowedPattern!));
}
validateParameterName(this.physicalName);
if (props.description && props.description?.length > 1024) {
throw new Error('Description cannot be longer than 1024 characters.');
}
const resource = new ssm.CfnParameter(this, 'Resource', {
allowedPattern: props.allowedPattern,
description: props.description,
name: this.physicalName,
tier: props.tier,
type: ParameterType.STRING_LIST,
value: props.stringListValue.join(','),
});
this.parameterName = this.getResourceNameAttribute(resource.ref);
this.parameterArn = arnForParameterName(this, this.parameterName, {
physicalName: props.parameterName || AUTOGEN_MARKER,
simpleName: props.simpleName,
});
this.parameterType = resource.attrType;
this.stringListValue = Fn.split(',', resource.attrValue);
}
}
/**
* Validates whether a supplied value conforms to the allowedPattern, granted neither is an unresolved token.
*
* @param value the value to be validated.
* @param allowedPattern the regular expression to use for validation.
*
* @throws if the ``value`` does not conform to the ``allowedPattern`` and neither is an unresolved token (per
* ``cdk.unresolved``).
*/
function _assertValidValue(value: string, allowedPattern: string): void {
if (Token.isUnresolved(value) || Token.isUnresolved(allowedPattern)) {
// Unable to perform validations against unresolved tokens
return;
}
if (!new RegExp(allowedPattern).test(value)) {
throw new Error(`The supplied value (${value}) does not match the specified allowedPattern (${allowedPattern})`);
}
}
function makeIdentityForImportedValue(parameterName: string) {
return `SsmParameterValue:${parameterName}:C96584B6-F00A-464E-AD19-53AFF4B05118`;
}
function validateParameterName(parameterName: string) {
if (Token.isUnresolved(parameterName)) { return; }
if (parameterName.length > 2048) {
throw new Error('name cannot be longer than 2048 characters.');
}
if (!parameterName.match(/^[\/\w.-]+$/)) {
throw new Error(`name must only contain letters, numbers, and the following 4 symbols .-_/; got ${parameterName}`);
}
}