forked from aquasecurity/saas-integrations
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sns.js
288 lines (261 loc) · 12.2 KB
/
sns.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
var async = require('async');
var AWS = require('aws-sdk');
var config = {
AQUA_CONSOLE_URL: 'https://cloud.aquasec.com'
};
var parseArn = function(arn) {
// Sample: arn:aws:sns:sa-east-1:123456789101:cloudsploit-test
var arnOne = arn.substring(12);
return arnOne.substring(0, arnOne.indexOf(':'));
};
var raw = function(topic, params, callback) {
if (!topic) return callback('No topic provided');
if (!params) return callback('No params provided');
if (!params.Message) return callback('No params Message provided');
if (!params.Subject) return callback('No params Subject provided');
if (!params.MessageStructure) params.MessageStructure = 'json';
if (!params.TopicArn) params.TopicArn = topic;
var sns = new AWS.SNS({region: parseArn(topic)});
sns.publish(params, function(err, data) {
if (err) return callback(err);
if (!data) return callback('Invalid response from AWS');
callback(null, data);
});
};
/*
* 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
* 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');
var payload = {
default: 'New CloudSploit scan available for account: ' + settings.account_name + ': ' + settings.num_pass + ' PASS; ' + settings.num_warn + ' WARN; ' + settings.num_fail + ' FAIL; ' + settings.num_unknown + ' UNKNOWN; ' + settings.num_new_risks + ' NEW',
email: 'New CloudSploit scan available for account: ' + settings.account_name + ': ' + settings.num_pass + ' PASS; ' + settings.num_warn + ' WARN; ' + settings.num_fail + ' FAIL; ' + settings.num_unknown + ' UNKNOWN; ' + settings.num_new_risks + ' NEW',
sms: 'New CloudSploit scan for: ' + settings.account_name + ': ' + settings.num_pass + ' PASS; ' + settings.num_warn + ' WARN; ' + settings.num_fail + ' FAIL; ' + settings.num_unknown + ' UNKNOWN; ' + settings.num_new_risks + ' NEW'
};
async.eachLimit(settings.endpoints, 5, function(topic, cb){
var params = {
Message: JSON.stringify(payload),
Subject: 'CloudSploit: New Scan Available'
};
raw(topic, params, 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.cloud) return callback('No settings cloud 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 warnOrFail = settings.result === 1 ? 'WARN' : 'FAIL';
var resourcesMsg;
if (settings.resources && settings.resources.length) {
resourcesMsg = settings.resources.join('\n ');
} else {
resourcesMsg = 'N/A';
}
var msg = `Connected ${settings.cloud} ${settings.account_type}: ` + settings.account_name +
' is in a ' + warnOrFail + ' state for the plugin: ' + settings.test_name +
`\n\n${settings.account_type}: ` + settings.account_name +
'\nStatus: ' + warnOrFail +
'\nPlugin: ' + settings.test_name +
'\nAffected Resources:\n ' + resourcesMsg;
async.eachLimit(settings.endpoints, 5, function(topic, cb){
var params = {
Message: JSON.stringify({
default: msg,
email: msg,
sms: settings.test_name + ' is ' + warnOrFail + ' for ' + settings.account_name
}),
Subject: 'CloudSploit: ' + settings.account_name + ': ' + settings.test_name + ' ' + warnOrFail
};
raw(topic, params, 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
* - account_type
* - cloud
* 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 msg = `${settings.account_type}: ` + settings.account_name +
'\nAction: ' + settings.event.action +
'\nRegion: ' + settings.event.region +
'\nUser: ' + settings.event.caller +
'\nIP Address: ' + settings.event.ip_address +
'\nMessage: ' + settings.event.message +
'\nOriginal Event:\n\n' +
JSON.stringify(settings.original, null, 4);
async.eachLimit(settings.endpoints, 5, function(topic, cb){
var params = {
Message: JSON.stringify({
default: msg,
email: msg,
sms: `Event Alert: ${settings.account_type}: ` + settings.account_name + ' action: ' + settings.event.action,
}),
Subject: `CloudSploit Event Alert for ${settings.cloud} ${settings.account_type}: ` + settings.account_name + ' action: ' + settings.event.action
};
raw(topic, params, 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
* - account_type
* 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 msg = type + 'Aqua Wave CSPM ' + verbAction + ' ' + verb +
`\nFor ${settings.account_type}: ` + settings.account_name + ' (' + settings.account_id + ')' +
'\n\nClick here to view: ' + config.AQUA_CONSOLE_URL + '/actions?remediation=' + settings.remediation_id +
'\n\n' + (settings.message ? 'Auto-remediation triggered by event: ' + settings.message : 'Manual remediation triggered via the Aqua CSPM console') +
'\nRemediation Detail' +
`\n${settings.account_type}:` + settings.account_name +
'\nCloud:' + settings.cloud.toUpperCase() +
'\nAction:' + settings.permissions +
'\nResources:' + settings.remediated_resources;
var sms = 'Aqua Wave CSPM ' + verbAction + ' ' + verb +
`\nFor ${settings.account_type}: ` + settings.account_name + ' (' + settings.account_id + ')' +
'\nClick here to view: ' + config.AQUA_CONSOLE_URL + '/actions?remediation=' + settings.remediation_id +
'\n\n' + (settings.message ? 'Auto-remediation triggered by event: ' + settings.message : 'Manual remediation triggered via the Aqua CSPM console');
if (settings.event_id) {
msg = msg.concat('\nTriggered via Event' +
'\nRegion:' + settings.region +
'\nUser:' + settings.caller +
'\nIP Address:' + settings.ip_address +
'\nAPI Call:' + settings.action);
}
async.eachLimit(settings.endpoints, 5, function(topic, cb){
let integrationName = settings.names[settings.endpoints.indexOf(topic)];
var params = {
Message: JSON.stringify({
default: msg,
email: msg,
sms: sms
}),
Subject: 'Aqua Wave CSPM ' + verbAction + ' ' + verb + ' ' + settings.account_name + ' (' + settings.account_id + ')'
};
raw(topic, params, function(err){
settings.remediation_file['integrations'].push({integration:integrationName, status:(err ? 2 : 0), host:topic, err:(err ? err : '')});
cb();
});
}, function(err){
callback(err);
});
};
var testConnection = function(integration, callback) {
if (!integration) return callback('No integration object provided');
if (!integration.sns_arn) return callback('No endpoint found');
var payload = {
Message: JSON.stringify({
default: 'Connection Test',
email: 'Connection Test',
sms: 'Connection Test'
}),
Subject: 'CloudSploit: Connection Test'
};
raw(integration.sns_arn, 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
};