-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(logger): filter out sensitive data, use serializers
- Loading branch information
1 parent
0d1adf5
commit b4dd5c5
Showing
4 changed files
with
59 additions
and
50 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,55 @@ | ||
const bunyan = require('bunyan'); | ||
|
||
const config = require('../config'); | ||
const configFile = require('../config'); | ||
const packageInfo = require('../package'); | ||
|
||
const logger = bunyan.createLogger({ | ||
name: packageInfo.name, | ||
level: config.logger.silent ? bunyan.FATAL + 1 : config.logger.level | ||
level: configFile.logger.silent ? bunyan.FATAL + 1 : configFile.logger.level | ||
}); | ||
|
||
// A helper to flatten the nested object. Copypasted from Google. | ||
function flattenObject(obj, prefix = '') { | ||
return Object.keys(obj).reduce((acc, k) => { | ||
const pre = prefix.length ? prefix + '.' : ''; | ||
if (typeof obj[k] === 'object' && obj[k] !== null && Object.prototype.toString.call(obj[k]) !== '[object Date]') { | ||
Object.assign(acc, flattenObject(obj[k], pre + k)); | ||
} else { | ||
acc[pre + k] = obj[k]; | ||
} | ||
|
||
return acc; | ||
}, {}); | ||
} | ||
|
||
/* eslint-disable */ | ||
function unflattenObject(data) { | ||
const result = {}; | ||
|
||
for (const i in data) { | ||
const keys = i.split('.'); | ||
keys.reduce((r, e, j) => { | ||
return r[e] || (r[e] = isNaN(Number(keys[j + 1])) ? (keys.length - 1 == j ? data[i] : {}) : []); | ||
}, result); | ||
} | ||
return result; | ||
} | ||
/* eslint-enable */ | ||
|
||
const filterFields = (body) => { | ||
const flatten = flattenObject(body); | ||
for (const field in flatten) { | ||
if (configFile.filter_fields.some((filterField) => field === filterField)) { | ||
flatten[field] = '*'.repeat(flatten[field].length); | ||
} | ||
} | ||
|
||
return unflattenObject(flatten); | ||
}; | ||
|
||
logger.addSerializers({ | ||
body: (body) => filterFields(body), | ||
config: (config) => filterFields(config) | ||
}); | ||
|
||
module.exports = logger; |
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 |
---|---|---|
@@ -1,15 +1,24 @@ | ||
const morgan = require('morgan'); | ||
const _ = require('lodash'); | ||
|
||
const log = require('./logger'); | ||
|
||
module.exports = morgan((tokens, req, res) => { | ||
const user = req.user | ||
? _.pick(req.user, ['id', 'username', 'first_name', 'last_name', 'email']) | ||
: undefined; | ||
|
||
const body = _.isEmpty(req.body) | ||
? undefined | ||
: req.body; | ||
|
||
log.info({ | ||
method: tokens.method(req, res), | ||
url: tokens.url(req, res), | ||
status: tokens.status(req, res), | ||
length: tokens.res(req, res, 'content-length'), | ||
'response-time': tokens['response-time'](req, res), | ||
user: req.user, | ||
body: req.body | ||
user, | ||
body | ||
}, 'Request processed'); | ||
}); |
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