-
Notifications
You must be signed in to change notification settings - Fork 692
/
Copy pathcollector.js
280 lines (242 loc) · 12 KB
/
collector.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
/*********************
Collector - The collector will query Oracle's APIs for the information required
to run the CloudSploit scans. This data will be returned in the callback
as a JSON object.
Arguments:
- OracleConfig: Required Authentication parameters for Oracle's REST API.
- settings: custom settings for the scan. Properties:
- skip_regions: (Optional) List of regions to skip
- api_calls: (Optional) If provided, will only query these APIs.
- Example:
{
'skip_regions': [', '],
'api_calls': ['EC2:describeInstances', 'S3:listBuckets']
}
- callback: Function to call when the collection is complete
*********************/
var async = require('async');
var helpers = require(__dirname + '/../../helpers/oracle');
var collectData = require(__dirname + '/../../helpers/shared');
var apiCalls = require(__dirname + '/../../helpers/oracle/api.js');
var calls = apiCalls.calls;
var postcalls = apiCalls.postcalls;
var finalcalls = apiCalls.finalcalls;
var regionSubscriptionService;
var globalServices = [
'core'
];
var processCall = function(OracleConfig, collection, settings, regions, call, service, serviceCb) {
// Loop through each of the service's functions
async.eachOfLimit(call, 10, function(callObj, callKey, callCb) {
if (settings.api_calls && settings.api_calls.indexOf(service + ':' + callKey) === -1) return callCb();
if (!collection[service][callKey]) collection[service][callKey] = {};
async.eachLimit(regions[service], helpers.MAX_REGIONS_AT_A_TIME, function(region, regionCb) {
if (region === 'default') {
region = OracleConfig.region ? OracleConfig.region : 'us-ashburn-1';
}
if (settings.skip_regions &&
settings.skip_regions.indexOf(region) > -1 &&
globalServices.indexOf(service) === -1) return regionCb();
// Ignore regions we are not subscribed to
if (collection[regionSubscriptionService.name] &&
collection[regionSubscriptionService.name][regionSubscriptionService.call] &&
collection[regionSubscriptionService.name][regionSubscriptionService.call][regionSubscriptionService.region] &&
collection[regionSubscriptionService.name][regionSubscriptionService.call][regionSubscriptionService.region].data &&
collection[regionSubscriptionService.name][regionSubscriptionService.call][regionSubscriptionService.region].data.filter(r => r.regionName == region) &&
collection[regionSubscriptionService.name][regionSubscriptionService.call][regionSubscriptionService.region].data.filter(r => r.regionName == region).length == 0) {
return regionCb();
}
if (!collection[service][callKey][region]) collection[service][callKey][region] = {};
if (callObj.reliesOnService) {
if (!callObj.reliesOnService.length) return regionCb();
// Ensure multiple pre-requisites are met
for (var reliedService in callObj.reliesOnService) {
if (callObj.reliesOnService[reliedService] && !collection[callObj.reliesOnService[reliedService]]) return regionCb();
if (callObj.reliesOnService[reliedService] &&
(!collection[callObj.reliesOnService[reliedService]] ||
!collection[callObj.reliesOnService[reliedService]][callObj.reliesOnCall[reliedService]] ||
!collection[callObj.reliesOnService[reliedService]][callObj.reliesOnCall[reliedService]][region] ||
!collection[callObj.reliesOnService[reliedService]][callObj.reliesOnCall[reliedService]][region].data)) return regionCb();
}
}
var LocalOracleConfig = JSON.parse(JSON.stringify(OracleConfig));
LocalOracleConfig.region = region;
LocalOracleConfig.service = service;
var executor = new helpers.OracleExecutor(LocalOracleConfig);
executor.run(collection, service, callObj, callKey, function(err, data) {
if (err && err.length) {
collection[service][callKey][region].err = err;
}
if (!data) return regionCb();
collection[service][callKey][region].data = data;
if (callObj.rateLimit) {
setTimeout(function() {
regionCb();
}, callObj.rateLimit);
} else {
regionCb();
}
});
}, function() {
callCb();
});
}, function() {
serviceCb();
});
};
let integrationCall = function(collection, settings, service, calls, postcalls, cback) {
let collect = JSON.parse(JSON.stringify(collection));
collect = Object.keys(collect).reduce((accumulator, key) => {
accumulator[key.toLowerCase()] = collect[key];
return accumulator;
}, {});
settings.previousCollection = Object.keys(settings.previousCollection).reduce((accumulator, key) => {
accumulator[key.toLowerCase()] = settings.previousCollection[key];
return accumulator;
}, {});
if (collect[service.toLowerCase()] &&
Object.keys(collect[service.toLowerCase()]) &&
Object.keys(collect[service.toLowerCase()]).length &&
collectData.callsCollected(service, collect, calls, postcalls)
) {
try {
collectData.processIntegration(service, settings, collect, calls, postcalls, false,function() {
cback();
});
} catch (e) {
console.log(`Error in storing ${service} service data: ${JSON.stringify(e)}`);
cback();
}
} else {
cback();
}
};
var getRegionSubscription = function(OracleConfig, collection, settings, calls, service, callKey, region, serviceCb) {
var LocalOracleConfig = JSON.parse(JSON.stringify(OracleConfig));
LocalOracleConfig.service = service;
if (!collection[service]) collection[service] = {};
if (!collection[service][callKey]) collection[service][callKey] = {};
if (!collection[service][callKey][region]) collection[service][callKey][region] = {};
var executor = new helpers.OracleExecutor(LocalOracleConfig);
executor.run(collection, service, calls[service][callKey], callKey, function(err, data) {
if (err) {
collection[service][callKey][region].err = err;
}
if (!data) return serviceCb();
collection[service][callKey][region].data = data;
serviceCb();
});
};
// Loop through all of the top-level collectors for each service
var collect = function(OracleConfig, settings, callback) {
var collection = {};
OracleConfig.region = OracleConfig.region ? OracleConfig.region : 'us-ashburn-1';
OracleConfig.maxRetries = 5;
OracleConfig.retryDelayOptions = {base: 300};
regionSubscriptionService = {name: 'regionSubscription', call: 'list', region: OracleConfig.region};
if (settings.gather) {
return callback(null, calls, postcalls, finalcalls);
}
var regions = helpers.regions(settings.govcloud);
let services = [];
getRegionSubscription(OracleConfig, collection, settings, calls, regionSubscriptionService.name, regionSubscriptionService.call, regionSubscriptionService.region, function() {
async.eachOfLimit(calls, 10, function(call, service, serviceCb) {
if (!collection[service]) collection[service] = {};
processCall(OracleConfig, collection, settings, regions, call, service, function() {
if (settings.identifier && calls[service].sendIntegration && calls[service].sendIntegration.enabled) {
if (!calls[service].sendIntegration.integrationReliesOn) {
integrationCall(collection, settings, service, calls, [], function() {
serviceCb();
});
} else {
services.push(service);
serviceCb();
}
} else {
serviceCb();
}
// serviceCb();
});
}, function() {
if (settings.identifier) {
async.each(services, function(serv, callB) {
integrationCall(collection, settings, serv, calls, [], callB);
}, function(err) {
if (err) {
console.log(err);
}
services = [];
});
}
// Now loop through the follow up calls
async.eachOfLimit(postcalls, 10, function(postCall, service, serviceCb) {
if (!collection[service]) collection[service] = {};
processCall(OracleConfig, collection, settings, regions, postCall, service, function() {
if (settings.identifier && postcalls[service].sendIntegration && postcalls[service].sendIntegration.enabled) {
if (!postcalls[service].sendIntegration.integrationReliesOn) {
integrationCall(collection, settings, service, [], [postcalls], function() {
serviceCb();
});
} else {
services.push(service);
serviceCb();
}
} else {
serviceCb();
}
// serviceCb();
});
}, function() {
if (settings.identifier) {
async.each(services, function(serv, callB) {
integrationCall(collection, settings, serv, [], [postcalls], callB);
}, function(err) {
if (err) {
console.log(err);
}
services = [];
});
}
// Now loop through the follow up calls
async.eachOfLimit(finalcalls, 10, function(finalCall, service, serviceCb) {
if (!collection[service]) collection[service] = {};
processCall(OracleConfig, collection, settings, regions, finalCall, service, function() {
if (settings.identifier && finalcalls[service].sendIntegration && finalcalls[service].sendIntegration.enabled) {
if (!finalcalls[service].sendIntegration.integrationReliesOn) {
integrationCall(collection, settings, service, [], [finalcalls], function() {
serviceCb();
});
} else {
services.push(service);
serviceCb();
}
} else {
serviceCb();
}
// serviceCb();
});
}, function() {
if (settings.identifier) {
async.each(services, function(serv, callB) {
integrationCall(collection, settings, serv, [], [finalcalls], callB);
}, function(err) {
if (err) {
console.log(err);
}
services = [];
});
}
//console.log(JSON.stringify(collection, null, 2));
callback(null, collection);
});
}, function() {
//console.log(JSON.stringify(collection, null, 2));
callback(null, collection);
});
}, function() {
//console.log(JSON.stringify(collection, null, 2));
callback(null, collection);
});
});
};
module.exports = collect;