-
Notifications
You must be signed in to change notification settings - Fork 4k
/
data-source.ts
488 lines (449 loc) · 13.1 KB
/
data-source.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
import { Construct } from 'constructs';
import { BaseAppsyncFunctionProps, AppsyncFunction } from './appsync-function';
import { CfnDataSource } from './appsync.generated';
import { IGraphqlApi } from './graphqlapi-base';
import { BaseResolverProps, Resolver } from './resolver';
import { ITable } from '../../aws-dynamodb';
import { IDomain as IElasticsearchDomain } from '../../aws-elasticsearch';
import { IEventBus } from '../../aws-events';
import { Grant, IGrantable, IPrincipal, IRole, Role, ServicePrincipal } from '../../aws-iam';
import { IFunction } from '../../aws-lambda';
import { IDomain as IOpenSearchDomain } from '../../aws-opensearchservice';
import { IServerlessCluster, IDatabaseCluster } from '../../aws-rds';
import { ISecret } from '../../aws-secretsmanager';
import { IResolvable, Lazy, Stack, Token } from '../../core';
/**
* Base properties for an AppSync datasource
*/
export interface BaseDataSourceProps {
/**
* The API to attach this data source to
*/
readonly api: IGraphqlApi;
/**
* The name of the data source
*
* @default - id of data source
*/
readonly name?: string;
/**
* the description of the data source
*
* @default - None
*/
readonly description?: string;
}
/**
* properties for an AppSync datasource backed by a resource
*/
export interface BackedDataSourceProps extends BaseDataSourceProps {
/**
* The IAM service role to be assumed by AppSync to interact with the data source
*
* @default - Create a new role
*/
readonly serviceRole?: IRole;
}
/**
* props used by implementations of BaseDataSource to provide configuration. Should not be used directly.
*/
export interface ExtendedDataSourceProps {
/**
* the type of the AppSync datasource
*/
readonly type: string;
/**
* configuration for DynamoDB Datasource
*
* @default - No config
*/
readonly dynamoDbConfig?: CfnDataSource.DynamoDBConfigProperty | IResolvable;
/**
* configuration for Elasticsearch data source
*
* @deprecated - use `openSearchConfig`
* @default - No config
*/
readonly elasticsearchConfig?: CfnDataSource.ElasticsearchConfigProperty | IResolvable;
/**
* configuration for OpenSearch data source
*
* @default - No config
*/
readonly openSearchServiceConfig?: CfnDataSource.OpenSearchServiceConfigProperty | IResolvable;
/**
* configuration for HTTP Datasource
*
* @default - No config
*/
readonly httpConfig?: CfnDataSource.HttpConfigProperty | IResolvable;
/**
* configuration for EventBridge Datasource
*
* @default - No config
*/
readonly eventBridgeConfig?: CfnDataSource.EventBridgeConfigProperty | IResolvable;
/**
* configuration for Lambda Datasource
*
* @default - No config
*/
readonly lambdaConfig?: CfnDataSource.LambdaConfigProperty | IResolvable;
/**
* configuration for RDS Datasource
*
* @default - No config
*/
readonly relationalDatabaseConfig?: CfnDataSource.RelationalDatabaseConfigProperty | IResolvable;
}
/**
* Abstract AppSync datasource implementation. Do not use directly but use subclasses for concrete datasources
*/
export abstract class BaseDataSource extends Construct {
/**
* the name of the data source
*/
public readonly name: string;
/**
* the underlying CFN data source resource
*/
public readonly ds: CfnDataSource;
protected api: IGraphqlApi;
protected serviceRole?: IRole;
constructor(scope: Construct, id: string, props: BackedDataSourceProps, extended: ExtendedDataSourceProps) {
super(scope, id);
if (extended.type !== 'NONE') {
this.serviceRole = props.serviceRole || new Role(this, 'ServiceRole', { assumedBy: new ServicePrincipal('appsync.amazonaws.com') });
}
// Replace unsupported characters from DataSource name. The only allowed pattern is: {[_A-Za-z][_0-9A-Za-z]*}
const name = (props.name ?? id);
const supportedName = Token.isUnresolved(name) ? name : name.replace(/[\W]+/g, '');
this.ds = new CfnDataSource(this, 'Resource', {
apiId: props.api.apiId,
name: supportedName,
description: props.description,
serviceRoleArn: this.serviceRole?.roleArn,
...extended,
});
this.name = supportedName;
this.api = props.api;
}
/**
* creates a new resolver for this datasource and API using the given properties
*/
public createResolver(id: string, props: BaseResolverProps): Resolver {
return new Resolver(this.api, id, {
api: this.api,
dataSource: this,
...props,
});
}
/**
* creates a new appsync function for this datasource and API using the given properties
*/
public createFunction(id: string, props: BaseAppsyncFunctionProps): AppsyncFunction {
return new AppsyncFunction(this.api, id, {
api: this.api,
dataSource: this,
...props,
});
}
}
/**
* Abstract AppSync datasource implementation. Do not use directly but use subclasses for resource backed datasources
*/
export abstract class BackedDataSource extends BaseDataSource implements IGrantable {
/**
* the principal of the data source to be IGrantable
*/
public readonly grantPrincipal: IPrincipal;
constructor(scope: Construct, id: string, props: BackedDataSourceProps, extended: ExtendedDataSourceProps) {
super(scope, id, props, extended);
this.grantPrincipal = this.serviceRole!;
}
}
/**
* Properties for an AppSync dummy datasource
*/
export interface NoneDataSourceProps extends BaseDataSourceProps {
}
/**
* An AppSync dummy datasource
*/
export class NoneDataSource extends BaseDataSource {
constructor(scope: Construct, id: string, props: NoneDataSourceProps) {
super(scope, id, props, {
type: 'NONE',
});
}
}
/**
* Properties for an AppSync DynamoDB datasource
*/
export interface DynamoDbDataSourceProps extends BackedDataSourceProps {
/**
* The DynamoDB table backing this data source
*/
readonly table: ITable;
/**
* Specify whether this DS is read only or has read and write permissions to the DynamoDB table
*
* @default false
*/
readonly readOnlyAccess?: boolean;
/**
* use credentials of caller to access DynamoDB
*
* @default false
*/
readonly useCallerCredentials?: boolean;
}
/**
* An AppSync datasource backed by a DynamoDB table
*/
export class DynamoDbDataSource extends BackedDataSource {
constructor(scope: Construct, id: string, props: DynamoDbDataSourceProps) {
super(scope, id, props, {
type: 'AMAZON_DYNAMODB',
dynamoDbConfig: {
tableName: props.table.tableName,
awsRegion: props.table.env.region,
useCallerCredentials: props.useCallerCredentials,
},
});
if (props.readOnlyAccess) {
props.table.grantReadData(this);
} else {
props.table.grantReadWriteData(this);
}
}
}
/**
* The authorization config in case the HTTP endpoint requires authorization
*/
export interface AwsIamConfig {
/**
* The signing region for AWS IAM authorization
*/
readonly signingRegion: string;
/**
* The signing service name for AWS IAM authorization
*/
readonly signingServiceName: string;
}
/**
* Properties for an AppSync http datasource
*/
export interface HttpDataSourceProps extends BackedDataSourceProps {
/**
* The http endpoint
*/
readonly endpoint: string;
/**
* The authorization config in case the HTTP endpoint requires authorization
*
* @default - none
*
*/
readonly authorizationConfig?: AwsIamConfig;
}
/**
* An AppSync datasource backed by a http endpoint
*/
export class HttpDataSource extends BackedDataSource {
constructor(scope: Construct, id: string, props: HttpDataSourceProps) {
const authorizationConfig = props.authorizationConfig ? {
authorizationType: 'AWS_IAM',
awsIamConfig: props.authorizationConfig,
} : undefined;
super(scope, id, props, {
type: 'HTTP',
httpConfig: {
endpoint: props.endpoint,
authorizationConfig,
},
});
}
}
/**
* Properties for an AppSync EventBridge datasource
*/
export interface EventBridgeDataSourceProps extends BackedDataSourceProps {
/**
* The EventBridge EventBus
*/
readonly eventBus: IEventBus;
}
/**
* An AppSync datasource backed by EventBridge
*/
export class EventBridgeDataSource extends BackedDataSource {
constructor(scope: Construct, id: string, props: EventBridgeDataSourceProps) {
super(scope, id, props, {
type: 'AMAZON_EVENTBRIDGE',
eventBridgeConfig: {
eventBusArn: props.eventBus.eventBusArn,
},
});
props.eventBus.grantPutEventsTo(this);
}
}
/**
* Properties for an AppSync Lambda datasource
*/
export interface LambdaDataSourceProps extends BackedDataSourceProps {
/**
* The Lambda function to call to interact with this data source
*/
readonly lambdaFunction: IFunction;
}
/**
* An AppSync datasource backed by a Lambda function
*/
export class LambdaDataSource extends BackedDataSource {
constructor(scope: Construct, id: string, props: LambdaDataSourceProps) {
super(scope, id, props, {
type: 'AWS_LAMBDA',
lambdaConfig: {
lambdaFunctionArn: props.lambdaFunction.functionArn,
},
});
props.lambdaFunction.grantInvoke(this);
}
}
/**
* Properties for an AppSync RDS datasource Aurora Serverless V1
*/
export interface RdsDataSourceProps extends BackedDataSourceProps {
/**
* The serverless cluster to call to interact with this data source
*/
readonly serverlessCluster: IServerlessCluster;
/**
* The secret containing the credentials for the database
*/
readonly secretStore: ISecret;
/**
* The name of the database to use within the cluster
*
* @default - None
*/
readonly databaseName?: string;
}
/**
* Properties for an AppSync RDS datasource Aurora Serverless V2
*/
export interface RdsDataSourcePropsV2 extends BackedDataSourceProps {
/**
* The serverless cluster to call to interact with this data source
*/
readonly serverlessCluster: IDatabaseCluster;
/**
* The secret containing the credentials for the database
*/
readonly secretStore: ISecret;
/**
* The name of the database to use within the cluster
*
* @default - None
*/
readonly databaseName?: string;
}
/**
* An AppSync datasource backed by RDS
*/
export class RdsDataSource extends BackedDataSource {
constructor(scope: Construct, id: string, props: RdsDataSourceProps)
constructor(scope: Construct, id: string, props: RdsDataSourcePropsV2) {
super(scope, id, props, {
type: 'RELATIONAL_DATABASE',
relationalDatabaseConfig: {
rdsHttpEndpointConfig: {
awsRegion: props.serverlessCluster.env.region,
dbClusterIdentifier: Lazy.string({
produce: () => {
return Stack.of(this).formatArn({
service: 'rds',
resource: `cluster:${props.serverlessCluster.clusterIdentifier}`,
});
},
}),
awsSecretStoreArn: props.secretStore.secretArn,
databaseName: props.databaseName,
},
relationalDatabaseSourceType: 'RDS_HTTP_ENDPOINT',
},
});
const clusterArn = Stack.of(this).formatArn({
service: 'rds',
resource: `cluster:${props.serverlessCluster.clusterIdentifier}`,
});
props.secretStore.grantRead(this);
// Change to grant with RDS grant becomes implemented
props.serverlessCluster.grantDataApiAccess(this);
Grant.addToPrincipal({
grantee: this,
actions: [
'rds-data:DeleteItems',
'rds-data:ExecuteSql',
'rds-data:GetItems',
'rds-data:InsertItems',
'rds-data:UpdateItems',
],
resourceArns: [clusterArn, `${clusterArn}:*`],
scope: this,
});
}
}
/**
* Properties for the Elasticsearch Data Source
*
* @deprecated - use `OpenSearchDataSourceProps` with `OpenSearchDataSource`
*/
export interface ElasticsearchDataSourceProps extends BackedDataSourceProps {
/**
* The elasticsearch domain containing the endpoint for the data source
*/
readonly domain: IElasticsearchDomain;
}
/**
* An Appsync datasource backed by Elasticsearch
*
* @deprecated - use `OpenSearchDataSource`
*/
export class ElasticsearchDataSource extends BackedDataSource {
constructor(scope: Construct, id: string, props: ElasticsearchDataSourceProps) {
super(scope, id, props, {
type: 'AMAZON_ELASTICSEARCH',
elasticsearchConfig: {
awsRegion: props.domain.env.region,
endpoint: `https://${props.domain.domainEndpoint}`,
},
});
props.domain.grantReadWrite(this);
}
}
/**
* Properties for the OpenSearch Data Source
*/
export interface OpenSearchDataSourceProps extends BackedDataSourceProps {
/**
* The OpenSearch domain containing the endpoint for the data source
*/
readonly domain: IOpenSearchDomain;
}
/**
* An Appsync datasource backed by OpenSearch
*/
export class OpenSearchDataSource extends BackedDataSource {
constructor(scope: Construct, id: string, props: OpenSearchDataSourceProps) {
super(scope, id, props, {
type: 'AMAZON_OPENSEARCH_SERVICE',
openSearchServiceConfig: {
awsRegion: props.domain.env.region,
endpoint: `https://${props.domain.domainEndpoint}`,
},
});
props.domain.grantReadWrite(this);
}
}