Light, fluffy, and always free - The AWS Local Emulator alternative
fix(tls): reuse the persisted self-signed certificate across restarts (#1799)
Alex Bannaa4356f25fb3c
diff --git a/src/main/java/io/github/hectorvent/floci/config/TlsConfigSource.java b/src/main/java/io/github/hectorvent/floci/config/TlsConfigSource.javaindex fdfee24d4b..9c986eed52 100644--- a/src/main/java/io/github/hectorvent/floci/config/TlsConfigSource.java+++ b/src/main/java/io/github/hectorvent/floci/config/TlsConfigSource.java@@ -63,6 +63,13 @@ public class TlsConfigSource implements ConfigSource {return;}+ // BouncyCastle may not be registered yet — this ConfigSource runs before CDI (@Startup beans)+ // and before quarkus-security's runtime provider registration. Register it up front so BOTH+ // certificate parsing (isSelfSigned/parseCertificate) and generation work; otherwise+ // parseCertificate fails "no such provider: BC", isSelfSigned returns false, and the persisted+ // self-signed certificate is needlessly regenerated on every restart.+ ensureBouncyCastleRegistered();+String certPath = resolveProperty("floci.tls.cert-path", "");String keyPath = resolveProperty("floci.tls.key-path", "");String selfSigned = resolveProperty("floci.tls.self-signed", "true");@@ -162,14 +169,16 @@ public class TlsConfigSource implements ConfigSource {return defaultValue;}+ private static void ensureBouncyCastleRegistered() {+ if (Security.getProvider(BouncyCastleProvider.PROVIDER_NAME) == null) {+ Security.addProvider(new BouncyCastleProvider());+ }+ }+private void generateSelfSignedCert(Path tlsDir, Path certFile, Path keyFile) {try {Files.createDirectories(tlsDir);-- // BouncyCastle may not be registered yet — ConfigSource runs before CDI- if (Security.getProvider(BouncyCastleProvider.PROVIDER_NAME) == null) {- Security.addProvider(new BouncyCastleProvider());- }+ ensureBouncyCastleRegistered();// Extract custom hostnames and combine with defaultsList<String> customHostnames = extractCustomHostnames();diff --git a/src/test/java/io/github/hectorvent/floci/config/TlsConfigSourceCertificateReuseTest.java b/src/test/java/io/github/hectorvent/floci/config/TlsConfigSourceCertificateReuseTest.javaindex 6bd2ed05e6..5db4c3f619 100644--- a/src/test/java/io/github/hectorvent/floci/config/TlsConfigSourceCertificateReuseTest.java+++ b/src/test/java/io/github/hectorvent/floci/config/TlsConfigSourceCertificateReuseTest.java@@ -175,10 +175,52 @@ class TlsConfigSourceCertificateReuseTest {assertEquals(initialModifiedTime, newModifiedTime,"Certificate should be reused (same timestamp)");- assertEquals(initialMetadata, newMetadata,+ assertEquals(initialMetadata, newMetadata,"Metadata should be unchanged");}+ /**+ * Regression: TlsConfigSource is a MicroProfile ConfigSource, so it runs before CDI and before+ * the security provider is registered — BouncyCastle is NOT yet available. Previously+ * parseCertificate then failed with "no such provider: BC", isSelfSigned returned false, and the+ * persisted certificate was regenerated on every restart. The constructor now registers+ * BouncyCastle up front, so an existing certificate is reused even when BC was not pre-registered.+ */+ @Test+ void testCertificateReusedWhenBouncyCastleNotPreRegistered() throws Exception {+ System.setProperty("floci.hostname", "floci");+ System.setProperty("floci.tls.enabled", "true");+ System.setProperty("floci.tls.self-signed", "true");+ System.setProperty("floci.storage.persistent-path", tempDir.toString());++ // First boot: generate the certificate.+ new TlsConfigSource();+ Path certFile = tempDir.resolve("tls").resolve("floci-selfsigned.crt");+ assertTrue(Files.exists(certFile), "Initial certificate should be generated");+ long initialModifiedTime = Files.getLastModifiedTime(certFile).toMillis();+ String initialCert = Files.readString(certFile);++ // Ensure a regeneration would be detectable via timestamp.+ Thread.sleep(100);++ // Second boot with BouncyCastle NOT registered — mirrors the real ConfigSource-before-CDI+ // ordering. The constructor must register it and REUSE the existing cert (not regenerate).+ boolean wasRegistered = Security.getProvider(BouncyCastleProvider.PROVIDER_NAME) != null;+ Security.removeProvider(BouncyCastleProvider.PROVIDER_NAME);+ try {+ new TlsConfigSource();+ } finally {+ if (wasRegistered && Security.getProvider(BouncyCastleProvider.PROVIDER_NAME) == null) {+ Security.addProvider(new BouncyCastleProvider());+ }+ }++ assertEquals(initialModifiedTime, Files.getLastModifiedTime(certFile).toMillis(),+ "Certificate should be reused (same timestamp) even when BouncyCastle was not pre-registered");+ assertEquals(initialCert, Files.readString(certFile),+ "Certificate content should be unchanged (reused, not regenerated)");+ }+/*** Test that certificate is regenerated when base URL hostname changes*/