Skip to content

Commit

Permalink
Adding filtering to the logger
Browse files Browse the repository at this point in the history
- Closes elastic#5036
- Add `applyFilterToKey()`
- Add test for `applyFilterToKey()`
- Add `filter` attribute to config for reporters
- Add `this.filter` method to `LogFormat` class
  • Loading branch information
simianhacker authored and epixa committed Nov 5, 2015
1 parent 05628ab commit 21a6660
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 4 deletions.
16 changes: 14 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 applyFilterToKey = require('./applyFilterToKey');

function serializeError(err) {
return {
Expand All @@ -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();
}
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
49 changes: 49 additions & 0 deletions src/server/logging/__tests__/applyFilterToKey.js
Original file line number Diff line number Diff line change
@@ -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'
}
}
});
});

});
23 changes: 23 additions & 0 deletions src/server/logging/applyFilterToKey.js
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
}
};
5 changes: 4 additions & 1 deletion src/server/logging/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 21a6660

Please sign in to comment.