-
Notifications
You must be signed in to change notification settings - Fork 1
/
integration.js
149 lines (127 loc) · 3.97 KB
/
integration.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
const { size } = require('lodash/fp');
const async = require('async');
const {
logging: { setLogger, getLogger },
errors: { parseErrorToReadableJson }
} = require('polarity-integration-utils');
const { validateOptions } = require('./server/userOptions');
const {
removePrivateIps,
removeEntityTypes,
getEntityTypes,
removeBlocklistedIpsAndDomains
} = require('./server/dataTransformations');
const {
getReportById,
getEvent,
getIndicators,
getReports,
getVulnerability,
getReportImage
} = require('./server/queries');
const assembleLookupResults = require('./server/assembleLookupResults');
const doLookup = async (entities, options, cb) => {
const Logger = getLogger();
try {
Logger.trace({ entities }, 'doLookup');
const entitiesWithoutBlocklistedEntities = removeBlocklistedIpsAndDomains(
entities,
options
);
const searchableEntities = removePrivateIps(entitiesWithoutBlocklistedEntities);
const nonCveEntities = removeEntityTypes('cve', searchableEntities);
const cveEntities = getEntityTypes('cve', searchableEntities);
const [indicators, vulnResults] = await Promise.all([
getIndicators(nonCveEntities, options),
getVulnerability(cveEntities, options)
]);
Logger.trace({
indicators,
vulnResults
});
const lookupResults = assembleLookupResults(
entities,
indicators,
vulnResults,
options
);
Logger.trace({ lookupResults }, 'Lookup Results');
cb(null, lookupResults);
} catch (error) {
const err = parseErrorToReadableJson(error);
Logger.error({ error, formattedError: err }, 'Get Lookup Results Failed');
cb({ detail: error.message || 'Lookup Failed', err });
}
};
const onMessage = async (payload, options, cb) => {
const Logger = getLogger();
Logger.trace({ payload }, 'onMessage');
switch (payload.action) {
case 'GET_EVENT':
try {
const event = await getEvent(payload.eventLink, options);
cb(null, event);
} catch (error) {
const err = parseErrorToReadableJson(error);
Logger.error({ error, formattedError: err }, 'Get Event Details Failed');
cb(err);
}
break;
case 'GET_REPORT_ASSETS':
try {
const assets = payload.assets;
const images = {};
await async.eachLimit(assets, 5, async (asset) => {
const reportImageAsBase64 = await getReportImage(asset, options);
images[asset] = reportImageAsBase64;
});
Logger.trace({ fetchedAssetNames: Object.keys(images) }, 'GET_REPORT_ASSETS');
cb(null, {
images
});
} catch (error) {
const err = parseErrorToReadableJson(error);
Logger.error({ error, formattedError: err }, 'GET_REPORT_ASSETS Failed');
cb(err);
}
break;
// Not currently used as the content of the report is returned by `getReports`
case 'GET_REPORT':
try {
const report = await getReportById(payload.reportId, options);
Logger.info({ assets: report.assets }, 'Image assets');
cb(null, report);
} catch (error) {
const err = parseErrorToReadableJson(error);
Logger.error({ error, formattedError: err }, 'GET_REPORT Failed');
cb(err);
}
break;
}
};
const onDetails = async (lookupObject, options, cb) => {
const Logger = getLogger();
try {
const reports = await getReports(lookupObject.entity, options);
if (size(reports)) {
lookupObject.data.details.reports = reports;
lookupObject.data.summary.push(`Report Count: ${reports.length}`);
Logger.trace(
{ lookup: lookupObject.data },
'Looking at the data after on details.'
);
}
cb(null, lookupObject.data);
} catch (error) {
const err = parseErrorToReadableJson(error);
Logger.error({ error, formattedError: err }, 'Get Reports Failed');
cb(err);
}
};
module.exports = {
startup: setLogger,
validateOptions,
doLookup,
onDetails,
onMessage
};