@tsmx/secure-config
Advanced tools
Comparing version 1.0.6 to 1.1.0
@@ -18,3 +18,4 @@ { | ||
"three" | ||
] | ||
], | ||
"nullvalue": null | ||
} |
{ | ||
"name": "@tsmx/secure-config", | ||
"version": "1.0.6", | ||
"version": "1.1.0", | ||
"description": "Handling multi-environment JSON configurations with encrypted secrets. Minimalistic, zero deps.", | ||
@@ -5,0 +5,0 @@ "main": "secure-config.js", |
@@ -71,4 +71,14 @@ # [**secure-config**](https://github.com/tsmx/secure-config) | ||
The key length must be 32 bytes! Different keys for each configuration environment are strongly recommended. | ||
The key length must be 32 bytes! The value set in `CONFIG_ENCRYPTION_KEY` has to be: | ||
- a string of 32 characters length, or | ||
- a hexadecimal value of 64 characters length (= 32 bytes) | ||
Otherwise an error will be thrown. | ||
Examples of valid key strings: | ||
- 32 byte string: `MySecretConfigurationKey-123$%&/` | ||
- 32 byte hex value: `9af7d400be4705147dc724db25bfd2513aa11d6013d7bf7bdb2bfe050593bd0f` | ||
Different keys for each configuration environment are strongly recommended. | ||
## Generating encrypted entries | ||
@@ -82,3 +92,3 @@ | ||
You can simply use `crypto` functions from NodeJS with the follwing snippet to create the encrypted entries: | ||
You can simply use `crypto` functions from NodeJS with the following snippet to create the encrypted entries: | ||
@@ -85,0 +95,0 @@ ```js |
@@ -7,9 +7,17 @@ const crypto = require('crypto'); | ||
function getKey() { | ||
const hexReg = new RegExp('^[0-9A-F]{64}$', 'i'); | ||
let result = null; | ||
if (!process.env.CONFIG_ENCRYPTION_KEY) { | ||
throw new Error('Environment variable CONFIG_ENCRYPTION_KEY not set.'); | ||
} | ||
else if (process.env.CONFIG_ENCRYPTION_KEY.toString().length !== 32) { | ||
else if (process.env.CONFIG_ENCRYPTION_KEY.toString().length == 32) { | ||
result = Buffer.from(process.env.CONFIG_ENCRYPTION_KEY); | ||
} | ||
else if (hexReg.test(process.env.CONFIG_ENCRYPTION_KEY)) { | ||
result = Buffer.from(process.env.CONFIG_ENCRYPTION_KEY, 'hex'); | ||
} | ||
else { | ||
throw new Error('CONFIG_ENCRYPTION_KEY length must be 32 bytes.'); | ||
} | ||
return Buffer.from(process.env.CONFIG_ENCRYPTION_KEY); | ||
return result; | ||
} | ||
@@ -16,0 +24,0 @@ |
@@ -22,2 +22,15 @@ describe('secure-config test suite', () => { | ||
it('tests a successful configuration retrival with a hexadecimal key', async (done) => { | ||
process.env['CONFIG_ENCRYPTION_KEY'] = '9af7d400be4705147dc724db25bfd2513aa11d6013d7bf7bdb2bfe050593bd0f'; | ||
process.env['NODE_ENV'] = 'hex'; | ||
const conf = require('../secure-config'); | ||
expect(conf.database.host).toBe('db.prod.com'); | ||
expect(conf.database.user).toBe('SecretUser-Hex'); | ||
expect(conf.database.password).toBe('SecretPassword-Hex'); | ||
expect(conf.filestorage.type).toBe('local'); | ||
expect(conf.filestorage.params.folder).toBe('/tmp/storage'); | ||
expect(conf.filestorage.params.storagepass).toBe('StoragePassword-Hex'); | ||
done(); | ||
}); | ||
it('tests a successful development configuration retrival', async (done) => { | ||
@@ -36,2 +49,3 @@ process.env['CONFIG_ENCRYPTION_KEY'] = '0123456789qwertzuiopasdfghjklyxc'; | ||
expect(conf.testarray.length).toBe(3); | ||
expect(conf.nullvalue).toBe(null); | ||
done(); | ||
@@ -38,0 +52,0 @@ }); |
16287
11
238
151
25