UNPKG

envilder

Version:

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

96 lines 5.45 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()); }); }; import { inject, injectable } from 'inversify'; import { EnvironmentVariable } from '../../domain/EnvironmentVariable.js'; import { TYPES } from '../../types.js'; let PushEnvToSsmCommandHandler = class PushEnvToSsmCommandHandler { constructor(secretProvider, variableStore, logger) { this.secretProvider = secretProvider; this.variableStore = variableStore; this.logger = logger; } /** * Handles the PushEnvToSsmCommand which imports environment variables * from a local file and pushes them to AWS SSM. * Uses a map file to determine the SSM parameter path for each environment variable. * * @param command - The PushEnvToSsmCommand containing mapPath and envFilePath */ handle(command) { return __awaiter(this, void 0, void 0, function* () { try { this.logger.info(`Starting push operation from '${command.envFilePath}' using map '${command.mapPath}'`); const paramMap = yield this.loadConfiguration(command); yield this.pushVariablesToSSM(paramMap, command); this.logger.info(`Successfully pushed environment variables from '${command.envFilePath}' to AWS SSM.`); } catch (error) { const errorMessage = error instanceof Error ? error.message : String(error); this.logger.error(`Failed to push environment file: ${errorMessage}`); throw error; } }); } loadConfiguration(command) { return __awaiter(this, void 0, void 0, function* () { this.logger.info(`Loading parameter map from '${command.mapPath}'`); const paramMap = yield this.variableStore.getMapping(command.mapPath); this.logger.info(`Loading environment variables from '${command.envFilePath}'`); const envVariables = yield this.variableStore.getEnvironment(command.envFilePath); this.logger.info(`Found ${Object.keys(paramMap).length} parameter mappings in map file`); this.logger.info(`Found ${Object.keys(envVariables).length} environment variables in env file`); return { paramMap, envVariables }; }); } pushVariablesToSSM(config, command) { return __awaiter(this, void 0, void 0, function* () { const { paramMap, envVariables } = config; const keysToProcess = Object.keys(paramMap); this.logger.info(`Processing ${keysToProcess.length} environment variables to push to AWS SSM`); const variableProcessingPromises = Object.entries(paramMap).map(([envKey, ssmPath]) => { return this.processVariable(envKey, ssmPath, envVariables, command.envFilePath); }); yield Promise.all(variableProcessingPromises); }); } processVariable(envKey, ssmPath, envVariables, envFilePath) { return __awaiter(this, void 0, void 0, function* () { if (Object.hasOwn(envVariables, envKey)) { const envVariable = new EnvironmentVariable(envKey, envVariables[envKey], true); yield this.secretProvider.setSecret(ssmPath, envVariables[envKey]); this.logger.info(`Pushed ${envKey}=${envVariable.maskedValue} to AWS SSM at path ${ssmPath}`); } else { this.logger.warn(`Warning: Environment variable ${envKey} not found in ${envFilePath}`); } }); } }; PushEnvToSsmCommandHandler = __decorate([ injectable(), __param(0, inject(TYPES.ISecretProvider)), __param(1, inject(TYPES.IVariableStore)), __param(2, inject(TYPES.ILogger)), __metadata("design:paramtypes", [Object, Object, Object]) ], PushEnvToSsmCommandHandler); export { PushEnvToSsmCommandHandler }; //# sourceMappingURL=PushEnvToSsmCommandHandler.js.map