forked from aquasecurity/saas-integrations
-
Notifications
You must be signed in to change notification settings - Fork 0
/
teams.js
385 lines (358 loc) · 13.2 KB
/
teams.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
var async = require('async');
var request = require('request');
var config = {
ICON_URL: 'https://cloud.aquasec.com/assets/img/aqua_logo.png',
SIGN_IN_URL: 'https://cloud.aquasec.com/signin',
AQUA_CONSOLE_URL: 'https://cloud.aquasec.com',
COLORS: {
DEFAULT: '172B4D',
SUCCESS: '1A7F44',
WARNING: '947E2A',
DANGER: 'FF0302',
UNKNOWN: '9A9A9A'
}
};
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.themeColor) payload.themeColor = config.COLORS.DEFAULT;
request({
url: webhookUrl,
method: 'POST',
json: payload
}, function(err, response) {
if (err) return callback(err);
if (!response) {
return callback('Invalid response from Microsoft Teams');
}
callback(null, response);
});
};
/*
* results - called to send results for a new scan report
* arguments:
* settings: an object containing the following properties:
* - endpoints
* - account_name
* - num_pass, num_warn, num_fail, num_unknown, num_new_risks
* - account_type
* 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 (!settings.account_type) return callback('No settings account type 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');
var payload = {
themeColor: config.COLORS.SUCCESS,
text: 'A new Aqua Wave CSPM scan is available',
sections: [{
activityTitle: `For ${settings.account_type}: ` + settings.account_name,
activitySubtitle: '[Click here](' + config.SIGN_IN_URL + ') to log in.',
activityImage: config.ICON_URL,
facts: []
}]
};
if (settings.num_pass) {
payload.sections[0].facts.push({
name: 'PASS',
value: settings.num_pass
});
}
if (settings.num_warn) {
payload.sections[0].facts.push({
name: 'WARN',
value: settings.num_warn
});
}
if (settings.num_fail) {
payload.sections[0].facts.push({
name: 'FAIL',
value: settings.num_fail
});
}
if (settings.num_unknown) {
payload.sections[0].facts.push({
name: 'UNKNOWN',
value: settings.num_unknown
});
}
if (settings.num_new_risks) {
payload.sections[0].facts.push({
name: 'NEW',
value: settings.num_new_risks
});
}
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
* 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.result !== 1 && settings.result !== 2) return callback('Settings result is not a valid number');
var payload = {
themeColor: settings.result == 1 ? config.COLORS.WARNING : config.COLORS.DANGER,
text: 'Aqua Wave CSPM Alert',
sections: [
{
activityTitle: 'For account: ' + settings.account_name,
activitySubtitle: '[Click here](' + config.SIGN_IN_URL + ') to log in.',
activityImage: config.ICON_URL,
facts: [
{
name: 'Account:',
value: settings.account_name
},
{
name: 'Priority:',
value: settings.result == 1 ? 'Warning' : 'Failure'
},
{
name: 'Plugin:',
value: settings.test_name
},
{
name: 'Message:',
value: settings.test_description
}
]
}
]
};
if (settings.resources && settings.resources.length) {
payload.sections[0].facts.push({
name: 'Affected Resources',
value: settings.resources.join(', ')
});
}
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 = {
themeColor: settings.result == 1 ? config.COLORS.WARNING : config.COLORS.DANGER,
text: 'Aqua Wave CSPM Event Alert',
sections: [{
activityTitle: `For ${settings.account_type}: ` + settings.account_name,
activitySubtitle: '[Click here](' + config.SIGN_IN_URL + ') to log in.',
activityImage: config.ICON_URL,
facts: [
{
name: `${settings.account_type}:`,
value: settings.account_name
},
{
name: 'Action:',
value: settings.event.action
},
{
name: 'Region:',
value: settings.event.region
},
{
name: 'User:',
value: settings.event.caller
},
{
name: 'IP Address:',
value: settings.event.ip_address
},
{
name: 'Message:',
value: settings.event.message
}
]
}]
};
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
* - 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 = {
themeColor: settings.result == 0 ? config.COLORS.SUCCESS : config.COLORS.DANGER,
text: type + 'Aqua Wave CSPM ' + verbAction + ' ' + verb +
` for ${settings.account_type}: ` + settings.account_name + ' (' + settings.account_id + ')',
sections: [{
activityTitle: (settings.message ? 'Auto-remediation triggered by event: ' + settings.message : 'Manual remediation triggered via the Aqua CSPM console'),
activitySubtitle: '[Click here to view](' + config.AQUA_CONSOLE_URL + '/actions?remediation=' + settings.remediation_id + ')',
activityImage: config.ICON_URL,
facts: [
{
name: 'Remediation Detail',
value: ''
},
{
name: `${settings.account_type}:`,
value: settings.account_name
},
{
name: 'Cloud:',
value: settings.cloud.toUpperCase()
},
{
name: 'Action:',
value: settings.permissions
},
{
name: 'Resources:',
value: settings.remediated_resources
}
]
}]
};
if (settings.event_id) {
let event = [
{
name: 'Triggered via Event',
value: ''
},
{
name: 'Region:',
value: settings.region
},
{
name: 'User:',
value: settings.caller
},
{
name: 'IP Address:',
value: settings.ip_address
},
{
name: 'API Call:',
value: settings.action
}
];
payload.sections[0].facts.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.teams_webhook_url) return callback('No webhook found');
var payload = {
text: 'Aqua Wave Integration Test',
attachments: [
{
fallback: 'Test Successful.',
color: 'good',
text: 'PASS'
}
]
};
raw(integration.teams_webhook_url, payload, function(err, result){
if (err) {
callback(err, null);
} else {
callback(null, result);
}
});
};
module.exports = {
raw: raw,
result: result,
alert: alert,
event: event,
remediation_notification: remediation_notification,
testConnection: testConnection
};