2

We have a nodejs application in which our basic requirement is to not use hard coded DB password from config.json file instead read it from Google Cloud Secret Manager and i am able to fetch that secret value from there.

But when i try to use it in my models/index.js via a async function i got the Promise { pending } error.

Here is my secret_manager.js file:

let _ = require('lodash');

// Imports the Secret Manager library
const { SecretManagerServiceClient } = require('@google-cloud/secret-manager');
let getSecret = async function (secretName) {

    try {

        if (!secretName) {
            throw new TypeError('secretName argument is required to getSecret()');
        }

        if (typeof secretName !== 'string') {
            throw new TypeError('secretName must be a string to getSecret()');
        }

        // Instantiates a client
        console.info('Creating a SecretManagerServiceClient')
        const client = new SecretManagerServiceClient();

        console.info('Calling a accessSecretVersion')
        const [version] = await client.accessSecretVersion({
            name: secretName,
        });

        // Extract the payload as a string.
        const payload = version.payload.data.toString('utf8');

        if (_.isNil(payload) || _.isEmpty(payload)) {
            let error = new Error()
            error.name = 'SecretAccessError';
            error.message = 'Invalid Secret Value for ' + secretName;
            console.error(error);
            throw error;
        }
        console.log("++payload=>")
        console.log(payload)
        return { Payload: payload }

    } catch (error) {
        error.name = "SecretAccessError"
        console.error(error)
        throw error;
    }
}
module.exports = {
    getSecret: getSecret
}

Below is my code in index.js file:

const secret = require('../secret_manager');

    // The name of the secret
    const secretName = 'my secret location in GoogleCloud'
    let secretPassword;

    let getSecret = async function(secretName)
    {
       let result = await secret.getSecret(secretName);
       return result.Payload;
    }

    if(env=='development'){
        secretPassword = getSecret(secretName);
    }else{
      secretPassword = getSecret(secretName);
    }
    console.log("secret passwprd is:")
     console.log(secretPassword)

When i start my server, here is my output:

[nodemon] starting `node start.js`
Creating a SecretManagerServiceClient
Calling a accessSecretVersion
secret passwprd is:
Promise { <pending> }
Running a GraphQL API server at http://localhost:4000/graphql in development environment!
++payload=>
**MYSECRETPASSWORD**

How can i use my secret manager value in index.js for sequelize db connection

Ravindra
  • 1,039
  • 2
  • 12
  • 26
  • Does this answer your question? [Why is my asynchronous function returning Promise { } instead of a value?](https://stackoverflow.com/questions/38884522/why-is-my-asynchronous-function-returning-promise-pending-instead-of-a-val) – sethvargo Mar 06 '20 at 17:22
  • @sethvargo thanks for your comment, i have resolved my issue and posted answer below – Ravindra Apr 28 '20 at 11:02

1 Answers1

1

I have added a then block for my secret password like:

    secretPassword.then(function (res) {
    -----Rest of the logic inside this---
    }

module.exports = db;

and this works for me

Ravindra
  • 1,039
  • 2
  • 12
  • 26