使用TLS 1.2安全连接到RabbitMQ

时间:2019-04-01 13:49:58

标签: rabbitmq windows-server-2008-r2 tls1.2 .net-4.7.2

我正在尝试使用TLS1.2连接到RabbitMQ服务器,但是我似乎做不到。我已经验证我的用户名和密码可以正常工作,因为我可以连接到RabbitMQ Web客户端。

using System;
using System.Net;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.Xml;
using System.Xml.Linq;
using System.Xml.XPath;
using RabbitMQ.Client;
using RabbitMQ.Client.Events;

namespace DigitalFulfillmentRabbitMQ
{
    public class RabbitMQService
    {
        public IConnection GetRabbitMqConnection()
        {
            ConnectionFactory connectionFactory = new ConnectionFactory();


           // ServicePointManager.CertificatePolicy = new TrustAllCertificatePolicy();
            // connectionFactory.Ssl.CertificateValidationCallback = CheckValidationResult();
            System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; 
            connectionFactory.HostName = ConfigurationManager.AppSettings["RabbitMQServer"].ToString();
            connectionFactory.VirtualHost = ConfigurationManager.AppSettings["RabbitMQVHOST"].ToString();
            connectionFactory.Port = Int32.Parse(ConfigurationManager.AppSettings["RabbitMQPort"].ToString());
            connectionFactory.UserName = ConfigurationManager.AppSettings["RabbitMQAccountUserName"].ToString();
            connectionFactory.Password = ConfigurationManager.AppSettings["RabbitMQAccountPassword"].ToString();
           // connectionFactory.Ssl.ServerName = System.Net.Dns.GetHostName();
            connectionFactory.Ssl.ServerName = ConfigurationManager.AppSettings["RabbitMQServer"].ToString();
            connectionFactory.Ssl.CertPath = ConfigurationManager.AppSettings["RabbitMQSSLCertPath"].ToString();
           // connectionFactory.Ssl.CertPassphrase = ConfigurationManager.AppSettings["RabbitMQSSLCertPassphrase"].ToString();
            connectionFactory.Ssl.Enabled = Convert.ToBoolean(ConfigurationManager.AppSettings["RabbitMQSSLIsEnabled"].ToString());
            connectionFactory.Ssl.Version = System.Security.Authentication.SslProtocols.Tls12;



            return connectionFactory.CreateConnection();
        }
    }
}

证书是我放在客户端上的.pem证书。我正在使用端口8071。证书路径的格式为“ D:\ RabbitMQ_DF_SIT_Server_certificate \ ca_certificate.pem”。我正在使用NuGet 5.1.0中的RabbitMQ客户端

我怀疑我是否需要证书验证回调方法,因为据我了解其单向连接。此应用程序只会使用,而不会发布。我想念什么?证书位于客户端服务器上,但本身未安装。

它抛出一个错误:

RabbitMQ.Client.Exceptions.BrokerUnreachableException:没有指定的端点可访问---> System.AggregateException:发生一个或多个错误。 ---> System.Security.Authentication.AuthenticationException:对SSPI的调用失败,请参阅内部异常。 ---> System.ComponentModel.Win32Exception:客户端和服务器无法通信,因为它们不具有通用算法

1 个答案:

答案 0 :(得分:0)

要将TLS与RabbitMQ一起使用,必须通过Windows %APPDATA%/RabbitMQ/rabbitmq.conf文件中的RabbitMQ配置启用此功能。

根据所使用的Erlang版本,配置可以是经典格式/ ini样式。有关更多详细信息,请参阅https://www.rabbitmq.com/configure.html

使用更新的ini样式配置格式,配置应如下所示

listeners.ssl.default = 5671

ssl_options.verify               = verify_none
ssl_options.fail_if_no_peer_cert = false
ssl_options.cacertfile           = <location to cacertfile.crt>
ssl_options.certfile             = <location to certfile.crt>
ssl_options.keyfile              = <location to private.key>

以上所有内容都可以通过我为此编写的脚本进行自动化和验证 https://gist.github.com/skewl84/a72321379a65c4c5cfd447f8806b5188

上面的脚本确实存在

  • 检查是否已安装OpenSSL,否则请下载以生成自签名证书
  • 修改RabbbitMQ配置以启用TLS
  • 在RabbitMQ上启用TLS后,使用OpenSSL测试TLS
相关问题