forked from alertlogic/cwe-collector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheckin.js
179 lines (165 loc) · 6.16 KB
/
checkin.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
/* -----------------------------------------------------------------------------
* @copyright (C) 2017, Alert Logic, Inc
* @doc
*
* Lambda collector health check functions.
*
* @end
* -----------------------------------------------------------------------------
*/
const AWS = require('aws-sdk');
const async = require('async');
const m_alServiceC = require('./lib/al_servicec');
const m_packageJson = require('./package.json');
const AZCOLLECT_ENDPOINT = process.env.azollect_api;
function checkCloudFormationStatus(event, callback) {
var stackName = event.StackName;
var cloudformation = new AWS.CloudFormation();
cloudformation.describeStacks({StackName: stackName}, function(err, data) {
if (err) {
return callback(errorMsg('CWE00001', stringify(err)));
} else {
var stackStatus = data.Stacks[0].StackStatus;
if (stackStatus == 'CREATE_COMPLETE' ||
stackStatus == 'UPDATE_COMPLETE' ||
stackStatus == 'UPDATE_IN_PROGRESS' ||
stackStatus == 'UPDATE_ROLLBACK_COMPLETE' ||
stackStatus == 'UPDATE_ROLLBACK_IN_PROGRESS' ||
stackStatus == 'UPDATE_ROLLBACK_COMPLETE_CLEANUP_IN_PROGRESS' ||
stackStatus == 'REVIEW_IN_PROGRESS') {
return callback(null);
} else {
return callback(errorMsg('CWE00002', 'CF stack has wrong status: ' + stackStatus));
}
}
});
}
function checkCloudWatchEventsRule(event, finalCallback) {
var cwe = new AWS.CloudWatchEvents();
async.waterfall([
function(callback) {
cwe.describeRule({Name: event.CloudWatchEventsRule}, function(err, data) {
if (err) {
return callback(errorMsg('CWE00003', stringify(err)));
} else {
if (data.State == 'ENABLED' &&
data.EventPattern == event.CweRulePattern) {
return callback(null);
} else {
return callback(errorMsg('CWE00004', 'CWE Rule is incorrectly configured: ' + stringify(data)));
}
}
});
},
function(callback) {
cwe.listTargetsByRule({Rule: event.CloudWatchEventsRule}, function(err, data) {
if (err) {
return callback(errorMsg('CWE00005', stringify(err)));
} else {
if (data.Targets.length == 1 &&
data.Targets[0].Arn == event.KinesisArn) {
return callback(null);
} else {
return callback(errorMsg('CWE00006', 'CWE rule ' + event.CloudWatchEventsRule + ' has incorrect target set'));
}
}
});
}
], finalCallback);
}
function checkEventSourceMapping(checkinEvent, context, callback) {
var lambda = new AWS.Lambda();
lambda.listEventSourceMappings({FunctionName: context.functionName},
function(err, data) {
if (err) {
return callback(errorMsg('CWE00010', stringify(err)));
} else {
var eventSource = data.EventSourceMappings.find(
obj => obj.EventSourceArn === checkinEvent.KinesisArn);
if (eventSource) {
return checkEventSourceStatus(checkinEvent, eventSource, callback);
} else {
return callback(errorMsg(
'CWE00015',
'Event source mapping doesn\'t exist: ' + stringify(data)));
}
}
});
}
function checkEventSourceStatus(checkinEvent, eventSource, callback) {
var lastProcessingResult = eventSource.LastProcessingResult;
var state = eventSource.State;
if (state === 'Enabled' &&
(lastProcessingResult === 'OK' ||
// At this point the assumption is that all kinesis and events configuration
// around collect lambda is correct and 'No records processed'
// means just no events being generated.
lastProcessingResult === 'No records processed')) {
return callback(null);
} else {
return callback(errorMsg('CWE00020', 'Incorrect event source mapping status: ' + stringify(eventSource)));
}
}
function checkHealth(event, context, finalCallback) {
async.waterfall([
function(callback) {
checkCloudFormationStatus(event, callback);
},
function(callback) {
checkCloudWatchEventsRule(event, callback);
},
function(callback) {
checkEventSourceMapping(event, context, callback);
}
],
function(errMsg) {
if (errMsg) {
console.warn('Health check failed with', errMsg);
return finalCallback(null, {
status: errMsg.status,
error_code: errMsg.code,
details: [errMsg.details]
});
} else {
return finalCallback(null, {
status: 'ok',
details: []
});
}
});
}
function sendCheckin(event, context, aimsC, healthStatus, callback) {
var checkinValues = {
collectorType : 'cwe',
awsAccountId : event.AwsAccountId,
region : process.env.AWS_REGION,
functionName : context.functionName,
status : healthStatus.status,
details : healthStatus.details,
error_code: healthStatus.error_code,
version : m_packageJson.version,
statistics : healthStatus.statistics
};
var azcollectSvc = new m_alServiceC.AzcollectC(AZCOLLECT_ENDPOINT, aimsC);
azcollectSvc.doCheckin(checkinValues)
.then(resp => {
return callback(null);
})
.catch(function(exception) {
return callback(`Checkin failed: ${exception}`);
});
}
function stringify(jsonObj) {
return JSON.stringify(jsonObj, null, 0);
}
function errorMsg(code, message) {
return {
status: 'error',
code: code,
details: message
};
}
module.exports = {
checkHealth : checkHealth,
sendCheckin : sendCheckin
};