183 lines
5.2 KiB
TypeScript
183 lines
5.2 KiB
TypeScript
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 { jjDecrypt, 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 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,
|
|
secretValue
|
|
}) => {
|
|
|
|
if (!fs.existsSync(absFilePath)) {
|
|
logger.error(`Secret ${absFilePath} is not exist`)
|
|
}
|
|
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)
|
|
const encryptionKey = getEncryptionKeyForEnv(envKey)
|
|
|
|
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)
|
|
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)
|
|
}
|
|
if (process.env.JJ_CLI_ARG_2 == 'decryptFile') {
|
|
|
|
await decryptFile(absFilePath)
|
|
|
|
|
|
process.exit(0)
|
|
}
|
|
if (process.env.JJ_CLI_ARG_2 == 'setSecretFromFile') {
|
|
|
|
await setSecretFileFromFile({
|
|
absFilePath,
|
|
secretKey,
|
|
secretAbsFilePath: secretValue
|
|
})
|
|
|
|
|
|
process.exit(0)
|
|
}
|
|
|
|
}
|
|
|
|
export default secrets |