Skip to content

Commit

Permalink
Merge pull request #5340 from epixa/backport-pr-5038
Browse files Browse the repository at this point in the history
Backport PR #5038 issue #5036
  • Loading branch information
epixa committed Nov 5, 2015
2 parents 6829404 + 7169d3b commit f8bad5d
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/server/config/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ module.exports = Joi.object({

events: Joi.any().default({}),
dest: Joi.string().default('stdout'),

filter: Joi.any().default({}),
json: Joi.boolean()
.when('dest', {
is: 'stdout',
Expand Down
12 changes: 10 additions & 2 deletions src/server/logging/LogFormat.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ let ansicolors = require('ansicolors');
let stringify = require('json-stringify-safe');
let querystring = require('querystring');
let inspect = require('util').inspect;
let applyFiltersToKeys = require('./applyFiltersToKeys');

function serializeError(err) {
return {
Expand All @@ -24,16 +25,23 @@ let levelColor = function (code) {
return ansicolors.red(code);
};


module.exports = class TransformObjStream extends Stream.Transform {
constructor() {
constructor(config) {
super({
readableObjectMode: false,
writableObjectMode: true
});
this.config = config;
}

filter(data) {
if (!this.config.filter) return data;
return applyFiltersToKeys(data, this.config.filter);
}

_transform(event, enc, next) {
var data = this.readEvent(event);
var data = this.filter(this.readEvent(event));
this.push(this.format(data) + '\n');
next();
}
Expand Down
2 changes: 1 addition & 1 deletion src/server/logging/LogReporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ let LogFormatString = require('./LogFormatString');
module.exports = class KbnLogger {
constructor(events, config) {
this.squeeze = new Squeeze(events);
this.format = config.json ? new LogFormatJson() : new LogFormatString();
this.format = config.json ? new LogFormatJson(config) : new LogFormatString(config);

if (config.dest === 'stdout') {
this.dest = process.stdout;
Expand Down
39 changes: 39 additions & 0 deletions src/server/logging/__tests__/applyFiltersToKeys.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
var applyFiltersToKeys = require('../applyFiltersToKeys');
var expect = require('expect.js');

describe('applyFiltersToKeys(obj, actionsByKey)', function () {
it('applies for each key+prop in actionsByKey', function () {
var data = applyFiltersToKeys({
a: {
b: {
c: 1
},
d: {
e: 'foobar'
}
},
req: {
headers: {
authorization: 'Basic dskd939k2i'
}
}
}, {
b: 'remove',
e: 'censor',
authorization: '/([^\\s]+)$/'
});

expect(data).to.eql({
a: {
d: {
e: 'XXXXXX',
},
},
req: {
headers: {
authorization: 'Basic XXXXXXXXXX'
}
}
});
});
});
42 changes: 42 additions & 0 deletions src/server/logging/applyFiltersToKeys.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
function toPojo(obj) {
return JSON.parse(JSON.stringify(obj));
}

function replacer(match, group) {
return (new Array(group.length + 1).join('X'));
}

function apply(obj, key, action) {
for (let k in obj) {
if (obj.hasOwnProperty(k)) {
let val = obj[k];
if (k === key) {
if (action === 'remove') {
delete obj[k];
}
else if (action === 'censor' && typeof val === 'object') {
delete obj[key];
}
else if (action === 'censor') {
obj[k] = ('' + val).replace(/./g, 'X');
}
else if (/\/.+\//.test(action)) {
var matches = action.match(/\/(.+)\//);
if (matches) {
let regex = new RegExp(matches[1]);
obj[k] = ('' + val).replace(regex, replacer);
}
}
} else if (typeof val === 'object') {
val = apply(val, key, action);
}
}
}
return obj;
}

module.exports = function (obj, actionsByKey) {
return Object.keys(actionsByKey).reduce((output, key) => {
return apply(output, key, actionsByKey[key]);
}, toPojo(obj));
};
9 changes: 8 additions & 1 deletion src/server/logging/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,14 @@ module.exports = function (kbnServer, server, config) {
reporter: require('./LogReporter'),
config: {
json: config.get('logging.json'),
dest: config.get('logging.dest')
dest: config.get('logging.dest'),
// I'm adding the default here because if you add another filter
// using the commandline it will remove authorization. I want users
// to have to explicitly set --logging.filter.authorization=none to
// have it show up int he logs.
filter: _.defaults(config.get('logging.filter'), {
authorization: 'remove'
})
},
events: _.transform(events, function (filtered, val, key) {
// provide a string compatible way to remove events
Expand Down

0 comments on commit f8bad5d

Please sign in to comment.