-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathlog4js-qradar-syslog-appender.js
429 lines (368 loc) · 15.7 KB
/
log4js-qradar-syslog-appender.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/**
* Licensed Materials - Property of IBM
* (c) Copyright IBM Corporation 2016, 2019. All Rights Reserved.
*
* Note to U.S. Government Users Restricted Rights:
* Use, duplication or disclosure restricted by GSA ADP Schedule
* Contract with IBM Corp.
*/
'use strict';
const syslogConnectionSingleton = require('./syslog-connection-singleton'),
base64Decode = require('./base64-decode'),
tls = require('tls'),
fs = require('fs'),
util = require('util'),
os = require('os'),
dgram = require('dgram'),
net = require('net'),
log4js = require('log4js');
const logger = log4js.getLogger('log4js-qradar-syslog-appender');
module.exports = {
configure: configure
};
function retryLogic(retryFunction, tries) {
// we are in circuit break mode. There is something wrong with the qradar connection. We won't try to
// send any log messages to qradar until the circuit is connected again.
if (syslogConnectionSingleton.circuitBreak) {
syslogConnectionSingleton.droppedMessages++;
return;
}
// initialize (or increment if already initialized) tries
if (!tries) {
tries = 1;
}
else {
tries++;
}
if (tries > syslogConnectionSingleton.MAX_TRIES) {
syslogConnectionSingleton.circuitBreak = true;
internalLog('Tried sending a message ' + syslogConnectionSingleton.MAX_TRIES +
' times but the client was not connected. ' +
'Initiating circuit breaker protocol. ' +
'For the next ' + syslogConnectionSingleton.CIRCUIT_BREAK_MINS +
' mins, we will not attempt to send any messages to QRadar.');
// circuit breaker logic - if detected bad connection, stop trying
// to send log messages to qradar for syslogConnectionSingleton.CIRCUIT_BREAK_MINS.
syslogConnectionSingleton.droppedMessages++;
setTimeout(connectCircuit.bind(this),
syslogConnectionSingleton.CIRCUIT_BREAK_MINS * 60 * 1000);
return;
}
setTimeout(retryFunction.bind(this, tries), 100);
return;
}
function connectCircuit() {
internalLog('Re-connecting the circuit. So far we have dropped ' +
syslogConnectionSingleton.droppedMessages + ' messages.');
syslogConnectionSingleton.circuitBreak = false;
}
function readBase64StringOrFile(base64, file, callback) {
if (base64) {
callback(null, base64Decode(base64));
} else {
fs.readFile(file, {'encoding': 'utf8'}, callback);
}
}
function loggingFunction(options, log, tries) {
// internal log message. Do not cause infinite loop by attempting to syslog server.
if (log.categoryName === 'log4js-qradar-syslog-appender') return;
if (syslogConnectionSingleton.shutdown) {
return;
}
// we are in circuit break mode. There is something wrong with the qradar connection. We won't try to
// send any log messages to qradar until the circuit is connected again.
if (syslogConnectionSingleton.circuitBreak) {
syslogConnectionSingleton.droppedMessages++;
return;
}
if (!syslogConnectionSingleton.connection && !syslogConnectionSingleton.connecting) {
syslogConnectionSingleton.connecting = true;
// udp
if (options.useUdpSyslog) {
attemptUdpConnection(options, log, tries);
}
else { // tcp
var boundFunction = attemptTcpConnection.bind(this, log, tries);
if (mutualAuthConfigured(options)) {
configureMutualAuth(options, boundFunction);
}
else {
boundFunction(options);
}
}
}
else {
logMessage(log, options, tries);
}
}
function attemptUdpConnection(options, log, tries) {
var client = dgram.createSocket('udp4');
syslogConnectionSingleton.connection = {
write: function (msg) {
client.send(msg, 0, msg.length, options.port, options.host, function (err) {
if (err && err !== 0) {
cleanupConnection(err, 'error');
retryLogic(loggingFunction.bind(this, options, log), tries);
}
});
},
destroy: function() {
client.close();
}
};
client.on('error', function(err) {
cleanupConnection(err, 'error');
retryLogic(loggingFunction.bind(this, options, log), tries);
});
syslogConnectionSingleton.connecting = false;
logMessage(log, options, tries);
}
function mutualAuthConfigured(options) {
return ( (options.certificateBase64 || options.certificatePath) &&
(options.privateKeyBase64 || options.privateKeyPath) &&
(options.caBase64 || options.caPath) );
}
// set up mutual auth.
function configureMutualAuth(options, callback) {
readBase64StringOrFile(options.certificateBase64, options.certificatePath, function(err, certificate) {
if (err) {
logger.error('Error while loading certificate from path: ' + options.certificatePath + ' Error: ' + JSON.stringify(err, null, 2));
return;
}
options.certificate = certificate;
readBase64StringOrFile(options.privateKeyBase64, options.privateKeyPath, function(err, key) {
if (err) {
logger.error('Error while loading private key from path: ' + options.privateKeyPath + ' Error: ' + JSON.stringify(err, null, 2));
return;
}
options.key = key;
readBase64StringOrFile(options.caBase64, options.caPath, function(err, caCert) {
if (err) {
logger.error('Error while loading ca key from path: ' + options.caPath + ' Error: ' + JSON.stringify(err, null, 2));
return;
}
options.caCert = caCert;
callback(options, true);
});
});
});
}
function attemptTcpConnection(log, tries, options, mutualAuth) {
var tlsOptions = {
cert: options.certificate,
key: options.key,
ca: options.caCert,
host: options.host,
port: options.port,
passphrase: options.passphrase,
facility: options.facility,
tag: options.tag,
leef: options.leef,
vendor: options.vendor,
product: options.product,
product_version: options.product_version,
rejectUnauthorized: options.rejectUnauthorized
};
var tcpLib;
if (mutualAuth) {
tcpLib = tls;
}
else {
tcpLib = net;
}
syslogConnectionSingleton.connection = tcpLib.connect(tlsOptions, connected.bind(this, log, options, tries));
syslogConnectionSingleton.connection.setEncoding('utf8');
syslogConnectionSingleton.connection.on('error', function(err) {
cleanupConnection(err, 'error');
retryLogic(loggingFunction.bind(this, options, log), tries);
});
syslogConnectionSingleton.connection.on('close', function(err) {
cleanupConnection(err, 'closed');
retryLogic(loggingFunction.bind(this, options, log), tries);
});
syslogConnectionSingleton.connection.on('end', function(err) {
cleanupConnection(err, 'ended');
retryLogic(loggingFunction.bind(this, options, log), tries);
});
}
function cleanupConnection(err, type) {
logger.warn('Connection ' + type + '. Error: ' + JSON.stringify(err, null, 2));
if (syslogConnectionSingleton.connection) {
syslogConnectionSingleton.connection.destroy();
syslogConnectionSingleton.connection = null;
}
syslogConnectionSingleton.connecting = false;
}
function appender(config) {
var options = {
host: process.env.log4js_syslog_appender_host || config.options && config.options.host,
port: process.env.log4js_syslog_appender_port || config.options && config.options.port,
useUdpSyslog: process.env.log4js_syslog_appender_useUdpSyslog !== undefined ? process.env.log4js_syslog_appender_useUdpSyslog : config.options && config.options.useUdpSyslog,
certificatePath: process.env.log4js_syslog_appender_certificatePath || config.options && config.options.certificatePath,
privateKeyPath: process.env.log4js_syslog_appender_privateKeyPath || config.options && config.options.privateKeyPath,
passphrase: process.env.log4js_syslog_appender_passphrase || config.options && config.options.passphrase || '',
caPath: process.env.log4js_syslog_appender_caPath || config.options && config.options.caPath,
certificateBase64: process.env.log4js_syslog_appender_certificateBase64 || config.options && config.options.certificateBase64,
privateKeyBase64: process.env.log4js_syslog_appender_privateKeyBase64 || config.options && config.options.privateKeyBase64,
caBase64: process.env.log4js_syslog_appender_caBase64 || config.options && config.options.caBase64,
facility: process.env.log4js_syslog_appender_facility || config.options && config.options.facility || '',
tag: process.env.log4js_syslog_appender_tag || config.options && config.options.tag || '',
leef: process.env.log4js_syslog_appender_leef || config.options && config.options.leef || '',
vendor: process.env.log4js_syslog_appender_vendor || config.options && config.options.vendor || '',
product: process.env.log4js_syslog_appender_product || config.options && config.options.product,
product_version: process.env.log4js_syslog_appender_product_version || config.options && config.options.product_version || '',
rejectUnauthorized: process.env.log4js_syslog_appender_rejectUnauthorized !== undefined ? process.env.log4js_syslog_appender_rejectUnauthorized : config.options && config.options.rejectUnauthorized,
url: process.env.log4js_syslog_appender_url || config.options && config.options.url || process.env.url || os.hostname() || ''
};
var stripOut = ['https://', 'http://'];
for (var i = 0; i < stripOut.length; i++) {
if (options.url.startsWith(stripOut[i])) {
options.url = options.url.slice(stripOut[i].length);
}
}
// make sure boolean flags work properly with string inputs
options.useUdpSyslog = parseToBoolean(options.useUdpSyslog); // default is false
options.rejectUnauthorized = parseToBoolean(options.rejectUnauthorized, true); // default is true
if (!verifyOptions(options)) {
return function() {};
}
// deep clone of options
var optionsClone = JSON.parse(JSON.stringify(options));
delete optionsClone.privateKeyBase64;
internalLog('Syslog appender is enabled with options: ' + JSON.stringify(optionsClone, null, 2), true);
syslogConnectionSingleton.shutdown = false;
const appender = (loggingEvent) => {
loggingFunction(options, loggingEvent);
};
appender.shutdown = (callback) => {
syslogConnectionSingleton.shutdown = true;
cleanupConnection('log4js is shutting down', 'shutting down');
callback();
}
return appender;
}
function connected(message, options, tries) {
syslogConnectionSingleton.connecting = false;
logger.warn('We have (re)connected to QRadar using a secure connection with ' +
(syslogConnectionSingleton.connection.authorized ? 'a valid ' : 'an INVALID ') +
'peer certificate. ' + syslogConnectionSingleton.droppedMessages + ' messages have been dropped.');
logMessage(message, options, tries);
}
function logMessage(log, options, tries) {
// we are in circuit break mode. There is something wrong with the qradar connection. We won't try to
// send any log messages to qradar until the circuit is connected again.
if (syslogConnectionSingleton.circuitBreak) {
syslogConnectionSingleton.droppedMessages++;
return;
}
// we got disconnected or are still connecting
// retry later when we are hopefully (re)connected
if (!syslogConnectionSingleton.connection || syslogConnectionSingleton.connecting) {
return retryLogic(loggingFunction.bind(this, options, log), tries);
}
// if theres a whitelist then only send those messages
var logWhitelist = process.env.log4js_syslog_appender_whitelist;
var categoriesToSend = logWhitelist && logWhitelist.split(',');
if (logWhitelist && categoriesToSend.indexOf(log.categoryName) === -1) return;
var formattedMessage = formatMessage(log.data.join(' | '), log.level && log.level.levelStr, options);
syslogConnectionSingleton.connection.write(formattedMessage);
}
function levelToSeverity(levelStr) {
var levels = [
'FATAL',
'ERROR',
'WARN',
'INFO',
'DEBUG',
'TRACE'
];
return levels.indexOf(levelStr) !== -1 ? levels.indexOf(levelStr) + 1 : 4;
}
function formatMessage(message, levelStr, options) {
// format as:
// HEADER STRUCTURED_DATA MESSAGE
// where HEADER = <PRI>VERSION TIMESTAMP HOSTNAME APP-NAME PROCID MSGID
// for details see the RFC here http://tools.ietf.org/html/rfc5424# .
return util.format(
'<%d>%d %s %s %s %d %s %s %s\n',
16*8+levelToSeverity(levelStr), // hardcoded facility of local0 which translates to 16 according to RFC. TODO: use passed in facility.
1,
new Date().toJSON(),
options.url,
options.product,
process.pid,
'-',
'-',
message || '{}'
);
}
function configure(config) {
if (process.env.log4js_syslog_appender_enabled !== 'true') {
return function() {};
} else {
return appender(config);
}
}
function parseToBoolean(val, defaultValue) {
if (defaultValue) { // default is true
return val !== 'false' && val !== false;
}
else { // default is false
return val === 'true' || val === true;
}
}
function verifyOptions(options) {
var requiredOptions = [
'log4js_syslog_appender_host',
'log4js_syslog_appender_port',
'log4js_syslog_appender_product'
];
var valid = true;
requiredOptions.forEach(function(option) {
var key = option.substring(option.lastIndexOf('_')+1);
if (!options[key]) {
internalLog(key + ' is a required option. It is settable with the ' +
option + ' environment variable.', true);
valid = false; // array.forEach is blocking
}
});
[
'log4js_syslog_appender_certificate',
'log4js_syslog_appender_privateKey',
'log4js_syslog_appender_ca',
].forEach(function (option) {
var key = option.split('_').pop();
if (!options[key + "Path"] && !options[key + "Base64"] && !options.useUdpSyslog) {
internalLog('In order to enable ' + 'mutual auth, either ' + key +
'Path or ' + key +
'Base64 are required options. It is settable with the ' +
option + ' environment variable.', true);
}
// Deprecated warnings.
if (options[key + "Path"]) {
if (options.useUdpSyslog) {
internalLog('WARNING env var ' + key +
'Path will not be used for unencrypted syslog UDP/514.', true);
} else {
internalLog('WARNING env var ' + key +
'Path is now deprecated and will be removed in a future' +
' release. Please switch to ' + key + 'Base64 instead.', true);
}
}
if (options[key + "Base64"] && options.useUdpSyslog) {
internalLog('WARNING env var ' + key +
'Base64 will not be used for unencrypted syslog UDP/514.', true);
}
})
return valid;
}
function internalLog(msg, delay) {
if (delay) { // let log4js configuration to complete before sending a message using it.
setTimeout(() => {
logger.info(`${msg}`);
}, 5000);
}
else {
logger.info(`${msg}`);
}
}