forked from aquasecurity/saas-integrations
-
Notifications
You must be signed in to change notification settings - Fork 0
/
splunk.js
370 lines (328 loc) · 13.8 KB
/
splunk.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
var async = require('async');
var request = require('request');
var raw = function(endpoint, token, payload, callback) {
if (!endpoint) return callback('No Splunk endpoint provided');
if (!token) return callback('No Splunk token provided');
if (!payload) return callback('No Splunk payload provided');
var payloadStr = '';
if (Array.isArray(payload)) {
for (var i = 0; i < payload.length; i++){
payloadStr += JSON.stringify(payload[i]);
}
} else {
payloadStr = JSON.stringify(payload);
}
request({
url: endpoint,
method: 'POST',
headers: {
'Authorization': 'Splunk ' + token,
'Content-type': 'application/json'
},
body: payloadStr
}, function(err, response){
if (err) return callback(err);
if (!response || !response.statusCode ) return callback('Invalid response from Splunk');
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
* 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.raw_results) return callback('No raw_results provided');
if (!settings.account_name) return callback('No 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');
async.each(settings.endpoints, function(endpoint, cb){
// Splunk endpoints are delimited by ":::" such as:
// endpoint:::token
// Split them for use with the raw function
var endpointSplit = endpoint.split(':::');
var splunkEndpoint = endpointSplit[0];
var splunkToken = endpointSplit[1];
if (!splunkEndpoint || !splunkToken) return cb();
var timestamp = new Date(settings.timestamp);
var epoch = timestamp.getTime()/1000;
var results = [];
var scan_result;
if (settings.flatten_results) {
for (var i = 0; i < settings.raw_results.length; i++){
var testLevel = settings.raw_results[i];
for (var j = 0; j < testLevel.results.length; j++){
var resultLevel = testLevel.results[j];
scan_result = {
'event': resultLevel,
'sourcetype': 'cloudsploit:scan_results'
};
if (epoch) scan_result.time = epoch;
if (settings.account_number) scan_result.event.account_number = settings.account_number;
scan_result.event.account_name = settings.account_name;
scan_result.event.group_name = settings.group_name;
scan_result.event.tags = settings.tags;
scan_result.event.cloud = settings.cloud;
scan_result.event.category = testLevel.category;
scan_result.event.test_id = testLevel.id;
scan_result.event.title = testLevel.title;
scan_result.event.severity = testLevel.severity;
results.push(scan_result);
}
}
} else {
for (var k = 0; k < settings.raw_results.length; k++){
scan_result = {
'event': settings.raw_results[k],
'sourcetype': 'cloudsploit:scan_results'
};
if (epoch) scan_result.time = epoch;
if (settings.account_number) scan_result.event.account_number = settings.account_number;
scan_result.event.account_name = settings.account_name;
scan_result.event.group_name = settings.group_name;
scan_result.event.tags = settings.tags;
scan_result.event.cloud = settings.cloud;
scan_result.event.num_pass = settings.num_pass;
scan_result.event.num_warn = settings.num_warn;
scan_result.event.num_fail = settings.num_fail;
scan_result.event.num_unknown = settings.num_unknown;
scan_result.event.num_new_risks = settings.num_new_risks;
results.push(scan_result);
}
}
raw(splunkEndpoint, splunkToken, results, 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 scan_id 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 resources = [];
if (settings.resources && settings.resources.length) {
resources = settings.resources;
}
var event = {
'status': warnOrFail,
'account': settings.account_name,
'tags': settings.tags,
'cloud': settings.cloud,
'plugin': settings.test_name,
'affected_resources': resources,
'test_description': settings.test_description,
'scan_id': settings.scan_id};
var timestamp = new Date(settings.timestamp);
var epoch = timestamp.getTime()/1000;
var payload = {
'event': event,
'sourcetype': 'cloudsploit:alert'
};
if (epoch) {
payload.time = epoch;
}
async.each(settings.endpoints, function(endpoint, cb){
// Splunk endpoints are delimited by ":::" such as:
// endpoint:::token
// Split them for use with the raw function
var endpointSplit = endpoint.split(':::');
var splunkEndpoint = endpointSplit[0];
var splunkToken = endpointSplit[1];
if (!splunkEndpoint || !splunkToken) return cb();
raw(splunkEndpoint, splunkToken, 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.event.result !== 1 && settings.event.result !== 2) return callback('Settings event result is not a valid number');
var warnOrFail = settings.event.result === 1 ? 'WARN' : 'FAIL';
var event = {
'severity': warnOrFail,
'account': settings.account_name,
'region': settings.event.region,
'user': settings.event.caller,
'ip_address': settings.event.ip_address,
'message': settings.event.message,
'original_event': settings.original};
var timestamp = new Date(settings.timestamp);
var epoch = timestamp.getTime()/1000;
var payload = {
'event': event,
'sourcetype': 'cloudsploit:event'
};
if (epoch) {
payload.time = epoch;
}
async.each(settings.endpoints, function(endpoint, cb){
// Splunk endpoints are delimited by ":::" such as:
// endpoint:::token
// Split them for use with the raw function
var endpointSplit = endpoint.split(':::');
var splunkEndpoint = endpointSplit[0];
var splunkToken = endpointSplit[1];
if (!splunkEndpoint || !splunkToken) return cb();
raw(splunkEndpoint, splunkToken, 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.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 infoOrFail = settings.result == 0 ? 'INFO' : 'FAIL';
var verb = (settings.result == 0) ? 'succeeded' : 'failed';
var verbAction = (settings.event_id) ? 'auto-remediation' : 'manual-remediation';
var message = type + 'Aqua Wave CSPM ' + verbAction + ' ' + verb + ' for account: ' + settings.account_name + ' (' + settings.account_id + ')' +
(settings.message ? 'Auto-remediation triggered by event: ' + settings.message : 'Manual remediation triggered via the Aqua CSPM console');
var event = {
'severity': infoOrFail,
'account': settings.account_name,
'account_id': settings.account_id,
'cloud': settings.cloud.toUpperCase(),
'action': settings.permissions,
'resources': settings.remediated_resources,
'message': message
};
if (settings.event_id) {
event.region = settings.region;
event.caller = settings.caller;
event.ip_address = settings.ip_address;
event.api_call = settings.action;
}
var timestamp = new Date(settings.timestamp);
var epoch = timestamp.getTime()/1000;
var payload = {
'event': event,
'sourcetype': 'cloudsploit:event'
};
if (epoch) {
payload.time = epoch;
}
async.each(settings.endpoints, function(endpoint, cb){
let integrationName = settings.names[settings.endpoints.indexOf(endpoint)];
// Splunk endpoints are delimited by ":::" such as:
// endpoint:::token
// Split them for use with the raw function
var endpointSplit = endpoint.split(':::');
var splunkEndpoint = endpointSplit[0];
var splunkToken = endpointSplit[1];
if (!splunkEndpoint || !splunkToken) return cb();
raw(splunkEndpoint, splunkToken, payload, function(err){
settings.remediation_file['integrations'].push({integration:integrationName, status:(err ? 2 : 0), host:splunkEndpoint, err:(err ? err : '')});
cb();
});
}, function(err){
callback(err);
});
};
var testConnection = function(integration, callback) {
if (!integration) return callback('No integration object provided');
if (!integration.splunk_endpoint) return callback('No splunk endpoint found');
if (!integration.splunk_token) return callback('No splunk token found');
var payload = {
'sourcetype': 'cloudsploit:alert',
'event': {'message':'Access log test message'}
};
// var timestamp = new Date();
// var epoch = timestamp.getTime()/1000;
//
// if(epoch) {
// payload.time = epoch;
// }
raw(integration.splunk_endpoint, integration.splunk_token, 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
};