@aminya/dotenv-vault
Version:
A secrets manager for .env files – from the same people that pioneered dotenv.
101 lines (100 loc) • 4.21 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.RotatekeyService = void 0;
const tslib_1 = require("tslib");
const chalk_1 = tslib_1.__importDefault(require("chalk"));
const axios_1 = tslib_1.__importDefault(require("axios"));
const vars_1 = require("../vars");
const core_1 = require("@oclif/core");
const log_service_1 = require("../services/log-service");
const abort_service_1 = require("../services/abort-service");
const login_service_1 = require("../services/login-service");
class RotatekeyService {
constructor(attrs = {}) {
this.cmd = attrs.cmd;
this.environment = attrs.environment;
this.dotenvMe = attrs.dotenvMe;
this.yes = attrs.yes;
this.log = new log_service_1.LogService({ cmd: attrs.cmd });
this.abort = new abort_service_1.AbortService({ cmd: attrs.cmd });
this.login = new login_service_1.LoginService({ cmd: attrs.cmd, dotenvMe: null, yes: this.yes });
}
async run() {
if (vars_1.vars.missingEnvVault) {
this.abort.missingEnvVault();
}
if (vars_1.vars.emptyEnvVault) {
this.abort.emptyEnvVault();
}
if (vars_1.vars.missingEnvMe(this.dotenvMe)) {
await this.login.login(false);
}
if (vars_1.vars.emptyEnvMe(this.dotenvMe)) {
await this.login.login(false);
}
if (!this.yes) {
const answer = await core_1.CliUx.ux.prompt(`${chalk_1.default.dim(this.log.pretextLocal)}Are you sure you want to rotate your ${this.environment} DOTENV_KEY? Type ${chalk_1.default.green('yes')} to continue`);
if (answer !== 'yes' && answer !== 'YES' && answer !== 'Yes') {
this.abort.quit();
}
}
const rotatekeyMsg = 'Rotating decryption key';
core_1.CliUx.ux.action.start(`${chalk_1.default.dim(this.log.pretextRemote)}${rotatekeyMsg}`);
await this.rotatekey();
}
async rotatekey() {
const options = {
method: 'POST',
headers: { 'content-type': 'application/json' },
data: {
DOTENV_VAULT: vars_1.vars.vaultValue,
DOTENV_ME: this.meUid,
environment: this.environment,
},
url: this.url,
};
try {
const resp = await (0, axios_1.default)(options);
const DOTENV_KEY = resp.data.data.DOTENV_KEY;
const PREVIOUS_DOTENV_KEY = resp.data.data.PREVIOUS_DOTENV_KEY;
core_1.CliUx.ux.action.stop();
this.log.plain(DOTENV_KEY);
this.log.plain('');
this.log.plain('1. Update DOTENV_KEY - comma-append the new value');
this.log.plain(`2. Rebuild (${vars_1.vars.cli} build)`);
this.log.plain('3. Deploy (git push)');
this.log.plain('4. Update DOTENV_KEY - remove the old value');
this.log.plain('');
this.log.plain('Example:');
this.log.plain(`DOTENV_KEY='${PREVIOUS_DOTENV_KEY},${DOTENV_KEY}'`);
}
catch (error) {
core_1.CliUx.ux.action.stop('aborting');
let errorMessage = null;
let errorCode = 'ROTATEKEY_ERROR';
let suggestions = [];
errorMessage = error;
if (error.response) {
errorMessage = error.response.data;
if (error.response.data && error.response.data.errors && error.response.data.errors[0]) {
const error1 = error.response.data.errors[0];
errorMessage = error1.message;
if (error1.code) {
errorCode = error1.code;
}
if (error1.suggestions) {
suggestions = error1.suggestions;
}
}
}
this.abort.error(errorMessage, { code: errorCode, ref: '', suggestions: suggestions });
}
}
get url() {
return vars_1.vars.apiUrl + '/rotatekey';
}
get meUid() {
return this.dotenvMe || vars_1.vars.meValue;
}
}
exports.RotatekeyService = RotatekeyService;