From 86906ebd9034844915c976462bbd733fa77d1944 Mon Sep 17 00:00:00 2001 From: Hector Ventura Date: Tue, 21 Apr 2026 23:03:24 -0500 Subject: [PATCH] fix(native,acm): fix native image reflection for acm (#559) --- pom.xml | 4 + .../core/common/BouncyCastleInitializer.java | 36 ++---- .../META-INF/native-image/reflect-config.json | 111 ++++++++++++++++-- .../native-image/resource-config.json | 5 + src/main/resources/application.yml | 29 +++-- 5 files changed, 134 insertions(+), 51 deletions(-) diff --git a/pom.xml b/pom.xml index e24ab5d618..e3c9fa5419 100644 --- a/pom.xml +++ b/pom.xml @@ -124,6 +124,10 @@ + + io.quarkus + quarkus-security + org.bouncycastle bcprov-jdk18on diff --git a/src/main/java/io/github/hectorvent/floci/core/common/BouncyCastleInitializer.java b/src/main/java/io/github/hectorvent/floci/core/common/BouncyCastleInitializer.java index 8cc6c5247a..ce7116e979 100644 --- a/src/main/java/io/github/hectorvent/floci/core/common/BouncyCastleInitializer.java +++ b/src/main/java/io/github/hectorvent/floci/core/common/BouncyCastleInitializer.java @@ -2,24 +2,15 @@ package io.github.hectorvent.floci.core.common; import io.quarkus.runtime.Startup; import jakarta.enterprise.context.ApplicationScoped; -import org.bouncycastle.jcajce.provider.asymmetric.EC; import org.bouncycastle.jce.provider.BouncyCastleProvider; import org.jboss.logging.Logger; import java.security.Security; /** - * Ensures BouncyCastle security provider is registered before any CDI beans are injected. - * - *

This class uses a static initializer block to guarantee the provider is registered - * during class loading, which happens before CDI injection. The {@code @Startup} annotation - * ensures this class is loaded early in the application lifecycle.

- * - *

Design decision: Using static initializer + @Startup instead of @Observes StartupEvent - * because StartupEvent observers run after CDI context is ready, which may be too late if - * other beans depend on BouncyCastle during their construction.

- * - * @see Quarkus CDI Startup + * Ensures BouncyCastle security provider is registered. + * With quarkus-security extension and quarkus.security.security-providers=BC, + * this is handled by Quarkus. */ @ApplicationScoped @Startup @@ -28,27 +19,14 @@ public class BouncyCastleInitializer { private static final Logger LOG = Logger.getLogger(BouncyCastleInitializer.class); static { - BouncyCastleProvider provider = new BouncyCastleProvider(); - // BC's ClassUtil.loadClass (used during provider setup) uses ClassLoader.loadClass, - // which in GraalVM native image only finds classes that are directly allocated in - // reachable code — not classes listed only in reflect-config.json or via class literals. - // If EC.Mappings failed to load internally, configure EC algorithms explicitly. - // Guard prevents IllegalStateException from double-registration in regular JVM mode. - EC.Mappings ecMappings = new EC.Mappings(); - if (provider.getService("KeyPairGenerator", "EC") == null) { - ecMappings.configure(provider); - } if (Security.getProvider(BouncyCastleProvider.PROVIDER_NAME) == null) { - Security.addProvider(provider); - LOG.info("Registered BouncyCastle security provider (static initializer)"); + Security.addProvider(new BouncyCastleProvider()); + LOG.info("Registered BouncyCastle security provider (manual fallback)"); + } else { + LOG.info("BouncyCastle provider already registered by Quarkus"); } } - /** - * No-op constructor. The actual initialization happens in the static block above. - * This bean exists solely to trigger class loading via {@code @Startup}. - */ public BouncyCastleInitializer() { - // Static initializer has already run by the time this constructor is called } } diff --git a/src/main/resources/META-INF/native-image/reflect-config.json b/src/main/resources/META-INF/native-image/reflect-config.json index 17fc5bf7fe..8af499b1b7 100644 --- a/src/main/resources/META-INF/native-image/reflect-config.json +++ b/src/main/resources/META-INF/native-image/reflect-config.json @@ -2345,57 +2345,148 @@ { "name": "org.bouncycastle.jce.provider.BouncyCastleProvider", "allDeclaredConstructors": true, + "allPublicConstructors": true, "allDeclaredMethods": true, "allDeclaredFields": true }, { "name": "org.bouncycastle.jcajce.provider.asymmetric.rsa.KeyFactorySpi", "allDeclaredConstructors": true, - "allDeclaredMethods": true + "allPublicConstructors": true, + "allDeclaredMethods": true, + "allDeclaredFields": true }, { "name": "org.bouncycastle.jcajce.provider.asymmetric.ec.KeyFactorySpi$EC", "allDeclaredConstructors": true, - "allDeclaredMethods": true + "allPublicConstructors": true, + "allDeclaredMethods": true, + "allDeclaredFields": true }, { "name": "org.bouncycastle.jcajce.provider.asymmetric.rsa.KeyPairGeneratorSpi", "allDeclaredConstructors": true, - "allDeclaredMethods": true + "allPublicConstructors": true, + "allDeclaredMethods": true, + "allDeclaredFields": true }, { "name": "org.bouncycastle.jcajce.provider.asymmetric.ec.KeyPairGeneratorSpi$EC", "allDeclaredConstructors": true, - "allDeclaredMethods": true + "allPublicConstructors": true, + "allDeclaredMethods": true, + "allDeclaredFields": true + }, + { + "name": "org.bouncycastle.jcajce.provider.asymmetric.x509.CertificateFactory", + "allDeclaredConstructors": true, + "allPublicConstructors": true, + "allDeclaredMethods": true, + "allDeclaredFields": true }, { "name": "org.bouncycastle.jcajce.provider.digest.SHA256$Digest", "allDeclaredConstructors": true, - "allDeclaredMethods": true + "allPublicConstructors": true, + "allDeclaredMethods": true, + "allDeclaredFields": true }, { "name": "org.bouncycastle.jcajce.provider.digest.SHA384$Digest", "allDeclaredConstructors": true, - "allDeclaredMethods": true + "allPublicConstructors": true, + "allDeclaredMethods": true, + "allDeclaredFields": true }, { "name": "org.bouncycastle.jcajce.provider.asymmetric.rsa.DigestSignatureSpi$SHA256", "allDeclaredConstructors": true, - "allDeclaredMethods": true + "allPublicConstructors": true, + "allDeclaredMethods": true, + "allDeclaredFields": true }, { "name": "org.bouncycastle.jcajce.provider.asymmetric.ec.SignatureSpi$ecDSA256", "allDeclaredConstructors": true, - "allDeclaredMethods": true + "allPublicConstructors": true, + "allDeclaredMethods": true, + "allDeclaredFields": true }, { "name": "org.bouncycastle.jcajce.provider.symmetric.AES$ECB", "allDeclaredConstructors": true, - "allDeclaredMethods": true + "allPublicConstructors": true, + "allDeclaredMethods": true, + "allDeclaredFields": true }, { "name": "org.bouncycastle.jcajce.provider.symmetric.PBEPBKDF2$PBKDF2withSHA256", "allDeclaredConstructors": true, - "allDeclaredMethods": true + "allPublicConstructors": true, + "allDeclaredMethods": true, + "allDeclaredFields": true + }, + { + "name": "org.bouncycastle.jcajce.provider.digest.SHA512$Digest", + "allDeclaredConstructors": true, + "allPublicConstructors": true, + "allDeclaredMethods": true, + "allDeclaredFields": true + }, + { + "name": "org.bouncycastle.jcajce.provider.asymmetric.rsa.DigestSignatureSpi$SHA512", + "allDeclaredConstructors": true, + "allPublicConstructors": true, + "allDeclaredMethods": true, + "allDeclaredFields": true + }, + { + "name": "org.bouncycastle.jcajce.provider.asymmetric.ec.SignatureSpi$ecDSA512", + "allDeclaredConstructors": true, + "allPublicConstructors": true, + "allDeclaredMethods": true, + "allDeclaredFields": true + }, + { + "name": "org.bouncycastle.jcajce.provider.symmetric.AES$CBC", + "allDeclaredConstructors": true, + "allPublicConstructors": true, + "allDeclaredMethods": true, + "allDeclaredFields": true + }, + { + "name": "org.bouncycastle.jcajce.provider.asymmetric.EC$Mappings", + "allDeclaredConstructors": true, + "allPublicConstructors": true, + "allDeclaredMethods": true, + "allDeclaredFields": true + }, + { + "name": "org.bouncycastle.jcajce.provider.asymmetric.RSA$Mappings", + "allDeclaredConstructors": true, + "allPublicConstructors": true, + "allDeclaredMethods": true, + "allDeclaredFields": true + }, + { + "name": "org.bouncycastle.jcajce.provider.asymmetric.X509$Mappings", + "allDeclaredConstructors": true, + "allPublicConstructors": true, + "allDeclaredMethods": true, + "allDeclaredFields": true + }, + { + "name": "org.bouncycastle.jcajce.provider.digest.SHA512$Mappings", + "allDeclaredConstructors": true, + "allPublicConstructors": true, + "allDeclaredMethods": true, + "allDeclaredFields": true + }, + { + "name": "org.bouncycastle.jcajce.provider.symmetric.AES$Mappings", + "allDeclaredConstructors": true, + "allPublicConstructors": true, + "allDeclaredMethods": true, + "allDeclaredFields": true } ] diff --git a/src/main/resources/META-INF/native-image/resource-config.json b/src/main/resources/META-INF/native-image/resource-config.json index ecde45ea96..4b3134dccf 100644 --- a/src/main/resources/META-INF/native-image/resource-config.json +++ b/src/main/resources/META-INF/native-image/resource-config.json @@ -1,4 +1,9 @@ { + "resources": { + "includes": [ + {"pattern": "META-INF/services/java.security.Provider"} + ] + }, "bundles": [ {"name": "com.sun.org.apache.xerces.internal.impl.msg.XMLMessages"}, {"name": "com.sun.org.apache.xerces.internal.impl.msg.XMLSerializerMessages"}, diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 063d55b636..92bdf2563a 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -4,23 +4,28 @@ quarkus: host: 0.0.0.0 limits: max-body-size: ${floci.max-request-size}M + security: + security-providers: BC shutdown: # Enables the pre-shutdown phase so stop hooks run before the HTTP server stops. # No 'delay' value is set - observers run synchronously, no artificial sleep is introduced. delay-enabled: true native: - additional-build-args: >- - --initialize-at-run-time=org.apache.hc.client5.http.impl.auth.NTLMEngineImpl, - --initialize-at-run-time=io.vertx.ext.mail.impl.sasl.NTLMEngineImpl, - --initialize-at-run-time=com.github.dockerjava.transport.NamedPipeSocket$Kernel32, - --initialize-at-run-time=org.bouncycastle.jcajce.provider.drbg.DRBG$Default, - --initialize-at-run-time=org.bouncycastle.jcajce.provider.drbg.DRBG$NonceAndIV, - --initialize-at-run-time=org.bouncycastle.crypto.prng.SP800SecureRandom, - --initialize-at-run-time=org.bouncycastle.jce.provider.BouncyCastleProvider, - --initialize-at-run-time=io.github.hectorvent.floci.core.common.BouncyCastleInitializer, - --initialize-at-run-time=io.github.hectorvent.floci.services.acm.AcmService, - --initialize-at-run-time=io.github.hectorvent.floci.services.acm.CertificateGenerator, - --initialize-at-run-time=io.github.hectorvent.floci.services.cognito.CognitoSrpHelper + additional-build-args: + - --report-unsupported-elements-at-runtime + - --allow-incomplete-classpath + - --enable-url-protocols=http,https + - --initialize-at-run-time=io.vertx.ext.mail.impl.sasl.NTLMEngineImpl + - --initialize-at-run-time=com.github.dockerjava.transport.NamedPipeSocket$Kernel32 + - --initialize-at-run-time=org.bouncycastle.jcajce.provider.drbg.DRBG$Default + - --initialize-at-run-time=org.bouncycastle.jcajce.provider.drbg.DRBG$NonceAndIV + - --initialize-at-run-time=org.bouncycastle.crypto.prng.SP800SecureRandom + - --initialize-at-run-time=org.bouncycastle.jcajce.provider.asymmetric.rsa.KeyPairGeneratorSpi + - --initialize-at-run-time=org.bouncycastle.jcajce.provider.asymmetric.x509.CertificateFactory + - --initialize-at-run-time=io.github.hectorvent.floci.core.common.BouncyCastleInitializer + - --initialize-at-run-time=io.github.hectorvent.floci.services.acm.AcmService + - --initialize-at-run-time=io.github.hectorvent.floci.services.acm.CertificateGenerator + - --initialize-at-run-time=io.github.hectorvent.floci.services.cognito.CognitoSrpHelper floci: port: 4566