UNPKG

envilder

Version:

A CLI and GitHub Action that securely centralizes your environment variables from AWS SSM or Azure Key Vault as a single source of truth

121 lines (116 loc) 8.26 kB
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 __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; import 'reflect-metadata'; import { dirname, join } from 'node:path'; import { fileURLToPath } from 'node:url'; import { Command } from 'commander'; import pc from 'picocolors'; import { DispatchActionCommand } from '../../../core/application/dispatch/DispatchActionCommand.js'; import { PackageVersionReader } from '../../../core/infrastructure/package/PackageVersionReader.js'; import { readMapFileConfig } from '../../../core/infrastructure/variableStore/FileVariableStore.js'; import { TYPES } from '../../../core/types.js'; import { executeWithSsoRecovery } from '../recovery/SsoLoginRecovery.js'; import { Startup } from '../Startup.js'; let serviceProvider; function executeCommand(options) { return __awaiter(this, void 0, void 0, function* () { const commandHandler = serviceProvider.get(TYPES.DispatchActionCommandHandler); const command = DispatchActionCommand.fromCliOptions(options); yield commandHandler.handleCommand(command); }); } export function main() { return __awaiter(this, void 0, void 0, function* () { const program = new Command(); const version = yield readPackageVersion(); const banner = ` ${pc.green('███████╗')}${pc.cyan('███╗ ██╗')}${pc.magenta('██╗ ██╗')}${pc.yellow('██╗')}${pc.red('██╗ ')}${pc.blue('██████╗ ')}${pc.green('███████╗')}${pc.cyan('██████╗ ')} ${pc.green('██╔════╝')}${pc.cyan('████╗ ██║')}${pc.magenta('██║ ██║')}${pc.yellow('██║')}${pc.red('██║ ')}${pc.blue('██╔══██╗')}${pc.green('██╔════╝')}${pc.cyan('██╔══██╗')} ${pc.green('█████╗ ')}${pc.cyan('██╔██╗ ██║')}${pc.magenta('██║ ██║')}${pc.yellow('██║')}${pc.red('██║ ')}${pc.blue('██║ ██║')}${pc.green('█████╗ ')}${pc.cyan('██████╔╝')} ${pc.green('██╔══╝ ')}${pc.cyan('██║╚██╗██║')}${pc.magenta('╚██╗ ██╔╝')}${pc.yellow('██║')}${pc.red('██║ ')}${pc.blue('██║ ██║')}${pc.green('██╔══╝ ')}${pc.cyan('██╔══██╗')} ${pc.green('███████╗')}${pc.cyan('██║ ╚████║')}${pc.magenta(' ╚████╔╝ ')}${pc.yellow('██║')}${pc.red('███████╗')}${pc.blue('██████╔╝')}${pc.green('███████╗')}${pc.cyan('██║ ██║')} ${pc.green('╚══════╝')}${pc.cyan('╚═╝ ╚═══╝')}${pc.magenta(' ╚═══╝ ')}${pc.yellow('╚═╝')}${pc.red('╚══════╝')}${pc.blue('╚═════╝ ')}${pc.green('╚══════╝')}${pc.cyan('╚═╝ ╚═╝')} ${pc.dim('Your secrets, one command away')} ${pc.dim('aws & azure')} ${pc.yellow('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━')} ${pc.green('WORLD 1-1')} ${pc.dim('— SELECT YOUR MISSION')} ${pc.yellow('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━')} ${pc.green('>')} ${pc.bold('Generate a .env file')} ${pc.dim('(pull secrets from the cloud)')} ${pc.cyan('envilder --map=envilder.json --envfile=.env')} ${pc.magenta('>')} ${pc.bold('Sync .env back to cloud')} ${pc.dim('(push secrets up)')} ${pc.cyan('envilder --push --map=envilder.json --envfile=.env')} ${pc.red('>')} ${pc.bold('Push a single secret')} ${pc.cyan('envilder --push --key=API_KEY --value=s3cret --secret-path=/my/path')} ${pc.blue('>')} ${pc.bold('Use Azure Key Vault')} ${pc.cyan('envilder --provider=azure --map=envilder.json --envfile=.env')} `; program .name('envilder') .description(banner) .version(version) .option('--map <path>', 'Path to the JSON file with environment variable mapping (required for most commands)') .option('--envfile <path>', 'Path to the .env file to be generated or imported (required for most commands)') .option('--profile <name>', 'AWS CLI profile to use (optional)') .option('--provider <name>', 'Cloud provider to use: aws or azure (default: aws)') .option('--vault-url <url>', 'Azure Key Vault URL (overrides $config.vaultUrl in map file)') .option('--push', 'Push local .env file back to cloud provider') .option('--key <name>', 'Single environment variable name to push (only with --push)') .option('--value <value>', 'Value of the single environment variable to push (only with --push)') .option('--secret-path <path>', 'Secret path in your cloud provider for the single variable (only with --push)') .option('--ssm-path <path>', '[DEPRECATED: use --secret-path] Alias for --secret-path') .hook('preAction', (thisCommand) => { const opts = thisCommand.opts(); if (opts.ssmPath) { console.warn(pc.yellow('⚠️ --ssm-path is deprecated and will be removed in a future release. Use --secret-path instead.')); if (!opts.secretPath) { thisCommand.setOptionValue('secretPath', opts.ssmPath); } } }) .action((_a) => __awaiter(this, void 0, void 0, function* () { var { provider, vaultUrl } = _a, options = __rest(_a, ["provider", "vaultUrl"]); const fileConfig = options.map ? yield readMapFileConfig(options.map) : {}; const config = Object.assign(Object.assign(Object.assign(Object.assign({}, fileConfig), (provider && { provider })), (vaultUrl && { vaultUrl })), (options.profile && { profile: options.profile })); const infraOptions = {}; const extraHosts = process.env.ENVILDER_ALLOWED_VAULT_HOSTS; if (extraHosts) { infraOptions.allowedVaultHosts = extraHosts .split(',') .map((h) => h.trim()); infraOptions.disableChallengeResourceVerification = true; } serviceProvider = Startup.build() .configureServices() .configureInfrastructure(config, infraOptions) .create(); yield executeWithSsoRecovery(() => executeCommand(options)); })); yield program.parseAsync(process.argv); }); } function readPackageVersion() { const __filename = fileURLToPath(import.meta.url); const __dirname = dirname(__filename); const packageJsonPath = join(__dirname, '../../../../../package.json'); return new PackageVersionReader().getVersion(packageJsonPath); } //# sourceMappingURL=Cli.js.map