Skip to content

Commit 77ceb77

Browse files
author
Anouar Hassine
committed
Adding SSL support for RabbitMq
1 parent 7ab9c10 commit 77ceb77

File tree

8 files changed

+176
-15
lines changed

8 files changed

+176
-15
lines changed

ReactiveXComponent/Configuration/BusDetails.cs

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11

2+
using System.Security.Authentication;
3+
24
namespace ReactiveXComponent.Configuration
35
{
46
public class BusDetails
@@ -8,14 +10,30 @@ public BusDetails()
810

911
}
1012

11-
public BusDetails(string username, string password, string host, string virtualHost, int port)
13+
public BusDetails(
14+
string username,
15+
string password,
16+
string host,
17+
string virtualHost,
18+
int port,
19+
bool sslEnabled = false,
20+
string sslServerName = "",
21+
string sslCertPath = "",
22+
string sslCertPassphrase = "",
23+
SslProtocols sslProtocol = SslProtocols.Default,
24+
bool sslAllowUntrustedServerCertificate = false)
1225
{
1326
Username = username;
1427
Password = password;
1528
Host = host;
1629
VirtualHost = virtualHost;
1730
Port = port;
18-
31+
SslEnabled = sslEnabled;
32+
SslServerName = sslServerName;
33+
SslCertPath = sslCertPath;
34+
SslCertPassphrase = sslCertPassphrase;
35+
SslProtocol = sslProtocol;
36+
SslAllowUntrustedServerCertificate = sslAllowUntrustedServerCertificate;
1937
}
2038

2139
public string Username { get; set; }
@@ -28,14 +46,32 @@ public BusDetails(string username, string password, string host, string virtualH
2846

2947
public int Port { get; set; }
3048

49+
public bool SslEnabled { get; set; }
50+
51+
public string SslServerName { get; set; }
52+
53+
public string SslCertPath { get; set; }
54+
55+
public string SslCertPassphrase { get; set; }
56+
57+
public SslProtocols SslProtocol { get; set; }
58+
59+
public bool SslAllowUntrustedServerCertificate { get; set; }
60+
3161
public BusDetails Clone()
3262
{
3363
return new BusDetails(
3464
Username,
3565
Password,
3666
Host,
3767
VirtualHost,
38-
Port);
68+
Port,
69+
SslEnabled,
70+
SslServerName,
71+
SslCertPath,
72+
SslCertPassphrase,
73+
SslProtocol,
74+
SslAllowUntrustedServerCertificate);
3975
}
4076
}
4177
}

ReactiveXComponent/Configuration/ConfigurationOverrides.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using ReactiveXComponent.Common;
1+
using System.Security.Authentication;
2+
using ReactiveXComponent.Common;
23

34
namespace ReactiveXComponent.Configuration
45
{
@@ -15,5 +16,17 @@ public class ConfigurationOverrides
1516
public string Password { get; set; }
1617

1718
public WebSocketType? WebSocketType { get; set; }
19+
20+
public bool? SslEnabled { get; set; }
21+
22+
public string SslServerName { get; set; }
23+
24+
public string SslCertPath { get; set; }
25+
26+
public string SslCertPassphrase { get; set; }
27+
28+
public SslProtocols? SslProtocol { get; set; }
29+
30+
public bool? SslAllowUntrustedServerCertificate { get; set; }
1831
}
1932
}

ReactiveXComponent/Configuration/XCApiTags.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,13 @@ public static class XCApiTags
2929
public const string Json = "Json";
3030
public const string Bson = "Bson";
3131
public const string GzipJson = "GzipJson";
32+
public const string WebsocketType = "type";
33+
public const string VirtualHost = "virtualHost";
34+
public const string BusSslEnabled = "sslEnabled";
35+
public const string BusSslServerName = "sslServerName";
36+
public const string BusSslCertPath = "sslCertPath";
37+
public const string BusSslCertPassphrase = "sslCertPassphrase";
38+
public const string BusSslProtocol = "sslProtocol";
39+
public const string BusSslAllowUntrustedServerCertificate = "sslAllowUntrustedServerCertificate";
3240
}
3341
}

ReactiveXComponent/Parser/XCApiConfigParser.cs

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.IO;
44
using System.Linq;
5+
using System.Security.Authentication;
56
using System.Xml;
67
using System.Xml.Linq;
78
using ReactiveXComponent.Common;
@@ -182,12 +183,44 @@ public string GetSerializationType()
182183
public BusDetails GetBusDetails()
183184
{
184185
XElement busInfos = _xcApiDescription.GetBusNode()?.FirstOrDefault();
186+
187+
var sslEnabledString = busInfos?.Attribute(XCApiTags.BusSslEnabled)?.Value;
188+
var sslEnabled = false;
189+
if (!string.IsNullOrEmpty(sslEnabledString))
190+
{
191+
bool.TryParse(sslEnabledString, out sslEnabled);
192+
}
193+
194+
var sslServerName = busInfos?.Attribute(XCApiTags.BusSslServerName)?.Value;
195+
var sslCertPath = busInfos?.Attribute(XCApiTags.BusSslCertPath)?.Value;
196+
var sslCertPassphrase = busInfos?.Attribute(XCApiTags.BusSslCertPassphrase)?.Value;
197+
198+
var sslProtocolString = busInfos?.Attribute(XCApiTags.BusSslProtocol)?.Value;
199+
SslProtocols sslProtocol = SslProtocols.Default;
200+
if (!string.IsNullOrEmpty(sslProtocolString))
201+
{
202+
Enum.TryParse(sslProtocolString, out sslProtocol);
203+
}
204+
205+
var sslAllowUntrustedServerCertificateString = busInfos?.Attribute(XCApiTags.BusSslAllowUntrustedServerCertificate)?.Value;
206+
var sslAllowUntrustedServerCertificate = false;
207+
if (!string.IsNullOrEmpty(sslAllowUntrustedServerCertificateString))
208+
{
209+
bool.TryParse(sslAllowUntrustedServerCertificateString, out sslAllowUntrustedServerCertificate);
210+
}
211+
185212
var busDetails = new BusDetails(
186-
busInfos?.Attribute("user")?.Value,
187-
busInfos?.Attribute("password")?.Value,
188-
busInfos?.Attribute("host")?.Value,
189-
busInfos?.Attribute("virtualHost")?.Value,
190-
Convert.ToInt32(busInfos?.Attribute("port")?.Value));
213+
busInfos?.Attribute(XCApiTags.User)?.Value,
214+
busInfos?.Attribute(XCApiTags.Password)?.Value,
215+
busInfos?.Attribute(XCApiTags.Host)?.Value,
216+
busInfos?.Attribute(XCApiTags.VirtualHost)?.Value,
217+
Convert.ToInt32(busInfos?.Attribute(XCApiTags.Port)?.Value),
218+
sslEnabled,
219+
sslServerName,
220+
sslCertPath,
221+
sslCertPassphrase,
222+
sslProtocol,
223+
sslAllowUntrustedServerCertificate);
191224

192225
return busDetails;
193226
}
@@ -197,16 +230,16 @@ public WebSocketEndpoint GetWebSocketEndpoint()
197230
XElement websocketInfos = _xcApiDescription.GetWebSocketNode()?.FirstOrDefault();
198231

199232
WebSocketType webSocketType;
200-
var webSocketTypeString = websocketInfos?.Attribute("type")?.Value;
233+
var webSocketTypeString = websocketInfos?.Attribute(XCApiTags.WebsocketType)?.Value;
201234
if (!Enum.TryParse(webSocketTypeString, out webSocketType))
202235
{
203236
throw new ReactiveXComponentException($"Could not parse communication type: {webSocketTypeString}");
204237
}
205238

206239
var webSocketEndpoint = new WebSocketEndpoint(
207-
websocketInfos?.Attribute("name")?.Value,
208-
websocketInfos?.Attribute("host")?.Value,
209-
websocketInfos?.Attribute("port")?.Value,
240+
websocketInfos?.Attribute(XCApiTags.Name)?.Value,
241+
websocketInfos?.Attribute(XCApiTags.Host)?.Value,
242+
websocketInfos?.Attribute(XCApiTags.Port)?.Value,
210243
webSocketType);
211244

212245
return webSocketEndpoint;

ReactiveXComponent/RabbitMq/RabbitMqConnection.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,36 @@ public IXCSession CreateSession(ConfigurationOverrides configurationOverrides =
5050
busDetails.Password = configurationOverrides.Password;
5151
}
5252

53+
if (configurationOverrides.SslEnabled != null)
54+
{
55+
busDetails.SslEnabled = configurationOverrides.SslEnabled.Value;
56+
}
57+
58+
if (configurationOverrides.SslServerName != null)
59+
{
60+
busDetails.SslServerName = configurationOverrides.SslServerName;
61+
}
62+
63+
if (configurationOverrides.SslCertPath != null)
64+
{
65+
busDetails.SslCertPath = configurationOverrides.SslCertPath;
66+
}
67+
68+
if (configurationOverrides.SslCertPassphrase != null)
69+
{
70+
busDetails.SslCertPassphrase = configurationOverrides.SslCertPassphrase;
71+
}
72+
73+
if (configurationOverrides.SslProtocol != null)
74+
{
75+
busDetails.SslProtocol = configurationOverrides.SslProtocol.Value;
76+
}
77+
78+
if (configurationOverrides.SslAllowUntrustedServerCertificate != null)
79+
{
80+
busDetails.SslAllowUntrustedServerCertificate = configurationOverrides.SslAllowUntrustedServerCertificate.Value;
81+
}
82+
5383
return new RabbitMqSession(_xcConfiguration, busDetails, _privateCommunicationIdentifier);
5484
}
5585
}

ReactiveXComponent/RabbitMq/RabbitMqSession.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Net.Security;
34
using RabbitMQ.Client;
45
using RabbitMQ.Client.Exceptions;
56
using ReactiveXComponent.Common;
@@ -39,6 +40,39 @@ private void InitConnection(BusDetails busDetails)
3940
Protocol = Protocols.DefaultProtocol
4041
};
4142

43+
if (busDetails.SslEnabled)
44+
{
45+
_factory.Ssl.Enabled = true;
46+
47+
_factory.Ssl.ServerName = busDetails.SslServerName;
48+
49+
if (!string.IsNullOrEmpty(busDetails.SslCertPath))
50+
{
51+
_factory.Ssl.CertPath = busDetails.SslCertPath;
52+
}
53+
54+
if (!string.IsNullOrEmpty(busDetails.SslCertPassphrase))
55+
{
56+
_factory.Ssl.CertPassphrase = busDetails.SslCertPassphrase;
57+
}
58+
59+
_factory.Ssl.Version = busDetails.SslProtocol;
60+
61+
if (busDetails.SslAllowUntrustedServerCertificate)
62+
{
63+
_factory.Ssl.CertificateValidationCallback += (sender, certificate, chain, errors) =>
64+
{
65+
if ((errors & SslPolicyErrors.RemoteCertificateNameMismatch) == SslPolicyErrors.RemoteCertificateNameMismatch ||
66+
(errors & SslPolicyErrors.RemoteCertificateNotAvailable) == SslPolicyErrors.RemoteCertificateNotAvailable)
67+
{
68+
return false;
69+
}
70+
71+
return true;
72+
};
73+
}
74+
}
75+
4276
_connection = _factory?.CreateConnection();
4377

4478
_connection.ConnectionShutdown += ConnectionOnConnectionShutdown;

ReactiveXComponentTest/Configuration/ConfigurationTests.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.IO;
3+
using System.Security.Authentication;
34
using NFluent;
45
using NUnit.Framework;
56
using ReactiveXComponent.Common;
@@ -77,7 +78,13 @@ public void GetBusDetailsTest()
7778
Check.That(busDetails.VirtualHost).IsEqualTo("myVirtualHost");
7879
Check.That(busDetails.Username).IsEqualTo("guest");
7980
Check.That(busDetails.Password).IsEqualTo("guest");
80-
Check.That(busDetails.Port).IsEqualTo(5672);
81+
Check.That(busDetails.Port).IsEqualTo(5671);
82+
Check.That(busDetails.SslEnabled).IsTrue();
83+
Check.That(busDetails.SslServerName).IsEqualTo("XComponent RMq");
84+
Check.That(busDetails.SslCertPath).IsEqualTo("some_cert_path");
85+
Check.That(busDetails.SslCertPassphrase).IsEqualTo("some_cert_pass");
86+
Check.That(busDetails.SslProtocol).IsEqualTo(SslProtocols.Default);
87+
Check.That(busDetails.SslAllowUntrustedServerCertificate).IsTrue();
8188
}
8289

8390
[Test]

ReactiveXComponentTest/RabbitMqTestApi.xcApi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<threading />
44
<serialization>Binary</serialization>
55
<communication>
6-
<bus name="rabbitmq" host="127.0.0.1" virtualHost="myVirtualHost" port="5672" user="guest" password="guest" type="RABBIT_MQ" />
6+
<bus name="rabbitmq" host="127.0.0.1" virtualHost="myVirtualHost" port="5671" user="guest" password="guest" type="RABBIT_MQ" sslEnabled="True" sslServerName="XComponent RMq" sslCertPath="some_cert_path" sslCertPassphrase="some_cert_pass" sslProtocol="Default" sslAllowUntrustedServerCertificate="True" />
77
</communication>
88
<clientAPICommunication>
99
<publish componentCode="-69981087" stateMachineCode="-829536631" eventType="UPDATE" topicType="output" communicationType="BUS" stateCode="0" eventCode="9" event="XComponent.HelloWorld.UserObject.SayHello" communication="rabbitmq">

0 commit comments

Comments
 (0)