Search code examples
rabbitmqamqprabbitmq-exchange

Unable to connect to RabbitMQ broker using amqps


Hi I would like to use amqps to connect to the rabbitmq broker, but it do not seem to be working.

ConnectionFactory cf = new ConnectionFactory();
Uri uri = new Uri("amqps://localhost:5671");
cf.Uri = uri;

I had already enabled the plugin "rabbitmq_auth_mechanism_ssl" and had configured the rabbitmq.conf to the following:

management.tcp.port       = 15672

management.ssl.port       = 15671
management.ssl.cacertfile = C:\\CA\\ca.cert.pem
management.ssl.certfile   = C:\\CA\\serca.cert.pem
management.ssl.keyfile    = C:\\CA\\private.key.pem


listeners.ssl.1 = 5671
ssl_options.cacertfile = C:\\CA\\ca.cert.pem
ssl_options.certfile   = C:\\CA\\serca.cert.pem
ssl_options.keyfile    = C:\\CA\\private.key.pem
ssl_options.password   = secret

ssl_options.verify     = verify_peer
ssl_options.fail_if_no_peer_cert = true

auth_mechanisms.1 = EXTERNAL
auth_mechanisms.2 = PLAIN
auth_mechanisms.3 = AMQPLAIN

Do I need to enable another plugin again inorder for it to work?

I would appreciate it if someone will kindly give me some guide / hints on using the amqps to make the connection to the RabbitMQ broker.


Solution

  • Ah I found the solution to do it. I am posting the answer here, hope it helps someone:

    In C#:

    ConnectionFactory cf = new ConnectionFactory();
    
                    Uri uri = new Uri("amqps://sample:sample@localhost");
                    cf.Port = AmqpTcpEndpoint.DefaultAmqpSslPort;
                    cf.Uri = uri;
                    var sslOptions = new SslOption
                    {
                        Enabled = true,
                        ServerName = "server cn",  
                        AcceptablePolicyErrors = System.Net.Security.SslPolicyErrors.RemoteCertificateChainErrors | 
                                                 System.Net.Security.SslPolicyErrors.RemoteCertificateNameMismatch |
                                                 System.Net.Security.SslPolicyErrors.RemoteCertificateNotAvailable,
    
                    };
                    cf.Ssl = sslOptions;
    

    in rabbitmq.conf

    management.tcp.port       = 15672
    
    management.ssl.port       = 15671
    management.ssl.cacertfile = C:\\CA\\ca.cert.pem
    management.ssl.certfile   = C:\\CA\\serca.cert.pem
    management.ssl.keyfile    = C:\\CA\\private.key.pem
    
    
    listeners.ssl.1 = 5671
    ssl_options.cacertfile = C:\\CA\\ca.cert.pem
    ssl_options.certfile   = C:\\CA\\serca.cert.pem
    ssl_options.keyfile    = C:\\CA\\private.key.pem
    ssl_options.password   = secret
    
    ssl_options.verify     = verify_peer
    ssl_options.fail_if_no_peer_cert = false    <<<< need to set this to false.
    
    auth_mechanisms.1 = EXTERNAL
    auth_mechanisms.2 = PLAIN
    auth_mechanisms.3 = AMQPLAIN
    

    Reference: https://www.squaremobius.net/amqp.node/ssl.html

    http://rabbitmq.1065348.n5.nabble.com/C-client-connect-using-SSL-td31134.html