Skip to content

Commit

Permalink
Merge pull request #2 from paulweb515/pwebster/syslog-udp-514
Browse files Browse the repository at this point in the history
Looks great, thank you Paul!
  • Loading branch information
hermansb authored Jul 21, 2016
2 parents 700997a + 636cde5 commit 9ff9e7d
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 7 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,21 @@ export log4js_syslog_appender_whitelist=audit-logs,audit-logs-v2
export log4js_syslog_appender_host=syslog.prd.ccs.ibmcloud.com
export log4js_syslog_appender_port=6514
export log4js_syslog_appender_product=otc-api
```

##Use with default syslog

You can use this appender with any default UDP syslog in unencrypted mode. The environment setup is very similar to above:

```
export log4js_syslog_appender_enabled=true
export log4js_syslog_appender_useUdpSyslog=true
export log4js_syslog_appender_whitelist=audit-logs,audit-logs-v2
export log4js_syslog_appender_host=localhost
export log4js_syslog_appender_port=514
export log4js_syslog_appender_product=otc-api
```


# Setting Certificates
There are two ways of setting the certs, either through a path (meaning you have to check it into a source control - kind of a nono or by setting the base64 encoded values as env vars - the right way).
Expand Down
48 changes: 42 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* Use, duplication or disclosure restricted by GSA ADP Schedule
* Contract with IBM Corp.
*/
/*eslint-env node */
'use strict';

var log4js = require('log4js'),
Expand All @@ -14,7 +15,8 @@ var log4js = require('log4js'),
tls = require('tls'),
fs = require('fs'),
util = require('util'),
os = require('os');
os = require('os'),
dgram = require('dgram');

module.exports = {
appender: appender,
Expand Down Expand Up @@ -80,6 +82,28 @@ function loggingFunction(options, log, tries) {

if (!syslogConnectionSingleton.connection && !syslogConnectionSingleton.connecting) {
syslogConnectionSingleton.connecting = true;
if (options.useUdpSyslog) {
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);
} else {

// set up mutual auth.
readBase64StringOrFile(options.certificateBase64, options.certificatePath, function(err, certificate) {
Expand Down Expand Up @@ -134,6 +158,7 @@ function loggingFunction(options, log, tries) {

});
});
}
} else {
logMessage(log, options, tries);
}
Expand Down Expand Up @@ -226,6 +251,7 @@ function configure(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 || config.options && config.options.useUdpSyslog || false,
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 || '',
Expand All @@ -241,7 +267,7 @@ function configure(config) {
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") {
Expand Down Expand Up @@ -272,23 +298,33 @@ function verifyOptions(options) {
}
});


[
'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"]) {
if (!options[key + "Path"] && !options[key + "Base64"] && !options.useUdpSyslog) {
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.');
if (options.useUdpSyslog) {
util.log('QRadar node-log4js-syslog-appender: WARNING env var ' +
key + 'Path will not be used for unencrypted syslog UDP/514.');
} else {
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.');
}
}
if (options[key + "Base64"] && options.useUdpSyslog) {
util.log('QRadar node-log4js-syslog-appender: WARNING env var ' +
key + 'Base64 will not be used for unencrypted syslog UDP/514.');
}
})

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "log4js-qradar-syslog-appender",
"version": "0.3.0",
"version": "0.3.1",
"description": "",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit 9ff9e7d

Please sign in to comment.