Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
509 views
in Technique[技术] by (71.8m points)

acl - 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...