-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsecrets.js
45 lines (38 loc) · 1.04 KB
/
secrets.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
44
45
const aws = require('aws-sdk');
// default region to use
const region = 'us-east-1';
// create a client to access secrets
let client = new aws.SecretsManager({
region: region
});
// get the secret as a promise
function getSecret(secretId) {
return new Promise((resolve, reject) => {
client.getSecretValue({ SecretId: secretId }, (err, data) => {
if (err) {
reject(err);
} else {
resolve(data);
}
});
});
}
// get the secret - as JSON - in a promise
async function getSecretJson(secretId) {
return getSecret(secretId)
.then(data => {
if ("SecretString" in data) {
return data.SecretString;
} else {
let buf = new ArrayBuffer(data.SecretBinary, "base64");
// decode the secret
return buf.toString("ascii");
}
})
// parse the scret as JSON
.then(secret => JSON.parse(secret));
}
module.exports = {
getSecret,
getSecretJson
};