@aminya/dotenv-vault
Version:
A secrets manager for .env files – from the same people that pioneered dotenv.
67 lines (66 loc) • 2.57 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.LocalDecryptService = void 0;
const log_service_1 = require("../../services/log-service");
const vars_1 = require("../../vars");
const dotenv_1 = require("dotenv");
class LocalDecryptService {
constructor(attrs = {}) {
this.cmd = attrs.cmd;
this.dotenvKey = attrs.dotenvKey;
this.log = new log_service_1.LogService({ cmd: attrs.cmd });
}
async run() {
const result = (0, dotenv_1.configDotenv)({ path: this.vaultPath });
const keys = this.dotenvKey.split(',');
const length = keys.length;
let decrypted;
for (let i = 0; i < length; i++) {
try {
// Get full key
const key = keys[i].trim();
// Get instructions for decrypt
const attrs = this._instructions(result, key);
// Decrypt
decrypted = (0, dotenv_1.decrypt)(attrs.ciphertext, attrs.key);
break;
}
catch (error) {
// last key
if (i + 1 >= length) {
throw error;
}
// try next key
}
}
this.log.plain(decrypted);
}
_instructions(result, dotenvKey) {
// Parse DOTENV_KEY. Format is a URI
const uri = new URL(dotenvKey);
// Get decrypt key
const key = uri.password;
if (!key) {
throw new Error('INVALID_DOTENV_KEY: Missing key part');
}
// Get environment
const environment = uri.searchParams.get('environment');
if (!environment) {
throw new Error('INVALID_DOTENV_KEY: Missing environment part');
}
// Get ciphertext payload
const environmentKey = `DOTENV_VAULT_${environment.toUpperCase()}`;
if (!result.parsed) {
throw new Error(`NOT_FOUND_DOTENV_ENVIRONMENT: Cannot locate environment ${environmentKey} in your .env.vault file. Run '${vars_1.vars.cli} local build' to include it.`);
}
const ciphertext = result.parsed[environmentKey]; // DOTENV_VAULT_PRODUCTION
if (!ciphertext) {
throw new Error(`NOT_FOUND_DOTENV_ENVIRONMENT: Cannot locate environment ${environmentKey} in your .env.vault file. Run '${vars_1.vars.cli} local build' to include it.`);
}
return { ciphertext, key };
}
get vaultPath() {
return '.env.vault';
}
}
exports.LocalDecryptService = LocalDecryptService;