export { }; const mstime = require('mstime'); // Load the AWS SDK var AWS = require('aws-sdk'), region = 'us-west-2', secret, decodedBinarySecret; // Create a Secrets Manager client var client = new AWS.SecretsManager({ region: region }); let secretData: any = null; let randomNumber = () => { return ((Math.random() * 0x100000000) >>> 0) + 'NODE' + ((Math.random() * 0x100000000) >>> 0) } /** * Function used to fetch the password for the SF user, from AWS Secret manager * The way secret are build is baseSecretName + username (username here is the SF username) * @notes If this is failing mostly you can't premission to access AWS Secret mananger, please get access for it or the baseSecretName + username doesn't exist in the AWS Secret mananger * baseSecretName you can find in your .env file which is like 'SF/Prod/' * @async * @stable * @internal * @param username * @param secretName */ export const secretPromise = (username: any, secretName: string) => { let id = randomNumber(); mstime.start(id); return new Promise((resolve, reject) => { client.getSecretValue({ SecretId: secretName + username }, function (err: any, data: any) { let secretDataStr: any = null; if (err) { reject(err) } else { // Decrypts secret using the associated KMS CMK. // Depending on whether the secret is a string or binary, one of these fields will be populated. if ('SecretString' in data) { secretDataStr = data.SecretString; let result = mstime.end(id); console.log(`Got secert key from AWS: 🕒 Time taken: ${result.last} ms`); } else { let buff = new Buffer(data.SecretBinary, 'base64'); decodedBinarySecret = buff.toString('ascii'); } secretData = JSON.parse(secretDataStr) resolve(secretData) } }); }) }