envilder
Version:
A CLI that securely centralizes your environment variables from AWS SSM as a single source of truth
73 lines • 3.69 kB
JavaScript
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());
});
};
export class Envilder {
constructor(keyVault, envFileManager) {
this.keyVault = keyVault;
this.envFileManager = envFileManager;
}
/**
* Orchestrates the process of fetching environment variable values from a key vault and writing them to a local environment file.
*
* Loads a parameter mapping from a JSON file, retrieves existing environment variables, fetches updated values from the key vault, merges them, and writes the result to the specified environment file.
*
* @param mapPath - Path to the JSON file mapping environment variable names to key vault parameter names.
* @param envFilePath - Path to the local environment file to read and update.
*/
run(mapPath, envFilePath) {
return __awaiter(this, void 0, void 0, function* () {
try {
const requestVariables = yield this.envFileManager.loadMapFile(mapPath);
const currentVariables = yield this.envFileManager.loadEnvFile(envFilePath);
const envilded = yield this.envild(requestVariables, currentVariables);
yield this.envFileManager.saveEnvFile(envFilePath, envilded);
console.log(`Environment File generated at '${envFilePath}'`);
}
catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
console.error(`Failed to generate environment file: ${errorMessage}`);
throw error;
}
});
}
envild(paramMap, existingEnvVariables) {
return __awaiter(this, void 0, void 0, function* () {
const errors = [];
for (const [envVar, secretName] of Object.entries(paramMap)) {
const error = yield this.processSecret(envVar, secretName, existingEnvVariables);
if (error) {
errors.push(error);
}
}
if (errors.length > 0) {
throw new Error(`Some parameters could not be fetched:\n${errors.join('\n')}`);
}
return existingEnvVariables;
});
}
processSecret(envVar, secretName, existingEnvVariables) {
return __awaiter(this, void 0, void 0, function* () {
try {
const value = yield this.keyVault.getSecret(secretName);
if (!value) {
console.error(`Warning: No value found for: '${secretName}'`);
return null;
}
existingEnvVariables[envVar] = value;
console.log(`${envVar}=${value.length > 10 ? '*'.repeat(value.length - 3) + value.slice(-3) : '*'.repeat(value.length)}`);
return null;
}
catch (error) {
console.error(`Error fetching parameter: '${secretName}'`);
return `ParameterNotFound: ${secretName}`;
}
});
}
}
//# sourceMappingURL=EnvilderHandler.js.map