-
Notifications
You must be signed in to change notification settings - Fork 99
/
enable_request_response_logging_for_apis.js
105 lines (92 loc) · 2.82 KB
/
enable_request_response_logging_for_apis.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
/**
* Enable all ApiGateway Apis logging to CloudWatch
* After enabling logging, log level will be INFO
*/
const AWS = require('aws-sdk');
const wait = require('./util/wait');
const cli = require('cli');
const awsConfigHelper = require('./util/awsConfigHelper');
const cliArgs = cli.parse({
region: ['r', 'AWS region', 'string']
});
if (!cliArgs.region) {
cli.getUsage();
}
let ApiGateway;
function isEmpty(obj) {
for (let prop in obj) {
if (obj.hasOwnProperty(prop))
return false;
}
return JSON.stringify(obj) === JSON.stringify({});
}
// Deploy api to stage
function createDeployment(restApiId, stageName) {
var params = {
restApiId,
/* required */
stageName
};
return ApiGateway.createDeployment(params).promise();
}
function updateStage(restApiId, stageName) {
const params = {
restApiId,
/* required */
stageName,
/* required */
patchOperations: [{
op: 'replace',
path: '/*/*/logging/dataTrace',
value: "true"
}]
};
return ApiGateway.updateStage(params).promise();
}
function hasRequestLoggingEnabled(stage) {
if (isEmpty(stage.methodSettings) || !stage.methodSettings['*/*'].dataTraceEnabled) {
return false;
}
return true;
}
function getRestApis() {
const params = {
// limit: 0,
// position: 'STRING_VALUE'
};
return ApiGateway.getRestApis(params).promise();
}
// Get info anout stage resource of api
function getStages(restApiId) {
const params = {
restApiId,
/* required */
};
return ApiGateway.getStages(params).promise();
}
async function enableRequestLogging() {
await awsConfigHelper.updateConfig(cliArgs.region);
ApiGateway = new AWS.APIGateway();
try {
const restApis = await getRestApis();
for (let i = 0; i < restApis.items.length; i++) {
const stages = await getStages(restApis.items[i].id);
console.log(`Rest API id is ${restApis.items[i].id} | ${restApis.items[i].name}`);
for (let j = 0; j < stages.item.length; j++) {
const hasLoggingEnabled = hasRequestLoggingEnabled(stages.item[j]);
console.log('Request and Response logging enable status: ', hasLoggingEnabled);
if (hasLoggingEnabled) {
continue;
}
const restApiId = restApis.items[i].id;
const stageName = stages.item[j].stageName;
console.log(`Enabling request logging for restapi id: ${restApiId} | ${restApis.items[i].name} and stage: ${stageName}`);
await updateStage(restApiId, stageName);
await wait(1000);
}
}
} catch (err) {
console.log(err);
}
}
enableRequestLogging();