Search code examples
node.jsaclconsul

Node.js : Pass token in consul kv get


I am using consul to get some configurations in my Node.js project.

I was able to access the consul key-value pairs with the code given below.

import consul from 'consul';
(new consul()).kv.get('config', (err, result) => {
    if (err) throw err;
    console.log("result", result);
});

(Using npm "consul")

Now, I have protected consul with ACL and have created 3 tokens: master_token, a token with write access and a token with read access

As the consul is now ACL protected, the code mentioned above shows me "permission denied" how can I pass this token in the kv.get method to authenticate my request ?

Thanks in advance.


Solution

  • When initializing the consul client you can use the defaults option to define a token which should be sent with every query.

    import consul from 'consul';
    
    var defaultRequestOptions = {
        token: '43d8f1cb-3c73-44a2-a1d6-c4fe1b9b1537'
    };
    
    (new consul({defaults: defaultRequestOptions})).kv.get('config', (err, result) => {
        if (err) throw err;
        console.log("result", result);
    });
    
    

    See Common Method Call Options for a full list of available default options.