This commit is contained in:
2024-11-13 01:17:25 -05:00
parent 0ed24ed43b
commit 9ac157315a
10 changed files with 148 additions and 24 deletions
+107
View File
@@ -0,0 +1,107 @@
import { dynamicStringHandler } from "@helpers/dynamicStringHandler";
import { getJJConfigByKeyChain } from "@helpers/jjConfigUtils"
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 { saveFileIfItChanged } from "@helpers/saveFileIfItChanged";
const logger = makeJJlogger({
filename: 'jj/cli/secrets.ts'
})
const getSecretFolder = () => {
let secretsFolder = getJJConfigByKeyChain(`secretsFolder`)
secretsFolder = dynamicStringHandler(secretsFolder, "", process.env, false)
return secretsFolder
}
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)
}
const encryptionKey = fs.readFileSync(`${secretsFolder}/~~${envKey}`, 'utf8')
return encryptionKey
}
const setSecretFile = async ({
absFilePath,
secretKey,
secretValue
}) => {
if (!fs.existsSync(absFilePath)) {
logger.error(`Secret ${absFilePath} is not exist`)
}
console.log('asdasd')
const envKey = path.basename(path.join(absFilePath, ".."))
const secretFileObjRaw = await fs.readFile(absFilePath, 'utf8')
const secretObj = JSON.parse(secretFileObjRaw)
const encryptionKey = getEncryptionKeyForEnv(envKey)
const encryptVal = jjEncrypt(secretValue, encryptionKey)
secretObj[secretKey.toUpperCase()] = `${envKey}--${encryptVal}`
saveFileIfItChanged(JSON.stringify(secretObj, null, 4), absFilePath)
}
const secrets = async () => {
const secretFilePath = process.env.JJ_CLI_ARG_3
const absFilePath = path.resolve(process.cwd(), secretFilePath)
const envKey = path.basename(path.join(absFilePath, ".."))
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)
}
const environment = getJJConfigByKeyChain(`environments['${envKey}']`)
if (_.isEmpty(environment)) {
logger.error(`environment is not exist`)
process.exit(1)
}
if (process.env.JJ_CLI_ARG_2 == 'setSecret') {
await setSecretFile({
absFilePath,
secretKey,
secretValue
})
process.exit(0)
}
// secretsFolder = dynamicStringHandler(secretsFolder, "", process.env, false)
// const encryptionKey = getEncryptionKeyForEnv(process.env.JJ_CLI_ARG_3)
// 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)
// console.log('secrets')
}
export default secrets