From 8aaa4fb1969f74245829f71e88d0852afa2d73a7 Mon Sep 17 00:00:00 2001 From: Aaron Stannard Date: Fri, 13 Nov 2026 19:24:32 +0000 Subject: [PATCH 1/2] Bump Akka.NET to 1.5.75 beta for TLS certificate rotation Points AkkaVersion and AkkaHostingVersion at 1.5.75-beta-1794596265, which adds TlsCertificateSource for Akka.Remote's DotNetty transport. --- Directory.Packages.props | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Directory.Packages.props b/Directory.Packages.props index 9b3bb38..62491cf 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -3,8 +3,8 @@ true true - 1.5.70 - 1.5.70 + 1.5.75-beta-1794596265 + 1.5.75-beta-1794596265 1.5.70 1.4.5 13.3.5 From 7a3e9be9bfa99d928360becbe25fda4de15e78a9 Mon Sep 17 00:00:00 2001 From: Aaron Stannard Date: Fri, 13 Nov 2026 19:24:32 +0000 Subject: [PATCH 2/2] Rotate Akka.Remote TLS certificates from a folder When AkkaSettings:TlsSettings:CertificatePath names a folder, use TlsCertificateSource.FromFolder so each node picks the newest usable certificate in it and new connections use it with no restart. A single file still loads once at startup, as before. The Aspire path now applies the same TLS settings on top of the Akka.Aspire plugin's remoting set-up, which otherwise ignores them. The Aspire host gains DrawTogether:TlsCertificatePath and DrawTogether:TlsCertificatePassword (off by default) to try this locally. generate-certs.ps1 now adds client authentication to the extended key usage: with mutual TLS a folder rejects a server-only certificate. certs/README.md shows how to try a rotation with two certificates. --- certs/README.md | 50 ++++++++++++++++++++ scripts/generate-certs.ps1 | 2 +- src/DrawTogether.AppHost/Configuration.cs | 11 +++++ src/DrawTogether.AppHost/Program.cs | 12 +++++ src/DrawTogether/Config/AkkaConfiguration.cs | 40 +++++++++++----- src/DrawTogether/Config/AkkaSettings.cs | 3 +- 6 files changed, 105 insertions(+), 13 deletions(-) diff --git a/certs/README.md b/certs/README.md index 2675f2f..be8328d 100644 --- a/certs/README.md +++ b/certs/README.md @@ -66,3 +66,53 @@ To generate new test certificates: ``` This will create fresh certificates with a 2-year validity period. + +## Rotating Certificates Without a Restart + +If `CertificatePath` names a **folder** instead of a file, each node reads the folder in the +background and uses the usable certificate that was issued most recently (latest start date) +for new connections. To rotate, copy the new `.pfx` into the folder. No restart, no config change. +All `.pfx` files in the folder must share the one `CertificatePassword`. + +The committed `akka-node.pfx` only allows server authentication, so a folder rejects it: +with mutual TLS (the default) a certificate must allow both server and client authentication. +Make your own test certificates instead, for example with OpenSSL: + +```bash +mkdir -p /tmp/akka-tls && cd /tmp/akka-tls +for name in cert-a cert-b; do + openssl req -x509 -newkey rsa:2048 -nodes -keyout $name.key -out $name.crt -days 365 \ + -subj "/CN=DrawTogether.Akka.Remote" \ + -addext "keyUsage=critical,digitalSignature,keyEncipherment" \ + -addext "extendedKeyUsage=serverAuth,clientAuth" + openssl pkcs12 -export -inkey $name.key -in $name.crt -passout pass:'Test123!' -out $name.pfx + sleep 2 # cert-b must start after cert-a +done +mkdir -p tls && cp cert-a.pfx tls/ +``` + +Run the Aspire host with TLS on and the folder as the certificate path (3 replicas): + +```bash +dotnet run --project src/DrawTogether.AppHost -- \ + --DrawTogether:TlsCertificatePath=/tmp/akka-tls/tls \ + --DrawTogether:TlsCertificatePassword='Test123!' \ + --DrawTogether:Replicas=3 +``` + +Then `cp /tmp/akka-tls/cert-b.pfx /tmp/akka-tls/tls/`. Within about a minute each node logs: + +```text +TLS client and server certificate changed from (...) to (...) +``` + +Check what a node presents on a new connection (the remoting port is in the +`Remoting started; listening on addresses` log line): + +```bash +openssl s_client -connect localhost: /dev/null | openssl x509 -noout -fingerprint -sha1 -dates +``` + +Open connections keep the old certificate until they close. Remove the old `.pfx` whenever you like. +Since validation is off for these self-signed test certificates (`ValidateCertificates = false`), +nodes on the old and new certificate keep talking during the change. diff --git a/scripts/generate-certs.ps1 b/scripts/generate-certs.ps1 index b80dad6..8804425 100644 --- a/scripts/generate-certs.ps1 +++ b/scripts/generate-certs.ps1 @@ -26,7 +26,7 @@ $cert = New-SelfSignedCertificate ` -FriendlyName "DrawTogether Akka.Remote Test Certificate" ` -HashAlgorithm SHA256 ` -KeyUsage DigitalSignature, KeyEncipherment ` - -TextExtension @("2.5.29.37={text}1.3.6.1.5.5.7.3.1") # Server Authentication + -TextExtension @("2.5.29.37={text}1.3.6.1.5.5.7.3.1,1.3.6.1.5.5.7.3.2") # Server + Client Authentication (mutual TLS) Write-Host "Certificate created with thumbprint: $($cert.Thumbprint)" -ForegroundColor Green diff --git a/src/DrawTogether.AppHost/Configuration.cs b/src/DrawTogether.AppHost/Configuration.cs index 9c66332..70209ad 100644 --- a/src/DrawTogether.AppHost/Configuration.cs +++ b/src/DrawTogether.AppHost/Configuration.cs @@ -23,4 +23,15 @@ public class DrawTogetherConfiguration /// Only relevant when is enabled. /// public int Replicas { get; set; } = 1; + + /// + /// Optional. A certificate folder (or a single .pfx file) for Akka.Remote TLS. + /// When set, TLS is turned on for every DrawTogether replica. Off by default. + /// + public string? TlsCertificatePath { get; set; } + + /// + /// Password for the PKCS#12 files in . + /// + public string? TlsCertificatePassword { get; set; } } \ No newline at end of file diff --git a/src/DrawTogether.AppHost/Program.cs b/src/DrawTogether.AppHost/Program.cs index 79caa77..d1b880c 100644 --- a/src/DrawTogether.AppHost/Program.cs +++ b/src/DrawTogether.AppHost/Program.cs @@ -47,6 +47,18 @@ drawTogether.WithHttpHealthCheck("/healthz/ready"); } +if (!string.IsNullOrWhiteSpace(drawTogetherAspireConfig.TlsCertificatePath)) +{ + // Self-signed test certificates, so validation stays off. + drawTogether + .WithEnvironment("AkkaSettings__TlsSettings__Enabled", "true") + .WithEnvironment("AkkaSettings__TlsSettings__CertificatePath", + Path.GetFullPath(drawTogetherAspireConfig.TlsCertificatePath)) + .WithEnvironment("AkkaSettings__TlsSettings__CertificatePassword", + drawTogetherAspireConfig.TlsCertificatePassword ?? string.Empty) + .WithEnvironment("AkkaSettings__TlsSettings__ValidateCertificates", "false"); +} + // PBM port still needs explicit endpoint since plugin doesn't handle it drawTogether.WithEndpoint(name: "pbm", protocol: System.Net.Sockets.ProtocolType.Tcp, env: "AkkaSettings__PbmOptions__Port"); diff --git a/src/DrawTogether/Config/AkkaConfiguration.cs b/src/DrawTogether/Config/AkkaConfiguration.cs index 4ea1e0c..daa4d42 100644 --- a/src/DrawTogether/Config/AkkaConfiguration.cs +++ b/src/DrawTogether/Config/AkkaConfiguration.cs @@ -5,6 +5,7 @@ using Akka.Discovery.Config.Hosting; using Akka.Discovery.KubernetesApi; using Akka.Hosting; +using Akka.IO; using Akka.Management; using Akka.Management.Cluster.Bootstrap; using Akka.Persistence.Sql.Config; @@ -47,6 +48,13 @@ public static IServiceCollection ConfigureAkka(this IServiceCollection services, b.WithRedisDiscovery(redisConn, cfg["Akka:Cluster:ServiceName"]); }, clusterConfigure: c => c.Roles = [roleName]); + + // The plugin sets up remoting without TLS. A second WithRemoting call adds + // only the TLS settings on top of the plugin's host and port. + if (akkaSettings.TlsSettings is { Enabled: true } tls) + { + builder.WithRemoting(new RemoteOptions { EnableSsl = true, Ssl = BuildSslOptions(tls) }); + } } else { @@ -194,21 +202,11 @@ public static AkkaConfigurationBuilder ConfigureNetwork(this AkkaConfigurationBu private static void ConfigureRemoteOptionsWithTls(AkkaSettings settings) { - var tlsSettings = settings.TlsSettings!; var remoteOptions = settings.RemoteOptions; - // Load the certificate - var certificate = tlsSettings.LoadCertificate(); - if (certificate is null) - throw new InvalidOperationException("TLS is enabled but no certificate could be loaded"); - // Configure SSL through RemoteOptions remoteOptions.EnableSsl = true; - remoteOptions.Ssl = new SslOptions - { - X509Certificate = certificate, - SuppressValidation = !tlsSettings.ValidateCertificates - }; + remoteOptions.Ssl = BuildSslOptions(settings.TlsSettings!); // Update seed nodes to use akka.ssl.tcp:// protocol if present if (settings.ClusterOptions.SeedNodes?.Length > 0) @@ -218,4 +216,24 @@ private static void ConfigureRemoteOptionsWithTls(AkkaSettings settings) .ToArray(); } } + + private static SslOptions BuildSslOptions(TlsSettings tlsSettings) + { + var ssl = new SslOptions { SuppressValidation = !tlsSettings.ValidateCertificates }; + + if (Directory.Exists(tlsSettings.CertificatePath)) + { + // A folder rotates: the node picks the newest usable certificate in it, with no restart. + ssl.CertificateSource = TlsCertificateSource.FromFolder(tlsSettings.CertificatePath, + string.IsNullOrEmpty(tlsSettings.CertificatePassword) ? null : tlsSettings.CertificatePassword); + } + else + { + // A single file loads once at startup, as before. + ssl.X509Certificate = tlsSettings.LoadCertificate() + ?? throw new InvalidOperationException("TLS is enabled but no certificate could be loaded"); + } + + return ssl; + } } diff --git a/src/DrawTogether/Config/AkkaSettings.cs b/src/DrawTogether/Config/AkkaSettings.cs index 15943dc..6bd8de3 100644 --- a/src/DrawTogether/Config/AkkaSettings.cs +++ b/src/DrawTogether/Config/AkkaSettings.cs @@ -49,7 +49,8 @@ public class TlsSettings public bool Enabled { get; set; } = false; /// - /// Path to the certificate file (.pfx or .p12) + /// Path to the certificate file (.pfx or .p12), or to a folder of them. + /// A folder rotates: each node uses the newest usable certificate in it, with no restart. /// public string? CertificatePath { get; set; }