1 | import * as fs from 'fs-extra'
|
2 | import globby = require('globby')
|
3 | import * as path from 'path'
|
4 | import { dimGrey, yellow } from '../colors'
|
5 | import { decryptRandomIVBuffer } from '../security/crypto.util'
|
6 |
|
7 | export interface DecryptCLIOptions {
|
8 | dir: string[]
|
9 | encKey: string
|
10 | algorithm?: string
|
11 | del?: boolean
|
12 | }
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | export function secretsDecrypt(
|
19 | dir: string[],
|
20 | encKey: string,
|
21 | algorithm?: string,
|
22 | del?: boolean,
|
23 | ): void {
|
24 | const patterns = dir.map(d => `${d}/**/*.enc`)
|
25 |
|
26 | const filenames = globby.sync(patterns)
|
27 |
|
28 | filenames.forEach(filename => {
|
29 | const enc = fs.readFileSync(filename)
|
30 | const plain = decryptRandomIVBuffer(enc, encKey, algorithm)
|
31 |
|
32 | const plainFilename = filename.slice(0, filename.length - '.enc'.length)
|
33 | fs.writeFileSync(plainFilename, plain)
|
34 |
|
35 | if (del) {
|
36 | fs.unlinkSync(filename)
|
37 | }
|
38 |
|
39 | console.log(` ${path.basename(filename)} > ${path.basename(plainFilename)}`)
|
40 | })
|
41 |
|
42 | console.log(`decrypted ${yellow(filenames.length)} files in ${dimGrey(dir.join(' '))}`)
|
43 | }
|