mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
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:
1 parent
418ec1bdae
commit
a4356f25fb
2 files changed
+57
-6
No files matched your search
@@ -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 defaults
|
||||
List<String> customHostnames = extractCustomHostnames();
|
||||
|
||||
+43
-1
@@ -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
|
||||
*/
|
||||
|
||||
Reference in new issue
Block a user