forked from aquasecurity/saas-integrations
-
Notifications
You must be signed in to change notification settings - Fork 0
/
slack.js
394 lines (363 loc) · 14 KB
/
slack.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
var async = require('async');
var request = require('request');
var config = {
SLACK_ICON_URL: 'https://cloud.aquasec.com/assets/img/aqua_logo.png',
SLACK_USERNAME: 'Aqua Wave',
AQUA_CONSOLE_URL: 'https://cloud.aquasec.com'
};
var raw = function(webhookUrl, payload, callback) {
if (!webhookUrl) return callback('No webhook URL provided');
if (!payload) return callback('No payload provided');
if (!payload.text) return callback('No payload text provided');
if (!payload.icon_url) payload.icon_url = config.SLACK_ICON_URL;
if (!payload.username) payload.username = config.SLACK_USERNAME;
if (!payload.attachments || !payload.attachments.length) return callback('No payload attachments provided');
request.post(webhookUrl, {form: {
payload: JSON.stringify(payload)
}}, function(err, response){
if (err) return callback(err);
if (!response || !response.body) {
return callback('Invalid response from Slack');
}
if (response.body !== 'ok') return callback(response);
callback(null, response);
});
};
/*
* results - called to send results for a new scan report
* arguments:
* settings: an object containing the following properties:
* - endpoints
* - account_name
* - account_type
* - num_pass, num_warn, num_fail, num_unknown, num_new_risks
* callback
*/
var result = function(settings, callback) {
if (!settings) return callback('No settings object provided');
if (!settings.endpoints) return callback('No settings endpoints provided');
if (!settings.account_name) return callback('No settings account_name provided');
if (typeof settings.num_pass !== 'number') return callback('Settings num_pass is not a valid number');
if (typeof settings.num_warn !== 'number') return callback('Settings num_warn is not a valid number');
if (typeof settings.num_fail !== 'number') return callback('Settings num_fail is not a valid number');
if (typeof settings.num_unknown !== 'number') return callback('Settings num_unknown is not a valid number');
if (typeof settings.num_new_risks !== 'number') return callback('Settings num_new_risks is not a valid number');
if (!settings.account_type) return callback('No settings account type provided');
var payload = {
text: `A new Aqua Wave scan is available for ${settings.account_type}: ` + settings.account_name + '. <https://cloud.aquasec.com/signin|Click here> to log in.',
attachments: []
};
if (settings.num_pass) {
payload.attachments.push({
fallback: 'There were ' + settings.num_pass + ' passing results.',
color: 'good',
text: settings.num_pass + ' PASS'
});
}
if (settings.num_warn) {
payload.attachments.push({
fallback: 'There were ' + settings.num_warn + ' warning results.',
color: 'warning',
text: settings.num_warn + ' WARN'
});
}
if (settings.num_fail) {
payload.attachments.push({
fallback: 'There were ' + settings.num_fail + ' failing results.',
color: 'danger',
text: settings.num_fail + ' FAIL'
});
}
if (settings.num_unknown) {
payload.attachments.push({
fallback: 'There were ' + settings.num_unknown + ' unknown results.',
color: '#9A9A9A',
text: settings.num_unknown + ' UNKNOWN'
});
}
if (settings.num_new_risks) {
payload.attachments.push({
fallback: 'There were ' + settings.num_new_risks + ' new risk results.',
color: '#9A9A9A',
text: settings.num_new_risks + ' NEW'
});
}
// No results to send
if (!payload.attachments.length) return callback();
async.each(settings.endpoints, function(webhookUrl, cb){
raw(webhookUrl, payload, function(err){
cb(err);
});
}, function(err){
callback(err);
});
};
/*
* alert - called to send an alert for a test
* arguments:
* settings: an object containing the following properties:
* - endpoints
* - account_name
* - test_name
* - test_description
* - result
* - scan_id
* - resources
* - account_type
* - cloud
* callback
*/
var alert = function(settings, callback) {
if (!settings) return callback('No settings object provided');
if (!settings.endpoints) return callback('No settings endpoints provided');
if (!settings.account_name) return callback('No settings account_name provided');
if (!settings.test_name) return callback('No settings test_name provided');
if (!settings.test_description) return callback('No settings test_description provided');
if (!settings.scan_id) return callback('No settings test_description provided');
if (!settings.account_type) return callback('No settings account type provided');
if (settings.result !== 1 && settings.result !== 2) return callback('Settings result is not a valid number');
var payload = {
text: 'Aqua Wave CSPM Alert for test: ' + settings.test_name + ` in ${settings.account_type}: ` + settings.account_name + ' <https://cloud.aquasec.com/signin|Click here> to log in.',
attachments: [
{
color: settings.result == 1 ? 'warning' : 'danger',
fields: [
{
title: 'Description',
value: settings.test_description,
short: false
},
{
title: `${settings.account_type}`,
value: settings.account_name,
short: true
},
{
title: 'Priority',
value: settings.result == 1 ? 'Warning' : 'Failure',
short: true
}
]
}
]
};
if (settings.resources && settings.resources.length) {
payload.attachments[0].fields.push({
title: 'Affected Resources',
value: settings.resources.join(', '),
short: false
});
}
async.each(settings.endpoints, function(webhookUrl, cb){
raw(webhookUrl, payload, function(err){
cb(err);
});
}, function(err){
callback(err);
});
};
/*
* event - called to send an event alert routing
* arguments:
* settings: an object containing the following properties:
* - endpoints
* - account_name
* - event
* - original
* callback
*/
var event = function(settings, callback) {
if (!settings) return callback('No settings object provided');
if (!settings.endpoints) return callback('No settings endpoints provided');
if (!settings.account_name) return callback('No settings account_name provided');
if (!settings.event) return callback('No settings event provided');
if (!settings.original) return callback('No settings original provided');
if (!settings.account_type) return callback('No settings account type provided');
if (settings.event.result !== 1 && settings.event.result !== 2) return callback('Settings event result is not a valid number');
var payload = {
text: `Aqua Wave CSPM Event Alert for ${settings.account_type}: ` + settings.account_name + ' <https://cloud.aquasec.com/signin|Click here> to log in.',
attachments: [
{
color: settings.event.result == 1 ? 'warning' : 'danger',
fields: [
{
title: 'Action',
value: settings.event.action,
short: true
},
{
title: 'Region',
value: settings.event.region,
short: true
},
{
title: 'IP Address',
value: settings.event.ip_address,
short: true
},
{
title: 'User',
value: settings.event.caller,
short: false
},
{
title: 'Message',
value: settings.event.message,
short: false
}
]
}
]
};
async.each(settings.endpoints, function(webhookUrl, cb){
raw(webhookUrl, payload, function(err){
cb(err);
});
}, function(err){
callback(err);
});
};
/*
* remediation_notification - called to send a remediation notification
* arguments:
* settings: an object containing the following properties:
* - endpoints
* - cloud
* - account_id
* - account_name
* - remediated_resources
* - remediation_type
* - permissions
* - remediation_id
* - esult
* - caller
* - region
* - action
* - ip_address
* - account_type
* - message
* - created
* callback
*/
var remediation_notification = function(settings, callback) {
if (!settings) return callback('No settings object provided');
if (!settings.endpoints) return callback('No settings endpoints provided');
if (!settings.names) return callback('No settings names provided');
if (!settings.cloud) return callback('No settings cloud provided');
if (!settings.account_id) return callback('No settings account_id provided');
if (!settings.account_name) return callback('No settings account_name provided');
if (!settings.remediated_resources) return callback('No settings remediated resources provided');
if (!settings.remediation_type) return callback('No settings remediation type provided');
if (!settings.permissions) return callback('No settings permissions provided');
if (!settings.remediation_id) return callback('No settings remediation_id provided');
if (!settings.account_type) return callback('No settings account type provided');
if (settings.result !== 0 && settings.result !== 1) return callback('Settings result is not a valid number');
if (!settings.created) return callback('No settings created provided');
var type = (settings.type == 'dryrun') ? 'Dry run: ' : '';
var verb = (settings.result == 0) ? 'succeeded' : 'failed';
var verbAction = (settings.event_id) ? 'auto-remediation' : 'manual-remediation';
var payload = {
author_icon: config.SLACK_ICON_URL,
text: type + 'Aqua Wave CSPM ' + verbAction + ' ' + verb + ' (' + settings.remediation_type + `) for ${settings.account_type}: ` + settings.account_name + ' (' + settings.account_id + ')' +
'\n' + (settings.message ? 'Auto-remediation triggered by event: ' + settings.message : 'Manual remediation triggered via the Aqua CSPM console') +
'\n <' + config.AQUA_CONSOLE_URL + '/actions?remediation=' + settings.remediation_id + '|Click here to view>',
attachments: [{
color: settings.result == 0 ? 'good' : 'danger',
fields: [
{
title: 'Remediation Detail',
value: '',
short: false
},
{
title: `${settings.account_type}:`,
value: settings.account_name,
short: false
},
{
title: 'Cloud:',
value: settings.cloud.toUpperCase(),
short: false
},
{
title: 'Action:',
value: settings.permissions,
short: false
},
{
title: 'Resources:',
value: settings.remediated_resources,
short: false
}
]
}]
};
if (settings.event_id) {
let event = [
{
title: 'Triggered via Event',
value: '',
short: false
},
{
title: 'Region:',
value: settings.region,
short: false
},
{
title: 'User:',
value: settings.caller,
short: false
},
{
title: 'IP Address:',
value: settings.ip_address,
short: false
},
{
title: 'API Call:',
value: settings.action,
short: false
}
];
payload.attachments[0].fields.push(...event);
}
async.each(settings.endpoints, function(webhookUrl, cb){
let integrationName = settings.names[settings.endpoints.indexOf(webhookUrl)];
raw(webhookUrl, payload, function(err){
settings.remediation_file['integrations'].push({integration:integrationName, status:(err ? 2 : 0), host:webhookUrl, err:(err ? err : '')});
cb();
});
}, function(err){
callback(err);
});
};
var testConnection = function(integration, callback) {
if (!integration) return callback('No integration object provided');
if (!integration.slack_webhook_url) return callback('No webhook found');
var payload = {
text: 'Aqua Wave Integration Test',
attachments: [
{
fallback: 'Test Successful.',
color: 'good',
text: 'PASS'
}
]
};
raw(integration.slack_webhook_url, payload, function(err, result){
if (err) {
callback(err);
} else {
callback(null, result);
}
});
};
module.exports = {
raw: raw,
result: result,
alert: alert,
event: event,
remediation_notification: remediation_notification,
testConnection: testConnection
};