-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathserverlessApigatewayLogRetentionPlugin.js
132 lines (116 loc) · 5.68 KB
/
serverlessApigatewayLogRetentionPlugin.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
const { CloudWatchLogsClient, PutRetentionPolicyCommand, DeleteRetentionPolicyCommand } = require("@aws-sdk/client-cloudwatch-logs");
const { APIGatewayClient, GetStageCommand, GetRestApisCommand } = require("@aws-sdk/client-api-gateway");
const { fromIni } = require('@aws-sdk/credential-provider-ini');
const { NodeHttpHandler } = require('@smithy/node-http-handler');
const { ProxyAgent } = require('proxy-agent');
class ApigatewayLogRetentionPlugin {
constructor(serverless, options) {
this.serverless = serverless;
this.options = options;
this.hooks = {
'after:deploy:deploy': this.setApigatewayLogRetention.bind(this, serverless),
};
}
getRequestHandlerWithProxy(){
const proxyAgent = new ProxyAgent();
return new NodeHttpHandler({ httpAgent: proxyAgent, httpsAgent: proxyAgent })
}
updateRetentionPolicy(logGroupName, retentionInDays, cloudWatchLogs) {
if (`${retentionInDays}`.toLowerCase() === 'never expire') {
return cloudWatchLogs.send(new DeleteRetentionPolicyCommand({ logGroupName }))
}
return cloudWatchLogs.send(new PutRetentionPolicyCommand({ logGroupName, retentionInDays }))
}
async getRestApiId(apiGateway) {
const apis = [];
let marker;
do {
const { items, position } = await apiGateway.send(new GetRestApisCommand({ position: marker, limit: 500 }));
apis.push(...(items || []));
marker = position;
} while (marker);
const customApiName = this.serverless.service.provider.apiName;
const apiName = customApiName || (this.serverless.service.provider.apiGateway?.shouldStartNameWithService
? `${this.serverless.service.getServiceName()}-${this.options.stage}`
: `${this.options.stage}-${this.serverless.service.getServiceName()}`);
const match = apis.find((api) => api.name === apiName);
if (!match) {
throw new Error(`Api ${apiName} does not exist.`);
}
return match.id;
}
async getAccessLogGroupName(restApiId, apiGateway) {
const params = {
restApiId,
stageName: this.options.stage,
};
const stageConfig = await apiGateway.send(new GetStageCommand(params));
if (stageConfig.accessLogSettings && stageConfig.accessLogSettings.destinationArn) {
return stageConfig.accessLogSettings.destinationArn.split('log-group:')[1];
}
throw new Error(
`Access log destination ARN not set! Please check access logging is enabled and destination ARN is configured in ApiGateway > stage > Logs/Tracing.`
);
}
async setApigatewayLogRetention() {
const {
service: {
custom: {
apigatewayLogRetention: {
accessLogging = { enabled: false },
executionLogging = { enabled: false },
} = {},
} = {},
provider: {
profile
} = {}
} = {},
} = this.serverless;
if (!accessLogging.enabled && !executionLogging.enabled) {
return;
}
const proxy = process.env.HTTP_PROXY || process.env.HTTPS_PROXY || process.env.FTP_PROXY || process.env.WSS_PROXY || process.env.WS_PROXY;
const awsClientConfig = {
region: this.serverless.getProvider('aws').getRegion(),
...(profile && { credentials: fromIni({ profile }) }),
...(proxy && { requestHandler: this.getRequestHandlerWithProxy() })
}
const cloudWatchLogs = new CloudWatchLogsClient(awsClientConfig);
const apiGateway = new APIGatewayClient(awsClientConfig);
let restApiId;
try {
restApiId = await this.getRestApiId(apiGateway);
} catch (e) {
const errorMessage = `serverless-apigateway-log-retention - ERROR: Failed to retrieve rest api id. ${e.message}`;
this.serverless.cli.log(errorMessage);
throw new Error(errorMessage);
}
if (accessLogging.enabled) {
try {
const accessLogGroupName = await this.getAccessLogGroupName(restApiId, apiGateway);
await this.updateRetentionPolicy(accessLogGroupName, accessLogging.days, cloudWatchLogs);
this.serverless.cli.log(
`serverless-apigateway-log-retention - Successfully set ApiGateway access log (${accessLogGroupName}) retention to ${accessLogging.days} days.`
);
} catch (e) {
const errorMessage = `serverless-apigateway-log-retention - ERROR: Failed to set ApiGateway access log retention. ${e.message}`;
this.serverless.cli.log(errorMessage);
throw new Error(errorMessage);
}
}
if (executionLogging.enabled) {
try {
const executionLogGroupName = `API-Gateway-Execution-Logs_${restApiId}/${this.options.stage}`;
await this.updateRetentionPolicy(executionLogGroupName, executionLogging.days, cloudWatchLogs);
this.serverless.cli.log(
`serverless-apigateway-log-retention - Successfully set ApiGateway execution log (${executionLogGroupName}) retention to ${executionLogging.days} days.`
);
} catch (e) {
const errorMessage = `serverless-apigateway-log-retention - ERROR: Failed to set ApiGateway execution log retention. ${e.message}`;
this.serverless.cli.log(errorMessage);
throw new Error(errorMessage);
}
}
}
}
module.exports = ApigatewayLogRetentionPlugin;