forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- 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
1 parent
05628ab
commit 21a6660
Showing
5 changed files
with
91 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
} | ||
} | ||
}); | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters