简体   繁体   English

无法设置 MQTT (Mosquito Docker) + SSL + MQTTNet (C#) + Dapr.io 绑定

[英]Can't set up MQTT (Mosquito Docker) + SSL + MQTTNet (C#) + Dapr.io binding

I am trying to set up a local MQTT broker using mosquito image and connect to it with MQTTNet as publisher and subscribe using Dapr MQTT binding.我正在尝试使用蚊子图像设置本地 MQTT 代理,并使用 MQTTNet 作为发布者连接到它,并使用 Dapr MQTT 绑定进行订阅。 Everything works fine if I am using anonymous mode (both in Docker self-hosted and in K8S).如果我使用匿名模式(在 Docker 自托管和 K8S 中),一切正常。 But we need to use SSL... and here things stopped working.但是我们需要使用 SSL... 在这里一切都停止了。 And I am literally out of ideas.我真的没主意了。

Anyone done this setup already?有人已经完成了这个设置吗?

I am following this guide for creating certificates: Mosquito SSL setup我正在按照本指南创建证书: Mosquito SSL setup

This is my broker Dockerfile:这是我的经纪人 Dockerfile:

FROM eclipse-mosquitto:latest

COPY ./mqtt/mosquitto/mosquitto.conf /mosquitto/config/mosquitto.conf
COPY ./mqtt/mosquitto/certificates /mosquitto/certs
COPY ./mqtt/mosquitto/certificates /mosquitto/ca_certificates

and mosquitto.conf file:和 mosquitto.conf 文件:

listener 8883
protocol mqtt

listener 9001
protocol websockets

allow_anonymous false

cafile ./mosquitto/certs/ca.crt
keyfile ./mosquitto/certs/server.key
certfile ./mosquitto/certs/server.crt

require_certificate true
use_identity_as_username true

tls_version tlsv1.2

Broker is starting fine:经纪人开始正常:

2023-01-02 23:44:16 1672699456: mosquitto version 2.0.15 starting 2023-01-02 23:44:16 1672699456:蚊子版本 2.0.15 开始
2023-01-02 23:44:16 1672699456: Config loaded from /mosquitto/config/mosquitto.conf. 2023-01-02 23:44:16 1672699456:从 /mosquitto/config/mosquitto.conf 加载的配置。
2023-01-02 23:44:16 1672699456: Opening ipv4 listen socket on port 8883. 2023-01-02 23:44:16 1672699456:在端口 8883 上打开 ipv4 侦听套接字。
2023-01-02 23:44:16 1672699456: Opening ipv6 listen socket on port 8883. 2023-01-02 23:44:16 1672699456:在端口 8883 上打开 ipv6 侦听套接字。
2023-01-02 23:44:16 1672699456: Opening websockets listen socket on port 9001. 2023-01-02 23:44:16 1672699456:在端口 9001 上打开 websockets 侦听套接字。
2023-01-02 23:44:16 1672699456: mosquitto version 2.0.15 running 2023-01-02 23:44:16 1672699456:mosquitto 版本 2.0.15 正在运行

Now, I am trying to use MQTTNet library and I wrote a simple c# console app:现在,我尝试使用 MQTTNet 库并编写了一个简单的 c# 控制台应用程序:

using MQTTnet;
using MQTTnet.Client;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Text.Json;

var caCert = new X509Certificate2("ca.crt");
var clientCert = new X509Certificate2("client.pfx", "password");

var url = "localhost";
var factory = new MqttFactory();
var client = factory.CreateMqttClient();
var options = new MqttClientOptionsBuilder()
    .WithTcpServer(url, 8883)
    .WithClientId("Device1")
    .WithTls(new MqttClientOptionsBuilderTlsParameters
    {
        UseTls = true,
        SslProtocol = System.Security.Authentication.SslProtocols.Tls12,
        CertificateValidationHandler = (certContext) =>
        {
            X509Chain chain = new X509Chain();
            chain.ChainPolicy.RevocationMode = X509RevocationMode.NoCheck;
            chain.ChainPolicy.RevocationFlag = X509RevocationFlag.ExcludeRoot;
            chain.ChainPolicy.VerificationFlags = X509VerificationFlags.NoFlag;
            chain.ChainPolicy.VerificationTime = DateTime.Now;
            chain.ChainPolicy.UrlRetrievalTimeout = new TimeSpan(0, 0, 0);
            chain.ChainPolicy.CustomTrustStore.Add(caCert);
            chain.ChainPolicy.TrustMode = X509ChainTrustMode.CustomRootTrust;

            var x5092 = new X509Certificate2(certContext.Certificate);

            return chain.Build(x5092);
        },
        AllowUntrustedCertificates = true,
        Certificates = new List<X509Certificate2>
        {
            clientCert, caCert
        },
    })
    .Build();

await client.ConnectAsync(options);

var message = new MqttApplicationMessageBuilder()
    .WithTopic("sensor/reporting")
    .WithPayload(Encoding.UTF8.GetBytes("test"))
    .Build();
await client.PublishAsync(message);

And I am not able to connect with client error:而且我无法连接客户端错误:

MQT.net.Exceptions.MqttCommunicationException: ' Received an unexpected EOF or 0 bytes from the transport stream.' MQT.net.Exceptions.MqttCommunicationException:“从传输 stream 收到意外的 EOF 或 0 字节。”

And server error:和服务器错误:

2023-01-02 23:47:00 1672699620: New connection from 172.19.0.1:37912 on port 8883. 2023-01-02 23:47:00 1672699620:端口 8883 上来自 172.19.0.1:37912 的新连接。
2023-01-02 23:47:00 1672699620: Client disconnected due to protocol error. 2023-01-02 23:47:00 1672699620:客户端因协议错误而断开连接。

Tried different options and never succeded.尝试了不同的选择,但从未成功。

On the subscriber side, I am following this guide: Dapr MQTT Binding with below config file:在订户方面,我遵循本指南: Dapr MQTT Binding with below config file:

apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
  name: sensorreporting
spec:
  type: bindings.mqtt
  version: v1
  metadata:
  - name: url
    value: ssl://localhost:8883
  - name: topic
    value: sensor/reporting
  - name: consumerID
    value: "{uuid}"
  - name: caCert
    value: -----BEGIN CERTIFICATE-----\nMIIEGzCCAwOgAwIBAgIUYbZRJpxF3knVBs4A9FIc9KU6YskwDQYJKoZIhvcNAQEL\nBQAwgZwxCzAJBgNVBAYTAkNBMRAwDgYDVQQIDAdBbGJlcnRhMRAwDgYDVQQHDAdD\nYWxnYXJ5MRMwEQYDVQQKDAptQ2xvdWRDb3JwMRYwFAYDVQQLDA1DZXJ0QXV0aEFk\nbWluMQ0wCwYDVQQDDARhZGNhMS0wKwYJKoZIhvcNAQkBFh5taWNoYWwua29zb3dz\na2lAbWNsb3VkY29ycC5jb20wHhcNMjMwMTAyMjE0MzU5WhcNMjgwMTAyMjE0MzU5\nWjCBnDELMAkGA1UEBhMCQ0ExEDAOBgNVBAgMB0FsYmVydGExEDAOBgNVBAcMB0Nh\nbGdhcnkxEzARBgNVBAoMCm1DbG91ZENvcnAxFjAUBgNVBAsMDUNlcnRBdXRoQWRt\naW4xDTALBgNVBAMMBGFkY2ExLTArBgkqhkiG9w0BCQEWHm1pY2hhbC5rb3Nvd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1ixlnfcRUf0Dv8CrivKmW4Lz226A3hS6AlWfzI9amEfG5AoGBAOdQ\nGcKSUijcNkASud4VZyOqNqjKtj5SKx1HblnOq7HERblPW53wD8Mg9jeQ8X7Flaox\n52m1W3EcO1VVLveQ6FYsBXVz+9FwHblztizVlty48XmzpQDbAL1xI+G0WT7cpDt8\nDdnlOmZX4hxBsmrkOFz/gP4yn6sL0u0wbRTk995xAoGAcBe1Jaguv04djEgefEF+\ngxpni+MQXviAQ5pOsKsnWHJjriINyZ+Gm9b6SnDv7m7AwirOSAVvTwyJX40Xp2Rr\ndJRl3hd8xzeUWVF6VPo1dVO9Tf41ttT1Qra9pKua/gYrhZSn/xqrelx4jkBrbZSz\nXBiZWQZNb6xu/OosO+9sP3ECgYEA3KoVaQCdUE2Dk2xJ1Asm8WcX7b40TQ+mAbB7\nHUGDmXpb6YRDKlAxOTzgpfGn8AAd3tbspHNWYHlGQQmaXKHogwxLyNh0ikAimyA/\n0lWQLdBgzHEMH9zMv+yHBnI+ETH7mqOlsxByvd9o3PvvcPl2q4EzVUyF9W66MXvZ\nE+26aIECgYBtDK+8hffO3N1yXW+tMFuCbbzMPrPJC5o4VII8pm+oNcsvW1Ao9kpZ\nL7yVVJCozo0omvuY2ohVchiQ5jC1nxd17Xa0DuIrKgtcEmpoae16e2h7kcSjv63y\nbKEEUIKRyWfhx6iJb8o03lq1UUxRDhaB5leiUUddibJyhh7EDfCHfQ==\n-----END RSA PRIVATE KEY-----

and dapr sidecar is failing with following error: dapr sidecar 失败并出现以下错误:

mqtt-sample-mqtt-api-dapr-1 | mqtt-示例-mqtt-api-dapr-1 | time="2023-01-02T23:19:05.428410597Z" level=fatal msg="process component sensorreporting error: [INIT_COMPONENT_FAILURE]: initialization error occurred for sensorreporting (bindings.mqtt/v1): mqtt binding error: invalid ca certificate" app_id=mqtt-api instance=2c64804e20f1 scope=dapr.runtime type=log ver=1.9.5 mqtt-sample-mqtt-api-dapr-1 exited with code 1 time="2023-01-02T23:19:05.428410597Z" level=fatal msg="进程组件传感器报告错误:[INIT_COMPONENT_FAILURE]:传感器报告发生初始化错误(bindings.mqtt/v1):mqtt绑定错误:无效的CA证书" app_id=mqtt-api instance=2c64804e20f1 scope=dapr.runtime type=log ver=1.9.5 mqtt-sample-mqtt-api-dapr-1 退出代码 1

I do not know where I am making mistake.我不知道我在哪里犯了错误。 I am going to test that on AKS now (maybe the problem is with running it locally? Anonymous mode is working with AKS).我现在要在 AKS 上测试它(也许问题出在本地运行它?匿名模式正在与 AKS 一起工作)。

I was able to set up the whole thing without SSL, but need this to go to prod.我能够在没有 SSL 的情况下设置整个东西,但需要这个到 go 来生产。

The TLS certificate setup is per listener, so if you want both the listener on port 8883 and the Websocker listener on port 9001 to use TLS then you need to specify the certificates twice eg TLS 证书设置是针对每个侦听器的,因此如果您希望端口 8883 上的侦听器和端口 9001 上的 Websocker 侦听器都使用 TLS,那么您需要指定证书两次,例如

listener 8883
protocol mqtt
cafile ./mosquitto/certs/ca.crt
keyfile ./mosquitto/certs/server.key
certfile ./mosquitto/certs/server.crt
require_certificate true
use_identity_as_username true
tls_version tlsv1.2

listener 9001
protocol websockets

allow_anonymous false
cafile ./mosquitto/certs/ca.crt
keyfile ./mosquitto/certs/server.key
certfile ./mosquitto/certs/server.crt


tls_version tlsv1.2

You also need to be very careful about requiring a client certificate for the websocker listener and using it as a the username, because if you are accessing from a Web Browser it is VERY tricky to set up client certificates.您还需要非常小心地要求 websocker 侦听器需要客户端证书并将其用作用户名,因为如果您从 Web 浏览器访问,则设置客户端证书非常棘手。 Hence I have removed that requirement from the WebSocket listener因此,我已从 WebSocket 侦听器中删除了该要求

You have the listener on port 8883 configured to use websockets;您将端口 8883 上的侦听器配置为使用 websockets; you appear to be connecting using plain MQTT over TCP, not over websockets.您似乎是通过 TCP 而不是通过 websockets 使用普通 MQTT 进行连接。

Your client needs to use the same protocol as the broker is configured for on the port the client connects to.您的客户端需要使用与代理在客户端连接到的端口上配置的相同协议。

Try commenting out the websockets line in the mosquitto config file, restarting the broker and connecting again.尝试注释掉 mosquitto 配置文件中的 websockets 行,重新启动代理并再次连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM