Merge branch 'budi-7010-encrypt-app-exports' into budi-7010/frontend-encrypt-app-exports

This commit is contained in:
Adria Navarro 2023-06-14 12:33:00 +01:00
commit 25eaf0feee
2 changed files with 39 additions and 10 deletions

View File

@ -120,12 +120,34 @@ export async function decryptFile(
const stretched = stretchString(secret, salt) const stretched = stretchString(secret, salt)
const decipher = crypto.createDecipheriv(ALGO, stretched, iv) const decipher = crypto.createDecipheriv(ALGO, stretched, iv)
inputFile.pipe(decipher).pipe(zlib.createGunzip()).pipe(outputFile) const unzip = zlib.createGunzip()
return new Promise<void>(r => { inputFile.pipe(decipher).pipe(unzip).pipe(outputFile)
return new Promise<void>((res, rej) => {
outputFile.on("finish", () => { outputFile.on("finish", () => {
outputFile.close() outputFile.close()
r() res()
})
inputFile.on("error", e => {
outputFile.close()
rej(e)
})
decipher.on("error", e => {
outputFile.close()
rej(e)
})
unzip.on("error", e => {
outputFile.close()
rej(e)
})
outputFile.on("error", e => {
outputFile.close()
rej(e)
}) })
}) })
} }

View File

@ -124,12 +124,19 @@ export function untarFile(file: { path: string }) {
return tmpPath return tmpPath
} }
async function decryptFiles(path: string) { async function decryptFiles(path: string, password: string) {
for (let file of fs.readdirSync(path)) { try {
const inputPath = join(path, file) for (let file of fs.readdirSync(path)) {
const outputPath = inputPath.replace(/\.enc$/, "") const inputPath = join(path, file)
await encryption.decryptFile(inputPath, outputPath, "password") const outputPath = inputPath.replace(/\.enc$/, "")
fs.rmSync(inputPath) await encryption.decryptFile(inputPath, outputPath, password)
fs.rmSync(inputPath)
}
} catch (err: any) {
if (err.message === "incorrect header check") {
throw new Error("File cannot be imported")
}
throw err
} }
} }
@ -154,7 +161,7 @@ export async function importApp(
if (template.file && (isTar || isDirectory)) { if (template.file && (isTar || isDirectory)) {
const tmpPath = isTar ? untarFile(template.file) : template.file.path const tmpPath = isTar ? untarFile(template.file) : template.file.path
if (isTar && template.file.password) { if (isTar && template.file.password) {
await decryptFiles(tmpPath) await decryptFiles(tmpPath, template.file.password)
} }
const contents = fs.readdirSync(tmpPath) const contents = fs.readdirSync(tmpPath)
// have to handle object import // have to handle object import