From 16913bd577f590a8ce726d1a221e5bef0ac57e1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CArslanKibria=E2=80=9D?= <“kibriya.mushtaq@ferrum.network”> Date: Tue, 25 Jul 2023 17:54:45 +0500 Subject: [PATCH] awsSecretsManager module added --- package.json | 3 ++- src/utils/awsSecretsManager.ts | 31 +++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 src/utils/awsSecretsManager.ts diff --git a/package.json b/package.json index d81c35d..afbcc7f 100644 --- a/package.json +++ b/package.json @@ -40,6 +40,7 @@ "mongoose": "^6.1.3", "mongoose-autopopulate": "^0.16.0", "winston": "^3.2.1", - "jsonwebtoken": "^8.5.1" + "jsonwebtoken": "^8.5.1", + "aws-sdk": "^2.1075.0" } } diff --git a/src/utils/awsSecretsManager.ts b/src/utils/awsSecretsManager.ts new file mode 100644 index 0000000..1866d0b --- /dev/null +++ b/src/utils/awsSecretsManager.ts @@ -0,0 +1,31 @@ +let AWS = require("aws-sdk"); + +let awsSecretsManager = async function () { + return new Promise(async (resolve, reject) => { + var client = new AWS.SecretsManager({ + region: process.env.REGION, + }); + client.getSecretValue( + { SecretId: process.env.SECRET_NAME }, + function (err: any, data: any) { + if (err) { + console.log("aws error: " + err); + reject(""); + } else { + if ("SecretString" in data) { + let secret = data.SecretString; + var secretJson = JSON.parse(secret); + (global as any).AWS_ENVIRONMENT = { + ...(global as any).AWS_ENVIRONMENT, + ...secretJson, + }; + } else { + } + resolve(""); + } + } + ); + }); +}; + +export default awsSecretsManager;