1

I am trying to read a value from Vault using the NodeJS. I am posting here to ensure my approach is correct.

Using the https://github.com/kr1sp1n/node-vault library, I have the following snippet of code:

var params = {
  apiVersion: 'v1',
  endpoint: "https://localhost:8200",
  token: "MY_TOKEN"
};

   var vault = require("node-vault")(params);
   vault.read('secret/mysecret/foo').then(v => {
     console.log(v);
   }).catch(e => console.error(e));

This returns the following block of JSON to me:

{ request_id: 'MY_ID',
  lease_id: '',
  renewable: false,
  lease_duration: 100,
  data: { value: 'MY_PASSWORD' },
  wrap_info: null,
  warnings: null,
  auth: null }

Specifically, I need to fetch the value of data.value (i.e. I need to fetch 'MY_PASSWORD'.

Would I perform JSON parsing within the 'then' block instead of printing the JSON to the console log like I am currently?

2 Answers 2

1

Yes, because vault.read() is asynchronous you need to access and parse the return value in the then()

vault.read('secret/mysecret/foo').then(v => {
    let parsed = JSON.parse(v);
    let pw = parsed.data.value //=> 'MY_PASSWORD'
}).catch(e => console.error(e));

Obviously, you'll probably want to do some error checks to make sure you have good json data, etc.

Sign up to request clarification or add additional context in comments.

Comments

1

The vault.read() call returns a promise and the 'then' method will be executed once the promise resolves. So, yes, you should be parsing it in there.

Comments

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.