Created
November 18, 2020 21:19
-
-
Save yeisoncruz16/a973eccba63e1f28a4a07caf6d425ef0 to your computer and use it in GitHub Desktop.
NodeJs script to get Secrets from secrets manager
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Secrets { | |
constructor(awsClient) { | |
this.client = awsClient; | |
} | |
async getSecret(SecretId){ | |
return new Promise((resolve) => { | |
this.client.getSecretValue({ SecretId }, (secretManagerError, data) => { | |
if (secretManagerError) { | |
if (secretManagerError.code === "DecryptionFailureException") | |
throw secretManagerError; | |
else if (secretManagerError.code === "InternalServiceErrorException") | |
throw secretManagerError; | |
else if (secretManagerError.code === "InvalidParameterException") | |
throw secretManagerError; | |
else if (secretManagerError.code === "InvalidRequestException") | |
throw secretManagerError; | |
else if (secretManagerError.code === "ResourceNotFoundException") | |
throw secretManagerError; | |
} else { | |
if ("SecretString" in data) { | |
let secret = JSON.parse(data.SecretString); | |
resolve(secret.TOKEN); | |
} else { | |
let buff = new Buffer(data.SecretBinary, "base64"); | |
let decodedBinarySecret = buff.toString("ascii"); | |
resolve(decodedBinarySecret); | |
} | |
} | |
}); | |
}); | |
} | |
} | |
module.exports = Secrets; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment