wip
This commit is contained in:
+36
-19
@@ -9,25 +9,40 @@ import { saveFileIfItChanged } from "@jjHelpers/saveFileIfItChanged";
|
||||
import * as YAML from "js-yaml";
|
||||
import { subRepositoryClean, subRepositoryPull, subRepositoryPushChanges } from "@jjHelpers/gitSubRepoUtils";
|
||||
import moment from "moment";
|
||||
import inquirer from "inquirer";
|
||||
import { bashRunAndReturnSync } from "@jjHelpers/bashUtils";
|
||||
|
||||
const logger = makeJJlogger({
|
||||
filename: "jj/cli/secrets.ts",
|
||||
});
|
||||
|
||||
export const getSecretFolder = () => {
|
||||
let secretsFolder = getJJConfigByKeyChain(`secretsFolder`);
|
||||
secretsFolder = dynamicStringHandler(secretsFolder, "", process.env, false);
|
||||
return secretsFolder;
|
||||
};
|
||||
// export const getSecretFolder = () => {
|
||||
// let secretsFolder = getJJConfigByKeyChain(`secretsFolder`);
|
||||
// secretsFolder = dynamicStringHandler(secretsFolder, "", process.env, false);
|
||||
// return secretsFolder;
|
||||
// };
|
||||
|
||||
export const getEncryptionKeyForEnv = (envKey) => {
|
||||
const secretsFolder = getSecretFolder();
|
||||
if (!fs.existsSync(`${secretsFolder}/~~${envKey}`)) {
|
||||
logger.error(`Encryption key for ${envKey} env not fond and nt exist `);
|
||||
process.exit(1);
|
||||
export const getEncryptionKeyForEnv = async (envKey) => {
|
||||
// const secretsFolder = getSecretFolder();
|
||||
const secretsFolder = process.env['JJ_SECRET_TMP_DIR'];
|
||||
if (!secretsFolder) {
|
||||
throw new Error("JJ_SECRET_TMP_DIR is not set");
|
||||
}
|
||||
const encryptionKey = fs.readFileSync(`${secretsFolder}/~~${envKey}`, "utf8");
|
||||
const encryptionKeyPath = `${secretsFolder}/~~${envKey}`;
|
||||
if (!fs.existsSync(encryptionKeyPath)) {
|
||||
const { encryptionKey } = await inquirer.prompt([
|
||||
{
|
||||
type: "password",
|
||||
name: "encryptionKey",
|
||||
message: `Enter encryption key for ${envKey}:`,
|
||||
mask: "*",
|
||||
validate: (value) => value.trim().length > 0 || "Encryption key cannot be empty",
|
||||
},
|
||||
]);
|
||||
await fs.ensureDir(secretsFolder);
|
||||
await fs.writeFile(encryptionKeyPath, encryptionKey, { encoding: "utf8", mode: 0o600, flag: "wx" });
|
||||
}
|
||||
const encryptionKey = fs.readFileSync(encryptionKeyPath, "utf8");
|
||||
return encryptionKey;
|
||||
};
|
||||
|
||||
@@ -56,7 +71,9 @@ export const decryptFile = async (absFilePath) => {
|
||||
}
|
||||
const absDirPath = path.dirname(absFilePath);
|
||||
const fileName = path.basename(absFilePath).replace(".json", "");
|
||||
const decryptFileJsonPath = `${absDirPath}/~~decrypt/${fileName}.json`
|
||||
const envKey = path.basename(path.join(absFilePath, ".."));
|
||||
const decryptDir = `${process.env['JJ_SECRET_TMP_DIR']}/${envKey}/~~decrypt`
|
||||
const decryptFileJsonPath = `${decryptDir}/${fileName}.json`
|
||||
if (fs.existsSync(decryptFileJsonPath) ) {
|
||||
const isOlderThanOriginal = await isFileOlder(decryptFileJsonPath, absFilePath)
|
||||
if(isOlderThanOriginal) {
|
||||
@@ -70,10 +87,10 @@ export const decryptFile = async (absFilePath) => {
|
||||
const secretFileObjRaw = await fs.readFile(absFilePath, "utf8");
|
||||
const secretObj = JSON.parse(secretFileObjRaw);
|
||||
|
||||
const envKey = path.basename(path.join(absFilePath, ".."));
|
||||
|
||||
|
||||
const decryptedSecretObj = {};
|
||||
const encryptionKey = getEncryptionKeyForEnv(envKey);
|
||||
const encryptionKey = await getEncryptionKeyForEnv(envKey);
|
||||
Object.keys(secretObj).forEach((key) => {
|
||||
const plainValue = secretObj[key];
|
||||
if (plainValue.includes(`${envKey}--`)) {
|
||||
@@ -91,13 +108,13 @@ export const decryptFile = async (absFilePath) => {
|
||||
|
||||
|
||||
|
||||
if (!fs.existsSync(`${absDirPath}/~~decrypt`)) {
|
||||
fs.mkdirSync(`${absDirPath}/~~decrypt`, { recursive: true });
|
||||
if (!fs.existsSync(`${decryptDir}`)) {
|
||||
fs.mkdirSync(`${decryptDir}`, { recursive: true });
|
||||
}
|
||||
|
||||
const decryptedJsonFormat = `${absDirPath}/~~decrypt/${fileName}.json`;
|
||||
const decryptedJsonFormat = `${decryptDir}/${fileName}.json`;
|
||||
fs.writeFileSync(decryptedJsonFormat, JSON.stringify(decryptedSecretObj, null, 4), "utf-8");
|
||||
const decryptedShFormat = `${absDirPath}/~~decrypt/${fileName}.sh`;
|
||||
const decryptedShFormat = `${decryptDir}/${fileName}.sh`;
|
||||
fs.writeFileSync(decryptedShFormat, shFormatDecrypt, "utf-8");
|
||||
logger.success(`decrypt file-secret for ${absFilePath} done`);
|
||||
return decryptedSecretObj;
|
||||
@@ -124,7 +141,7 @@ export const setSecretFile = async ({ absFilePath, secretKey, secretValue }) =>
|
||||
const envKey = path.basename(path.join(absFilePath, ".."));
|
||||
const secretFileObjRaw = await fs.readFile(absFilePath, "utf8");
|
||||
const secretObj = JSON.parse(secretFileObjRaw);
|
||||
const encryptionKey = getEncryptionKeyForEnv(envKey);
|
||||
const encryptionKey = await getEncryptionKeyForEnv(envKey);
|
||||
|
||||
const encryptVal = jjEncrypt(secretValue, encryptionKey);
|
||||
secretObj[secretKey.toUpperCase()] = `${envKey}--${encryptVal}`;
|
||||
|
||||
Reference in New Issue
Block a user