-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
385 lines (353 loc) · 12.8 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
"use strict";
/**
* Follow these steps to configure the webhook in Slack:
*
* 1. Navigate to https://<your-team-domain>.slack.com/services/new
*
* 2. To send a message to an existing webhook select "Manage" then
* "Custom Integrations" then "Incoming WebHooks" then edit the configuration.
* To create a new webhook click "Add to Slack"
*
* 4. Copy the webhook URL from the setup instructions (e.g. "https://hooks.slack.com/services/abc123").
*
* 5. On the Lambda console window click Edit the environment variable section and environment
* variable "slackChannel" and add for the value the channel name, e.g. "#dockstore-dev-testing".
* Add the environment variable "hookUrl" and for the value use the webhook URL
*
*/
const url = require("url");
const https = require("https");
const { EC2Client, DescribeInstancesCommand } = require("@aws-sdk/client-ec2");
// The Slack URL to send the message to
const hookUrl = process.env.hookUrl;
// The Slack channel to send a message to stored in the slackChannel environment variable
const defaultSlackChannel = process.env.slackChannel;
const dockstoreEnvironment = process.env.dockstoreEnvironment;
const snsTopicToSlackChannel = JSON.parse(process.env.snsTopicToSlackChannel);
// Enumerate all the AWS instances in the current region
// and find the one with the instance ID in question.
// Then go through all the tags on that instance and
// find the one with the key 'Name', whose value
// is the name displayed on the console of the instance.
function getInstanceNameAndSendMsgToSlack(
slackChannel,
targetInstanceId,
messageText,
processEventCallback,
callback
) {
instanceName(targetInstanceId).then((friendlyName) => {
callback(
slackChannel,
messageText,
friendlyName,
targetInstanceId,
processEventCallback
);
});
}
/**
* If the instanceId exists and has a tag whose key is "Name", returns that tag's value, otherwise returns null.
* @param instanceId
* @returns {Promise<void>}
*/
async function instanceName(instanceId) {
const client = new EC2Client();
const command = new DescribeInstancesCommand({
InstanceIds: [instanceId],
});
try {
const result = await client.send(command); // Throws if the instance is not found
const instance = result.Reservations[0].Instances; // Safe to assume, as previous line would have thrown
const tagInstanceNameKey = instance.Tags.find((tag) => "Name" === tag.Key);
return tagInstanceNameKey?.Value || null;
} catch (e) {
console.error("Error describing instance", e);
}
return null;
}
function constructMsgAndSendToSlack(
slackChannel,
messageText,
targetInstanceName,
targetInstanceId,
callback
) {
console.info(`Found instance name:${targetInstanceName}`);
if (targetInstanceName) {
messageText =
messageText + ` to target: ${targetInstanceName} (${targetInstanceId})`;
} else {
messageText = messageText + ` to target: ${targetInstanceId}`;
}
sendMessageToSlack(slackChannel, messageText, callback);
}
function postMessage(message, callback) {
const body = JSON.stringify(message);
console.info("body is:" + body);
const options = url.parse(hookUrl);
options.method = "POST";
options.headers = {
"Content-Type": "application/json",
"Content-Length": Buffer.byteLength(body),
};
const postReq = https.request(options, (res) => {
const chunks = [];
res.setEncoding("utf8");
res.on("data", (chunk) => chunks.push(chunk));
res.on("end", () => {
if (callback) {
callback({
body: chunks.join(""),
statusCode: res.statusCode,
statusMessage: res.statusMessage,
});
}
});
return res;
});
postReq.write(body);
postReq.end();
}
function sendMessageToSlack(slackChannel, messageText, callback) {
const slackMessage = {
channel: slackChannel,
text: messageText,
username: `Dockstore ${dockstoreEnvironment} Notification`,
icon_emoji: ":exclamation:",
};
postMessage(slackMessage, (response) => {
if (response.statusCode < 400) {
console.info("Message posted successfully on Slack");
callback(null);
} else if (response.statusCode < 500) {
console.error(
`Error posting message to Slack API: ${response.statusCode} - ${response.statusMessage}`
);
callback(null); // Don't retry because the error is due to a problem with the request
} else {
// Let Lambda retry
callback(
`Server error when processing message: ${response.statusCode} - ${response.statusMessage}`
);
}
});
}
function awsConfigMessageText(message) {
const alarmName = message.detail.configRuleName;
const newState = message.detail.newEvaluationResult.complianceType;
// If the event has a resource in the details, include that in the message
const resource = message.detail.resourceId
? `for ${message.detail.resourceId}`
: "";
return `${alarmName} state is now ${newState} ${resource}`;
}
function trustedAdvisorMessageText(message) {
const detailType = message["detail-type"];
const msgStatus = message.detail["status"];
const checkName = message.detail["check-name"];
const checkItemDetails = JSON.stringify(
message.detail["check-item-detail"],
null,
2
);
return `${detailType} with status ${msgStatus} for Dockstore ${dockstoreEnvironment} in region ${message.region} for check ${checkName}. Details are:\n${checkItemDetails}`;
}
function guardDutyMessageText(message) {
let messageText =
`A GuardDuty Finding was reported on Dockstore ` +
dockstoreEnvironment +
` in region: ` +
message.region +
`.`;
return messageText + ` The description is: ` + message.detail.description;
}
function ssmOrSigninMessageText(message) {
const eventName = message.detail.eventName;
const sourceIPAddress = message.detail.sourceIPAddress;
let messageText = `uninitialized message text`;
if (message.source === "aws.ssm") {
let user;
if (message.detail.userIdentity.type === "IAMUser") {
user = message.detail.userIdentity.userName;
} else if (message.detail.userIdentity.type === "AssumedRole") {
const accountId = message.detail.userIdentity.accountId;
// Don't display account ID
user = message.detail.userIdentity.arn.replace(
accountId,
"X".repeat(accountId.length)
);
}
messageText = `${user} initiated AWS Systems Manager (SSM) event ${eventName}`;
} else if (message.source === "aws.signin") {
const userType = message.detail.userIdentity.type;
messageText = `A user initiated AWS sign-in event ${eventName} as ${userType}`;
}
if (
Object.prototype.hasOwnProperty.call(message.detail, "errorCode") &&
message.detail["errorCode"]
) {
const errorCode = message.detail.errorCode;
messageText = messageText + ` but received error code: ${errorCode}`;
}
messageText =
messageText +
` on Dockstore ` +
dockstoreEnvironment +
` in region: ` +
message.region +
`.`;
return messageText + ` Event was initiated from IP ${sourceIPAddress}`;
}
function cloudWatchEventBridgeAlarmMessageText(message) {
const alarmName = message.detail.alarmName;
const newState = message.detail.state.value;
const newStateReason = message.detail.state.reason;
return `${alarmName} state is now ${newState}: ${newStateReason}`;
}
function alarmMessageText(message) {
const alarmName = message.AlarmName;
const newState = message.NewStateValue;
const reason = message.NewStateReason;
return `${alarmName} state is now ${newState}: ${reason}`;
}
function dockstoreDeployerMessageText(message) {
return message.message;
}
// https://docs.aws.amazon.com/AmazonS3/latest/userguide/notification-content-structure.html
// https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/CloudWatchEventsandEventPatterns.html
function s3ActivityMessageText(message) {
const userName = message.detail.userIdentity["userName"];
const eventName = message.detail["eventName"];
const awsRegion = message.detail["awsRegion"];
const bucketName = message.detail.requestParameters["bucketName"];
return `${userName} generated S3 event ${eventName} from region ${awsRegion} for bucket ${bucketName} in Dockstore ${dockstoreEnvironment}`;
}
function ecsActivityMessageText(message) {
if (message["detail-type"] === "ECS Task State Change") {
return ecsTaskStateChangeMessageText(message);
} else {
return ecsAutoScalingMessageText(message);
}
}
function ecsTaskStateChangeMessageText(message) {
const taskArn = message?.detail?.taskArn;
const lastStatus = message?.detail?.lastStatus;
let messageText = `Task ${taskArn} is now ${lastStatus}`;
const taskContainers = message?.detail?.containers;
if (Array.isArray(taskContainers)) {
const taskContainerNames = taskContainers
?.map((container) => container.name)
?.join(", ");
messageText += `\nContainers: ${taskContainerNames}`;
}
["startedAt", "stoppedAt", "stoppedReason"].forEach((name) => {
const value = message?.detail?.[name];
if (value != undefined) {
messageText += `\n${name}: ${value}`;
}
});
return messageText;
}
function ecsAutoScalingMessageText(message) {
const serviceName = message.detail.requestParameters.service;
const newDesiredCount = message.detail.requestParameters.desiredCount;
const currentRunningCount =
message.detail.responseElements.service.runningCount;
return `Auto scaling event triggered for ECS service ${serviceName}. Desired count of tasks updated from ${currentRunningCount} to ${newDesiredCount}`;
}
function messageTextFromMessageObject(message) {
if (message.source === "aws.config") {
return awsConfigMessageText(message);
} else if (message.source === "aws.trustedadvisor") {
return trustedAdvisorMessageText(message);
} else if (message.source === "aws.guardduty") {
return guardDutyMessageText(message);
} else if (message.source === "aws.ssm" || message.source === "aws.signin") {
return ssmOrSigninMessageText(message);
} else if (message.source === "aws.s3") {
return s3ActivityMessageText(message);
} else if (message.source === "dockstore.deployer") {
return dockstoreDeployerMessageText(message);
} else if (message.source === "aws.ecs") {
return ecsActivityMessageText(message);
} else if (message.source === "aws.cloudwatch") {
return cloudWatchEventBridgeAlarmMessageText(message);
} else {
return alarmMessageText(message);
}
}
function addInstanceDetails(message) {
if (message.source === "aws.ssm" || message.source === "aws.signin") {
return (
Object.prototype.hasOwnProperty.call(
message.detail,
"requestParameters"
) &&
message.detail["requestParameters"] &&
Object.prototype.hasOwnProperty.call(
message.detail.requestParameters,
"target"
)
);
}
return false;
}
// Set the Slack channel based on the input SNS Topic to Slack Channel map
// in the snsTopicToSlackChannel env var with format
// {"<SNS Topic resource id>":"<slack channel name>"}
// E.g.
// {"slack-low-priority-topic":"dockstore-testing",
// "slack-medium-priority-topic":"dockstore-dev-alerts",
// "slack-high-priority-topic":"dockstore-alerts",
// "SSM-to-Slack-SNSTopicToSlack-1VI4KZW1DSADS":"dockstore-dev-testing" }
// input: SNS topic ARN
function setSlackChannelBasedOnSNSTopic(topicArn) {
// Get the SNS Topic resource ID from the AWS ARN
// https://docs.aws.amazon.com/general/latest/gr/aws-arns-and-namespaces.html
const snsTopicResourceID = topicArn.slice(topicArn.lastIndexOf(":") + 1);
if (Object.keys(snsTopicToSlackChannel).includes(snsTopicResourceID)) {
return snsTopicToSlackChannel[snsTopicResourceID];
} else {
return defaultSlackChannel;
}
}
function processEvent(event, callback) {
console.log(event);
let message;
let messageText;
try {
message = JSON.parse(event.Records[0].Sns.Message);
messageText = messageTextFromMessageObject(message);
} catch (e) {
message = event.Records[0].Sns.Message;
messageText = message;
console.log("Plain message received: " + messageText);
}
const topicArn = event.Records[0].Sns.TopicArn;
const slackChannel = setSlackChannelBasedOnSNSTopic(topicArn);
console.info("Slack channel is " + slackChannel);
if (addInstanceDetails(message)) {
const targetInstanceId = message.detail.requestParameters.target;
getInstanceNameAndSendMsgToSlack(
slackChannel,
targetInstanceId,
messageText,
callback,
constructMsgAndSendToSlack
);
} else {
sendMessageToSlack(slackChannel, messageText, callback);
}
}
exports.handler = (event, context, callback) => {
// This will record the event in the CloudWatch logs
console.info(
"cloud-watch-to-slack-testing EVENT\n" + JSON.stringify(event, null, 2)
);
if (hookUrl) {
processEvent(event, callback);
} else {
callback("Hook URL has not been set.");
}
};