forked from IBM/node-log4js-qradar-syslog-appender
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
296 lines (259 loc) · 12.3 KB
/
index.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
/**
* Licensed Materials - Property of IBM
* (c) Copyright IBM Corporation 2016. 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';
var log4js = require('log4js'),
syslogConnectionSingleton = require('./syslog-connection-singleton'),
base64Decode = require('./base64-decode'),
tls = require('tls'),
fs = require('fs'),
util = require('util'),
os = require('os');
module.exports = {
appender: appender,
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;
util.log('QRadar Syslog Appender: 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 Logmet.');
// 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() {
util.log('QRadar Syslog Appender: 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, callback);
}
};
function loggingFunction(options, log, 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;
}
if (!syslogConnectionSingleton.connection && !syslogConnectionSingleton.connecting) {
syslogConnectionSingleton.connecting = true;
// set up mutual auth.
readBase64StringOrFile(options.certificateBase64, options.certificatePath, function(err, certificate) {
if (err) {
console.error('Error while loading certificate from path: ' + options.certificatePath + ' Error: ' + JSON.stringify(err, null, 2));
return;
}
readBase64StringOrFile(options.privateKeyBase64, options.privateKeyPath, function(err, key) {
if (err) {
console.error('Error while loading private key from path: ' + options.privateKeyPath + ' Error: ' + JSON.stringify(err, null, 2));
return;
}
readBase64StringOrFile(options.caBase64, options.caPath, function(err, caCert) {
if (err) {
console.error('Error while loading ca key from path: ' + options.caPath + ' Error: ' + JSON.stringify(err, null, 2));
return;
}
var tlsOptions = {
cert: certificate,
key: key,
ca: 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
};
syslogConnectionSingleton.connection = tls.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);
});
});
});
});
} else {
logMessage(log, options, tries);
}
};
function cleanupConnection(err, type) {
console.warn('QRadar Syslog appender: connection ' + type + '. Error: ' + JSON.stringify(err, null, 2));
if (syslogConnectionSingleton.connection) {
syslogConnectionSingleton.connection.destroy();
syslogConnectionSingleton.connection = null;
}
syslogConnectionSingleton.connecting = false;
};
function appender(options) {
return loggingFunction.bind(this, options);
};
function connected(message, options, tries) {
syslogConnectionSingleton.connecting = false;
console.warn('QRadar Syslog appender: we have reconnected to QRadar. ' +
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. Try to reconnect
if (!syslogConnectionSingleton.connection) {
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(),
process.env.url || os.hostname(),
options.product,
process.pid,
'-',
'-',
message || '{}'
);
};
function configure(config) {
if (process.env.log4js_syslog_appender_enabled !== 'true') {
return function() {};
} else {
if (config.appender) {
log4js.loadAppender(config.appender.type);
config.actualAppender = log4js.appenderMakers[config.appender.type](config.appender);
}
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,
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 || config.options && config.options.rejectUnauthorized || true
};
// This option is a boolean, but if a string is passed in, we need to
// coerce ourselves.
if (options.rejectUnauthorized === "false") {
options.rejectUnauthorized = false;
}
if (!verifyOptions(options)) {
return function() {};
}
util.log('Syslog appender is enabled');
return appender(options);
}
};
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]) {
util.log('QRadar node-log4js-syslog-appender: ' + key + ' is a required option. It is settable with the ' + option + ' environment variable.');
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"]) {
util.log('QRadar node-log4js-syslog-appender: Either ' + key + 'Path or ' + key + 'Base64 are required options. It is settable with the ' + option + ' environment variable.');
valid = false; // array.forEach is blocking
}
// Deprecated warnings.
if (options[key + "Path"]) {
util.log('QRadar node-log4js-syslog-appender: WARNING env var ' +
key + 'Path is now deprecated and will be removed in a future' +
' relase. Please switch to ' + key + 'Base64 instead.');
}
})
return valid;
};