Skip to content

Commit 2d4cbb5

Browse files
eabaaminchenkovAliaksei MinchankouAaronontheweb
authored
[BACKPORT #6038] SSL Configuration fails even EnbleSsl property is set to false (#6043)
* SSL Configuration Fails even EnbleSsl property is set to false (#6038) * Respect EnableSsl configuration propert * Update DotNettySslSupportSpec.cs * Update DotNettySslSupportSpec.cs * Update DotNettyTransportSettings.cs * Moved enableSsl variable initialization outside return statement Co-authored-by: Aliaksei Minchankou <Aliaksei.Minchankou@nreca.coop> Co-authored-by: Aaron Stannard <aaron@petabridge.com> * SSL * (Parameter 'certificatePath') * TestKit * Fixed up assertion to no longer be whitespace sensitive * [Bug] using FluentAssertions; Co-authored-by: aminchenkov <alexei.minchenkov@gmail.com> Co-authored-by: Aliaksei Minchankou <Aliaksei.Minchankou@nreca.coop> Co-authored-by: Aaron Stannard <aaron@petabridge.com>
1 parent 9db6ec2 commit 2d4cbb5

File tree

2 files changed

+123
-26
lines changed

2 files changed

+123
-26
lines changed

src/core/Akka.Remote.Tests/Transport/DotNettySslSupportSpec.cs

+119-24
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,16 @@
66
//-----------------------------------------------------------------------
77

88
using System;
9+
using System.Linq;
10+
using System.Security.Cryptography;
911
using System.Security.Cryptography.X509Certificates;
12+
using System.Threading.Tasks;
1013
using Akka.Actor;
1114
using Akka.Configuration;
1215
using Akka.TestKit;
1316
using Xunit;
1417
using Xunit.Abstractions;
18+
using FluentAssertions;
1519
using static Akka.Util.RuntimeDetector;
1620

1721
namespace Akka.Remote.Tests.Transport
@@ -21,7 +25,7 @@ public class DotNettySslSupportSpec : AkkaSpec
2125
#region Setup / Config
2226

2327
// valid to 01/01/2037
24-
private static readonly string ValidCertPath = "Resources/akka-validcert.pfx";
28+
private const string ValidCertPath = "Resources/akka-validcert.pfx";
2529

2630
private const string Password = "password";
2731

@@ -52,6 +56,32 @@ private static Config TestConfig(string certPath, string password)
5256
}");
5357
}
5458

59+
private static Config TestConfig(bool enableSsl, string certPath, string password)
60+
{
61+
var config = ConfigurationFactory.ParseString(@"
62+
akka {
63+
loglevel = DEBUG
64+
actor.provider = ""Akka.Remote.RemoteActorRefProvider,Akka.Remote""
65+
remote {
66+
dot-netty.tcp {
67+
port = 0
68+
hostname = ""127.0.0.1""
69+
enable-ssl = """ + enableSsl.ToString().ToLowerInvariant() + @"""
70+
log-transport = true
71+
}
72+
}
73+
}");
74+
return !enableSsl
75+
? config
76+
: config.WithFallback(@"akka.remote.dot-netty.tcp.ssl {
77+
suppress-validation = """ + enableSsl.ToString().ToLowerInvariant() + @"""
78+
certificate {
79+
path = """ + certPath + @"""
80+
password = """ + password + @"""
81+
}
82+
}");
83+
}
84+
5585
private static Config TestThumbprintConfig(string thumbPrint)
5686
{
5787
var config = ConfigurationFactory.ParseString(@"
@@ -80,35 +110,47 @@ private static Config TestThumbprintConfig(string thumbPrint)
80110
}");
81111
}
82112

83-
private ActorSystem sys2;
84-
private Address address1;
85-
private Address address2;
113+
private ActorSystem _sys2;
114+
private Address _address1;
115+
private Address _address2;
86116

87-
private ActorPath echoPath;
117+
private ActorPath _echoPath;
88118

89119
private void Setup(string certPath, string password)
90120
{
91-
sys2 = ActorSystem.Create("sys2", TestConfig(certPath, password));
92-
InitializeLogger(sys2);
121+
_sys2 = ActorSystem.Create("sys2", TestConfig(certPath, password));
122+
InitializeLogger(_sys2);
123+
124+
var echo = _sys2.ActorOf(Props.Create<Echo>(), "echo");
125+
126+
_address1 = RARP.For(Sys).Provider.DefaultAddress;
127+
_address2 = RARP.For(_sys2).Provider.DefaultAddress;
128+
_echoPath = new RootActorPath(_address2) / "user" / "echo";
129+
}
130+
131+
private void Setup(bool enableSsl, string certPath, string password)
132+
{
133+
_sys2 = ActorSystem.Create("sys2", TestConfig(enableSsl, certPath, password));
134+
InitializeLogger(_sys2);
93135

94-
var echo = sys2.ActorOf(Props.Create<Echo>(), "echo");
136+
var echo = _sys2.ActorOf(Props.Create<Echo>(), "echo");
95137

96-
address1 = RARP.For(Sys).Provider.DefaultAddress;
97-
address2 = RARP.For(sys2).Provider.DefaultAddress;
98-
echoPath = new RootActorPath(address2) / "user" / "echo";
138+
_address1 = RARP.For(Sys).Provider.DefaultAddress;
139+
_address2 = RARP.For(_sys2).Provider.DefaultAddress;
140+
_echoPath = new RootActorPath(_address2) / "user" / "echo";
99141
}
100142

101143
private void SetupThumbprint(string certPath, string password)
102144
{
103145
InstallCert();
104-
sys2 = ActorSystem.Create("sys2", TestThumbprintConfig(Thumbprint));
105-
InitializeLogger(sys2);
146+
_sys2 = ActorSystem.Create("sys2", TestThumbprintConfig(Thumbprint));
147+
InitializeLogger(_sys2);
106148

107-
var echo = sys2.ActorOf(Props.Create<Echo>(), "echo");
149+
var echo = _sys2.ActorOf(Props.Create<Echo>(), "echo");
108150

109-
address1 = RARP.For(Sys).Provider.DefaultAddress;
110-
address2 = RARP.For(sys2).Provider.DefaultAddress;
111-
echoPath = new RootActorPath(address2) / "user" / "echo";
151+
_address1 = RARP.For(Sys).Provider.DefaultAddress;
152+
_address2 = RARP.For(_sys2).Provider.DefaultAddress;
153+
_echoPath = new RootActorPath(_address2) / "user" / "echo";
112154
}
113155

114156
#endregion
@@ -134,12 +176,12 @@ public void Secure_transport_should_be_possible_between_systems_sharing_the_same
134176

135177
AwaitAssert(() =>
136178
{
137-
Sys.ActorSelection(echoPath).Tell("hello", probe.Ref);
179+
Sys.ActorSelection(_echoPath).Tell("hello", probe.Ref);
138180
probe.ExpectMsg("hello", TimeSpan.FromSeconds(3));
139181
}, TimeSpan.FromSeconds(30), TimeSpan.FromMilliseconds(100));
140182
}
141183

142-
[Fact]
184+
[Fact(Skip = "Racy in Azure AzDo CI/CD")]
143185
public void Secure_transport_should_be_possible_between_systems_using_thumbprint()
144186
{
145187
// skip this test due to linux/mono certificate issues
@@ -154,7 +196,7 @@ public void Secure_transport_should_be_possible_between_systems_using_thumbprint
154196
{
155197
AwaitAssert(() =>
156198
{
157-
Sys.ActorSelection(echoPath).Tell("hello", probe.Ref);
199+
Sys.ActorSelection(_echoPath).Tell("hello", probe.Ref);
158200
probe.ExpectMsg("hello", TimeSpan.FromMilliseconds(100));
159201
}, TimeSpan.FromSeconds(3), TimeSpan.FromMilliseconds(100));
160202
});
@@ -173,20 +215,62 @@ public void Secure_transport_should_NOT_be_possible_between_systems_using_SSL_an
173215
var probe = CreateTestProbe();
174216
Assert.Throws<RemoteTransportException>(() =>
175217
{
176-
Sys.ActorSelection(echoPath).Tell("hello", probe.Ref);
218+
Sys.ActorSelection(_echoPath).Tell("hello", probe.Ref);
177219
probe.ExpectNoMsg();
178220
});
179221
}
180222

181-
#region helper classes / methods
223+
[Fact]
224+
public void If_EnableSsl_configuration_is_true_but_not_valid_certificate_is_provided_than_ArgumentNullException_should_be_thrown()
225+
{
226+
// skip this test due to linux/mono certificate issues
227+
if (IsMono) return;
228+
229+
var aggregateException = Assert.Throws<AggregateException>( () =>
230+
{
231+
Setup(true, null, Password);
232+
});
233+
234+
var realException = GetInnerMostException<ArgumentNullException>(aggregateException);
235+
Assert.NotNull(realException);
236+
realException.Message.Should().Contain("Path to SSL certificate was not found (by default it can be found under `akka.remote.dot-netty.tcp.ssl.certificate.path`");
237+
}
238+
239+
[Fact]
240+
public void If_EnableSsl_configuration_is_true_but_not_valid_certificate_password_is_provided_than_WindowsCryptographicException_should_be_thrown()
241+
{
242+
// skip this test due to linux/mono certificate issues
243+
if (IsMono) return;
244+
245+
var aggregateException = Assert.Throws<AggregateException>(() =>
246+
{
247+
Setup(true, ValidCertPath, null);
248+
});
182249

250+
var realException = GetInnerMostException<CryptographicException>(aggregateException);
251+
Assert.NotNull(realException);
252+
// TODO: this error message is not correct, but wanted to keep this assertion here in case someone else
253+
// wants to fix it in the future.
254+
//Assert.Equal("The specified network password is not correct.", realException.Message);
255+
}
256+
257+
[Theory]
258+
[InlineData(ValidCertPath, null)]
259+
[InlineData(null, Password)]
260+
[InlineData(null, null)]
261+
[InlineData(ValidCertPath, Password)]
262+
public void If_EnableSsl_configuration_is_false_than_no_exception_should_be_thrown_even_no_cert_detail_were_provided(string certPath, string password)
263+
{
264+
Setup(false, certPath, password);
265+
}
183266

267+
#region helper classes / methods
184268
protected override void Dispose(bool disposing)
185269
{
186270
base.Dispose(disposing);
187271
if (disposing)
188272
{
189-
Shutdown(sys2, TimeSpan.FromSeconds(3));
273+
Shutdown(_sys2, TimeSpan.FromSeconds(3));
190274
}
191275

192276
}
@@ -217,7 +301,18 @@ private void RemoveCert()
217301
}
218302
}
219303

220-
public class Echo : ReceiveActor
304+
private T GetInnerMostException<T>(Exception ex) where T : Exception
305+
{
306+
Exception currentEx = ex;
307+
while (currentEx.InnerException != null)
308+
{
309+
currentEx = currentEx.InnerException;
310+
}
311+
312+
return currentEx as T;
313+
}
314+
315+
private class Echo : ReceiveActor
221316
{
222317
public Echo()
223318
{

src/core/Akka.Remote/Transport/DotNetty/DotNettyTransportSettings.cs

+4-2
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,11 @@ public static DotNettyTransportSettings Create(Config config)
7676

7777
var batchWriterSettings = new BatchWriterSettings(config.GetConfig("batching"));
7878

79+
var enableSsl = config.GetBoolean("enable-ssl", false);
80+
7981
return new DotNettyTransportSettings(
8082
transportMode: transportMode == "tcp" ? TransportMode.Tcp : TransportMode.Udp,
81-
enableSsl: config.GetBoolean("enable-ssl", false),
83+
enableSsl: enableSsl,
8284
connectTimeout: config.GetTimeSpan("connection-timeout", TimeSpan.FromSeconds(15)),
8385
hostname: host,
8486
publicHostname: !string.IsNullOrEmpty(publicHost) ? publicHost : host,
@@ -87,7 +89,7 @@ public static DotNettyTransportSettings Create(Config config)
8789
serverSocketWorkerPoolSize: ComputeWorkerPoolSize(config.GetConfig("server-socket-worker-pool")),
8890
clientSocketWorkerPoolSize: ComputeWorkerPoolSize(config.GetConfig("client-socket-worker-pool")),
8991
maxFrameSize: ToNullableInt(config.GetByteSize("maximum-frame-size", null)) ?? 128000,
90-
ssl: config.HasPath("ssl") ? SslSettings.Create(config.GetConfig("ssl")) : SslSettings.Empty,
92+
ssl: config.HasPath("ssl") && enableSsl ? SslSettings.Create(config.GetConfig("ssl")) : SslSettings.Empty,
9193
dnsUseIpv6: config.GetBoolean("dns-use-ipv6", false),
9294
tcpReuseAddr: ResolveTcpReuseAddrOption(config.GetString("tcp-reuse-addr", "off-for-windows")),
9395
tcpKeepAlive: config.GetBoolean("tcp-keepalive", true),

0 commit comments

Comments
 (0)