-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
index.ts
412 lines (369 loc) · 12 KB
/
index.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
import {
Annotations,
aws_iam,
aws_kms,
aws_lambda,
CustomResource,
Duration,
ITaggable,
Lazy,
ResourceProps,
Stack,
TagManager,
TagType,
} from 'aws-cdk-lib';
import { Construct } from 'constructs';
import path = require('path');
const resourceType = 'Custom::EC2-Key-Pair';
const ID = `CFN::Resource::${resourceType}`;
const createdByTag = 'CreatedByCfnCustomResource';
const cleanID = ID.replace(/:+/g, '-');
const lambdaTimeout = 3; // minutes
export enum PublicKeyFormat {
OPENSSH = 'OPENSSH',
PEM = 'PEM',
}
/**
* Definition of EC2 Key Pair
*/
export interface KeyPairProps extends ResourceProps {
/**
* Name of the Key Pair
*
* In AWS Secrets Manager the key will be prefixed with `ec2-ssh-key/`.
*
* The name can be up to 255 characters long. Valid characters include _, -, a-z, A-Z, and 0-9.
*/
readonly name: string;
/**
* The description for the key in AWS Secrets Manager
* @default - ''
*/
readonly description?: string;
/**
* The KMS key used to encrypt the Secrets Manager secrets with
*
* This needs to be a key created in the same stack. You cannot use a key imported via ARN, because the keys access policy will need to be modified.
*
* @default - `alias/aws/secretsmanager`
*/
readonly kms?: aws_kms.Key;
/**
* The KMS key to use to encrypt the private key with
*
* This needs to be a key created in the same stack. You cannot use a key imported via ARN, because the keys access policy will need to be modified.
*
* If no value is provided, the property `kms` will be used instead.
*
* @default - `this.kms`
*/
readonly kmsPrivateKey?: aws_kms.Key;
/**
* The KMS key to use to encrypt the public key with
*
* This needs to be a key created in the same stack. You cannot use a key imported via ARN, because the keys access policy will need to be modified.
*
* If no value is provided, the property `kms` will be used instead.
*
* @default - `this.kms`
*/
readonly kmsPublicKey?: aws_kms.Key;
/**
* Import a public key instead of creating it
*
* If no public key is provided, a new key pair will be created.
*/
readonly publicKey?: string;
/**
* Store the public key as a secret
*
* @default - false
*/
readonly storePublicKey?: boolean;
/**
* Expose the public key as property `publicKeyValue`
*
* @default - false
*/
readonly exposePublicKey?: boolean;
/**
* Format for public key.
*
* Relevant only if the public key is stored and/or exposed.
*
* @default - OPENSSH
*/
readonly publicKeyFormat?: PublicKeyFormat;
/**
* When the resource is destroyed, after how many days the private and public key in the AWS Secrets Manager should be deleted.
*
* Valid values are 0 and 7 to 30
*
* @default 0
*/
readonly removeKeySecretsAfterDays?: number;
/**
* Prefix for the secret in AWS Secrets Manager.
*
* @default `ec2-ssh-key/`
*/
readonly secretPrefix?: string;
/**
* A prefix for all resource names.
*
* By default all resources are prefixed with the stack name to avoid collisions with other stacks. This might cause problems when you work with long stack names and can be overridden through this parameter.
*
* @default Name of the stack
*/
readonly resourcePrefix?: string;
}
/**
* An EC2 Key Pair
*/
export class KeyPair extends Construct implements ITaggable {
/**
* The lambda function that is created
*/
public readonly lambda: aws_lambda.IFunction;
/**
* ARN of the private key in AWS Secrets Manager
*/
public readonly privateKeyArn: string = '';
/**
* ARN of the public key in AWS Secrets Manager
*/
public readonly publicKeyArn: string = '';
/**
* The public key.
*
* Only filled, when `exposePublicKey = true`
*/
public readonly publicKeyValue: string = '';
/**
* Name of the Key Pair
*/
public readonly keyPairName: string = '';
/**
* ID of the Key Pair
*/
public readonly keyPairID: string = '';
/**
* Resource tags
*/
public readonly tags: TagManager;
public readonly prefix: string = '';
/**
* Defines a new EC2 Key Pair. The private key will be stored in AWS Secrets Manager
*/
constructor(scope: Construct, id: string, props: KeyPairProps) {
super(scope, id);
if (
props.removeKeySecretsAfterDays &&
(props.removeKeySecretsAfterDays < 0 ||
(props.removeKeySecretsAfterDays > 0 &&
props.removeKeySecretsAfterDays < 7) ||
props.removeKeySecretsAfterDays > 30)
) {
Annotations.of(this).addError(
`Parameter removeKeySecretsAfterDays must be 0 or between 7 and 30. Got ${props.removeKeySecretsAfterDays}`
);
}
if (
props.publicKey?.length &&
props.publicKeyFormat !== undefined &&
props.publicKeyFormat !== PublicKeyFormat.OPENSSH
) {
Annotations.of(this).addError(
'When importing a key, the format has to be of type OpenSSH'
);
}
const stack = Stack.of(this).stackName;
this.prefix = props.resourcePrefix || stack;
if (this.prefix.length + cleanID.length > 62)
// Cloudformation limits names to 63 characters.
Annotations.of(this).addError(
`Cloudformation limits names to 63 characters.
Prefix ${this.prefix} is too long to be used as a prefix for your roleName. Define parameter resourcePrefix?:`
);
this.lambda = this.ensureLambda();
this.tags = new TagManager(TagType.MAP, 'Custom::EC2-Key-Pair');
this.tags.setTag(createdByTag, ID);
const kmsPrivate = props.kmsPrivateKey || props.kms;
const kmsPublic = props.kmsPublicKey || props.kms;
const key = new CustomResource(this, `EC2-Key-Pair-${props.name}`, {
serviceToken: this.lambda.functionArn,
resourceType: resourceType,
properties: {
Name: props.name,
Description: props.description || '',
KmsPrivate: kmsPrivate?.keyArn || 'alias/aws/secretsmanager',
KmsPublic: kmsPublic?.keyArn || 'alias/aws/secretsmanager',
PublicKey: props.publicKey || '',
StorePublicKey: props.storePublicKey || false,
ExposePublicKey: props.exposePublicKey || false,
PublicKeyFormat: props.publicKeyFormat || PublicKeyFormat.OPENSSH,
RemoveKeySecretsAfterDays: props.removeKeySecretsAfterDays || 0,
SecretPrefix: props.secretPrefix || 'ec2-ssh-key/',
StackName: stack,
Tags: Lazy.any({
produce: () => this.tags.renderTags(),
}),
},
});
if (typeof props.kms !== 'undefined') {
props.kms.grantEncryptDecrypt(this.lambda.role!);
key.node.addDependency(props.kms);
key.node.addDependency(this.lambda.role!);
}
if (typeof props.kmsPrivateKey !== 'undefined') {
props.kmsPrivateKey.grantEncryptDecrypt(this.lambda.role!);
key.node.addDependency(props.kmsPrivateKey);
key.node.addDependency(this.lambda.role!);
}
if (typeof props.kmsPublicKey !== 'undefined') {
props.kmsPublicKey.grantEncryptDecrypt(this.lambda.role!);
key.node.addDependency(props.kmsPublicKey);
key.node.addDependency(this.lambda.role!);
}
this.privateKeyArn = key.getAttString('PrivateKeyARN');
this.publicKeyArn = key.getAttString('PublicKeyARN');
this.publicKeyValue = key.getAttString('PublicKeyValue');
this.keyPairName = key.getAttString('KeyPairName');
this.keyPairID = key.getAttString('KeyPairID');
}
private ensureLambda(): aws_lambda.Function {
const stack = Stack.of(this);
const constructName = 'EC2-Key-Name-Manager-Lambda';
const existing = stack.node.tryFindChild(constructName);
if (existing) {
return existing as aws_lambda.Function;
}
const resources = [`arn:${stack.partition}:ec2:*:*:key-pair/*`];
const policy = new aws_iam.ManagedPolicy(
stack,
'EC2-Key-Pair-Manager-Policy',
{
managedPolicyName: `${this.prefix}-${cleanID}`,
description: `Used by Lambda ${cleanID}, which is a custom CFN resource, managing EC2 Key Pairs`,
statements: [
new aws_iam.PolicyStatement({
actions: ['ec2:DescribeKeyPairs'],
resources: ['*'],
}),
new aws_iam.PolicyStatement({
actions: [
'ec2:CreateKeyPair',
'ec2:CreateTags',
'ec2:ImportKeyPair',
],
conditions: {
StringLike: {
'aws:RequestTag/CreatedByCfnCustomResource': ID,
},
},
resources,
}),
new aws_iam.PolicyStatement({
// allow delete/update, only if createdByTag is set
actions: ['ec2:CreateTags', 'ec2:DeleteKeyPair', 'ec2:DeleteTags'],
conditions: {
StringLike: {
'ec2:ResourceTag/CreatedByCfnCustomResource': ID,
},
},
resources,
}),
new aws_iam.PolicyStatement({
// we need this to check if a secret exists before attempting to delete it
actions: ['secretsmanager:ListSecrets'],
resources: ['*'],
}),
new aws_iam.PolicyStatement({
actions: [
'secretsmanager:CreateSecret',
'secretsmanager:TagResource',
],
conditions: {
StringLike: {
'aws:RequestTag/CreatedByCfnCustomResource': ID,
},
},
resources: ['*'],
}),
new aws_iam.PolicyStatement({
// allow delete/update, only if createdByTag is set
actions: [
'secretsmanager:DeleteResourcePolicy',
'secretsmanager:DeleteSecret',
'secretsmanager:DescribeSecret',
'secretsmanager:GetResourcePolicy',
'secretsmanager:GetSecretValue',
'secretsmanager:ListSecretVersionIds',
'secretsmanager:PutResourcePolicy',
'secretsmanager:PutSecretValue',
'secretsmanager:RestoreSecret',
'secretsmanager:UntagResource',
'secretsmanager:UpdateSecret',
'secretsmanager:UpdateSecretVersionStage',
],
conditions: {
StringLike: {
'secretsmanager:ResourceTag/CreatedByCfnCustomResource': ID,
},
},
resources: ['*'],
}),
],
}
);
const role = new aws_iam.Role(stack, 'EC2-Key-Pair-Manager-Role', {
roleName: `${this.prefix}-${cleanID}`,
description: `Used by Lambda ${cleanID}, which is a custom CFN resource, managing EC2 Key Pairs`,
assumedBy: new aws_iam.ServicePrincipal('lambda.amazonaws.com'),
managedPolicies: [
policy,
aws_iam.ManagedPolicy.fromAwsManagedPolicyName(
'service-role/AWSLambdaBasicExecutionRole'
),
],
});
const fn = new aws_lambda.Function(stack, constructName, {
functionName: `${this.prefix}-${cleanID}`,
role: role,
description: 'Custom CFN resource: Manage EC2 Key Pairs',
runtime: aws_lambda.Runtime.NODEJS_14_X,
handler: 'index.handler',
code: aws_lambda.Code.fromAsset(
path.join(__dirname, '../lambda/code.zip')
),
timeout: Duration.minutes(lambdaTimeout),
});
return fn;
}
/**
* Grants read access to the private key in AWS Secrets Manager
*/
grantReadOnPrivateKey(grantee: aws_iam.IGrantable) {
return this.grantRead(this.privateKeyArn, grantee);
}
/**
* Grants read access to the public key in AWS Secrets Manager
*/
grantReadOnPublicKey(grantee: aws_iam.IGrantable) {
return this.grantRead(this.publicKeyArn, grantee);
}
private grantRead(arn: string, grantee: aws_iam.IGrantable) {
const result = aws_iam.Grant.addToPrincipal({
grantee,
actions: [
'secretsmanager:DescribeSecret',
'secretsmanager:GetResourcePolicy',
'secretsmanager:GetSecretValue',
'secretsmanager:ListSecretVersionIds',
],
resourceArns: [arn],
scope: this,
});
return result;
}
}