-
Notifications
You must be signed in to change notification settings - Fork 2
/
QualysProcessor.js
283 lines (238 loc) · 11.7 KB
/
QualysProcessor.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
/*
* Qualys Processor - Main class to parse and map Qualys WS Response Data
*
* Fruition Partners - @author: [email protected]
*/
// Fruition SN-Sublime Sync config metadata
// __fileURL = https://cmsenergydev.service-now.com/sys_script_include.do?sys_id=4db79c480f60b100f9fb00dce1050e76
// __fieldName = script
// __authentication = STORED
var QualysProcessor = Class.create();
QualysProcessor.prototype = {
initialize: function() {
this.isDebugOn = gs.getProperty('qualys.cmdb.integration.debug', true);
this.processSoftware = gs.getProperty('qualys.cmdb.integration.importSoftware', true);
this.xmlHelper = new XMLHelper();
this.resultParser = new QualysResultParser();
this.totalProcessedCnt = 0;
this.startTime = gs.nowDateTime();
},
/*
* Main entry point to parse and map WS XML. Called from the WS Client.
* Loops through each Server entry maps fields and related tables.
*/
process: function(xmlStr) {
//Converts XML String to a JS Object. Much easier to parse and iterate over.
var xmlJS = this.xmlHelper.toObject(xmlStr);
this._logDebug('QualysProcessor Called. Beginning to process XML Document:\n' + xmlStr);
// Loop for each Host found in XML
var hosts = xmlJS.RESPONSE.HOST_LIST.HOST;
for (var host in hosts) {
try {
//Skip PC Hosts from Qualys. We only want Servers
var currentHost = hosts[host];
if (currentHost.NETBIOS.slice(0,2).toUpperCase() == 'PC'
|| currentHost.NETBIOS.slice(0,2).toUpperCase() == 'FW'
|| currentHost.NETBIOS.slice(0,2).toUpperCase() == 'HH'){
this._logDebug('Skipping PC Host: ' + currentHost.NETBIOS);
continue;
}
//Figure out what table to map to
var snTable = this._getSNTable(currentHost.NETBIOS.toString());
// sn some run time Statistics
var runTime = gs.dateDiff(this.startTime, gs.nowDateTime(), true);
gs.log('Processing Server: ' + currentHost.NETBIOS
+ '\nTotal Processed: ' + this.totalProcessedCnt
+ '\nRun Time: ' + runTime, 'QualysIntegration');
// Parse out Detection XML section specially since it contains uniquely formatted data.
// Calls out to our QualysResultParser libary for each Detection found.
var detections = currentHost.DETECTION_LIST.DETECTION;
var detectionObj = {};
for (var detecionNumber in detections) {
var qid = detections[detecionNumber].QID;
var results = detections[detecionNumber].RESULTS;
detectionObj[qid] = this.resultParser.getParsedResults(qid, results);
//this._logDebug('Detection QID: ' + qid + ' = ' + new JSON().encode(detectionObj[qid]));
}
// Build out our Mapping object (Name : Value pairs) for the current Server.
// Will be passed to a helper function mapFields() later on.
var fieldMapping = {
name: currentHost.NETBIOS,
serial_number: this._getDetectionVal(detectionObj['45208'],'system_serial_number'),
os_domain: currentHost.DNS.slice(currentHost.DNS.indexOf('.') + 1),
os: currentHost.OS,
ip_address: currentHost.IP,
dns_domain: currentHost.DNS,
mac_address: this._getDetectionVal(detectionObj['43007'],'mac_address'),
cpu_manufacturer: this._getDetectionVal(detectionObj['105054'],'vendoridentifier'),
cpu_type: this._getDetectionVal(detectionObj['105054'], 'processornamestring'),
cpu_speed: this._getDetectionVal(detectionObj['105054'], '~mhz'),
cpu_count: detectionObj['105054'].length,
discovery_source: 'Qualys',
u_ce_source: 'Qualys'
};
// Call to helper function which Updates/Inserts Server and returns it's sysid
// Params: Coelesce Fields Array, Table name, Field Mapping Object
var updatedServerId = this._mapFields(['name','serial'], snTable, fieldMapping);
// Maintain Related Table - Network Adaptors
this._maintainNetworkAdaptors(detectionObj, updatedServerId);
// Maintain Related Table - Installed Software
if (this.processSoftware) {
this._maintainInstalledSoftware(detectionObj, updatedServerId);
}
this.totalProcessedCnt++;
}
catch (err) {
gs.logError('QualysProcessor - Unhandled error processing host. Resuming next host. Error Msg: ' + err, 'QualysIntegration');
}
}
},
_getSNTable: function(netbios) {
var snTable = '';
var hostnameCode = netbios.slice(3,6).toUpperCase();
var grClassMapping = new GlideRecord("u_qualys_class_mapping");
grClassMapping.addQuery("u_hostname_code", hostnameCode);
grClassMapping.query();
if (grClassMapping.next()) {
snTable = grClassMapping.u_sn_table;
}
//No match found. Default to a table
else {
snTable = "cmdb_ci";
}
return snTable;
},
_maintainNetworkAdaptors: function(detectionObj, serverId) {
if (detectionObj['45099']) {
var updatedAdaptors = [];
//Loop for each Net Adaptor, Build mapping object.
for (var i = 0; i != detectionObj['45099'].length; i++) {
//Build Mapping
var currentAdaptor = detectionObj['45099'][i];
var adaptorMapping = {
name: currentAdaptor.name,
ip_address: currentAdaptor.ip_address,
cmdb_ci: serverId,
discovery_source: 'Qualys',
u_ce_source: 'Qualys'
};
// Map data for each Adaptor and add sysID to array.
var adaptorSysID = this._mapFields(['name','ip_address','cmdb_ci'], 'cmdb_ci_network_adapter', adaptorMapping);
updatedAdaptors.push(adaptorSysID);
}
// Maintain Network Adaptors Table
// Removes any adaptors for current server that wasn't just updated.
var softwareGR = new GlideRecord('cmdb_ci_network_adapter');
softwareGR.addQuery('cmdb_ci',serverId);
softwareGR.addQuery('sys_id','NOT IN',updatedAdaptors.toString());
softwareGR.deleteMultiple();
}
},
/*
* Loops on software array from detection results and maintains Installed Software and Application CI tables
* For the Installed Software table, any software that wasn't scanned is deleted in order to maintain the table. This funcitons like OOB Discovery and the SCCM Integration
* Inputs: Takes in detection object and server id.
*/
_maintainInstalledSoftware: function(detectionObj, serverId) {
// Looks for Software from two Detection fields (Win or Unix)
if (detectionObj['90235'] || detectionObj['45141']) {
var softwareDetection = detectionObj['90235'] ? '90235' : '45141';
var updatedSoftware = [];
//Loop for each Software, Build mapping object.
for (var i = 0; i != detectionObj[softwareDetection].length; i++) {
//Build Installed Software Mapping
var currentSoftware = detectionObj[softwareDetection][i];
var installDate = currentSoftware.install_date ? currentSoftware.install_date : 'Not Found';
var publisher = currentSoftware.publisher ? currentSoftware.publisher : 'Not Found';
var softwareMapping = {
display_name: currentSoftware.display_name,
version: currentSoftware.display_version,
install_date: installDate == 'Not Found' ? '' : installDate,
publisher: publisher == 'Not Found' ? '' : publisher,
installed_on: serverId,
discovery_source: 'Qualys'
};
// Map data for each Software Install and add sysID to array.
var softwareSysID = this._mapFields(['display_name','installed_on'], 'cmdb_sam_sw_install', softwareMapping);
updatedSoftware.push(softwareSysID);
/* Build Application CI Mapping also
* Exclude any software that is a KB Patch.
* Reg Ex Looks for "(KB" followed by 4 to 10 numbers followed by ")"
*/
var regPattern = /\(KB\d{3}.*\)/i;
if (!regPattern.test(currentSoftware.display_name.toString())) {
var applicationCIMapping = {
name: currentSoftware.display_name,
version: currentSoftware.display_version,
discovery_source: 'Qualys',
u_ce_source: 'Qualys'
}
// Map data for each Software CI
this._mapFields(['name'], 'cmdb_ci_appl', applicationCIMapping);
}
}
//End Software Loop
// Maintain Installed Software Table
// Removes any software for current server that wasn't just updated.
var softwareGR = new GlideRecord('cmdb_sam_sw_install');
softwareGR.addQuery('installed_on',serverId);
softwareGR.addQuery('sys_id','NOT IN',updatedSoftware.toString());
softwareGR.deleteMultiple();
}
},
/*
* Field mapping helper function.
* Takes in fieldMapping JSON object. Required to have a "coelseceFields", "table", and "records" field in object.
*/
_mapFields: function(coalesceFields, tableName, mappingData) {
//Check that coelesce and table fieldnames exist
if (coalesceFields && tableName && mappingData) {
//Setup Glide Query
//Loop on each coalesceField and build search query
var mappingGR = new GlideRecord(tableName);
for (var i = 0; i != coalesceFields.length; i++) {
var currentField = coalesceFields[i];
mappingGR.addQuery(currentField, mappingData[currentField]);
}
mappingGR.query();
//Determine if Update or Insert
if (mappingGR.hasNext()) {
//this._logDebug('Update Transaction');
mappingGR.next();
for (var key in mappingData) {
mappingGR[key] = mappingData[key];
}
mappingGR.update();
} else {
//this._logDebug('Insert Transaction');
mappingGR.initialize();
for (var key in mappingData) {
mappingGR[key] = mappingData[key];
}
mappingGR.insert();
}
//this._logDebug('Processed ' + mappingGR.sys_id + ' in table ' + tableName);
return mappingGR.sys_id.toString();
}
},
/*
* Helper Utility to get values for single row Detection Results
*/
_getDetectionVal: function(obj, field) {
if (obj && obj[0] && field) {
return obj[0][field];
} else {
return '';
}
},
/*
* Debug Function that logs if debugging is on.
*/
_logDebug: function(msg) {
if (this.isDebugOn != 'true') {
return;
}
gs.log(msg, 'QualysIntegration');
},
type: 'QualysProcessor'
};