Skip to content

Commit

Permalink
Merge branch '2-dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
Aschen committed Jan 19, 2021
2 parents 9f387c9 + 03004c5 commit 6dba24b
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 24 deletions.
7 changes: 6 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "kuzzle-vault",
"version": "2.0.0",
"version": "2.0.1",
"description": "Share and version sensitive data with your team by using cryptography.",
"main": "build/src/index.js",
"types": "build/src/index.d.ts",
Expand All @@ -25,7 +25,9 @@
"url": "https://github.com/kuzzleio/kuzzle-vault/issues"
},
"homepage": "https://github.com/kuzzleio/kuzzle-vault#readme",
"dependencies": {},
"dependencies": {
"yaml": "^1.10.0"
},
"devDependencies": {
"@types/mocha": "^7.0.2",
"@types/mock-fs": "^4.10.0",
Expand Down
34 changes: 18 additions & 16 deletions src/Cryptonomicon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,25 +56,27 @@ export default class Cryptonomicon {
*
* @returns {Object} Object with decrypted values
*/
decryptObject (encryptedSecrets: any, path?: string): {} {
const secrets: any = {};
decryptObject (encryptedSecrets: any): {} {
if (Array.isArray(encryptedSecrets)) {
const secrets: any = [];

for (const value of Object.values(encryptedSecrets)) {
secrets.push(
typeof value === 'string'
? this.decryptString(value)
: this.decryptObject(value)
);
}

for (const key of Object.keys(encryptedSecrets)) {
const value: string|any = encryptedSecrets[key];
return secrets;
}

const currentPath = [path, key].filter(e => e).join('.');
const secrets: any = {}

if (value && typeof value === 'object' && !Array.isArray(value)) {
secrets[key] = this.decryptObject(value, currentPath);
}
else if (typeof value === 'string') {
try {
secrets[key] = this.decryptString(value);
}
catch (error) {
throw new Error(`Error when decrypting "${currentPath}": ${error.message}`);
}
}
for (const [key, value] of Object.entries(encryptedSecrets)) {
secrets[key] = typeof value === 'string'
? this.decryptString(value)
: this.decryptObject(value);
}

return secrets;
Expand Down
25 changes: 20 additions & 5 deletions src/Vault.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@

'use strict';

import * as fs from 'fs'
import Cryptonomicon from './Cryptonomicon'
import * as fs from 'fs';

import * as YAML from 'yaml';

import Cryptonomicon from './Cryptonomicon';

export default class Vault {
public cryptonomicon: Cryptonomicon;
Expand Down Expand Up @@ -54,7 +57,16 @@ export default class Vault {
this.secrets = {};
}

decrypt (encryptedVaultPath: string): void {
/**
* Decrypt the provided file with the vault key
*
* @param encryptedVaultPath Path to the encrypted file
* @param options
* - `format`: encrypted file format, either `json` (default) or `yaml`
*/
decrypt (encryptedVaultPath: string, options?: { format?: 'json' | 'yaml' }): {} {
const { format } = options || { format: 'json' };

if (this.cryptonomicon.emptyKey) {
throw new Error('No Vault key provided');
}
Expand All @@ -63,16 +75,19 @@ export default class Vault {
throw new Error(`Unable to find vault at "${encryptedVaultPath}"`);
}

const parser = format === 'json' ? JSON.parse : YAML.parse;

let encryptedSecrets;
try {
encryptedSecrets = JSON.parse(fs.readFileSync(encryptedVaultPath, 'utf-8'));

encryptedSecrets = parser(fs.readFileSync(encryptedVaultPath, 'utf-8'));
}
catch (error) {
throw new Error(`Cannot parse encrypted secrets from file "${encryptedVaultPath}": ${error.message}`);
}

this.secrets = this.cryptonomicon.decryptObject(encryptedSecrets);

return this.secrets;
}
}

0 comments on commit 6dba24b

Please sign in to comment.