fix(dp): auto-bootstrap self-signed certificate for data protection key encryption
Generates a self-signed X.509 certificate on first run and stores it alongside DP keys in the persistent volume. Uses ProtectKeysWithCertificate to encrypt data protection keys at rest. Prevents the 'No XML encryptor configured' startup warning.
This commit is contained in:
1 parent
51589dd988
commit
8278c75cf4
1 file changed
+36
-4
@@ -1,4 +1,6 @@
|
||||
using System.IO;
|
||||
using System.Security.Cryptography;
|
||||
using System.Security.Cryptography.X509Certificates;
|
||||
using Heimdall.Application.Services;
|
||||
using Heimdall.Domain.Entities;
|
||||
using Heimdall.Infrastructure.Data;
|
||||
@@ -64,12 +66,42 @@ public static class DependencyInjection
|
||||
.AddDefaultTokenProviders();
|
||||
|
||||
// Data Protection (for camera credential encryption)
|
||||
// Keys are persisted to /var/lib/heimdall/dp-keys to survive container restarts.
|
||||
// Without this, Docker container restart permanently breaks camera credential decryption.
|
||||
services.AddDataProtection()
|
||||
.PersistKeysToFileSystem(new DirectoryInfo("/var/lib/heimdall/dp-keys"))
|
||||
// Keys are persisted to /var/lib/heimdall/dp-keys via a Docker volume.
|
||||
// Without the volume, container restart permanently breaks credential decryption.
|
||||
var dpDir = new DirectoryInfo("/var/lib/heimdall/dp-keys");
|
||||
var dpBuilder = services.AddDataProtection()
|
||||
.PersistKeysToFileSystem(dpDir)
|
||||
.SetApplicationName("Heimdall");
|
||||
|
||||
// Auto-bootstrap a self-signed certificate for key encryption.
|
||||
// This prevents the "No XML encryptor configured" warning and ensures
|
||||
// DP keys are encrypted at rest within the persistent volume.
|
||||
var certFile = new FileInfo(Path.Combine(dpDir.FullName, "dp-cert.pfx"));
|
||||
if (!certFile.Exists)
|
||||
{
|
||||
dpDir.Create();
|
||||
using var rsa = RSA.Create(4096);
|
||||
var req = new CertificateRequest(
|
||||
"CN=Heimdall-DataProtection",
|
||||
rsa,
|
||||
HashAlgorithmName.SHA256,
|
||||
RSASignaturePadding.Pkcs1);
|
||||
req.CertificateExtensions.Add(new X509BasicConstraintsExtension(false, false, 0, false));
|
||||
req.CertificateExtensions.Add(new X509KeyUsageExtension(
|
||||
X509KeyUsageFlags.KeyEncipherment | X509KeyUsageFlags.DataEncipherment, false));
|
||||
req.CertificateExtensions.Add(new X509EnhancedKeyUsageExtension(
|
||||
new OidCollection { new("1.3.6.1.4.1.311.80.1") }, false)); // Data Protection OID
|
||||
using var cert = req.CreateSelfSigned(
|
||||
DateTimeOffset.UtcNow.AddDays(-1),
|
||||
DateTimeOffset.UtcNow.AddYears(10));
|
||||
File.WriteAllBytes(certFile.FullName, cert.Export(X509ContentType.Pfx, ""));
|
||||
}
|
||||
|
||||
#pragma warning disable SYSLIB0057 // X509Certificate2(string, string) is obsolete — X509CertificateLoader not available in this SDK
|
||||
using var dpCert = new X509Certificate2(certFile.FullName, "");
|
||||
#pragma warning restore SYSLIB0057
|
||||
dpBuilder.ProtectKeysWithCertificate(dpCert);
|
||||
|
||||
// JWT
|
||||
services.AddScoped<IJwtService, JwtService>();
|
||||
|
||||
|
||||
Reference in new issue
Block a user