/
gateway-route-spec.ts
396 lines (352 loc) · 11.2 KB
/
gateway-route-spec.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
import { Construct } from 'constructs';
import { CfnGatewayRoute } from './appmesh.generated';
import { HeaderMatch } from './header-match';
import { HttpRouteMethod } from './http-route-method';
import { HttpGatewayRoutePathMatch } from './http-route-path-match';
import { validateGrpcMatchArrayLength, validateGrpcGatewayRouteMatch } from './private/utils';
import { QueryParameterMatch } from './query-parameter-match';
import { Protocol } from './shared-interfaces';
import { IVirtualService } from './virtual-service';
/**
* Configuration for gateway route host name match.
*/
export interface GatewayRouteHostnameMatchConfig {
/**
* GatewayRoute CFN configuration for host name match.
*/
readonly hostnameMatch: CfnGatewayRoute.GatewayRouteHostnameMatchProperty;
}
/**
* Used to generate host name matching methods.
*/
export abstract class GatewayRouteHostnameMatch {
/**
* The value of the host name must match the specified value exactly.
*
* @param name The exact host name to match on
*/
public static exactly(name: string): GatewayRouteHostnameMatch {
return new GatewayRouteHostnameMatchImpl({ exact: name });
}
/**
* The value of the host name with the given name must end with the specified characters.
*
* @param suffix The specified ending characters of the host name to match on
*/
public static endsWith(suffix: string): GatewayRouteHostnameMatch {
return new GatewayRouteHostnameMatchImpl({ suffix });
}
/**
* Returns the gateway route host name match configuration.
*/
public abstract bind(scope: Construct): GatewayRouteHostnameMatchConfig;
}
class GatewayRouteHostnameMatchImpl extends GatewayRouteHostnameMatch {
constructor(
private readonly matchProperty: CfnGatewayRoute.GatewayRouteHostnameMatchProperty,
) {
super();
}
bind(_scope: Construct): GatewayRouteHostnameMatchConfig {
return {
hostnameMatch: this.matchProperty,
};
}
}
/**
* The criterion for determining a request match for this GatewayRoute.
*/
export interface HttpGatewayRouteMatch {
/**
* Specify how to match requests based on the 'path' part of their URL.
*
* @default - matches requests with any path
*/
readonly path?: HttpGatewayRoutePathMatch;
/**
* Specifies the client request headers to match on. All specified headers
* must match for the gateway route to match.
*
* @default - do not match on headers
*/
readonly headers?: HeaderMatch[];
/**
* The gateway route host name to be matched on.
*
* @default - do not match on host name
*/
readonly hostname?: GatewayRouteHostnameMatch;
/**
* The method to match on.
*
* @default - do not match on method
*/
readonly method?: HttpRouteMethod;
/**
* The query parameters to match on.
* All specified query parameters must match for the route to match.
*
* @default - do not match on query parameters
*/
readonly queryParameters?: QueryParameterMatch[];
/**
* When `true`, rewrites the original request received at the Virtual Gateway to the destination Virtual Service name.
* When `false`, retains the original hostname from the request.
*
* @default true
*/
readonly rewriteRequestHostname?: boolean;
/**
* The port number to match on.
*
* @default - no default port
*/
readonly port?: number;
}
/**
* The criterion for determining a request match for this GatewayRoute
*/
export interface GrpcGatewayRouteMatch {
/**
* Create service name based gRPC gateway route match.
*
* @default - no matching on service name
*/
readonly serviceName?: string;
/**
* Create host name based gRPC gateway route match.
*
* @default - no matching on host name
*/
readonly hostname?: GatewayRouteHostnameMatch;
/**
* Create metadata based gRPC gateway route match.
* All specified metadata must match for the route to match.
*
* @default - no matching on metadata
*/
readonly metadata?: HeaderMatch[];
/**
* When `true`, rewrites the original request received at the Virtual Gateway to the destination Virtual Service name.
* When `false`, retains the original hostname from the request.
*
* @default true
*/
readonly rewriteRequestHostname?: boolean;
/**
* The port to match from the request.
*
* @default - do not match on port
*/
readonly port?: number;
}
/**
* Base options for all gateway route specs.
*/
export interface CommonGatewayRouteSpecOptions {
/**
* The priority for the gateway route. When a Virtual Gateway has multiple gateway routes, gateway route match
* is performed in the order of specified value, where 0 is the highest priority,
* and first matched gateway route is selected.
*
* @default - no particular priority
*/
readonly priority?: number;
}
/**
* Properties specific for HTTP Based GatewayRoutes
*/
export interface HttpGatewayRouteSpecOptions extends CommonGatewayRouteSpecOptions {
/**
* The criterion for determining a request match for this GatewayRoute.
* When path match is defined, this may optionally determine the path rewrite configuration.
*
* @default - matches any path and automatically rewrites the path to '/'
*/
readonly match?: HttpGatewayRouteMatch;
/**
* The VirtualService this GatewayRoute directs traffic to
*/
readonly routeTarget: IVirtualService;
}
/**
* Properties specific for a gRPC GatewayRoute
*/
export interface GrpcGatewayRouteSpecOptions extends CommonGatewayRouteSpecOptions {
/**
* The criterion for determining a request match for this GatewayRoute
*/
readonly match: GrpcGatewayRouteMatch;
/**
* The VirtualService this GatewayRoute directs traffic to
*/
readonly routeTarget: IVirtualService;
}
/**
* All Properties for GatewayRoute Specs
*/
export interface GatewayRouteSpecConfig {
/**
* The spec for an http gateway route
*
* @default - no http spec
*/
readonly httpSpecConfig?: CfnGatewayRoute.HttpGatewayRouteProperty;
/**
* The spec for an http2 gateway route
*
* @default - no http2 spec
*/
readonly http2SpecConfig?: CfnGatewayRoute.HttpGatewayRouteProperty;
/**
* The spec for a grpc gateway route
*
* @default - no grpc spec
*/
readonly grpcSpecConfig?: CfnGatewayRoute.GrpcGatewayRouteProperty;
/**
* The priority for the gateway route. When a Virtual Gateway has multiple gateway routes, gateway route match
* is performed in the order of specified value, where 0 is the highest priority,
* and first matched gateway route is selected.
*
* @default - no particular priority
*/
readonly priority?: number;
}
/**
* Used to generate specs with different protocols for a GatewayRoute
*/
export abstract class GatewayRouteSpec {
/**
* Creates an HTTP Based GatewayRoute
*
* @param options - no http gateway route
*/
public static http(options: HttpGatewayRouteSpecOptions): GatewayRouteSpec {
return new HttpGatewayRouteSpec(options, Protocol.HTTP);
}
/**
* Creates an HTTP2 Based GatewayRoute
*
* @param options - no http2 gateway route
*/
public static http2(options: HttpGatewayRouteSpecOptions): GatewayRouteSpec {
return new HttpGatewayRouteSpec(options, Protocol.HTTP2);
}
/**
* Creates an gRPC Based GatewayRoute
*
* @param options - no grpc gateway route
*/
public static grpc(options: GrpcGatewayRouteSpecOptions): GatewayRouteSpec {
return new GrpcGatewayRouteSpec(options);
}
/**
* Called when the GatewayRouteSpec type is initialized. Can be used to enforce
* mutual exclusivity with future properties
*/
public abstract bind(scope: Construct): GatewayRouteSpecConfig;
}
class HttpGatewayRouteSpec extends GatewayRouteSpec {
readonly match?: HttpGatewayRouteMatch;
/**
* The VirtualService this GatewayRoute directs traffic to
*/
readonly routeTarget: IVirtualService;
/**
* Type of route you are creating
*/
readonly routeType: Protocol;
readonly priority?: number;
constructor(options: HttpGatewayRouteSpecOptions, protocol: Protocol.HTTP | Protocol.HTTP2) {
super();
this.routeTarget = options.routeTarget;
this.routeType = protocol;
this.match = options.match;
this.priority = options.priority;
}
public bind(scope: Construct): GatewayRouteSpecConfig {
const pathMatchConfig = (this.match?.path ?? HttpGatewayRoutePathMatch.startsWith('/')).bind(scope);
const rewriteRequestHostname = this.match?.rewriteRequestHostname;
const prefixPathRewrite = pathMatchConfig.prefixPathRewrite;
const wholePathRewrite = pathMatchConfig.wholePathRewrite;
const httpConfig: CfnGatewayRoute.HttpGatewayRouteProperty = {
match: {
prefix: pathMatchConfig.prefixPathMatch,
path: pathMatchConfig.wholePathMatch,
hostname: this.match?.hostname?.bind(scope).hostnameMatch,
method: this.match?.method,
headers: this.match?.headers?.map(header => header.bind(scope).headerMatch),
queryParameters: this.match?.queryParameters?.map(queryParameter => queryParameter.bind(scope).queryParameterMatch),
port: this.match?.port,
},
action: {
target: {
virtualService: {
virtualServiceName: this.routeTarget.virtualServiceName,
},
},
rewrite: rewriteRequestHostname !== undefined || prefixPathRewrite || wholePathRewrite
? {
hostname: rewriteRequestHostname === undefined
? undefined
: {
defaultTargetHostname: rewriteRequestHostname? 'ENABLED' : 'DISABLED',
},
prefix: prefixPathRewrite,
path: wholePathRewrite,
}
: undefined,
},
};
return {
priority: this.priority,
httpSpecConfig: this.routeType === Protocol.HTTP ? httpConfig : undefined,
http2SpecConfig: this.routeType === Protocol.HTTP2 ? httpConfig : undefined,
};
}
}
class GrpcGatewayRouteSpec extends GatewayRouteSpec {
readonly match: GrpcGatewayRouteMatch;
/**
* The VirtualService this GatewayRoute directs traffic to
*/
readonly routeTarget: IVirtualService;
readonly priority?: number;
constructor(options: GrpcGatewayRouteSpecOptions) {
super();
this.match = options.match;
this.routeTarget = options.routeTarget;
this.priority = options.priority;
}
public bind(scope: Construct): GatewayRouteSpecConfig {
const metadataMatch = this.match.metadata;
validateGrpcGatewayRouteMatch(this.match);
validateGrpcMatchArrayLength(metadataMatch);
return {
grpcSpecConfig: {
match: {
serviceName: this.match.serviceName,
hostname: this.match.hostname?.bind(scope).hostnameMatch,
port: this.match.port,
metadata: metadataMatch?.map(metadata => metadata.bind(scope).headerMatch),
},
action: {
target: {
virtualService: {
virtualServiceName: this.routeTarget.virtualServiceName,
},
},
rewrite: this.match.rewriteRequestHostname === undefined
? undefined
: {
hostname: {
defaultTargetHostname: this.match.rewriteRequestHostname ? 'ENABLED' : 'DISABLED',
},
},
},
},
priority: this.priority,
};
}
}