Skip to content

Commit 1e8d606

Browse files
Fix DotNettySslSetup being ignored when HOCON has valid SSL config (#7918) (#7919)
* Add failing test to expose DotNettySslSetup override bug (#7917) Added test case that demonstrates DotNettySslSetup settings being ignored when HOCON has valid certificate configuration. The test configures: - HOCON with valid certificate path and settings - DotNettySslSetup with different certificate and settings Expected: DotNettySslSetup should take precedence (programmatic over config) Actual: HOCON certificate is used, DotNettySslSetup is completely ignored The bug occurs because CreateOrDefault() tries HOCON first and only uses the programmatic setup as an exception fallback. This test fails and will pass once the fix is applied to make programmatic setup take precedence. Existing tests didn't catch this because they only test the exception-based fallback path (HOCON with enable-ssl=true but no certificate path). * Fix DotNettySslSetup being ignored when HOCON has valid SSL config (#7917) Changed SSL settings initialization to prioritize programmatic DotNettySslSetup over HOCON configuration, fixing the precedence order bug. Changes: - Modified DotNettyTransportSettings.Create() to check sslSettings (from DotNettySslSetup) first before parsing HOCON configuration - Changed SslSettings.Create() from private to internal to enable direct usage - Previous behavior: HOCON always tried first, programmatic setup only used as exception fallback - New behavior: Programmatic setup takes precedence, HOCON used if not provided This ensures programmatic configuration properly overrides HOCON defaults, which is the expected behavior for Setup-based configuration in Akka.NET. The bug existed since DotNettySslSetup was introduced in July 2023 (commit 588d5d6). Existing tests passed only because they triggered the exception-based fallback path (HOCON with enable-ssl=true but no certificate path).
1 parent 77ba03c commit 1e8d606

File tree

2 files changed

+54
-2
lines changed

2 files changed

+54
-2
lines changed

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

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,58 @@ public void Four_parameter_setup_should_configure_transport_settings_with_all_va
195195
Assert.True(settings.Ssl.ValidateCertificateHostname); // explicitly set to true
196196
}
197197

198+
[Fact(DisplayName = "DotNettySslSetup should override HOCON certificate configuration (Bug #7917)")]
199+
public void DotNettySslSetup_should_override_HOCON_certificate()
200+
{
201+
// This test exposes the bug where HOCON certificate wins over DotNettySslSetup
202+
// when HOCON has valid certificate configuration
203+
204+
// HOCON certificate
205+
const string hoconCertPath = "Resources/akka-validcert.pfx";
206+
var hoconCert = new X509Certificate2(hoconCertPath, Password, X509KeyStorageFlags.DefaultKeySet);
207+
208+
// Programmatic setup certificate (different from HOCON)
209+
const string setupCertPath = "Resources/akka-client-cert.pfx";
210+
var setupCert = new X509Certificate2(setupCertPath, Password, X509KeyStorageFlags.DefaultKeySet);
211+
212+
var sslSetup = new DotNettySslSetup(setupCert, suppressValidation: true, requireMutualAuthentication: false, validateCertificateHostname: true);
213+
214+
var actorSystemSetup = ActorSystemSetup.Empty
215+
.And(BootstrapSetup.Create().WithConfig(ConfigurationFactory.ParseString($@"
216+
akka {{
217+
actor.provider = ""Akka.Remote.RemoteActorRefProvider,Akka.Remote""
218+
remote.dot-netty.tcp {{
219+
port = 0
220+
hostname = ""127.0.0.1""
221+
enable-ssl = true
222+
ssl {{
223+
certificate {{
224+
path = ""{hoconCertPath}""
225+
password = ""{Password}""
226+
}}
227+
suppress-validation = false
228+
require-mutual-authentication = true
229+
validate-certificate-hostname = false
230+
}}
231+
}}
232+
}}")))
233+
.And(sslSetup);
234+
235+
using var sys = ActorSystem.Create("test", actorSystemSetup);
236+
237+
// Verify that DotNettyTransportSettings.Create uses the setup correctly
238+
var settings = DotNettyTransportSettings.Create(sys);
239+
240+
Assert.True(settings.EnableSsl);
241+
242+
// BUG: DotNettySslSetup should take precedence over HOCON, but currently HOCON wins
243+
// because CreateOrDefault tries HOCON first, and only uses the setup as an exception fallback
244+
Assert.Equal(setupCert.Thumbprint, settings.Ssl.Certificate.Thumbprint); // Should be setupCert, not hoconCert
245+
Assert.True(settings.Ssl.SuppressValidation); // From DotNettySslSetup
246+
Assert.False(settings.Ssl.RequireMutualAuthentication); // From DotNettySslSetup, not HOCON
247+
Assert.True(settings.Ssl.ValidateCertificateHostname); // From DotNettySslSetup, not HOCON
248+
}
249+
198250
#region helper classes / methods
199251

200252
protected override void AfterAll()

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ public static DotNettyTransportSettings Create(Config config, SslSettings? sslSe
203203
ServerSocketWorkerPoolSize: ComputeWorkerPoolSize(config.GetConfig("server-socket-worker-pool")),
204204
ClientSocketWorkerPoolSize: ComputeWorkerPoolSize(config.GetConfig("client-socket-worker-pool")),
205205
MaxFrameSize: ToNullableInt(config.GetByteSize("maximum-frame-size", null)) ?? 128000,
206-
Ssl: enableSsl ? SslSettings.CreateOrDefault(config.GetConfig("ssl"), sslSettings) : SslSettings.Empty,
206+
Ssl: enableSsl ? (sslSettings ?? SslSettings.Create(config.GetConfig("ssl"))) : SslSettings.Empty,
207207
DnsUseIpv6: config.GetBoolean("dns-use-ipv6"),
208208
TcpReuseAddr: ResolveTcpReuseAddrOption(config.GetString("tcp-reuse-addr", "off-for-windows")),
209209
TcpKeepAlive: config.GetBoolean("tcp-keepalive", true),
@@ -266,7 +266,7 @@ public static SslSettings CreateOrDefault(Config config, SslSettings? @default =
266266
}
267267
}
268268

269-
private static SslSettings Create(Config config)
269+
internal static SslSettings Create(Config config)
270270
{
271271
if (config.IsNullOrEmpty())
272272
throw new ConfigurationException($"Failed to create {typeof(DotNettyTransportSettings)}: DotNetty SSL HOCON config was not found (default path: `akka.remote.dot-netty.tcp.ssl`)");

0 commit comments

Comments
 (0)