-
Notifications
You must be signed in to change notification settings - Fork 73
/
type-safe-rest-api.ts
460 lines (432 loc) · 15 KB
/
type-safe-rest-api.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
/*! Copyright [Amazon.com](http://amazon.com/), Inc. or its affiliates. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0 */
import * as fs from "fs";
import * as path from "path";
import { PDKNag } from "@aws/pdk-nag";
import { CustomResource, Duration, Size, Stack } from "aws-cdk-lib";
import {
AccessLogFormat,
ApiDefinition,
Cors,
LogGroupLogDestination,
MethodLoggingLevel,
RestApiBaseProps,
SpecRestApi,
} from "aws-cdk-lib/aws-apigateway";
import {
Effect,
PolicyDocument,
PolicyStatement,
Role,
ServicePrincipal,
} from "aws-cdk-lib/aws-iam";
import {
CfnPermission,
Code,
Function as LambdaFunction,
Runtime,
} from "aws-cdk-lib/aws-lambda";
import { LogGroup } from "aws-cdk-lib/aws-logs";
import { Asset } from "aws-cdk-lib/aws-s3-assets";
import {
CfnIPSet,
CfnWebACL,
CfnWebACLAssociation,
} from "aws-cdk-lib/aws-wafv2";
import { Provider } from "aws-cdk-lib/custom-resources";
import { NagSuppressions } from "cdk-nag";
import { Construct } from "constructs";
import { PrepareApiSpecCustomResourceProperties } from "./prepare-spec-event-handler";
import {
prepareApiSpec,
PrepareApiSpecOptions,
} from "./prepare-spec-event-handler/prepare-spec";
import { SerializedCorsOptions, TypeSafeApiOptions } from "./spec";
import {
prepareSecuritySchemes,
serializeAsAuthorizerReference,
} from "./spec/api-gateway-auth";
import { getAuthorizerFunctions } from "./spec/api-gateway-integrations";
import { OpenApiGatewayWebAcl } from "./waf/open-api-gateway-web-acl";
import { TypeSafeApiWebAclOptions } from "./waf/types";
/**
* Configuration for the TypeSafeRestApi construct
*/
export interface TypeSafeRestApiProps
extends RestApiBaseProps,
TypeSafeApiOptions {
/**
* Path to the JSON open api spec
*/
readonly specPath: string;
/**
* Options for the AWS WAF v2 WebACL associated with the api. By default, a Web ACL with the AWS default managed
* rule set will be associated with the API. These options may disable or override the defaults.
*/
readonly webAclOptions?: TypeSafeApiWebAclOptions;
/**
* A Size(in bytes, kibibytes, mebibytes etc) that is used to enable compression (with non-negative
* between 0 and 10485760 (10M) bytes, inclusive) or disable compression
* (when undefined) on an API. When compression is enabled, compression or
* decompression is not applied on the payload if the payload size is
* smaller than this value. Setting it to zero allows compression for any
* payload size.
*
* @default - Compression is disabled.
*/
readonly minCompressionSize?: Size;
}
/**
* A construct for creating an api gateway rest api based on the definition in the OpenAPI spec.
*/
export class TypeSafeRestApi extends Construct {
/**
* Underlying API Gateway API construct
*/
public readonly api: SpecRestApi;
/**
* The OpenAPI specification with applied API gateway extensions
*/
readonly extendedApiSpecification: any;
/**
* Reference to the webacl, if created
*/
readonly webAcl?: CfnWebACL;
/**
* Reference to the IP set if created
*/
readonly ipSet?: CfnIPSet;
/**
* Reference to the web acl association if created
*/
readonly webAclAssociation?: CfnWebACLAssociation;
constructor(scope: Construct, id: string, props: TypeSafeRestApiProps) {
super(scope, id);
const {
integrations,
specPath,
operationLookup,
defaultAuthorizer,
corsOptions,
...options
} = props;
// Upload the spec to s3 as an asset
const inputSpecAsset = new Asset(this, "InputSpec", {
path: specPath,
});
// We'll output the prepared spec in the same asset bucket
const preparedSpecOutputKeyPrefix = `${inputSpecAsset.s3ObjectKey}-prepared`;
const stack = Stack.of(this);
// Lambda name prefix is truncated to 48 characters (16 below the max of 64)
const lambdaNamePrefix = `${PDKNag.getStackPrefix(stack)
.split("/")
.join("-")
.slice(0, 40)}${this.node.addr.slice(-8).toUpperCase()}`;
const prepareSpecLambdaName = `${lambdaNamePrefix}PrepSpec`;
const prepareSpecRole = new Role(this, "PrepareSpecRole", {
assumedBy: new ServicePrincipal("lambda.amazonaws.com"),
inlinePolicies: {
logs: new PolicyDocument({
statements: [
new PolicyStatement({
effect: Effect.ALLOW,
actions: [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents",
],
resources: [
`arn:aws:logs:${stack.region}:${stack.account}:log-group:/aws/lambda/${prepareSpecLambdaName}`,
`arn:aws:logs:${stack.region}:${stack.account}:log-group:/aws/lambda/${prepareSpecLambdaName}:*`,
],
}),
],
}),
s3: new PolicyDocument({
statements: [
new PolicyStatement({
effect: Effect.ALLOW,
actions: ["s3:getObject"],
resources: [
inputSpecAsset.bucket.arnForObjects(inputSpecAsset.s3ObjectKey),
],
}),
new PolicyStatement({
effect: Effect.ALLOW,
actions: ["s3:putObject"],
resources: [
// The output file will include a hash of the prepared spec, which is not known until deploy time since
// tokens must be resolved
inputSpecAsset.bucket.arnForObjects(
`${preparedSpecOutputKeyPrefix}/*`
),
],
}),
],
}),
},
});
["AwsSolutions-IAM5", "AwsPrototyping-IAMNoWildcardPermissions"].forEach(
(RuleId) => {
NagSuppressions.addResourceSuppressions(
prepareSpecRole,
[
{
id: RuleId,
reason:
"Cloudwatch resources have been scoped down to the LogGroup level, however * is still needed as stream names are created just in time.",
appliesTo: [
{
regex: `/^Resource::arn:aws:logs:${PDKNag.getStackRegionRegex(
stack
)}:${PDKNag.getStackAccountRegex(
stack
)}:log-group:/aws/lambda/${prepareSpecLambdaName}:\*/g`,
},
],
},
{
id: RuleId,
reason:
"S3 resources have been scoped down to the appropriate prefix in the CDK asset bucket, however * is still needed as since the prepared spec hash is not known until deploy time.",
appliesTo: [
{
regex: `/^Resource::arn:${PDKNag.getStackPartitionRegex(
stack
)}:s3:.*/${preparedSpecOutputKeyPrefix}/\*/g`,
},
],
},
],
true
);
}
);
// Create a custom resource for preparing the spec for deployment (adding integrations, authorizers, etc)
const prepareSpec = new LambdaFunction(this, "PrepareSpecHandler", {
handler: "index.handler",
runtime: Runtime.NODEJS_18_X,
code: Code.fromAsset(
path.join(__dirname, "./prepare-spec-event-handler")
),
timeout: Duration.seconds(30),
role: prepareSpecRole,
functionName: prepareSpecLambdaName,
});
const providerFunctionName = `${lambdaNamePrefix}PrepSpecProvider`;
const providerRole = new Role(this, "PrepareSpecProviderRole", {
assumedBy: new ServicePrincipal("lambda.amazonaws.com"),
inlinePolicies: {
logs: new PolicyDocument({
statements: [
new PolicyStatement({
effect: Effect.ALLOW,
actions: [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents",
],
resources: [
`arn:aws:logs:${stack.region}:${stack.account}:log-group:/aws/lambda/${providerFunctionName}`,
`arn:aws:logs:${stack.region}:${stack.account}:log-group:/aws/lambda/${providerFunctionName}:*`,
],
}),
],
}),
},
});
const provider = new Provider(this, "PrepareSpecProvider", {
onEventHandler: prepareSpec,
role: providerRole,
providerFunctionName,
});
["AwsSolutions-IAM5", "AwsPrototyping-IAMNoWildcardPermissions"].forEach(
(RuleId) => {
NagSuppressions.addResourceSuppressions(
providerRole,
[
{
id: RuleId,
reason:
"Cloudwatch resources have been scoped down to the LogGroup level, however * is still needed as stream names are created just in time.",
},
],
true
);
}
);
["AwsSolutions-L1", "AwsPrototyping-LambdaLatestVersion"].forEach(
(RuleId) => {
NagSuppressions.addResourceSuppressions(
provider,
[
{
id: RuleId,
reason:
"Latest runtime cannot be configured. CDK will need to upgrade the Provider construct accordingly.",
},
],
true
);
}
);
const serializedCorsOptions: SerializedCorsOptions | undefined =
corsOptions && {
allowHeaders: corsOptions.allowHeaders || [
...Cors.DEFAULT_HEADERS,
"x-amz-content-sha256",
],
allowMethods: corsOptions.allowMethods || Cors.ALL_METHODS,
allowOrigins: corsOptions.allowOrigins,
statusCode: corsOptions.statusCode || 204,
};
const prepareSpecOptions: PrepareApiSpecOptions = {
defaultAuthorizerReference:
serializeAsAuthorizerReference(defaultAuthorizer),
integrations: Object.fromEntries(
Object.entries(integrations).map(([operationId, integration]) => [
operationId,
{
integration: integration.integration.render({
operationId,
scope: this,
...operationLookup[operationId],
corsOptions: serializedCorsOptions,
operationLookup,
}),
methodAuthorizer: serializeAsAuthorizerReference(
integration.authorizer
),
options: integration.options,
},
])
),
securitySchemes: prepareSecuritySchemes(
this,
integrations,
defaultAuthorizer,
options.apiKeyOptions
),
corsOptions: serializedCorsOptions,
operationLookup,
apiKeyOptions: options.apiKeyOptions,
};
// Spec preparation will happen in a custom resource lambda so that references to lambda integrations etc can be
// resolved. However, we also prepare inline to perform some additional validation at synth time.
const spec = JSON.parse(fs.readFileSync(specPath, "utf-8"));
this.extendedApiSpecification = prepareApiSpec(spec, prepareSpecOptions);
const prepareApiSpecCustomResourceProperties: PrepareApiSpecCustomResourceProperties =
{
inputSpecLocation: {
bucket: inputSpecAsset.bucket.bucketName,
key: inputSpecAsset.s3ObjectKey,
},
outputSpecLocation: {
bucket: inputSpecAsset.bucket.bucketName,
key: preparedSpecOutputKeyPrefix,
},
...prepareSpecOptions,
};
const prepareSpecCustomResource = new CustomResource(
this,
"PrepareSpecCustomResource",
{
serviceToken: provider.serviceToken,
properties: prepareApiSpecCustomResourceProperties,
}
);
// Create the api gateway resources from the spec, augmenting the spec with the properties specific to api gateway
// such as integrations or auth types
this.api = new SpecRestApi(this, id, {
apiDefinition: this.node.tryGetContext("type-safe-api-local")
? ApiDefinition.fromInline(this.extendedApiSpecification)
: ApiDefinition.fromBucket(
inputSpecAsset.bucket,
prepareSpecCustomResource.getAttString("outputSpecKey")
),
deployOptions: {
accessLogDestination: new LogGroupLogDestination(
new LogGroup(this, `AccessLogs`)
),
accessLogFormat: AccessLogFormat.clf(),
loggingLevel: MethodLoggingLevel.INFO,
},
...options,
});
this.api.node.addDependency(prepareSpecCustomResource);
// While the api will be updated when the output path from the custom resource changes, CDK still needs to know when
// to redeploy the api. This is achieved by including a hash of the spec in the logical id (internalised in the
// addToLogicalId method since this is how changes of individual resources/methods etc trigger redeployments in CDK)
this.api.latestDeployment?.addToLogicalId(this.extendedApiSpecification);
// Grant API Gateway permission to invoke the integrations
Object.keys(integrations).forEach((operationId) => {
integrations[operationId].integration.grant({
operationId,
scope: this,
api: this.api,
...operationLookup[operationId],
operationLookup,
});
});
// Grant API Gateway permission to invoke each custom authorizer lambda (if any)
getAuthorizerFunctions(props).forEach(({ label, function: lambda }) => {
new CfnPermission(this, `LambdaPermission-${label}`, {
action: "lambda:InvokeFunction",
principal: "apigateway.amazonaws.com",
functionName: lambda.functionArn,
sourceArn: stack.formatArn({
service: "execute-api",
resource: this.api.restApiId,
resourceName: "*/*",
}),
});
});
// Create and associate the web acl if not disabled
if (!props.webAclOptions?.disable) {
const acl = new OpenApiGatewayWebAcl(this, `${id}-Acl`, {
...props.webAclOptions,
apiDeploymentStageArn: this.api.deploymentStage.stageArn,
});
this.webAcl = acl.webAcl;
this.ipSet = acl.ipSet;
this.webAclAssociation = acl.webAclAssociation;
}
["AwsSolutions-IAM4", "AwsPrototyping-IAMNoManagedPolicies"].forEach(
(RuleId) => {
NagSuppressions.addResourceSuppressions(
this,
[
{
id: RuleId,
reason:
"Cloudwatch Role requires access to create/read groups at the root level.",
appliesTo: [
{
regex: `/^Policy::arn:${PDKNag.getStackPartitionRegex(
stack
)}:iam::aws:policy/service-role/AmazonAPIGatewayPushToCloudWatchLogs$/g`,
},
],
},
],
true
);
}
);
["AwsSolutions-APIG2", "AwsPrototyping-APIGWRequestValidation"].forEach(
(RuleId) => {
NagSuppressions.addResourceSuppressions(
this,
[
{
id: RuleId,
reason:
"This construct implements fine grained validation via OpenApi.",
},
],
true
);
}
);
}
}