forked from alertlogic/cwe-collector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
321 lines (293 loc) · 9.87 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
/* -----------------------------------------------------------------------------
* @copyright (C) 2017, Alert Logic, Inc
* @doc
*
* Lambda function for collecting Amazon CloudWatch events and ingesting them
* into Alert Logic backend.
*
* @end
* -----------------------------------------------------------------------------
*/
const https = require('https');
const util = require('util');
const AWS = require('aws-sdk');
const async = require('async');
const zlib = require('zlib');
const m_alServiceC = require('./lib/al_servicec');
const m_alAws = require('./lib/al_aws');
const m_checkin = require('./checkin');
const m_packageJson = require('./package.json');
const response = require('cfn-response');
let AIMS_CREDS;
const INGEST_ENDPOINT = process.env.ingest_api;
const AL_ENDPOINT = process.env.al_api;
const AZCOLLECT_ENDPOINT = process.env.azollect_api;
function getDecryptedCredentials(callback) {
if (AIMS_CREDS) {
return callback(null);
} else {
const kms = new AWS.KMS();
kms.decrypt(
{CiphertextBlob: new Buffer(process.env.aims_secret_key, 'base64')},
(err, data) => {
if (err) {
return callback(err);
} else {
AIMS_CREDS = {
access_key_id: process.env.aims_access_key_id,
secret_key: data.Plaintext.toString('ascii')
};
return callback(null);
}
});
}
}
function getAlAuth(callback) {
var aimsC = new m_alServiceC.AimsC(AL_ENDPOINT, AIMS_CREDS);
aimsC.authenticate().then(
ok => { return callback(null, aimsC); },
err => { return callback(err); }
);
}
function getKinesisData(event, callback) {
async.map(event.Records, function(record, mapCallback) {
var cwEvent = new Buffer(record.kinesis.data, 'base64').toString('utf-8');
try {
return mapCallback(null, JSON.parse(cwEvent));
} catch (ex) {
console.warn('Event parse failed.', ex);
console.warn('Skipping', record.kinesis.data);
return mapCallback(null, {});
}
}, callback);
}
function filterGDEvents(cwEvents, callback) {
async.filter(cwEvents,
function(cwEvent, filterCallback){
return filterCallback(null, cwEvent.source && cwEvent.source == 'aws.guardduty');
},
callback
);
}
function formatMessages(event, context, callback) {
async.waterfall([
function(asyncCallback) {
getKinesisData(event, asyncCallback);
},
function(kinesisData, asyncCallback) {
filterGDEvents(kinesisData, asyncCallback);
},
function(collectedData, asyncCallback) {
if (collectedData.length > 0) {
return asyncCallback(null, JSON.stringify({
collected_batch : {
source_id : context.invokedFunctionArn,
collected_messages : collectedData
}
}));
} else {
return asyncCallback(null, '');
}
}],
callback);
}
function sendToIngest(event, context, aimsC, collectedBatch, callback) {
zlib.deflate(collectedBatch, function(compressionErr, compressed) {
if (compressionErr) {
return callback(compressionErr);
} else {
var ingest = new m_alServiceC.IngestC(INGEST_ENDPOINT, aimsC);
ingest.sendSecmsgs(compressed)
.then(resp => {
return callback(null, resp);
})
.catch(exception =>{
return callback(`Unable to send to Ingest ${exception}`);
});
}
});
}
function processResultInContext(context, err, result) {
if (err) {
return context.fail(err);
} else {
return context.succeed();
}
}
function processCheckin(event, context) {
async.waterfall([
function(callback) {
return getDecryptedCredentials(callback);
},
function(callback) {
return getAlAuth(callback);
},
function(aimsC, callback) {
return m_checkin.checkHealth(event, context, function(err, healthStatus) {
return callback(err, aimsC, healthStatus);
});
},
function(aimsC, healthStatus, callback) {
return getStatistics(context, event, function(err, response) {
healthStatus.statistics = response;
return callback(err, aimsC, healthStatus);
});
},
function(aimsC, healthStatus, callback) {
return m_checkin.sendCheckin(event, context, aimsC, healthStatus, callback);
}
],
function(err, result) {
return processResultInContext(context, err, result);
});
}
function sendRegistration(event, context, aimsC, isRegistration, callback) {
var registrationValues = {
collectorType : 'cwe',
awsAccountId : event.ResourceProperties.AwsAccountId,
region : process.env.AWS_REGION,
functionName : context.functionName,
stackName : event.ResourceProperties.StackName,
version : m_packageJson.version,
collectRule : event.ResourceProperties.CollectRule
};
var azcollectSvc = new m_alServiceC.AzcollectC(AZCOLLECT_ENDPOINT, aimsC);
if (isRegistration) {
azcollectSvc.doRegistration(registrationValues)
.then(resp => {
return callback(null);
})
.catch(exception => {
return callback(`Registration failed: ${exception}`);
});
} else {
azcollectSvc.doDeregistration(registrationValues)
.then(resp => {
return callback(null);
})
.catch(exception => {
return callback(`De-registration failed: ${exception}`);
});
}
}
function processRegistration(event, context, isRegistration) {
async.waterfall([
function(callback) {
getDecryptedCredentials(callback);
},
function(callback) {
getAlAuth(callback);
},
function(aimsC, callback) {
sendRegistration(event, context, aimsC, isRegistration, callback);
}
],
function(err, result) {
if (err) {
return response.send(event, context, response.FAILED, {Error: err});
} else {
return response.send(event, context, response.SUCCESS);
}
});
}
function processKinesisRecords(event, context) {
async.waterfall([
function(callback) {
getDecryptedCredentials(callback);
},
function(callback) {
getAlAuth(callback);
},
function(aimsC, callback) {
formatMessages(event, context, function(formatError, collectedData) {
return callback(formatError, aimsC, collectedData);
});
},
function(aimsC, collectedData, callback) {
if (collectedData != '') {
sendToIngest(event, context, aimsC, collectedData, callback);
} else {
return callback(null);
}
}
],
function(err, result) {
processResultInContext(context, err, result);
});
}
function processScheduledEvent(event, context) {
console.info("Processing scheduled event: ", event);
switch (event.Type) {
case 'SelfUpdate':
m_alAws.selfUpdate(function(err) {
if (err) {
return context.fail(err);
} else {
return context.succeed();
}
});
break;
case 'Checkin':
processCheckin(event, context);
break;
default:
return context.fail("Unknown scheduled event detail type: " + event.type);
}
}
function getStatistics(context, event, finalCallback) {
const kinesisName = m_alAws.arnToName(event.KinesisArn);
async.waterfall([
function(asyncCallback) {
return m_alAws.getLambdaMetrics(context.functionName,
'Invocations',
[],
asyncCallback);
},
function(statistics, asyncCallback) {
return m_alAws.getLambdaMetrics(context.functionName,
'Errors',
statistics,
asyncCallback);
},
function(statistics, asyncCallback) {
return m_alAws.getKinesisMetrics(kinesisName,
'IncomingRecords',
statistics,
asyncCallback);
},
function(statistics, asyncCallback) {
return m_alAws.getKinesisMetrics(kinesisName,
'IncomingBytes',
statistics,
asyncCallback);
},
function(statistics, asyncCallback) {
return m_alAws.getKinesisMetrics(kinesisName,
'ReadProvisionedThroughputExceeded',
statistics,
asyncCallback);
},
function(statistics, asyncCallback) {
return m_alAws.getKinesisMetrics(kinesisName,
'WriteProvisionedThroughputExceeded',
statistics,
asyncCallback);
}
], finalCallback);
};
exports.handler = function(event, context) {
switch (event.RequestType) {
case 'ScheduledEvent':
return processScheduledEvent(event, context);
case 'Create':
return processRegistration(event, context, true);
case 'Delete':
return processRegistration(event, context, false);
default:
if (event.Records) {
return processKinesisRecords(event, context);
} else {
return context.fail('Unknown event source: ' + event.source);
}
}
};