登录
首页 >  Golang >  Go问答

tls:使用 streadway/amqp 为 RabbitMQ 启用 tls 时握手失败

来源:stackoverflow

时间:2024-04-06 20:42:33 464浏览 收藏

哈喽!大家好,很高兴又见面了,我是golang学习网的一名作者,今天由我给大家带来一篇《tls:使用 streadway/amqp 为 RabbitMQ 启用 tls 时握手失败》,本文主要会讲到等等知识点,希望大家一起学习进步,也欢迎大家关注、点赞、收藏、转发! 下面就一起来看看吧!

问题内容

我正在尝试使用 streadway/amqp 在 go 中使用 amqps:// 连接到 rabbitmq。我可以与 amqp:// 成功连接。启用 tls 并使用 amqps:// 时,出现以下错误:

panic: remote error: tls: handshake failure

rabbitmq 在 docker 中运行,具有以下环境变量和设置:

environment:
    rabbitmq_ssl_cacertfile: /ca_certificate.pem
    rabbitmq_ssl_certfile: /server_certificate.pem
    rabbitmq_ssl_keyfile: /server_key.pem
ports:
    - 5671:5671 # note that 5671 is for tls and 5672 is non-tls
volumes:
    - ./ca_certificate.pem:/ca_certificate.pem:ro
    - ./server_certificate.pem:/server_certificate.pem:ro
    - ./server_key.pem:/server_key.pem:ro

我已经使用 amqp/streadway 尝试了以下操作:

err := amqp.dialtls(amqps://guest:guest@localhost:5671", nil)
if err != nil {
    panic(err)
}

我还尝试读取证书文件、创建密钥对、将证书颁发机构附加到证书池,并在具有以下功能的 tls.config{} 中以这种方式使用它:

  • tls.loadx509keypair()
  • x509.newcertpool().appendcertsfrompem()

我使用 mkcert 为 127.0.0.1、localhost、rabbitmq 生成证书。

根据一些与 rabbitmq 无关的答案,有些人认为密码可能是错误的。所以我看了一下rabbitmq使用的密码:

$ openssl s_client -connect localhost:5671 -tls1

protocol  : tlsv1
cipher    : ecdhe-rsa-aes256-sha

verify return code: 0 (ok)

当我运行上述命令时,还会出现一两个错误,但我猜测这是因为我没有在此命令中提供 ca 证书(我使用的是 macos)。也许相关,也许不相关,因为我对 postgres 没有这个问题,例如:

验证错误:num=19:证书链中的自签名证书 验证返回:0 4644699756:错误:1401e410:ssl例程:connect_cr_finished:sslv3警报握手失败:/appleinternal/buildroot/library/caches/com.apple.xbs/sources/libressl/libressl-47.100.4/libressl-2.8/ssl/ssl_pkt.c :1200:ssl 警报号码 40

然后我在 golang 中使用以下 tls.config 设置:

tlsConfig := &tls.Config{
    Certificates: []tls.Certificate{cert}, // from tls.LoadX509KeyPair
    RootCAs:      caCertPool,
    CipherSuites: []uint16{
        tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384, // these look like they match the Cipher above
        tls.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
    },
    CurvePreferences:         []tls.CurveID{tls.CurveP521, tls.CurveP384, tls.CurveP256},
    PreferServerCipherSuites: true,
    InsecureSkipVerify:       true,
    MinVersion:               tls.VersionTLS10,
}

我仍然遇到同样的问题。我非常怀疑这是图书馆的问题,这一定是我做错了什么,但它是什么?


解决方案


我复制了你的设置。它不起作用,因为您需要使用客户端证书配置 amqp 连接。

使用 mkcertmkcert -clientrabbitmq.test localhost 127.0.0.1 ::1(注意 -client 标志)。

之后,您只需使用 tls.loadx509keypair 将客户端证书传递到 amqp tlsconfig 中,它应该就可以工作:

cert, err := tls.LoadX509KeyPair("./rabbitmq.test+3-client.pem", "./rabbitmq.test+3-client-key.pem")

    // Load CA cert
    caCert, err := ioutil.ReadFile("./rootCA.pem") // The same you configured in your MQ server
    if err != nil {
        log.Fatal(err)
    }
    caCertPool := x509.NewCertPool()
    caCertPool.AppendCertsFromPEM(caCert)

    tlsConfig := &tls.Config{
        Certificates: []tls.Certificate{cert}, // from tls.LoadX509KeyPair
        RootCAs:      caCertPool,
        // ...other options are just the same as yours
    }

    conn, err := amqp.DialTLS("amqps://test:[email protected]:5671", tlsConfig)
    if err != nil {
        panic(err) // does not panic!
    }

    // ... application code

ps:在我的设置中,我使用了一些与您不同的名称(用户/密码/容器),但这些应该是不相关的

好了,本文到此结束,带大家了解了《tls:使用 streadway/amqp 为 RabbitMQ 启用 tls 时握手失败》,希望本文对你有所帮助!关注golang学习网公众号,给大家分享更多Golang知识!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>