diff --git a/conf/myconf-production.json b/conf/myconf-production.json new file mode 100644 index 0000000..b4cfc5e --- /dev/null +++ b/conf/myconf-production.json @@ -0,0 +1,24 @@ +{ + "info": "myconf", + "database": { + "host": "db.prod.com", + "user": "ENCRYPTED|5cf569c8229d05034f173119795158fd|1d71ad3d86e402d2ebb8b2a1ef796862", + "password": "ENCRYPTED|9a4e5fdbbfe10b1d2b81a9ec9d46c6d1|95bf57f6ef988b80fa75577ed2da04c3b4549ac2de03c45fbbb8b4033b58b415" + }, + "filestorage": { + "type": "local", + "params": { + "folder": "/tmp/storage", + "storagepass": "ENCRYPTED|7a7e87414b68919f70604e9e71d38f52|872dba2abe6f3a4bc4fa89b93234a86fbb4577f4ba5aa89949e4e4d0b16111e2" + } + }, + "testarray": [ + { + "arrayItemKey": "ENCRYPTED|ad271fa483d82796e021a9c199e8b52b|420cf1c6e90590fa360a1a6fe8fb559c" + }, + { + "arrayItemKey": "ENCRYPTED|33ef4527795eafd1019bc9d00d4a4ad2|e2aa1207886da4fcc33916610d082d4b" + } + ], + "__hmac": "e35117392455eab129d8bdc57ec348be98a9d93c892382a255ce4a0d393be102" +} \ No newline at end of file diff --git a/conf/myconf.json b/conf/myconf.json new file mode 100644 index 0000000..fd9bb59 --- /dev/null +++ b/conf/myconf.json @@ -0,0 +1,35 @@ +{ + "info": "myconf", + "database": { + "host": "127.0.0.1", + "user": "ENCRYPTED|9edcd5a6bc5ed6868e6c3340019f5d3a|bc1857aab6981b903fab75ccb5c5244b", + "password": "ENCRYPTED|45aa7c597b470d24c4552ff9b7a5b919|30c26f4fb8e63f2986b1a605028b5dd8" + }, + "filestorage": { + "type": "local", + "params": { + "folder": "/tmp/storage", + "storagepass": "ENCRYPTED|6e4cf6d21bd9cfcfb124efbc100250b2|9ba4f2727eedbc428647422947555ca1" + } + }, + "testarray": [ + "one", + "two", + "three", + { + "arrayItemKey": "ENCRYPTED|ad271fa483d82796e021a9c199e8b52b|420cf1c6e90590fa360a1a6fe8fb559c", + "additionalItem1": "value1" + }, + { + "arrayItemKey": "ENCRYPTED|33ef4527795eafd1019bc9d00d4a4ad2|e2aa1207886da4fcc33916610d082d4b", + "additionalItem1": "value1", + "additionalItem2": 12 + }, + [ + { + "subArrayItemKey": "ENCRYPTED|492a92a7e47089c81d05dab652992d8a|854dcababd0e9225310a10e76d1fea88435fc4489123f82b1899169fcf9de58e" + } + ] + ], + "nullvalue": null +} \ No newline at end of file diff --git a/test/secure-config-advanced.test.js b/test/secure-config-advanced.test.js index 2c3345d..643727a 100644 --- a/test/secure-config-advanced.test.js +++ b/test/secure-config-advanced.test.js @@ -57,4 +57,47 @@ describe('secure-config adcanced features test suite (v2 features)', () => { expect(() => { require('../secure-config')({ hmacValidation: true }); }).toThrow('HMAC validation failed.'); }); + it('tests a successful production configuration retrieval with custom file prefix', () => { + process.env['CONFIG_ENCRYPTION_KEY'] = key; + const conf = require('../secure-config')({ prefix: 'myconf' }); + expect(conf.info).toEqual('myconf'); + expect(conf.database.host).toBe('127.0.0.1'); + expect(conf.database.user).toBe('SecretUser'); + expect(conf.database.password).toBe('SecretPassword'); + expect(conf.filestorage.type).toBe('local'); + expect(conf.filestorage.params.folder).toBe('/tmp/storage'); + expect(conf.filestorage.params.storagepass).toBe('StoragePassword'); + expect(conf.testarray).toBeDefined(); + expect(Array.isArray(conf.testarray)).toBeTruthy(); + expect(conf.testarray.length).toBe(6); + expect(conf.testarray[0]).toEqual('one'); + expect(conf.testarray[1]).toEqual('two'); + expect(conf.testarray[2]).toEqual('three'); + expect(conf.testarray[3].arrayItemKey).toEqual('itemValue1'); + expect(conf.testarray[3].additionalItem1).toEqual('value1'); + expect(conf.testarray[4].arrayItemKey).toEqual('itemValue2'); + expect(conf.testarray[4].additionalItem1).toEqual('value1'); + expect(conf.testarray[4].additionalItem2).toEqual(12); + expect(conf.testarray[5].length).toEqual(1); + expect(conf.testarray[5][0].subArrayItemKey).toEqual('subArrayItemValue'); + expect(conf.nullvalue).toBe(null); + }); + + it('tests a successful production configuration retrieval with custom file prefix and HMAC validation', () => { + process.env['CONFIG_ENCRYPTION_KEY'] = key; + process.env['NODE_ENV'] = 'production'; + const conf = require('../secure-config')({ prefix: 'myconf', hmacValidation: true }); + expect(conf.info).toEqual('myconf'); + expect(conf.database.host).toBe('db.prod.com'); + expect(conf.database.user).toBe('SecretUser-Prod'); + expect(conf.database.password).toBe('SecretPassword-Prod'); + expect(conf.filestorage.type).toBe('local'); + expect(conf.filestorage.params.folder).toBe('/tmp/storage'); + expect(conf.filestorage.params.storagepass).toBe('StoragePassword-Prod'); + expect(conf.testarray.length).toEqual(2); + expect(conf.testarray[0].arrayItemKey).toEqual('itemValue1'); + expect(conf.testarray[1].arrayItemKey).toEqual('itemValue2'); + + }); + }); \ No newline at end of file diff --git a/test/secure-config.test.js b/test/secure-config.test.js index 9bc6e2d..05519e4 100644 --- a/test/secure-config.test.js +++ b/test/secure-config.test.js @@ -63,6 +63,7 @@ describe('secure-config basic features test suite (v1 features)', () => { expect(conf.testarray[5].length).toEqual(1); expect(conf.testarray[5][0].subArrayItemKey).toEqual('subArrayItemValue'); expect(conf.nullvalue).toBe(null); + expect(conf.info).toBeUndefined(); }); it('tests a successful configuration retrieval without any encryption', () => {