UNPKG

dotencr

Version:

Encrypt and decrypt individual lines inside a .env file. Supports multiple encryption keys. Keys and dotenv files can be read text file or read from the environment.

151 lines 7.1 kB
#!/usr/bin/env node "use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const lodash_1 = __importDefault(require("lodash")); const args_1 = require("./args"); const crypto_key_manager_1 = require("./crypto_key_manager"); const dotenv_processor_1 = require("./dotenv_processor"); const feedback_1 = require("./feedback"); const util_1 = require("./util"); const crypto_wrapper_1 = require("./crypto_wrapper"); const fs_1 = __importDefault(require("fs")); // import path from "path"; const assert_plus_1 = __importDefault(require("assert-plus")); // Entire file needs to be refactored // Begin refactoring // // export const convertCommandLineTupleToBuffer = (keyvalue: string): Buffer => { // // Convert the command line string into a buffer // // The user passed in a single param, eg. // // UNENCRYPTED__DEV__EMAIL_API=EM.74f2f09e-7fde-4de8-afd8-82bd5050cc5c // // CIPHERED__DEV__EMAIL_API=FhdQgQOC6th8fmti.IBxk8vKOTR4nYHPhDEJVB8ql7EW4rkd/tTxbXREjuMPrsoE0cbxu.JOv1f+AODXEUOUoACmgAQg== // return Buffer.from(keyvalue); // }; // export const convertCommandLineTripleToBuffer = ( // key: string, // name: string, // value: string, // isEncryption: boolean // ) => { // // The user passed in three params, eg. // // DEV EMAIL_API EM.74f2f09e-7fde-4de8-afd8-82bd5050cc5c // // DEV EMAIL_API FhdQgQOC6th8fmti.IBxk8vKOTR4nYHPhDEJVB8ql7EW4rkd/tTxbXREjuMPrsoE0cbxu.JOv1f+AODXEUOUoACmgAQg== // return isEncryption // ? Buffer.from(`UNENCRYPTED__${key}__${name}=${value}`) // : Buffer.from(`CIPHERED__${key}__${name}=${value}`); // }; // const encryptDecryptCommandLine = ( // buffer: Buffer, // isEncryption: boolean, // cryptoKeyManager: CryptoKeyManager, // forceIvToFixedValue: boolean // ): void => { // const dotenvProcessor = new DotenvProcessor(cryptoKeyManager, buffer, forceIvToFixedValue); // const res = isEncryption ? dotenvProcessor.encrypt() : dotenvProcessor.decrypt(); // // Check the results of the encryption/decryption // res.forEach((x) => { // if (x.status === StatusCryptoWrapperResult.success) { // console.log(`${x.newDotenvVariableName}=${x.result}`); // } else { // console.log(x.failureExplanation); // } // }); // }; const convertCommandLineToBuffer = (args, isEncryption) => { let buffer; // Convert the command line string into a buffer if (args.keyvalue) { // The user passed in a single param, eg. // UNENCRYPTED__DEV__EMAIL_API=EM.74f2f09e-7fde-4de8-afd8-82bd5050cc5c // CIPHERED__DEV__EMAIL_API=FhdQgQOC6th8fmti.IBxk8vKOTR4nYHPhDEJVB8ql7EW4rkd/tTxbXREjuMPrsoE0cbxu.JOv1f+AODXEUOUoACmgAQg== buffer = Buffer.from(args.keyvalue); return buffer; } assert_plus_1.default.ok(args.key && args.name && args.value); // The user passed in three params, eg. // DEV EMAIL_API EM.74f2f09e-7fde-4de8-afd8-82bd5050cc5c // DEV EMAIL_API FhdQgQOC6th8fmti.IBxk8vKOTR4nYHPhDEJVB8ql7EW4rkd/tTxbXREjuMPrsoE0cbxu.JOv1f+AODXEUOUoACmgAQg== buffer = isEncryption ? Buffer.from(`UNENCRYPTED__${args.key}__${args.name}=${args.value}`) : Buffer.from(`CIPHERED__${args.key}__${args.name}=${args.value}`); return buffer; }; const encryptDecryptCommandLine = (args, cryptoKeyManager, forceIvToFixedValue) => { const isEncryption = ["xenc", "kenc"].includes(args._[0]); let buffer = convertCommandLineToBuffer(args, isEncryption); const dotenvProcessor = new dotenv_processor_1.DotenvProcessor(cryptoKeyManager, buffer, forceIvToFixedValue); const res = isEncryption ? dotenvProcessor.encrypt() : dotenvProcessor.decrypt(); // Check the results of the encryption/decryption res.forEach((x) => { if (x.status === crypto_wrapper_1.StatusCryptoWrapperResult.success) { console.log(`${x.newDotenvVariableName}=${x.result}`); } else { console.log(x.failureExplanation); } }); }; const processDotenvFilesImplementation = (args, dotenvProcessor, dotenvOutputFileName) => { if (args._.includes("test")) { const feedback = dotenvProcessor.test(); return feedback; } let backupFilePath = undefined; if (args.backup) { backupFilePath = dotenvProcessor.createBackupFile(args.backupFolder); // create backup. Do this first. } const isEncryption = ["enc"].includes(args._[0]); const feedback = isEncryption ? dotenvProcessor.encryptToFile(dotenvOutputFileName) : dotenvProcessor.decryptToFile(dotenvOutputFileName); // Now that we have the feedback object, add any messages feedback.backupFileName = backupFilePath; return feedback_1.coerceFeedbackPathsToRelativePaths(feedback); }; const processDotenvFiles = (envPath, args, cryptoKeyManager, forceIvToFixedValue) => { // Process dotenv files // Resolve the output and backup file paths const dotenvOutputFileName = args.output ? util_1.replaceFilePath(envPath, args.output) : envPath; // Process each file const dotenvProcessor = new dotenv_processor_1.DotenvProcessor(cryptoKeyManager, envPath, forceIvToFixedValue); let feedback = processDotenvFilesImplementation(args, dotenvProcessor, dotenvOutputFileName); // Where was the file written to feedback.dotenvOutputFileName = dotenvOutputFileName; return feedback_1.coerceFeedbackPathsToRelativePaths(feedback); }; exports.mainImplementation = (args, forceIvToFixedValue) => { const cryptoKeyManager = new crypto_key_manager_1.CryptoKeyManager(); // Load the keys from the environment. Could be loaded from a dotenv file. cryptoKeyManager.loadFromEnv(); // Encrypt/decrypt a command line param or a file if (lodash_1.default.intersection(["xenc", "xdec", "kenc", "kdec"], args._).length) { encryptDecryptCommandLine(args, cryptoKeyManager, forceIvToFixedValue); } else { const files = util_1.fileGlob(args.file); if (!files.length) { console.log("No files found"); process.exitCode = 20; return; } // Feedback for all the files processed const feedbackAll = []; files.forEach((envPath) => { const feedback = processDotenvFiles(envPath, args, cryptoKeyManager, forceIvToFixedValue); feedbackAll.push(util_1.sortObjectByKeys(feedback)); }); const feedbackJson = JSON.stringify(feedbackAll, null, "\t"); // console.log(feedbackJson); fs_1.default.writeFileSync(args.jsonOutput, feedbackJson); } }; // No need to call 'mainImplementation' when running unit tests. It is called // explicitly within the unit tests. /* istanbul ignore next */ if (!process.env.JEST_WORKER_ID) { exports.mainImplementation(args_1.parseArgs(process.argv.slice(2)), false); } //# sourceMappingURL=main.js.map