Skip to content

Instantly share code, notes, and snippets.

@yeisoncruz16
Created November 18, 2020 21:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yeisoncruz16/a973eccba63e1f28a4a07caf6d425ef0 to your computer and use it in GitHub Desktop.
Save yeisoncruz16/a973eccba63e1f28a4a07caf6d425ef0 to your computer and use it in GitHub Desktop.
NodeJs script to get Secrets from secrets manager
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