-
Notifications
You must be signed in to change notification settings - Fork 10
/
decrypt-helper.js
43 lines (36 loc) · 1.07 KB
/
decrypt-helper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
const GoogleKMS = require('@google-cloud/kms').KeyManagementServiceClient
const decrypt = async ciphertext => {
const client = new GoogleKMS()
const locationId = process.env.GOOGLE_LOCATION_ID
const keyRingId = process.env.GOOGLE_KEYRING_ID
const cryptoKeyId = process.env.GOOGLE_KEY_ID
const projectId = process.env.GOOGLE_PROJECT_ID
const name = client.cryptoKeyPath(
projectId,
locationId,
keyRingId,
cryptoKeyId
)
return new Promise(async (resolve, reject) => {
try {
ciphertext = ciphertext.toString('base64')
const [result] = await client.decrypt({ name, ciphertext })
const buff = Buffer.from(result.plaintext, 'base64')
const text = buff.toString('ascii')
resolve(text)
} catch (e) {
reject(JSON.stringify(e))
}
})
}
module.exports.decryptVars = async () =>
new Promise(async (resolve, reject) => {
try {
process.env.MOLTIN_CLIENT_SECRET = await decrypt(
Buffer.from(process.env.MOLTIN_CLIENT_SECRET, 'base64')
)
resolve()
} catch (e) {
reject(e)
}
})