From a17e216a8711312819764600e562c90f83b00ef7 Mon Sep 17 00:00:00 2001 From: Stan Lagun Date: Wed, 17 Jul 2013 11:36:42 +0400 Subject: [PATCH] Added support for RabbitMQ SSL-secured connections Change-Id: Ie11e926e72ae1e2f2840468afb2a7d1652b3553d --- WindowsAgent/Program.cs | 2 +- WindowsAgent/RabbitMqClient.cs | 27 +++++++++++++++++++++++---- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/WindowsAgent/Program.cs b/WindowsAgent/Program.cs index 0717e2fe..d0bd9225 100644 --- a/WindowsAgent/Program.cs +++ b/WindowsAgent/Program.cs @@ -30,7 +30,7 @@ namespace Mirantis.Murano.WindowsAgent { base.OnStart(args); - Log.Info("Version 0.3"); + Log.Info("Version 0.4"); this.rabbitMqClient = new RabbitMqClient(); diff --git a/WindowsAgent/RabbitMqClient.cs b/WindowsAgent/RabbitMqClient.cs index a4b4634a..962a0be8 100644 --- a/WindowsAgent/RabbitMqClient.cs +++ b/WindowsAgent/RabbitMqClient.cs @@ -3,6 +3,9 @@ using System.Collections.Generic; using System.Configuration; using System.Linq; using System.Net; +using System.Net.Security; +using System.Security.Authentication; +using System.Security.Cryptography.X509Certificates; using System.Text; using System.Threading.Tasks; using NLog; @@ -18,13 +21,29 @@ namespace Mirantis.Murano.WindowsAgent static RabbitMqClient() { - connectionFactory = new ConnectionFactory { + var ssl = new SslOption { + Enabled = bool.Parse(ConfigurationManager.AppSettings["rabbitmq.ssl"] ?? "false"), + Version = SslProtocols.Default, + AcceptablePolicyErrors = bool.Parse(ConfigurationManager.AppSettings["rabbitmq.allowInvalidCA"] ?? "true") ? + SslPolicyErrors.RemoteCertificateNameMismatch : SslPolicyErrors.None + }; + + var sslServerName = ConfigurationManager.AppSettings["rabbitmq.sslServerName"] ?? ""; + ssl.ServerName = sslServerName; + if (String.IsNullOrWhiteSpace(sslServerName)) + { + ssl.AcceptablePolicyErrors |= SslPolicyErrors.RemoteCertificateNameMismatch; + } + + connectionFactory = new ConnectionFactory { HostName = ConfigurationManager.AppSettings["rabbitmq.host"] ?? "localhost", UserName = ConfigurationManager.AppSettings["rabbitmq.user"] ?? "guest", Password = ConfigurationManager.AppSettings["rabbitmq.password"] ??"guest", - Protocol = Protocols.FromEnvironment(), + Protocol = Protocols.DefaultProtocol, VirtualHost = ConfigurationManager.AppSettings["rabbitmq.vhost"] ?? "/", - RequestedHeartbeat = 10 + Port = int.Parse(ConfigurationManager.AppSettings["rabbitmq.port"] ?? "5672"), + RequestedHeartbeat = 10, + Ssl = ssl }; } @@ -45,7 +64,7 @@ namespace Mirantis.Murano.WindowsAgent } var session = connection.CreateModel(); session.BasicQos(0, 1, false); - //session.QueueDeclare(queueName, true, false, false, null); + session.QueueDeclare(queueName, true, false, false, null); var consumer = new QueueingBasicConsumer(session); var consumeTag = session.BasicConsume(queueName, false, consumer); var e = (RabbitMQ.Client.Events.BasicDeliverEventArgs) consumer.Queue.Dequeue();