0

function credential(secretFromVault) {
  const creddetails = new ClientSecretCredential(clientId, tenantId, cleintSecret);
  // Build the URL to reach your key vault
  const url = `https://<vaultName>.vault.azure.net/`;

  // Lastly, create our secrets client and connect to the service
  const client = new SecretClient(url, creddetails);

  const secretName = secretFromVault;
  return new Promise(resolve => {
    client.getSecret(secretName).then(latestSecret => {
      console.log(`value from secret is`, latestSecret.value);
      resolve(latestSecret.value)
    })
  })
}
const dbUserName = credential(constants.pgDbUserName)
const dbPassword = credential(constants.pgDbPassword)

const hostname = constants.pgHostname;
const port = constants.pgPort;
const dbName = constants.pgDbName;
const sequelize = new Sequelize(dbName, dbUserName, dbPassword, {
  host: hostname,
  port: port,
  dialect: constants.dialectName,
  logging: false,
  pool: {
    max: constants.pgMaxPoolConnections,
    min: constants.pgMinPoolConnections,
    acquire: constants.pgMakeConnectionTimeOut,
    idle: constants.pgIdleTimeout
  }
});
sequelize.authenticate()
  .then(() => {
    console.log('Successfully connected.');
    User.sync();
    Credentials.sync();
    App.sync();
    Entity.sync();
    EntityField.sync();
    Relationship.sync();
  })
  .catch(err => console.log('Error: ' + err))

I am using the above code to make connection to postgres database. But I am receiving below error on execution of node index command, index.js is not attached here. I want dbUSerName and dbUserpassword values to be passed in the sequelize connection string after fetched from the vault. but the values are promise which I am not able to resolve.

error: uncaughtException: The "string" argument must be of type string or an instance of Buffer or ArrayBuffer. Received an instance of Promise

1 Answer 1

1

credential function returns Promise, you need to call it as a promise function.

You can read about promises here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise

I think it will be better to wrap your code in a async function and use await before calling credential


async function main() {
    const dbUserName = await credential(constants.pgDbUserName);
    const dbPassword = await credential(constants.pgDbPassword);
 
    // Your code
}

main();
Sign up to request clarification or add additional context in comments.

18 Comments

After sequelize.authenticate, I am exporting sequelize using module.exports and using it for sequelize.define(). If I add this inside main() function, then dbUserName and dbPassword are not defined for const sequelize = new Sequelize(dbName, dbUserName, dbPassword. so I will have to keep it inside a function and call it from mainv fucntion. If I do so, this module.exports = { sequelize } is not working. I am not getting any idea, should I export the function ?
yes, you can export async function
If I export the function, then use this const dbConnection = require('../config/dbconfig')//the config file present above const Credentials = dbConnection.sequelize.define('credentials', {, I am getting the below error TypeError: Cannot read property 'define' of undefined
Are you returning sequelize from the main?
yes, I have kept it inside main, if I keep my sequelize statement outside main, then it's getting executed beofre the credentials are received from vault and I am getting, expected string but received promise
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.