【发布时间】:2019-10-15 21:08:20
【问题描述】:
我发现无法在客户端使用 python 的 pika 库与 RabbitMQ 代理建立加密连接。我的出发点是 pika 教程示例here,但我无法让它工作。我进行了如下操作。
(1) RabbitMQ 配置文件为:
listeners.tcp.default = 5672
listeners.ssl.default = 5671
ssl_options.verify = verify_peer
ssl_options.fail_if_no_peer_cert = false
ssl_options.cacertfile = /etc/cert/tms.crt
ssl_options.certfile = /etc/cert/tms.crt
ssl_options.keyfile = /etc/cert/tmsPrivKey.pem
auth_mechanisms.1 = PLAIN
auth_mechanisms.2 = AMQPLAIN
auth_mechanisms.3 = EXTERNAL
(2) rabbitmq-auth-mechanism-ssl 插件 使用以下命令启用:
rabbitmq-plugins enable rabbitmq_auth_mechanism_ssl
通过检查启用状态确认成功启用:rabbitmq-plugins list。
(3) TLS 证书 的正确性已通过使用 here 所述的 openssl 工具进行验证。
(4) 建立连接的客户端程序是:
#!/usr/bin/env python
import logging
import pika
import ssl
from pika.credentials import ExternalCredentials
logging.basicConfig(level=logging.INFO)
context = ssl.create_default_context(
cafile="/Xyz/sampleNodeCert/tms.crt")
context.load_cert_chain("/Xyz/sampleNodeCert/node.crt",
"/Xyz/sampleNodeCert/nodePrivKey.pem")
ssl_options = pika.SSLOptions(context, '127.0.0.1')
conn_params = pika.ConnectionParameters(host='127.0.0.1',
port=5671,
ssl_options=ssl_options,
credentials=ExternalCredentials())
with pika.BlockingConnection(conn_params) as conn:
ch = conn.channel()
ch.queue_declare("foobar")
ch.basic_publish("", "foobar", "Hello, world!")
print(ch.basic_get("foobar"))
(5) 客户端程序失败并显示以下错误消息:
pika.exceptions.ProbableAuthenticationError: ConnectionClosedByBroker: (403) 'ACCESS_REFUSED - Login was refused using authentication mechanism EXTERNAL. For details see the broker logfile.'
(6) RabbitMQ 代理中的日志消息为:
2019-10-15 20:17:46.028 [info] <0.642.0> accepting AMQP connection <0.642.0> (127.0.0.1:48252 -> 127.0.0.1:5671)
2019-10-15 20:17:46.032 [error] <0.642.0> Error on AMQP connection <0.642.0> (127.0.0.1:48252 -> 127.0.0.1:5671, state: starting):
EXTERNAL login refused: user 'CN=www.node.com,O=Node GmbH,L=NodeTown,ST=NodeProvince,C=DE' - invalid credentials
2019-10-15 20:17:46.043 [info] <0.642.0> closing AMQP connection <0.642.0> (127.0.0.1:48252 -> 127.0.0.1:5671)
(7) 进行此测试的环境是在 Erlang 22.0.7 上使用 RabbitMQ 3.7.17 的 Ubuntu 18.04。客户端使用python3版本3.6.8。
问题:有人知道我的测试失败的原因吗?在哪里可以找到使用 pika 设置到 RabbitMQ 的加密连接的完整工作示例?
注意:我熟悉 this post,但帖子中的提示都没有帮助到我。
【问题讨论】:
-
请在
pika-python邮件列表中提问,我们可以在那里继续讨论。我维护 Pika 并设置了客户端证书身份验证,所以我知道它可以工作。很可能问题出在auth_mechanisms的顺序(尝试将EXTERNAL放在首位)或您如何在RabbitMQ 中创建CN=www.node.com,O=Node GmbH,L=NodeTown,ST=NodeProvince,C=DE用户。我假设您正在使用 Pika1.1.0。参见this message。