WIP
This commit is contained in:
+69
-9
@@ -4,8 +4,10 @@ import { makeJJlogger } from "@helpers/jjLogger";
|
|||||||
import _ from 'lodash';
|
import _ from 'lodash';
|
||||||
import fs from 'fs-extra';
|
import fs from 'fs-extra';
|
||||||
import path from "path";
|
import path from "path";
|
||||||
import { jjDecrypt, jjEncrypt } from "@helpers/cryptoUtils";
|
import { cryptoHashHex, generateHasDir, generateMD5, jjDecrypt, jjEncrypt } from "@helpers/cryptoUtils";
|
||||||
import { saveFileIfItChanged } from "@helpers/saveFileIfItChanged";
|
import { saveFileIfItChanged } from "@helpers/saveFileIfItChanged";
|
||||||
|
import * as YAML from "js-yaml";
|
||||||
|
import { subRepositoryClean } from "@helpers/gitSubRepoUtils";
|
||||||
|
|
||||||
const logger = makeJJlogger({
|
const logger = makeJJlogger({
|
||||||
filename: 'jj/cli/secrets.ts'
|
filename: 'jj/cli/secrets.ts'
|
||||||
@@ -114,15 +116,76 @@ const setSecretFile = async ({
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
const commitSecretMeta = async () => {
|
||||||
|
|
||||||
|
const metadataRepoFolderRelative = getJJConfigByKeyChain(`metadataRepo.path`)
|
||||||
|
await subRepositoryClean(metadataRepoFolderRelative)
|
||||||
|
|
||||||
|
logger.log(`commitSecretMeta for ${process.env.JJ_CLI_SELECTED_COMMIT} start`)
|
||||||
|
|
||||||
|
const environments = getJJConfigByKeyChain(`environments`)
|
||||||
|
|
||||||
|
const environmentsKeys = Object.keys(environments)
|
||||||
|
|
||||||
|
const metadataSecretPathBase = `${process.env.JJ_WORKSPACE_DIR}/${metadataRepoFolderRelative}/metadata/${process.env.JJ_CLI_SELECTED_COMMIT}`
|
||||||
|
const secretsBasePath = `${process.env.JJ_WORKSPACE_DIR}/${metadataRepoFolderRelative}/secretsHash`
|
||||||
|
|
||||||
|
for (const environmentKey of environmentsKeys) {
|
||||||
|
|
||||||
|
const envMetadataFolder = `${metadataSecretPathBase}/${environmentKey}`
|
||||||
|
if (!fs.existsSync(envMetadataFolder)) {
|
||||||
|
fs.mkdirSync(envMetadataFolder, { recursive: true })
|
||||||
|
}
|
||||||
|
const secretsMetadataPath = `${envMetadataFolder}/jj_secrets.yaml`
|
||||||
|
if (!fs.existsSync(secretsMetadataPath)) {
|
||||||
|
await fs.writeFile(secretsMetadataPath, YAML.dump({}, {}), "utf8")
|
||||||
|
}
|
||||||
|
const currentMetadataSecretsObj = YAML.load(await fs.readFile(secretsMetadataPath, "utf8")) || {}
|
||||||
|
|
||||||
|
const secretsHashEnvPath = `${secretsBasePath}/${environmentKey}`
|
||||||
|
if (!fs.existsSync(secretsHashEnvPath)) {
|
||||||
|
fs.mkdirSync(secretsHashEnvPath, { recursive: true })
|
||||||
|
}
|
||||||
|
const currentSecretsEnvPath = `${process.env.JJ_WORKSPACE_DIR}/secrets/${environmentKey}`
|
||||||
|
|
||||||
|
let jsonSourceFormatEncryptedSecrets = fs.readdirSync(currentSecretsEnvPath)
|
||||||
|
jsonSourceFormatEncryptedSecrets = jsonSourceFormatEncryptedSecrets.filter((file) => file.includes(".json"))
|
||||||
|
|
||||||
|
for (const jsonSourceFormatEncryptedSecret of jsonSourceFormatEncryptedSecrets) {
|
||||||
|
const encryptedSecretFilePath = `${currentSecretsEnvPath}/${jsonSourceFormatEncryptedSecret}`
|
||||||
|
const encryptedSecretFileHash = (await generateHasDir(encryptedSecretFilePath)).hash
|
||||||
|
const secretFileNameWithoutExtension = jsonSourceFormatEncryptedSecret.split(".json")[0]
|
||||||
|
const secretHashFileNameWithoutExtension = `${secretFileNameWithoutExtension}__${encryptedSecretFileHash}`
|
||||||
|
const secretHashFileName = `${secretHashFileNameWithoutExtension}.json`
|
||||||
|
const secretsHashFile = `${secretsHashEnvPath}/${secretHashFileName}`
|
||||||
|
if (!fs.existsSync(secretsHashFile)) {
|
||||||
|
fs.copyFileSync(encryptedSecretFilePath, secretsHashFile)
|
||||||
|
}
|
||||||
|
_.set(currentMetadataSecretsObj, `secrets.${environmentKey}.${secretFileNameWithoutExtension}`, encryptedSecretFileHash)
|
||||||
|
await fs.writeFile(secretsMetadataPath, YAML.dump(currentMetadataSecretsObj, {}), "utf8")
|
||||||
|
|
||||||
|
// const secretHash = hash7((await generateDirectoryeHash(encryptedSecretFilePath)).hash)
|
||||||
|
// let secretName = jsonSourceFormatEncryptedSecret.split(".json")[0]
|
||||||
|
// const configSecretName = `${secretName}_${secretHash}`
|
||||||
|
// const configFile = `${environmentSecretsFolder}/${configSecretName}.json`
|
||||||
|
|
||||||
|
// if (!fs.existsSync(configFile)) {
|
||||||
|
// fs.copyFileSync(encryptedSecretFilePath, configFile)
|
||||||
|
// }
|
||||||
|
|
||||||
|
// _.set(currentCommitMetadataSecrets, `secrets.${environmentToPush}.${secretName}`, secretHash)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.success(`commitSecretMeta for ${process.env.JJ_CLI_SELECTED_COMMIT} done`)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
const secrets = async () => {
|
const secrets = async () => {
|
||||||
|
|
||||||
if (process.env.JJ_CLI_ARG_2 == 'commitSecretMeta') {
|
if (process.env.JJ_CLI_ARG_2 == 'commitSecretMeta') {
|
||||||
|
|
||||||
|
await commitSecretMeta()
|
||||||
// TODO: Jason
|
|
||||||
console.log(process.env)
|
|
||||||
|
|
||||||
process.exit(0)
|
process.exit(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -135,10 +198,6 @@ const secrets = async () => {
|
|||||||
const secretKey = process.env.JJ_CLI_ARG_4
|
const secretKey = process.env.JJ_CLI_ARG_4
|
||||||
const secretValue = process.env.JJ_CLI_ARG_5
|
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}']`)
|
const environment = getJJConfigByKeyChain(`environments['${envKey}']`)
|
||||||
|
|
||||||
@@ -181,3 +240,4 @@ const secrets = async () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export default secrets
|
export default secrets
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,9 @@
|
|||||||
|
|
||||||
import AES from 'crypto-js/AES';
|
import AES from 'crypto-js/AES';
|
||||||
import CryptoJS from 'crypto-js';
|
import CryptoJS from 'crypto-js';
|
||||||
|
import crypto from "crypto"
|
||||||
|
import * as hashFolderModule from "folder-hash"
|
||||||
|
|
||||||
|
|
||||||
export const jjDecrypt = (encryptedText, key) => {
|
export const jjDecrypt = (encryptedText, key) => {
|
||||||
const bytes = CryptoJS.AES.decrypt(encryptedText, key);
|
const bytes = CryptoJS.AES.decrypt(encryptedText, key);
|
||||||
@@ -14,3 +17,57 @@ export const jjEncrypt = (text, key) => {
|
|||||||
return AES.encrypt(text, key).toString();
|
return AES.encrypt(text, key).toString();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const generateMD5 = (input) => {
|
||||||
|
return crypto.createHash("md5").update(input).digest("hex");
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
export function generateSha1(str) {
|
||||||
|
return crypto.createHash("sha1").update(str).digest("hex")
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
export async function generateHasDir(dir, excludeFoldersExtra = [], excludeFilesExtra = []) {
|
||||||
|
// @see https://www.npmjs.com/package/folder-hash
|
||||||
|
let sourceHashes = null
|
||||||
|
|
||||||
|
try {
|
||||||
|
sourceHashes = await hashFolderModule.hashElement(dir, {
|
||||||
|
encoding: "hex",
|
||||||
|
folders: {
|
||||||
|
ignoreRootName: true,
|
||||||
|
exclude: [
|
||||||
|
"node_modules",
|
||||||
|
"dist",
|
||||||
|
"build",
|
||||||
|
".next",
|
||||||
|
".git",
|
||||||
|
"*~~",
|
||||||
|
...excludeFoldersExtra
|
||||||
|
],
|
||||||
|
},
|
||||||
|
files: {
|
||||||
|
exclude: [
|
||||||
|
".DS_Store",
|
||||||
|
"**.DS_Store",
|
||||||
|
".env",
|
||||||
|
...excludeFilesExtra
|
||||||
|
],
|
||||||
|
matchBasename: true,
|
||||||
|
matchPath: false,
|
||||||
|
ignoreRootName: true,
|
||||||
|
},
|
||||||
|
symbolicLinks: {
|
||||||
|
ignoreTargetPath: false,
|
||||||
|
ignoreTargetContent: true,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
} catch (err) {
|
||||||
|
if (err.code === "EMFILE") {
|
||||||
|
throw new Error(`Too many open file descriptors! Increase the max open file descriptors limit for your operating system.`)
|
||||||
|
}
|
||||||
|
throw err
|
||||||
|
}
|
||||||
|
|
||||||
|
return sourceHashes
|
||||||
|
}
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
import { bashRunAndReturnSync } from "./bashUtils"
|
||||||
|
import { makeJJlogger } from "./jjLogger"
|
||||||
|
|
||||||
|
const logger = makeJJlogger({
|
||||||
|
filename: 'helpers/gitSubRepoUtils.ts'
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
|
export const subRepositoryClean = async (subRepoRelPath) => {
|
||||||
|
const repo = `${process.env.JJ_WORKSPACE_DIR}/${subRepoRelPath}`
|
||||||
|
logger.log(`clean repository ${repo} start`)
|
||||||
|
const command = `
|
||||||
|
pushd "${repo}" > /dev/null
|
||||||
|
git stash --include-untracked || true
|
||||||
|
git clean -f -X -e ".~envFormat" -e ".~jsonFormat" -e ".~shellFormat" || true
|
||||||
|
git clean -f -x -e ".~envFormat" -e ".~jsonFormat" -e ".~shellFormat" || true
|
||||||
|
git clean -xdf -e ".~envFormat" -e ".~jsonFormat" -e ".~shellFormat" || true
|
||||||
|
git reset --hard || true
|
||||||
|
|
||||||
|
popd > /dev/null
|
||||||
|
`
|
||||||
|
const res = await bashRunAndReturnSync({
|
||||||
|
command: ""
|
||||||
|
})
|
||||||
|
|
||||||
|
logger.log(res)
|
||||||
|
logger.success(`clean repository ${repo} done`)
|
||||||
|
}
|
||||||
@@ -78,7 +78,7 @@ export const jjCliYargs = (cli = true) => {
|
|||||||
envsObj[`JJ_CLI_OPTIONS`] = JSON.stringify(allOptionsMap)
|
envsObj[`JJ_CLI_OPTIONS`] = JSON.stringify(allOptionsMap)
|
||||||
|
|
||||||
if(!envsObj['JJ_CLI_OPTION_COMMIT']){
|
if(!envsObj['JJ_CLI_OPTION_COMMIT']){
|
||||||
envsObj['JJ_CLI_SELECTED_COMMIT'] = envsObj['JJ_WORKSPACE_CURRENT_COMMIT']
|
envsObj['JJ_CLI_SELECTED_COMMIT'] = process.env['JJ_WORKSPACE_CURRENT_COMMIT']
|
||||||
} else {
|
} else {
|
||||||
envsObj['JJ_CLI_SELECTED_COMMIT'] = envsObj['JJ_CLI_OPTION_COMMIT']
|
envsObj['JJ_CLI_SELECTED_COMMIT'] = envsObj['JJ_CLI_OPTION_COMMIT']
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,6 +11,7 @@
|
|||||||
"chalk": "^5.3.0",
|
"chalk": "^5.3.0",
|
||||||
"crypto-js": "^4.2.0",
|
"crypto-js": "^4.2.0",
|
||||||
"flatted": "^3.3.1",
|
"flatted": "^3.3.1",
|
||||||
|
"folder-hash": "^4.0.4",
|
||||||
"fs-extra": "^11.2.0",
|
"fs-extra": "^11.2.0",
|
||||||
"inquirer": "^12.0.1",
|
"inquirer": "^12.0.1",
|
||||||
"lodash": "^4.17.21",
|
"lodash": "^4.17.21",
|
||||||
|
|||||||
Reference in New Issue
Block a user