Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
<!-- Required so the transitive-only security pins below actually take effect. -->
<CentralPackageTransitivePinningEnabled>true</CentralPackageTransitivePinningEnabled>
<AkkaVersion>1.5.70</AkkaVersion>
<AkkaHostingVersion>1.5.70</AkkaHostingVersion>
<AkkaVersion>1.5.75-beta-1794596265</AkkaVersion>
<AkkaHostingVersion>1.5.75-beta-1794596265</AkkaHostingVersion>
<AkkaManagementVersion>1.5.70</AkkaManagementVersion>
<PetabridgeCmdVersion>1.4.5</PetabridgeCmdVersion>
<AspireVersion>13.3.5</AspireVersion>
Expand Down
50 changes: 50 additions & 0 deletions certs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <A thumbprint> (...) to <B thumbprint> (...)
```

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:<port> </dev/null 2>/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.
2 changes: 1 addition & 1 deletion scripts/generate-certs.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
11 changes: 11 additions & 0 deletions src/DrawTogether.AppHost/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,15 @@ public class DrawTogetherConfiguration
/// Only relevant when <see cref="UseAkkaManagement"/> is enabled.
/// </summary>
public int Replicas { get; set; } = 1;

/// <summary>
/// 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.
/// </summary>
public string? TlsCertificatePath { get; set; }

/// <summary>
/// Password for the PKCS#12 files in <see cref="TlsCertificatePath"/>.
/// </summary>
public string? TlsCertificatePassword { get; set; }
}
12 changes: 12 additions & 0 deletions src/DrawTogether.AppHost/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
40 changes: 29 additions & 11 deletions src/DrawTogether/Config/AkkaConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -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)
Expand All @@ -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;
}
}
3 changes: 2 additions & 1 deletion src/DrawTogether/Config/AkkaSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ public class TlsSettings
public bool Enabled { get; set; } = false;

/// <summary>
/// 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.
/// </summary>
public string? CertificatePath { get; set; }

Expand Down
Loading