UNPKG

envilder

Version:

A CLI that securely centralizes your environment variables from AWS SSM as a single source of truth

115 lines 6.14 kB
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var __param = (this && this.__param) || function (paramIndex, decorator) { return function (target, key) { decorator(target, key, paramIndex); } }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var PullSsmToEnvCommandHandler_1; import { inject, injectable } from 'inversify'; import { EnvironmentVariable } from '../../domain/EnvironmentVariable.js'; import { TYPES } from '../../types.js'; let PullSsmToEnvCommandHandler = PullSsmToEnvCommandHandler_1 = class PullSsmToEnvCommandHandler { constructor(secretProvider, variableStore, logger) { this.secretProvider = secretProvider; this.variableStore = variableStore; this.logger = logger; } /** * Handles the PullSsmToEnvCommand which orchestrates the process of fetching * environment variable values from a key vault and writing them to a local environment file. * * @param command - The PullSsmToEnvCommand containing mapPath and envFilePath */ handle(command) { return __awaiter(this, void 0, void 0, function* () { try { const { requestVariables, currentVariables } = yield this.loadVariables(command); const envilded = yield this.envild(requestVariables, currentVariables); yield this.saveEnvFile(command.envFilePath, envilded); this.logger.info(`${PullSsmToEnvCommandHandler_1.SUCCESS_MESSAGES.ENV_GENERATED}'${command.envFilePath}'`); } catch (_error) { const errorMessage = _error instanceof Error ? _error.message : String(_error); this.logger.error(`${PullSsmToEnvCommandHandler_1.ERROR_MESSAGES.FETCH_FAILED}${errorMessage}`); throw _error; } }); } loadVariables(command) { return __awaiter(this, void 0, void 0, function* () { const requestVariables = yield this.variableStore.getMapping(command.mapPath); const currentVariables = yield this.variableStore.getEnvironment(command.envFilePath); return { requestVariables, currentVariables }; }); } saveEnvFile(envFilePath, variables) { return __awaiter(this, void 0, void 0, function* () { yield this.variableStore.saveEnvironment(envFilePath, variables); }); } envild(paramMap, existingEnvVariables) { return __awaiter(this, void 0, void 0, function* () { const secretProcessingPromises = Object.entries(paramMap).map((_a) => __awaiter(this, [_a], void 0, function* ([envVar, secretName]) { return this.processSecret(envVar, secretName, existingEnvVariables); })); const results = yield Promise.all(secretProcessingPromises); const errors = results.filter((error) => error !== null); if (errors.length > 0) { throw new Error(`${PullSsmToEnvCommandHandler_1.ERROR_MESSAGES.PARAM_NOT_FOUND}${errors.join('\n')}`); } return existingEnvVariables; }); } processSecret(envVar, secretName, existingEnvVariables) { return __awaiter(this, void 0, void 0, function* () { try { const value = yield this.secretProvider.getSecret(secretName); if (!value) { this.logger.warn(`${PullSsmToEnvCommandHandler_1.ERROR_MESSAGES.NO_VALUE_FOUND}'${secretName}'`); return null; } existingEnvVariables[envVar] = value; const envVariable = new EnvironmentVariable(envVar, value, true); this.logger.info(`${envVariable.name}=${envVariable.maskedValue}`); return null; } catch (_error) { this.logger.error(`${PullSsmToEnvCommandHandler_1.ERROR_MESSAGES.ERROR_FETCHING}'${secretName}'`); return `ParameterNotFound: ${secretName}`; } }); } }; PullSsmToEnvCommandHandler.ERROR_MESSAGES = { FETCH_FAILED: 'Failed to generate environment file: ', PARAM_NOT_FOUND: 'Some parameters could not be fetched:\n', NO_VALUE_FOUND: 'Warning: No value found for: ', ERROR_FETCHING: 'Error fetching parameter: ', }; PullSsmToEnvCommandHandler.SUCCESS_MESSAGES = { ENV_GENERATED: 'Environment File generated at ', }; PullSsmToEnvCommandHandler = PullSsmToEnvCommandHandler_1 = __decorate([ injectable(), __param(0, inject(TYPES.ISecretProvider)), __param(1, inject(TYPES.IVariableStore)), __param(2, inject(TYPES.ILogger)), __metadata("design:paramtypes", [Object, Object, Object]) ], PullSsmToEnvCommandHandler); export { PullSsmToEnvCommandHandler }; //# sourceMappingURL=PullSsmToEnvCommandHandler.js.map