ts-configurable
Advanced tools
Comparing version 1.0.0 to 2.0.0
# CHANGELOG | ||
## 2.0.0 | ||
- Breaking changes: options object for decryption changed: from `decryptionSecrets` to `decryption`: `secrets`, `setNullOnDecryptionFailure` | ||
- Implement setNullOnDecryptionFailure flag | ||
## 1.0.0 | ||
@@ -4,0 +8,0 @@ |
@@ -27,3 +27,3 @@ "use strict"; | ||
enforceReadonly: true, | ||
decryptionSecrets: false, | ||
decryption: false, | ||
}; | ||
@@ -119,5 +119,12 @@ return new nconf_1.Provider() | ||
} | ||
const decryptionKeys = encryption_utils_1.getDecryptionKeys(options.decryptionSecrets); | ||
// Decryption options | ||
let decryption = false; | ||
if (options.decryption) { | ||
decryption = { | ||
keys: encryption_utils_1.getDecryptionKeys(options.decryption.secrets), | ||
setNullOnDecryptionFailure: options.decryption.setNullOnDecryptionFailure, | ||
}; | ||
} | ||
const config = getConfig(options, ConfigClass, constructorOptions.config); | ||
object_utils_1.assignValuesByTemplate(this, new ConfigClass(), config, options, decryptionKeys, ConfigClass.name); | ||
object_utils_1.assignValuesByTemplate(this, new ConfigClass(), config, options, decryption, ConfigClass.name); | ||
} | ||
@@ -124,0 +131,0 @@ }; |
/// <reference types="node" /> | ||
import { DecryptionSecret } from './interfaces'; | ||
export declare function getDecryptionKeys(secrets: false | DecryptionSecret[]): null | Buffer[]; | ||
export declare function getDecryptionKeys(secrets: DecryptionSecret[]): null | Buffer[]; | ||
/** | ||
@@ -5,0 +5,0 @@ * Encrypt the given plaintext |
@@ -78,3 +78,3 @@ "use strict"; | ||
function getDecryptionKeys(secrets) { | ||
const keys = (secrets || []) | ||
const keys = secrets | ||
.filter(secret => !!secret) | ||
@@ -134,5 +134,7 @@ .map(secret => { | ||
} | ||
// Attempt decryption with all of the provided keys | ||
for (const key of keys) { | ||
try { | ||
const plaintext = decrypt(key, ciphertext); | ||
// Decryption successful, return plaintext | ||
return plaintext; | ||
@@ -142,5 +144,6 @@ } | ||
} | ||
return ciphertext; | ||
// Decryption not successful, throw an error | ||
throw Error('Could not decrypt ciphertext!'); | ||
} | ||
exports.attemptDecryption = attemptDecryption; | ||
//# sourceMappingURL=encryption-utils.js.map |
@@ -36,2 +36,8 @@ import { DotenvConfigOptions } from 'dotenv'; | ||
export declare type DecryptionSecret = IDecryptionSecretRaw | IDecryptionSecretFile | IDecryptionSecretEnvironment; | ||
export interface IDecryptionOptions { | ||
/** Decryption secrets each used to attempt the decryption of any encrypted configuration value */ | ||
secrets: DecryptionSecret[]; | ||
/** Whether to set a non-decryptable configuration value to null (default: false) */ | ||
setNullOnDecryptionFailure?: boolean; | ||
} | ||
/** Options for the @Configurable() decorator */ | ||
@@ -53,4 +59,4 @@ export interface IDecoratorOptions { | ||
loadEnvFromFile?: false | DotenvConfigOptions; | ||
/** Decryption secrets used to attempt decryption of encrypted configuration values (default: false) */ | ||
decryptionSecrets?: false | DecryptionSecret[]; | ||
/** Whether to attempt decryption of encrypted configuration values (default: false) */ | ||
decryption?: false | IDecryptionOptions; | ||
} | ||
@@ -57,0 +63,0 @@ export interface IConstructorOptions<T> { |
@@ -8,2 +8,6 @@ /// <reference types="node" /> | ||
export declare function isObject(val: any): boolean; | ||
export interface IDecryptionOptions { | ||
keys: Buffer[]; | ||
setNullOnDecryptionFailure: boolean; | ||
} | ||
/** | ||
@@ -15,4 +19,5 @@ * Assign values to an object based on a template object's properties and their respective types | ||
* @param options Options object | ||
* @param decryption Decryption options | ||
* @param parent Contains the current nested position in the object using the nested object dot notation (e.g. "obj1.nestedobj2.obj3"), used for error messages only | ||
*/ | ||
export declare function assignValuesByTemplate(obj: object, template: object, config: object, options: IDecoratorOptions, decryptionKeys: Buffer[], parent: string): void; | ||
export declare function assignValuesByTemplate(obj: object, template: object, config: object, options: IDecoratorOptions, decryption: false | IDecryptionOptions, parent: string): void; |
@@ -18,10 +18,20 @@ "use strict"; | ||
* @param options Options object | ||
* @param decryption Decryption options | ||
* @param parent Contains the current nested position in the object using the nested object dot notation (e.g. "obj1.nestedobj2.obj3"), used for error messages only | ||
*/ | ||
function assignValuesByTemplate(obj, template, config, options, decryptionKeys, parent) { | ||
function assignValuesByTemplate(obj, template, config, options, decryption, parent) { | ||
Object.keys(template).forEach(key => { | ||
const templateValue = template[key]; | ||
let value = config[key]; | ||
if (decryptionKeys && decryptionKeys.length > 0) { | ||
value = encryption_utils_1.attemptDecryption(decryptionKeys, value); | ||
if (decryption) { | ||
try { | ||
value = encryption_utils_1.attemptDecryption(decryption.keys, value); | ||
} | ||
catch (err) { | ||
// Decryption was not successful | ||
if (decryption.setNullOnDecryptionFailure) { | ||
obj[key] = null; | ||
return; | ||
} | ||
} | ||
} | ||
@@ -31,3 +41,3 @@ if (isObject(templateValue)) { | ||
obj[key] = {}; | ||
assignValuesByTemplate(obj[key], templateValue, value, options, decryptionKeys, `${parent}.${key}`); | ||
assignValuesByTemplate(obj[key], templateValue, value, options, decryption, `${parent}.${key}`); | ||
} | ||
@@ -34,0 +44,0 @@ else if (!options.strictObjectStructureChecking) { |
{ | ||
"name": "ts-configurable", | ||
"version": "1.0.0", | ||
"version": "2.0.0", | ||
"license": "MIT", | ||
@@ -5,0 +5,0 @@ "author": "Benjamin Assadsolimani (derbenoo)", |
@@ -135,4 +135,4 @@ # TS-Configurable | ||
##### options.decryptionSecrets: _`false`_ | `DecryptionSecret[]` | ||
Decryption secrets used to attempt decryption of encrypted configuration values (default: false) | ||
##### options.decryption: _`false`_ | `IDecryptionOptions` | ||
Whether to attempt decryption of encrypted configuration values (default: false) | ||
@@ -139,0 +139,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
71503
524