-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSecretsManager.js
55 lines (48 loc) · 2.26 KB
/
SecretsManager.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
46
47
48
49
50
51
52
53
54
55
/**
* Reference example for AWS secrete manager https://github.com/awsdocs/aws-doc-sdk-examples/blob/master/javascript/example_code/secrets/secrets_getsecretvalue.js
* Created wrapper class on reference
*
**/
'use strict'
const AWS = require('aws-sdk');
class SecretsManager {
/**
* Uses AWS Secrets Manager to retrieve a secret
*/
static async getSecret (secretName, region){
const config = { region : region }
var secret, decodedBinarySecret;
let secretsManager = new AWS.SecretsManager(config);
try {
let secretValue = await secretsManager.getSecretValue({SecretId: secretName}).promise();
if ('SecretString' in secretValue) {
return secret = secretValue.SecretString;
} else {
let buff = new Buffer(secretValue.SecretBinary, 'base64');
return decodedBinarySecret = buff.toString('ascii');
}
} catch (err) {
if (err.code === 'DecryptionFailureException')
// Secrets Manager can't decrypt the protected secret text using the provided KMS key.
// Deal with the exception here, and/or rethrow at your discretion.
throw err;
else if (err.code === 'InternalServiceErrorException')
// An error occurred on the server side.
// Deal with the exception here, and/or rethrow at your discretion.
throw err;
else if (err.code === 'InvalidParameterException')
// You provided an invalid value for a parameter.
// Deal with the exception here, and/or rethrow at your discretion.
throw err;
else if (err.code === 'InvalidRequestException')
// You provided a parameter value that is not valid for the current state of the resource.
// Deal with the exception here, and/or rethrow at your discretion.
throw err;
else if (err.code === 'ResourceNotFoundException')
// We can't find the resource that you asked for.
// Deal with the exception here, and/or rethrow at your discretion.
throw err;
}
}
}
module.exports = SecretsManager;