forked from sid88in/serverless-appsync-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
612 lines (554 loc) · 20.6 KB
/
index.js
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
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
const fs = require('fs');
const path = require('path');
const {
validateSchema, printError, parse, buildASTSchema,
} = require('graphql');
const runPlayground = require('./graphql-playground');
const getConfig = require('./get-config');
const MIGRATION_DOCS = 'https://github.com/sid88in/serverless-appsync-plugin/blob/master/README.md#cfn-migration';
const RESOURCE_API = "GraphQlApi";
const RESOURCE_API_CLOUDWATCH_LOGS_ROLE = "GraphQlApiCloudWatchLogsRole";
const RESOURCE_API_KEY = "GraphQlApiKeyDefault";
const RESOURCE_SCHEMA = "GraphQlSchema";
const RESOURCE_URL = "GraphQlApiUrl";
class ServerlessAppsyncPlugin {
constructor(serverless, options) {
this.serverless = serverless;
this.options = options;
this.provider = this.serverless.getProvider('aws');
this.commands = {
'delete-appsync': {
usage: 'Helps you delete AppSync API',
lifecycleEvents: ['delete'],
},
'graphql-playground': {
usage: 'Runs a local graphql playground instance using your appsync config',
options: {
clientId: {
usage: 'Specify your cognito client id (for AMAZON_COGNITO_USER_POOLS authType)',
required: false,
},
username: {
usage: 'Specify your username (for AMAZON_COGNITO_USER_POOLS authType)',
shortcut: 'u',
required: false,
},
password: {
usage: 'Specify your password (for AMAZON_COGNITO_USER_POOLS authType)',
shortcut: 'p',
required: false,
},
jwtToken: {
usage: 'Specify your jwtToken (for OPENID_CONNECT authType)',
required: false,
},
apiKey: {
usage: 'Specify your appsync api key (for API_KEY authType)',
required: false,
},
port: {
usage: 'Specify the local port graphql playground should run from',
required: false,
},
},
lifecycleEvents: ['run'],
},
'deploy-appsync': {
usage: 'DEPRECATED: Helps you deploy AppSync API',
lifecycleEvents: ['deploy'],
},
'update-appsync': {
usage: 'DEPRECATED: Helps you update AppSync API',
lifecycleEvents: ['update'],
},
};
const generateMigrationErrorMessage = command => () => {
throw new this.serverless.classes.Error(`serverless-appsync: ${command} `
+ `is no longer supported. See ${MIGRATION_DOCS} for more information`);
};
// Issue 159 - as of Serverless 1.12.0, before:deploy:initialize is replaced
// by package:initialize.
this.hooks = {
'package:initialize': () => this.validateSchemas(),
'delete-appsync:delete': () => this.deleteGraphQLEndpoint(),
'graphql-playground:run': () => this.runGraphqlPlayground(),
'deploy-appsync:deploy': generateMigrationErrorMessage('deploy-appsync'),
'update-appsync:update': generateMigrationErrorMessage('update-appsync'),
'after:aws:package:finalize:mergeCustomProviderResources': () => this.addResources(),
};
}
loadConfig() {
return getConfig(
this.serverless.service.custom.appSync,
this.serverless.service.provider,
this.serverless.config.servicePath,
);
}
getSchemas() {
const config = this.loadConfig();
const awsTypes = `
scalar AWSDate
scalar AWSTime
scalar AWSDateTime
scalar AWSTimestamp
scalar AWSEmail
scalar AWSJSON
scalar AWSURL
scalar AWSPhone
scalar AWSIPAddress
`;
return config.map(apiConfig => `${apiConfig.schema} ${awsTypes}`);
}
validateSchemas() {
const schemas = this.getSchemas();
const asts = schemas.map(schema => buildASTSchema(parse(schema)));
const errors = asts.reduce((accumulatedErrors, currentAst) => {
const currentErrors = validateSchema(currentAst);
if (!currentErrors.length) {
return accumulatedErrors;
} else {
return accumulatedErrors.concat(currentErrors || []);
}
}, []);
if (!errors.length) {
return;
}
errors.forEach((error) => {
this.serverless.cli.log(printError(error));
});
throw new this.serverless.classes.Error('Cannot proceed invalid graphql SDL in one or more schemas.');
}
deleteGraphQLEndpoint() {
const config = this.loadConfig();
return Promise.all(config.map(apiConfig => {
const { apiId } = apiConfig;
if (!apiId) {
throw new this.serverless.classes.Error('serverless-appsync: no apiId is defined. If you are not '
+ `migrating from a previous version of the plugin this is expected. See ${MIGRATION_DOCS} '
+ 'for more information`);
}
this.serverless.cli.log(`Deleting GraphQL Endpoint (${apiId})...`);
return this.provider
.request('AppSync', 'deleteGraphqlApi', {
apiId,
})
.then((data) => {
if (data) {
this.serverless.cli.log(`Successfully deleted GraphQL Endpoint: ${apiId}`);
}
});
}));
}
runGraphqlPlayground() {
// Use the first config or config map
const firstConfig = this.loadConfig()[0];
runPlayground(this.serverless.service, this.provider, firstConfig, this.options).then((url) => {
this.serverless.cli.log(`Graphql Playground Server Running at: ${url}`);
});
}
addResources() {
const config = this.loadConfig();
const resources = this.serverless.service.provider.compiledCloudFormationTemplate.Resources;
const outputs = this.serverless.service.provider.compiledCloudFormationTemplate.Outputs;
config.forEach(apiConfig => {
if (apiConfig.apiId) {
this.serverless.cli.log('WARNING: serverless-appsync has been updated in a breaking way and your '
+ 'service is configured using a reference to an existing apiKey in '
+ '`custom.appSync` which is used in the legacy deploy scripts. This deploy will create '
+ `new graphql resources and WILL NOT update your existing api. See ${MIGRATION_DOCS} for `
+ 'more information');
}
Object.assign(resources, this.getGraphQlApiEndpointResource(apiConfig));
Object.assign(resources, this.getApiKeyResources(apiConfig));
Object.assign(resources, this.getGraphQLSchemaResource(apiConfig));
Object.assign(resources, this.getCloudWatchLogsRole(apiConfig));
Object.assign(resources, this.getDataSourceIamRolesResouces(apiConfig));
Object.assign(resources, this.getDataSourceResources(apiConfig));
Object.assign(resources, this.getResolverResources(apiConfig));
Object.assign(outputs, this.getGraphQlApiOutputs(apiConfig));
Object.assign(outputs, this.getApiKeyOutputs(apiConfig));
});
}
getGraphQlApiEndpointResource(config) {
const logicalIdGraphQLApi = this.getLogicalId(config, RESOURCE_API);
const logicalIdCloudWatchLogsRole = this.getLogicalId(config, RESOURCE_API_CLOUDWATCH_LOGS_ROLE);
return {
[logicalIdGraphQLApi]: {
Type: 'AWS::AppSync::GraphQLApi',
Properties: {
Name: config.name,
AuthenticationType: config.authenticationType,
UserPoolConfig: config.authenticationType !== 'AMAZON_COGNITO_USER_POOLS' ? undefined : {
AwsRegion: config.userPoolConfig.awsRegion || config.region,
DefaultAction: config.userPoolConfig.defaultAction,
UserPoolId: config.userPoolConfig.userPoolId,
AppIdClientRegex: config.userPoolConfig.appIdClientRegex,
},
OpenIDConnectConfig: config.authenticationType !== 'OPENID_CONNECT' ? undefined : {
Issuer: config.openIdConnectConfig.issuer,
ClientId: config.openIdConnectConfig.clientId,
IatTTL: config.openIdConnectConfig.iatTTL,
AuthTTL: config.openIdConnectConfig.authTTL,
},
LogConfig: !config.logConfig ? undefined : {
CloudWatchLogsRoleArn:
config.logConfig.loggingRoleArn ||
{ "Fn::GetAtt": [logicalIdCloudWatchLogsRole, "Arn"] },
FieldLogLevel: config.logConfig.level,
},
},
},
};
}
getApiKeyResources(config) {
if (config.authenticationType !== 'API_KEY') {
return {};
}
const logicalIdGraphQLApi = this.getLogicalId(config, RESOURCE_API);
const logicalIdApiKey = this.getLogicalId(config, RESOURCE_API_KEY);
return {
[logicalIdApiKey]: {
Type: 'AWS::AppSync::ApiKey',
Properties: {
ApiId: { 'Fn::GetAtt': [logicalIdGraphQLApi, 'ApiId'] },
Description: `serverless-appsync-plugin: AppSync API Key for ${logicalIdApiKey}`,
Expires: Math.floor(Date.now() / 1000) + (365 * 24 * 60 * 60),
},
},
};
}
getCloudWatchLogsRole(config) {
if (!config.logConfig || config.logConfig.loggingRoleArn) {
return {};
}
const logicalIdCloudWatchLogsRole = this.getLogicalId(config, RESOURCE_API_CLOUDWATCH_LOGS_ROLE);
return {
[logicalIdCloudWatchLogsRole]: {
Type: 'AWS::IAM::Role',
Properties: {
"AssumeRolePolicyDocument": {
"Version" : "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": [ "appsync.amazonaws.com" ]
},
"Action": [ "sts:AssumeRole" ]
}
]
},
Policies: [
{
PolicyName: "GraphQlApiCloudWatchLogsPolicy",
PolicyDocument: {
Version: "2012-10-17",
Statement: [
{
"Effect": "Allow",
"Action": [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Resource": [
{
"Fn::Sub" : [
"arn:aws:logs:${region}:${AWS::AccountId}:*",
{
region: config.region
}
]
}
]
}
],
}
},
]
}
}
};
}
getDataSourceIamRolesResouces(config) {
return config.dataSources.reduce((acc, ds) => {
// Only generate DataSource Roles for compatible types
// and if `serviceRoleArn` not provided
if (
['AWS_LAMBDA', 'AMAZON_DYNAMODB', 'AMAZON_ELASTICSEARCH'].indexOf(ds.type) === -1
|| (ds.config && ds.config.serviceRoleArn)
) {
return acc;
}
let statements;
if (ds.config && ds.config.iamRoleStatements) {
statements = ds.config.iamRoleStatements;
} else {
// Try to generate default statements for the given DataSource.
statements = this.getDefaultDataSourcePolicyStatements(ds, config);
// If we could not generate it, skip this step.
if (false === statements) {
return acc;
}
}
const resource = {
Type: 'AWS::IAM::Role',
Properties: {
"AssumeRolePolicyDocument": {
"Version" : "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": [ "appsync.amazonaws.com" ]
},
"Action": [ "sts:AssumeRole" ]
}
]
},
Policies: [
{
PolicyName: this.getDataSourceCfnName(ds.name) + "Policy",
PolicyDocument: {
Version: "2012-10-17",
Statement: statements,
}
},
]
}
};
const logicalIdDataSource = this.getLogicalId(config, this.getDataSourceCfnName(ds.name) + "Role");
return Object.assign({}, acc, { [logicalIdDataSource]: resource });
}, {});
}
getDefaultDataSourcePolicyStatements(ds, config) {
const defaultStatement = {
Effect: "Allow",
Action: [],
Resource: []
};
switch (ds.type) {
case 'AWS_LAMBDA':
// Allow "invoke" for the Datasource's function and its aliases/versions
defaultStatement.Action = ["lambda:invokeFunction"];
defaultStatement.Resource = [
ds.config.lambdaFunctionArn,
{ "Fn::Join" : [ ":", [ ds.config.lambdaFunctionArn, '*' ] ] }
];
break;
case 'AMAZON_DYNAMODB':
// Allow any action on the Datasource's table
defaultStatement.Action = [
"dynamodb:DeleteItem",
"dynamodb:GetItem",
"dynamodb:PutItem",
"dynamodb:Query",
"dynamodb:Scan",
"dynamodb:UpdateItem",
];
const resourceArn = {
"Fn::Join" : [
":",
[
'arn',
'aws',
'dynamodb',
ds.config.region || config.region,
{ "Ref" : "AWS::AccountId" },
{ "Fn::Join" : [ "/", ['table', ds.config.tableName] ] },
]
]
};
defaultStatement.Resource = [
resourceArn,
{ "Fn::Join" : [ "/", [resourceArn, '*'] ] },
];
break;
case 'AMAZON_ELASTICSEARCH':
// Allow any action on the Datasource's ES endpoint
defaultStatement.Action = [
"es:ESHttpDelete",
"es:ESHttpGet",
"es:ESHttpHead",
"es:ESHttpPost",
"es:ESHttpPut",
];
const rx = /^https:\/\/([a-z0-9\-]+\.\w{2}\-[a-z]+\-\d\.es\.amazonaws\.com)$/;
const result = rx.exec(ds.config.endpoint);
if (!result) {
throw new this.serverless.classes.Error(`Invalid AWS ElasticSearch endpoint: '${ds.config.endpoint}`);
}
defaultStatement.Resource = [
{
"Fn::Join" : [ ":", [
'arn',
'aws',
'es',
ds.config.region || config.region,
{ "Ref" : "AWS::AccountId" },
`domain/${result[1]}`
]]
},
];
break;
default:
// unknown or non compatible type
return false;
}
return [defaultStatement];
}
getDataSourceResources(config) {
const logicalIdGraphQLApi = this.getLogicalId(config, RESOURCE_API);
return config.dataSources.reduce((acc, ds) => {
const resource = {
Type: 'AWS::AppSync::DataSource',
Properties: {
ApiId: { 'Fn::GetAtt': [logicalIdGraphQLApi, 'ApiId'] },
Name: ds.name,
Description: ds.description,
Type: ds.type,
},
};
// If a serviceRoleArn was given for this DataSource, use it
if (ds.config && ds.config.serviceRoleArn) {
resource.Properties.ServiceRoleArn = ds.config.serviceRoleArn;
} else {
const logicalIdDataSourceRole = this.getLogicalId(config, this.getDataSourceCfnName(ds.name) + "Role");
// If a Role Resource was generated for this DataSource, use it
const role = this.serverless.service.provider.compiledCloudFormationTemplate.Resources[logicalIdDataSourceRole];
if (role) {
resource.Properties.ServiceRoleArn = { 'Fn::GetAtt': [logicalIdDataSourceRole, 'Arn'] }
}
}
if (ds.type === 'AWS_LAMBDA') {
resource.Properties.LambdaConfig = {
LambdaFunctionArn: ds.config.lambdaFunctionArn,
};
} else if (ds.type === 'AMAZON_DYNAMODB') {
resource.Properties.DynamoDBConfig = {
AwsRegion: ds.config.region || config.region,
TableName: ds.config.tableName,
UseCallerCredentials: !!ds.config.useCallerCredentials,
};
} else if (ds.type === 'AMAZON_ELASTICSEARCH') {
resource.Properties.ElasticsearchConfig = {
AwsRegion: ds.config.region || config.region,
Endpoint: ds.config.endpoint,
};
} else if (ds.type === 'HTTP') {
resource.Properties.HttpConfig = {
Endpoint: ds.config.endpoint,
};
} else if (ds.type !== 'NONE') {
throw new this.serverless.classes.Error(`Data Source Type not supported: '${ds.type}`);
}
const logicalIdDataSource = this.getLogicalId(config, this.getDataSourceCfnName(ds.name));
return Object.assign({}, acc, { [logicalIdDataSource]: resource });
}, {});
}
getGraphQLSchemaResource(config) {
const logicalIdGraphQLApi = this.getLogicalId(config, RESOURCE_API);
const logicalIdGraphQLSchema = this.getLogicalId(config, RESOURCE_SCHEMA);
return {
[logicalIdGraphQLSchema]: {
Type: 'AWS::AppSync::GraphQLSchema',
Properties: {
Definition: config.schema,
ApiId: { 'Fn::GetAtt': [logicalIdGraphQLApi, 'ApiId'] },
},
},
};
}
getResolverResources(config) {
return config.mappingTemplates.reduce((acc, tpl) => {
const reqTemplPath = path.join(config.mappingTemplatesLocation, tpl.request);
const respTemplPath = path.join(config.mappingTemplatesLocation, tpl.response);
const requestTemplate = fs.readFileSync(reqTemplPath, 'utf8');
const responseTemplate = fs.readFileSync(respTemplPath, 'utf8');
const logicalIdGraphQLApi = this.getLogicalId(config, RESOURCE_API);
const logicalIdGraphQLSchema = this.getLogicalId(config, RESOURCE_SCHEMA);
const logicalIdResolver = this.getLogicalId(
config,
`GraphQlResolver${this.getCfnName(tpl.type)}${this.getCfnName(tpl.field)}`
);
const logicalIdDataSource = this.getLogicalId(config, this.getDataSourceCfnName(tpl.dataSource));
return Object.assign({}, acc, {
[logicalIdResolver]: {
Type: 'AWS::AppSync::Resolver',
DependsOn: logicalIdGraphQLSchema,
Properties: {
ApiId: { 'Fn::GetAtt': [logicalIdGraphQLApi, 'ApiId'] },
TypeName: tpl.type,
FieldName: tpl.field,
DataSourceName: { 'Fn::GetAtt': [logicalIdDataSource, 'Name'] },
RequestMappingTemplate: this.processTemplate(requestTemplate, config),
ResponseMappingTemplate: this.processTemplate(responseTemplate, config),
},
},
});
}, {});
}
getLogicalId(config, resourceType) {
// Similar to serverless' implementation of functions
// (e.g. getUser becomes GetUserLambdaFunction for CloudFormation logical ID,
// myService becomes MyServiceGraphQLApi or `MyService${resourceType}`)
if (config.isSingleConfig) {
// This will ensure people with CloudFormation stack dependencies on the previous
// version of the plugin doesn't break their {@code deleteGraphQLEndpoint} functionality
return this.getCfnName(resourceType);
} else {
return this.getCfnName(config.name[0].toUpperCase() + config.name.slice(1) + resourceType);
}
}
getGraphQlApiOutputs(config) {
const logicalIdGraphQLApi = this.getLogicalId(config, RESOURCE_API);
const logicalIdGraphQLApiUrlOutput = this.getLogicalId(config, RESOURCE_URL);
return {
[logicalIdGraphQLApiUrlOutput]: {
Value: { 'Fn::GetAtt': [logicalIdGraphQLApi, 'GraphQLUrl'] },
},
};
}
getApiKeyOutputs(config) {
if (config.authenticationType !== 'API_KEY') {
return {};
}
const logicalIdApiKey = this.getLogicalId(config, RESOURCE_API_KEY);
const logicalIdApiKeyOutput = logicalIdApiKey;
return {
[logicalIdApiKeyOutput]: {
Value: { 'Fn::GetAtt': [logicalIdApiKey, 'ApiKey'] },
},
};
}
getCfnName(name) {
return name.replace(/[^a-zA-Z0-9]/g, '');
}
getDataSourceCfnName(name) {
return `GraphQlDs${this.getCfnName(name)}`;
}
processTemplate(template, config) {
// TODO use serverless variable parser and serverless variable syntax config
const variableSyntax = RegExp(/\${([\w\d-_]+)}/g);
const configVariables = Object.keys(config.substitutions);
const templateVariables = [];
let searchResult;
// eslint-disable-next-line no-cond-assign
while ((searchResult = variableSyntax.exec(template)) !== null) {
templateVariables.push(searchResult[1]);
}
const substitutions = configVariables
.filter(value => templateVariables.indexOf(value) > -1)
.filter((value, index, array) => array.indexOf(value) === index)
.reduce(
(accum, value) => Object.assign(accum, { [value]: config.substitutions[value] }),
{},
);
// if there are substitutions for this template then add fn:sub
if (Object.keys(substitutions).length > 0) {
return { 'Fn::Sub': [template, substitutions] };
}
return template;
}
}
module.exports = ServerlessAppsyncPlugin;