fix(tls): reuse the persisted self-signed certificate across restarts (#1799)

TlsConfigSource is a MicroProfile ConfigSource, so it runs before CDI
startup and before quarkus-security registers its runtime security
providers. BouncyCastle was therefore not yet registered when the source
checked whether the persisted certificate could be reused. isSelfSigned()
calls CertificateGenerator.parseCertificate(), which failed with
"no such provider: BC", so isSelfSigned() returned false and the
self-signed certificate was needlessly regenerated on every restart.

Register BouncyCastle at the top of the constructor (before any
certificate parsing) by hoisting the existing inline registration in
generateSelfSignedCert() into a shared ensureBouncyCastleRegistered()
helper. The reuse check now parses the existing certificate successfully
and keeps it across restarts.
This commit is contained in:
Alex Banna authored and GitHub committed 2026-07-29 21:47:15 -05:00
1 parent 418ec1bdae
commit a4356f25fb
2 files changed
+57 -6

No files matched your search

@@ -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
*/