/
updateStage.js
403 lines (351 loc) · 12.6 KB
/
updateStage.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
'use strict';
const _ = require('lodash');
const BbPromise = require('bluebird');
const ServerlessError = require('../../../../../../../../classes/Error').ServerlessError;
const defaultApiGatewayLogFormat = [
'requestId: $context.requestId',
'ip: $context.identity.sourceIp',
'caller: $context.identity.caller',
'user: $context.identity.user',
'requestTime: $context.requestTime',
'httpMethod: $context.httpMethod',
'resourcePath: $context.resourcePath',
'status: $context.status',
'protocol: $context.protocol',
'responseLength: $context.responseLength',
].join(', ');
const defaultApiGatewayLogLevel = 'INFO';
// NOTE --> Keep this file in sync with ../stage.js
// NOTE: This code was written since there are problem setting up dedicated CloudFormation
// Stage resource (see https://github.com/serverless/serverless/pull/5692#issuecomment-467849311 for more information).
module.exports = {
defaultApiGatewayLogLevel,
updateStage() {
return BbPromise.try(() => {
const provider = this.state.service.provider;
this.hasTracingConfigured = provider.tracing && provider.tracing.apiGateway != null;
this.hasMetricsConfigured = provider.apiGateway && provider.apiGateway.metrics != null;
this.hasLogsConfigured = provider.logs && provider.logs.restApi != null;
this.hasTagsConfigured = provider.tags != null || provider.stackTags != null;
if (
!this.hasTracingConfigured &&
!this.hasLogsConfigured &&
!this.hasTagsConfigured &&
!this.hasMetricsConfigured
) {
return null;
}
// this array is used to gather all the patch operations we need to
// perform on the stage
this.apiGatewayStagePatchOperations = [];
this.apiGatewayTagResourceParams = [];
this.apiGatewayUntagResourceParams = [];
this.apiGatewayStageState = {};
this.apiGatewayDeploymentId = null;
this.apiGatewayRestApiId = null;
return resolveAccountInfo
.call(this)
.then(resolveRestApiId.bind(this))
.then(() => {
if (this.apiGatewayRestApiId) return resolveDeploymentId.call(this);
return null;
})
.then(() => {
// Do not update APIGW-wide settings, in case external APIGW is referenced
if (this.isExternalRestApi) return null;
if (!this.apiGatewayDeploymentId) {
// Could not resolve REST API id automatically
if (!this.serverless.utils.isEventUsed(this.state.service.functions, 'http')) {
return null;
}
if (!this.hasTracingConfigured && !this.hasLogsConfigured) {
// Do crash if there are no API Gateway customizations to apply
return null;
}
const errorMessage = [
'Rest API id could not be resolved.\n',
'This might be caused by a custom API Gateway configuration.\n\n',
'In given setup stage specific options such as ',
'`tracing`, `logs` and `tags` are not supported.\n\n',
'Please update your configuration (or open up an issue if you feel ',
"that there's a way to support your setup).",
].join('');
throw new ServerlessError(errorMessage);
}
return resolveStage
.call(this)
.then(ensureStage.bind(this))
.then(handleTracing.bind(this))
.then(handleMetrics.bind(this))
.then(handleLogs.bind(this))
.then(handleTags.bind(this))
.then(applyUpdates.bind(this))
.then(addTags.bind(this))
.then(removeTags.bind(this))
.then(removeAccessLoggingLogGroup.bind(this));
});
});
},
};
function resolveAccountInfo() {
// eslint-disable-next-line no-return-assign
return this.provider.getAccountInfo().then(account => {
this.accountId = account.accountId;
this.partition = account.partition;
});
}
function resolveApiGatewayResource(resources) {
const apiGatewayResources = _.pickBy(
resources,
resource => resource.Type === 'AWS::ApiGateway::RestApi'
);
const apiGatewayResourcesIds = Object.keys(apiGatewayResources);
if (apiGatewayResourcesIds.length !== 1) return null;
const apiGatewayResourceId = apiGatewayResourcesIds[0];
if (
!Object.keys(resources).some(key => {
const resource = resources[key];
if (resource.Type !== 'AWS::ApiGateway::Deployment') return false;
if (!resource.Properties || !resource.Properties.RestApiId) return false;
return resource.Properties.RestApiId.Ref === apiGatewayResourceId;
})
) {
return null;
}
return apiGatewayResources[apiGatewayResourceId];
}
function resolveRestApiId() {
return new BbPromise(resolve => {
const provider = this.state.service.provider;
const externalRestApiId = provider.apiGateway && provider.apiGateway.restApiId;
if (externalRestApiId) {
this.isExternalRestApi = true;
resolve(null);
return;
}
const apiGatewayResource = resolveApiGatewayResource(
this.serverless.service.provider.compiledCloudFormationTemplate.Resources
);
if (
!apiGatewayResource &&
// If there are 'http' events, assume that there is API Gateway configured
// it's just probably hidden in nested stack (some rely on plugins that split stacks)
!this.serverless.utils.isEventUsed(this.state.service.functions, 'http')
) {
resolve(null);
return;
}
const apiName = apiGatewayResource
? apiGatewayResource.Properties.Name
: provider.apiName || `${this.options.stage}-${this.state.service.service}`;
const resolveFromAws = position =>
this.provider.request('APIGateway', 'getRestApis', { position, limit: 500 }).then(result => {
const restApi = result.items.find(api => api.name === apiName);
if (restApi) return restApi.id;
if (result.position) return resolveFromAws(result.position);
return null;
});
resolve(resolveFromAws());
}).then(restApiId => {
this.apiGatewayRestApiId = restApiId;
});
}
function resolveStage() {
const restApiId = this.apiGatewayRestApiId;
return this.provider
.request('APIGateway', 'getStage', {
restApiId,
stageName: this.options.stage,
})
.then(res => {
this.apiGatewayStageState = res;
})
.catch(() => {
// fail silently...
});
}
function resolveDeploymentId() {
if (!Object.keys(this.apiGatewayStageState).length) {
const restApiId = this.apiGatewayRestApiId;
return this.provider
.request('APIGateway', 'getDeployments', {
restApiId,
limit: 500,
})
.then(res => {
if (res.items.length) {
// there will ever only be 1 deployment associated
const deployment = res.items.shift();
return deployment.id;
}
return null;
})
.then(deploymentId => {
this.apiGatewayDeploymentId = deploymentId;
});
}
return BbPromise.resolve();
}
function ensureStage() {
if (!Object.keys(this.apiGatewayStageState).length) {
const restApiId = this.apiGatewayRestApiId;
const deploymentId = this.apiGatewayDeploymentId;
return this.provider.request('APIGateway', 'createStage', {
deploymentId,
restApiId,
stageName: this.options.stage,
});
}
return BbPromise.resolve();
}
function handleTracing() {
if (!this.hasTracingConfigured) return;
const tracingEnabled = this.state.service.provider.tracing.apiGateway;
let operation = { op: 'replace', path: '/tracingEnabled', value: 'false' };
if (tracingEnabled) {
operation = { op: 'replace', path: '/tracingEnabled', value: 'true' };
}
this.apiGatewayStagePatchOperations.push(operation);
}
function handleMetrics() {
if (!this.hasMetricsConfigured) return;
const metricsEnabled = this.state.service.provider.apiGateway.metrics;
const operation = {
op: 'replace',
path: '/*/*/metrics/enabled',
value: metricsEnabled ? 'true' : 'false',
};
this.apiGatewayStagePatchOperations.push(operation);
}
function handleLogs() {
if (!this.hasLogsConfigured) return;
const logs = this.state.service.provider.logs.restApi;
const ops = this.apiGatewayStagePatchOperations;
let operations = [
{ op: 'replace', path: '/*/*/logging/dataTrace', value: 'false' },
{ op: 'replace', path: '/*/*/logging/loglevel', value: 'OFF' },
];
if (logs) {
const service = this.state.service.service;
const stage = this.options.stage;
const region = this.options.region;
const partition = this.partition;
const logGroupName = `/aws/api-gateway/${service}-${stage}`;
operations = [];
let logFormat = defaultApiGatewayLogFormat;
if (logs.format) {
logFormat = logs.format;
}
const executionLogging = logs.executionLogging == null ? true : logs.executionLogging;
let level = defaultApiGatewayLogLevel;
if (!executionLogging) {
level = 'OFF';
} else if (logs.level) {
level = logs.level;
}
const accessLogging = logs.accessLogging == null ? true : logs.accessLogging;
if (accessLogging) {
const resourceArn = `arn:${partition}:logs:${region}:${this.accountId}:log-group:${logGroupName}`;
const destinationArn = {
op: 'replace',
path: '/accessLogSettings/destinationArn',
value: resourceArn,
};
const format = {
op: 'replace',
path: '/accessLogSettings/format',
value: logFormat,
};
operations.push(destinationArn, format);
} else {
// this is required to remove any existing log setting
operations.push({
op: 'remove',
path: '/accessLogSettings',
});
}
const fullExecutionData = logs.fullExecutionData == null ? true : logs.fullExecutionData;
operations.push({
op: 'replace',
path: '/*/*/logging/dataTrace',
value: String(Boolean(fullExecutionData)),
});
operations.push({ op: 'replace', path: '/*/*/logging/loglevel', value: level });
}
ops.push.apply(ops, operations); // eslint-disable-line prefer-spread
}
function handleTags() {
if (!this.hasTagsConfigured) return;
const provider = this.state.service.provider;
const tagsMerged = _.mapValues(Object.assign({}, provider.stackTags, provider.tags), v =>
String(v)
);
const currentTags = this.apiGatewayStageState.tags || {};
const tagKeysToBeRemoved = Object.keys(currentTags).filter(
currentKey => typeof tagsMerged[currentKey] !== 'string'
);
const restApiId = this.apiGatewayRestApiId;
const stageName = this.options.stage;
const region = this.options.region;
const partition = this.partition;
const resourceArn = `arn:${partition}:apigateway:${region}::/restapis/${restApiId}/stages/${stageName}`;
if (tagKeysToBeRemoved.length > 0) {
this.apiGatewayUntagResourceParams.push({
resourceArn,
tagKeys: tagKeysToBeRemoved,
});
}
if (!_.isEqual(currentTags, tagsMerged) && Object.keys(tagsMerged).length > 0) {
this.apiGatewayTagResourceParams.push({
resourceArn,
tags: tagsMerged,
});
}
}
function addTags() {
const requests = this.apiGatewayTagResourceParams.map(tagResourceParam =>
this.provider.request('APIGateway', 'tagResource', tagResourceParam)
);
return BbPromise.all(requests);
}
function removeTags() {
const requests = this.apiGatewayUntagResourceParams.map(untagResourceParam =>
this.provider.request('APIGateway', 'untagResource', untagResourceParam)
);
return BbPromise.all(requests);
}
function applyUpdates() {
const restApiId = this.apiGatewayRestApiId;
const patchOperations = this.apiGatewayStagePatchOperations;
if (patchOperations.length) {
return this.provider.request('APIGateway', 'updateStage', {
restApiId,
stageName: this.options.stage,
patchOperations,
});
}
return BbPromise.resolve();
}
function removeAccessLoggingLogGroup() {
const service = this.state.service.service;
const provider = this.state.service.provider;
const stage = this.options.stage;
const logGroupName = `/aws/api-gateway/${service}-${stage}`;
let accessLogging = provider.logs && provider.logs.restApi;
if (accessLogging) {
accessLogging = accessLogging.accessLogging == null ? true : accessLogging.accessLogging;
}
// if there are no logs setup (or the user has disabled them) we need to
// ensure that the log group is removed. Otherwise we'll run into duplicate
// log group name issues when logs are enabled again
if (!accessLogging) {
return this.provider
.request('CloudWatchLogs', 'deleteLogGroup', {
logGroupName,
})
.catch(() => {
// fail silently...
});
}
return BbPromise.resolve();
}