WIP
This commit is contained in:
+93
-17
@@ -4,8 +4,7 @@ import { makeJJlogger } from "@helpers/jjLogger";
|
||||
import _ from 'lodash';
|
||||
import fs from 'fs-extra';
|
||||
import path from "path";
|
||||
import { existsSync } from 'fs';
|
||||
import { jjEncrypt } from "@helpers/cryptoUtils";
|
||||
import { jjDecrypt, jjEncrypt } from "@helpers/cryptoUtils";
|
||||
import { saveFileIfItChanged } from "@helpers/saveFileIfItChanged";
|
||||
|
||||
const logger = makeJJlogger({
|
||||
@@ -31,6 +30,67 @@ const getEncryptionKeyForEnv = (envKey) => {
|
||||
}
|
||||
|
||||
|
||||
const decryptFile = async (absFilePath) => {
|
||||
if (!fs.existsSync(absFilePath)) {
|
||||
logger.error(`Secret ${absFilePath} is not exist`)
|
||||
}
|
||||
logger.log(`decrypt file-secret for ${absFilePath} start`)
|
||||
|
||||
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)
|
||||
Object.keys(secretObj).forEach((key) => {
|
||||
const plainValue = secretObj[key]
|
||||
if (plainValue.includes(`${envKey}--`)) {
|
||||
decryptedSecretObj[key] = jjDecrypt(plainValue.replace(`${envKey}--`, ""), encryptionKey)
|
||||
return
|
||||
}
|
||||
decryptedSecretObj[key] = plainValue
|
||||
return
|
||||
})
|
||||
|
||||
let shFormatDecrypt = ""
|
||||
Object.keys(decryptedSecretObj).forEach((key) => {
|
||||
shFormatDecrypt += `export ${key}="${decryptedSecretObj[key]}"\n`
|
||||
})
|
||||
|
||||
const absDirPath = path.dirname(absFilePath);
|
||||
const fileName = path.basename(absFilePath).replace('.json', '');
|
||||
|
||||
if (!fs.existsSync(`${absDirPath}/~~decrypt`)) {
|
||||
fs.mkdirSync(`${absDirPath}/~~decrypt`, { recursive: true })
|
||||
}
|
||||
|
||||
const decryptedJsonFormat = `${absDirPath}/~~decrypt/${fileName}.json`
|
||||
fs.writeFileSync(decryptedJsonFormat, JSON.stringify(decryptedSecretObj, null, 4), 'utf-8');
|
||||
const decryptedShFormat = `${absDirPath}/~~decrypt/${fileName}.sh`
|
||||
fs.writeFileSync(decryptedShFormat, shFormatDecrypt, 'utf-8');
|
||||
logger.success(`decrypt file-secret for ${absFilePath} done`)
|
||||
}
|
||||
|
||||
|
||||
const setSecretFileFromFile = async ({
|
||||
absFilePath,
|
||||
secretKey,
|
||||
secretAbsFilePath
|
||||
}) => {
|
||||
|
||||
if (!fs.existsSync(secretAbsFilePath)) {
|
||||
logger.error(`Secret original ${secretAbsFilePath} is not exist`)
|
||||
}
|
||||
|
||||
const secretValue = fs.readFileSync(secretAbsFilePath, 'utf8')
|
||||
return await setSecretFile({
|
||||
absFilePath,
|
||||
secretKey,
|
||||
secretValue
|
||||
})
|
||||
}
|
||||
|
||||
const setSecretFile = async ({
|
||||
absFilePath,
|
||||
secretKey,
|
||||
@@ -40,7 +100,7 @@ const setSecretFile = async ({
|
||||
if (!fs.existsSync(absFilePath)) {
|
||||
logger.error(`Secret ${absFilePath} is not exist`)
|
||||
}
|
||||
console.log('asdasd')
|
||||
logger.log(`adding secret for ${absFilePath} with Key ${secretKey.toUpperCase()} start`)
|
||||
const envKey = path.basename(path.join(absFilePath, ".."))
|
||||
const secretFileObjRaw = await fs.readFile(absFilePath, 'utf8')
|
||||
const secretObj = JSON.parse(secretFileObjRaw)
|
||||
@@ -49,12 +109,24 @@ const setSecretFile = async ({
|
||||
const encryptVal = jjEncrypt(secretValue, encryptionKey)
|
||||
secretObj[secretKey.toUpperCase()] = `${envKey}--${encryptVal}`
|
||||
saveFileIfItChanged(JSON.stringify(secretObj, null, 4), absFilePath)
|
||||
logger.success(`adding secret for ${absFilePath} with Key ${secretKey.toUpperCase()} done`)
|
||||
await decryptFile(absFilePath)
|
||||
}
|
||||
|
||||
|
||||
|
||||
const secrets = async () => {
|
||||
|
||||
if (process.env.JJ_CLI_ARG_2 == 'commitSecretMeta') {
|
||||
|
||||
|
||||
// TODO: Jason
|
||||
console.log(process.env)
|
||||
|
||||
process.exit(0)
|
||||
}
|
||||
|
||||
|
||||
const secretFilePath = process.env.JJ_CLI_ARG_3
|
||||
|
||||
const absFilePath = path.resolve(process.cwd(), secretFilePath)
|
||||
@@ -63,10 +135,10 @@ const secrets = async () => {
|
||||
const secretKey = process.env.JJ_CLI_ARG_4
|
||||
const secretValue = process.env.JJ_CLI_ARG_5
|
||||
|
||||
if (!envKey || !secretKey || !secretValue) {
|
||||
logger.error(`!envKey || !secretKey || !secretValue`)
|
||||
process.exit(1)
|
||||
}
|
||||
// if (!envKey || !secretKey || !secretValue) {
|
||||
// logger.error(`!envKey || !secretKey || !secretValue`)
|
||||
// process.exit(1)
|
||||
// }
|
||||
|
||||
const environment = getJJConfigByKeyChain(`environments['${envKey}']`)
|
||||
|
||||
@@ -87,21 +159,25 @@ const secrets = async () => {
|
||||
|
||||
process.exit(0)
|
||||
}
|
||||
if (process.env.JJ_CLI_ARG_2 == 'decryptFile') {
|
||||
|
||||
await decryptFile(absFilePath)
|
||||
|
||||
|
||||
// secretsFolder = dynamicStringHandler(secretsFolder, "", process.env, false)
|
||||
// const encryptionKey = getEncryptionKeyForEnv(process.env.JJ_CLI_ARG_3)
|
||||
process.exit(0)
|
||||
}
|
||||
if (process.env.JJ_CLI_ARG_2 == 'setSecretFromFile') {
|
||||
|
||||
// console.log(encryptionKey)
|
||||
|
||||
// console.log(secretsFolder)
|
||||
|
||||
// console.log(process.env.JJ_CLI_ARG_2)
|
||||
// console.log(process.env.JJ_CLI_ARG_4)
|
||||
// console.log(process.env.JJ_CLI_ARG_5)
|
||||
await setSecretFileFromFile({
|
||||
absFilePath,
|
||||
secretKey,
|
||||
secretAbsFilePath: secretValue
|
||||
})
|
||||
|
||||
|
||||
// console.log('secrets')
|
||||
process.exit(0)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export default secrets
|
||||
Reference in New Issue
Block a user