/
index.js
139 lines (122 loc) · 4.75 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
'use strict';
const BbPromise = require('bluebird');
const _ = require('lodash');
module.exports = {
compileMethods() {
this.permissionMapping = [];
this.validated.events.forEach(event => {
const resourceId = this.getResourceId(event.http.path);
const resourceName = this.getResourceName(event.http.path);
const requestParameters = {};
if (event.http.request && event.http.request.parameters) {
_.forEach(event.http.request.parameters, (value, key) => {
requestParameters[key] = value.required === undefined ? value : value.required;
});
}
const template = {
Type: 'AWS::ApiGateway::Method',
Properties: {
HttpMethod: event.http.method.toUpperCase(),
RequestParameters: requestParameters,
ResourceId: resourceId,
RestApiId: this.provider.getApiGatewayRestApiId(),
OperationName: event.http.operationId,
},
};
if (event.http.private) {
template.Properties.ApiKeyRequired = true;
} else {
template.Properties.ApiKeyRequired = false;
}
const methodLogicalId = this.provider.naming.getMethodLogicalId(
resourceName,
event.http.method
);
const validatorLogicalId = this.provider.naming.getValidatorLogicalId(
resourceName,
event.http.method
);
const lambdaLogicalId = this.provider.naming.getLambdaLogicalId(event.functionName);
const functionObject = this.serverless.service.functions[event.functionName];
const lambdaAliasName = functionObject.targetAlias && functionObject.targetAlias.name;
const lambdaAliasLogicalId =
functionObject.targetAlias && functionObject.targetAlias.logicalId;
const singlePermissionMapping = {
resourceName,
lambdaLogicalId,
lambdaAliasName,
lambdaAliasLogicalId,
event,
};
this.permissionMapping.push(singlePermissionMapping);
_.merge(
template,
this.getMethodAuthorization(event.http),
this.getMethodIntegration(event.http, {
lambdaLogicalId,
lambdaAliasName,
}),
this.getMethodResponses(event.http)
);
let extraCognitoPoolClaims;
if (event.http.authorizer) {
const claims = event.http.authorizer.claims || [];
extraCognitoPoolClaims = claims.map(claim => {
if (typeof claim === 'string') {
const colonIndex = claim.indexOf(':');
if (colonIndex !== -1) {
const subClaim = claim.substring(colonIndex + 1);
return `"${subClaim}": "$context.authorizer.claims['${claim}']"`;
}
}
return `"${claim}": "$context.authorizer.claims.${claim}"`;
});
}
const requestTemplates = template.Properties.Integration.RequestTemplates;
_.forEach(requestTemplates, (value, key) => {
let claimsString = '';
if (extraCognitoPoolClaims && extraCognitoPoolClaims.length > 0) {
claimsString = extraCognitoPoolClaims.join(',').concat(',');
}
requestTemplates[key] = value.replace('extraCognitoPoolClaims', claimsString);
});
this.apiGatewayMethodLogicalIds.push(methodLogicalId);
if (event.http.request && event.http.request.schema) {
for (const requestSchema of Object.entries(event.http.request.schema)) {
const contentType = requestSchema[0];
const schema = requestSchema[1];
const modelLogicalId = this.provider.naming.getModelLogicalId(
resourceName,
event.http.method,
contentType
);
template.Properties.RequestValidatorId = { Ref: validatorLogicalId };
template.Properties.RequestModels = template.Properties.RequestModels || {};
template.Properties.RequestModels[contentType] = { Ref: modelLogicalId };
_.merge(this.serverless.service.provider.compiledCloudFormationTemplate.Resources, {
[modelLogicalId]: {
Type: 'AWS::ApiGateway::Model',
Properties: {
RestApiId: this.provider.getApiGatewayRestApiId(),
ContentType: contentType,
Schema: schema,
},
},
[validatorLogicalId]: {
Type: 'AWS::ApiGateway::RequestValidator',
Properties: {
RestApiId: this.provider.getApiGatewayRestApiId(),
ValidateRequestBody: true,
ValidateRequestParameters: true,
},
},
});
}
}
_.merge(this.serverless.service.provider.compiledCloudFormationTemplate.Resources, {
[methodLogicalId]: template,
});
});
return BbPromise.resolve();
},
};