使用python访问consul时出现权限被拒绝的情况

使用python访问consul时出现权限被拒绝的情况

尝试调试时帕特罗尼连接到 consul,我尝试自己编写简单的 python 连接器到 consul。

这就是我正在做的(python3,Centos7)

import consul
c = consul.Consul(host='consul-host',port=port,token='some-token')
c.kv.get('/v1/kv/some/long/path/bar')

这就是我得到的(与patroni产生的错误相同):

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.6/site-packages/consul/base.py", line 554, in get
    params=params)
  File "/usr/local/lib/python3.6/site-packages/consul/std.py", line 22, in get
    self.session.get(uri, verify=self.verify, cert=self.cert)))
  File "/usr/local/lib/python3.6/site-packages/consul/base.py", line 223, in cb
    CB._status(response, allow_404=allow_404)
  File "/usr/local/lib/python3.6/site-packages/consul/base.py", line 181, in _status
    raise ACLPermissionDenied(response.body)
consul.base.ACLPermissionDenied: rpc error making call: Permission denied

Curl 可以很好地与该令牌配合使用。

curl --header "X-Consul-Token:some-token" http://consul-host:port/v1/kv/some/long/path/bar | jq .

[
  {
    "LockIndex": 0,
    "Key": "/v1/kv/some/long/path/bar",
    "Flags": 0,
    "Value": "Zm9v",
    "CreateIndex": 2951475,
    "ModifyIndex": 2951475
  }
]

你知道我做错了什么吗?谢谢。

答案1

您的回溯指向consul/base.py:554

return self.agent.http.get(
    CB.json(index=True, decode=decode, one=one),
    '/v1/kv/%s' % key,
    params=params)

由于您正在传递密钥,因此'/v1/kv/some/long/path/bar'它将到达端点http://consul-host:port/v1/kv/v1/kv/some/long/path/bar并不是http://consul-host:port/v1/kv/some/long/path/bar

相关内容