Adding filtering to the logger

- Closes #5036
- Add `applyFilterToKey()`
- Add test for `applyFilterToKey()`
- Add `filter` attribute to config for reporters
- Add `this.filter` method to `LogFormat` class
This commit is contained in:
Chris Cowan 2015-09-25 11:27:58 -07:00 committed by Court Ewing
parent 05628ab4cf
commit 21a6660103
5 changed files with 91 additions and 4 deletions

View file

@ -6,6 +6,7 @@ let ansicolors = require('ansicolors');
let stringify = require('json-stringify-safe');
let querystring = require('querystring');
let inspect = require('util').inspect;
let applyFilterToKey = require('./applyFilterToKey');
function serializeError(err) {
return {
@ -24,16 +25,27 @@ 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) {
_.each(this.config.filter, (action, key) => {
applyFilterToKey(data, key, action);
});
}
return data;
}
_transform(event, enc, next) {
var data = this.readEvent(event);
var data = this.filter(this.readEvent(event));
this.push(this.format(data) + '\n');
next();
}

View file

@ -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;

View file

@ -0,0 +1,49 @@
var applyFilterToKey = require('../applyFilterToKey');
var expect = require('expect.js');
function fixture() {
return {
req: {
headers: {
authorization: 'Basic dskd939k2i'
}
}
};
}
describe('applyFilterToKey(obj, key, action)', function () {
it('should remove a key from an object recursivly', function () {
var data = fixture();
applyFilterToKey(data, 'authorization', 'remove');
expect(data).to.eql({
req: { headers: {} }
});
});
it('should censor a key in an object recursivly', function () {
var data = fixture();
applyFilterToKey(data, 'authorization', 'censor');
expect(data).to.eql({
req: {
headers: {
authorization: 'XXXXXXXXXXXXXXXX'
}
}
});
});
it('should censor key with a RegEx in an object recursivly', function () {
var data = fixture();
var regex = /([^\s]+)$/;
applyFilterToKey(data, 'authorization', regex);
expect(data).to.eql({
req: {
headers: {
authorization: 'Basic XXXXXXXXXX'
}
}
});
});
});

View file

@ -0,0 +1,23 @@
function replacer(match, group) {
return (new Array(group.length + 1).join('X'));
}
module.exports = function applyFilterToKey(obj, key, action) {
for (let k in obj) {
if (obj.hasOwnProperty(k)) {
let val = obj[k];
if (typeof val === 'object') {
applyFilterToKey(val, key, action);
} else if (k === key) {
val = '' + val;
if (action === 'remove') delete obj[k];
if (action === 'censor') {
obj[k] = val.replace(/./g, 'X');
};
if (action instanceof RegExp) {
obj[k] = val.replace(action, replacer);
}
}
}
}
};

View file

@ -42,7 +42,10 @@ 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'),
filter: {
authorization: 'remove'
}
},
events: _.transform(events, function (filtered, val, key) {
// provide a string compatible way to remove events