Skip to content
Snippets Groups Projects
Commit 6cf8c9bc authored by Sigmund, Dominik's avatar Sigmund, Dominik
Browse files

Merge branch '2-add-a-function-to-allow-for-a-secure-display' into 'master'

Resolve "Add a Function to allow for a secure display"

Closes #2

See merge request general/config!2
parents 67b64d44 57bc2cdf
Branches
Tags
1 merge request!1Master
Pipeline #7095 canceled
......@@ -23,10 +23,28 @@ It reads from the following sources, performing a deep merge:
Enviroment Variables can target deep nested settings:
The Setting _setting.deep.key_ can be reached with *SETTING_DEEP_KEY*
You may use the function _reload()_ to reload the config from all sources.
`config.reload()``
You may use the function __reload()_ to reload the config from all sources.
`config._reload()``
This makes *reload* a reserved keyword
This makes *_reload* a reserved keyword
You may use the function __show()_ to display the config without:
- password
- secret
- token
- key
- apiKey
- apiToken
- apiSecret
- user
- username
Values will be replaced with the value "redacted"
`config._show()``
This makes *_show* a reserved keyword
If you give a basePath, the config-Files are used from there.
Else the main dir of the application will be used.
......
......@@ -42,7 +42,7 @@ module.exports = function(basePath = undefined) {
return schema[pList[len-1]]
}
config.reload = function () {
config._reload = function () {
try {
fs.accessSync(configDefaults)
config = merge(this, JSON.parse(fs.readFileSync(configDefaults, 'utf8')))
......@@ -78,6 +78,29 @@ module.exports = function(basePath = undefined) {
}
}
}
config.reload()
config._show = function () {
// clone the config object
let config = JSON.parse(JSON.stringify(this))
// remove _reload
delete config._reload
// remove _show
delete config._show
// remove _set
delete config._set
// remove _get
delete config._get
// redact all nested objects where the key is
// 'password' or 'secret' or 'token' or 'key' or 'apiKey' or 'apiToken' or 'apiSecret'
// or 'user' or 'username'
let keys = objectDeepKeys(config)
for (let index = 0; index < keys.length; index++) {
const element = keys[index]
if (element.toLowerCase().includes('password') || element.toLowerCase().includes('secret') || element.toLowerCase().includes('token') || element.toLowerCase().includes('key') || element.toLowerCase().includes('apikey') || element.toLowerCase().includes('apitoken') || element.toLowerCase().includes('apisecret') || element.toLowerCase().includes('user') || element.toLowerCase().includes('username')) {
set(config, element, 'REDACTED')
}
}
return config
}
config._reload()
return config
}
\ No newline at end of file
......@@ -119,7 +119,7 @@ describe('config', function() {
expect(config.setting).toBe('file:file.txt')
})
it('should reload if asked', async function() {
it('should _reload if asked', async function() {
await fs.writeFile('config.defaults.json', JSON.stringify(jsonDefaults))
await fs.writeFile('config.json', JSON.stringify(jsonLocals))
......@@ -130,7 +130,7 @@ describe('config', function() {
jsonLocals.setting = 'reloaded-value'
await fs.writeFile('config.json', JSON.stringify(jsonLocals))
config.reload()
config._reload()
await fs.unlink('config.json')
await fs.unlink('config.defaults.json')
......@@ -138,4 +138,15 @@ describe('config', function() {
expect(config.setting).toBe('reloaded-value')
})
it('should redact passwords if using _show', async function() {
await fs.writeFile('config.defaults.json', JSON.stringify(jsonDefaults))
await fs.writeFile('config.json', JSON.stringify(jsonLocals))
let config = new Config()
await fs.unlink('config.json')
await fs.unlink('config.defaults.json')
console.dir(config._show())
expect(config._show().even.deeper.key).toBe('REDACTED')
})
})
This diff is collapsed.
{
"name": "@general/config",
"version": "1.11.0",
"version": "1.12.0",
"description": "Simple Config with ENV Support",
"main": "index.js",
"scripts": {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment