davsclaus commented on code in PR #30945: URL: https://github.com/apache/camel/pull/30945#discussion_r4726254731
########## components/camel-pqc/src/main/java/org/apache/camel/component/pqc/BouncyCastlePqcAdapterSupport.java: ########## @@ -0,0 +1,110 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.pqc; + +import java.security.GeneralSecurityException; +import java.security.Provider; +import java.util.ServiceConfigurationError; +import java.util.ServiceLoader; + +import org.apache.camel.component.pqc.spi.Bc186PqcAdapter; +import org.apache.camel.component.pqc.spi.Bc187PqcAdapter; +import org.apache.camel.component.pqc.spi.PqcBouncyCastleAdapter; + +/** Selects a typed, version-specific Bouncy Castle adapter using the installed provider version. */ +public final class BouncyCastlePqcAdapterSupport { + + private BouncyCastlePqcAdapterSupport() { + } + + public static PqcBouncyCastleAdapter adapter() { Review Comment: This is resolved again on every call (provider lookup + `ServiceLoader` scan), and it is reached from every `sign`. Please resolve once and cache it. ########## components/camel-pqc/src/main/java/org/apache/camel/component/pqc/BouncyCastlePqcAdapterSupport.java: ########## @@ -0,0 +1,110 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.pqc; + +import java.security.GeneralSecurityException; +import java.security.Provider; +import java.util.ServiceConfigurationError; +import java.util.ServiceLoader; + +import org.apache.camel.component.pqc.spi.Bc186PqcAdapter; +import org.apache.camel.component.pqc.spi.Bc187PqcAdapter; +import org.apache.camel.component.pqc.spi.PqcBouncyCastleAdapter; + +/** Selects a typed, version-specific Bouncy Castle adapter using the installed provider version. */ +public final class BouncyCastlePqcAdapterSupport { + + private BouncyCastlePqcAdapterSupport() { + } + + public static PqcBouncyCastleAdapter adapter() { + final Provider provider; + try { + provider = BouncyCastleSupport.getProvider("KeyPairGenerator", "LMS"); + } catch (GeneralSecurityException e) { + throw new IllegalStateException("Cannot determine the installed Bouncy Castle version", e); + } + + String version = provider.getVersionStr(); + ClassLoader providerLoader = provider.getClass().getClassLoader(); + if (version.equals("1.86") || version.startsWith("1.86.")) { + return load(Bc186PqcAdapter.class, "1.86", providerLoader); + } + if (version.equals("1.87") || version.startsWith("1.87.")) { + return load(Bc187PqcAdapter.class, "1.87", providerLoader); + } + throw new IllegalStateException( Review Comment: Blocker: any BC version other than 1.86.x/1.87.x ends here (1.85, the next 1.88, vendor builds). Because `getStatefulKeyRemaining()` and the `parameterSpec` resolution go through this, it takes down signing for every algorithm, not just LMS/XMSS. Could it pick by available capability (new LMS/XMSS types first, then the old ones) and default sensibly for an unknown version instead of throwing? ########## components/camel-pqc/src/main/java/org/apache/camel/component/pqc/BouncyCastlePqcAdapterSupport.java: ########## @@ -0,0 +1,110 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.pqc; + +import java.security.GeneralSecurityException; +import java.security.Provider; +import java.util.ServiceConfigurationError; +import java.util.ServiceLoader; + +import org.apache.camel.component.pqc.spi.Bc186PqcAdapter; +import org.apache.camel.component.pqc.spi.Bc187PqcAdapter; +import org.apache.camel.component.pqc.spi.PqcBouncyCastleAdapter; + +/** Selects a typed, version-specific Bouncy Castle adapter using the installed provider version. */ +public final class BouncyCastlePqcAdapterSupport { + + private BouncyCastlePqcAdapterSupport() { + } + + public static PqcBouncyCastleAdapter adapter() { + final Provider provider; + try { + provider = BouncyCastleSupport.getProvider("KeyPairGenerator", "LMS"); + } catch (GeneralSecurityException e) { + throw new IllegalStateException("Cannot determine the installed Bouncy Castle version", e); + } + + String version = provider.getVersionStr(); + ClassLoader providerLoader = provider.getClass().getClassLoader(); + if (version.equals("1.86") || version.startsWith("1.86.")) { + return load(Bc186PqcAdapter.class, "1.86", providerLoader); + } + if (version.equals("1.87") || version.startsWith("1.87.")) { + return load(Bc187PqcAdapter.class, "1.87", providerLoader); + } + throw new IllegalStateException( + "camel-pqc has typed compatibility adapters for Bouncy Castle 1.86 and 1.87; " + + "the installed provider is " + provider.getName() + " " + version); + } + + private static <T extends PqcBouncyCastleAdapter> T load( + Class<T> serviceType, String expectedVersion, ClassLoader providerLoader) { + ClassLoader contextLoader = Thread.currentThread().getContextClassLoader(); + ClassLoader componentLoader = BouncyCastlePqcAdapterSupport.class.getClassLoader(); + ClassLoader[] loaders = { providerLoader, contextLoader, componentLoader }; + Throwable firstFailure = null; + + for (int i = 0; i < loaders.length; i++) { + ClassLoader loader = loaders[i]; + if (loader == null || alreadyTried(loaders, i, loader)) { + continue; + } + try { + var adapter = ServiceLoader.load(serviceType, loader).findFirst(); Review Comment: Camel normally discovers pluggable implementations through the `FactoryFinder` of the `CamelContext` (`META-INF/services/org/apache/camel/...`) rather than `java.util.ServiceLoader` over several class loaders. That keeps it working with Spring Boot, Quarkus and Karaf class loading. ########## components/camel-pqc/src/main/java/org/apache/camel/component/pqc/PQCProducer.java: ########## @@ -326,22 +321,39 @@ private String currentAlgorithm() { private PQCMetrics createMetrics() { CamelContext camelContext = getEndpoint().getCamelContext(); - // Only touch Micrometer if it is actually on the classpath, keeping it an optional dependency. - // Use reflection to avoid a static class reference that GraalVM native image would follow. - if (camelContext.getClassResolver().resolveClass("io.micrometer.core.instrument.MeterRegistry") == null) { + ClassLoader contextLoader = Thread.currentThread().getContextClassLoader(); + ClassLoader componentLoader = PQCMetricsProvider.class.getClassLoader(); + + PQCMetrics metrics = createMetrics(camelContext, contextLoader); + if (metrics != PQCMetrics.NOOP) { + return metrics; + } + if (componentLoader != null && componentLoader != contextLoader) { + metrics = createMetrics(camelContext, componentLoader); + if (metrics != PQCMetrics.NOOP) { + return metrics; + } + } + return PQCMetrics.NOOP; + } + + /** Loads optional metrics integrations through the Camel-owned SPI; no reflective class lookup is used. */ + private PQCMetrics createMetrics(CamelContext camelContext, ClassLoader classLoader) { + if (classLoader == null) { return PQCMetrics.NOOP; } try { - Class<?> clazz = camelContext.getClassResolver() - .resolveClass("org.apache.camel.component.pqc.metrics.PQCMicrometerMetrics"); - if (clazz == null) { - return PQCMetrics.NOOP; + for (PQCMetricsProvider provider : ServiceLoader.load(PQCMetricsProvider.class, classLoader)) { Review Comment: Same as for the adapter: `FactoryFinder` is the Camel way to find an optional implementation. Also, moving the Micrometer metrics to `camel-pqc-micrometer` means existing users silently lose them on upgrade. That needs an upgrade-guide entry, and is probably better as a separate PR. ########## components/camel-pqc/src/main/java/org/apache/camel/component/pqc/PQCProducer.java: ########## @@ -1616,29 +1614,15 @@ private void warnKeyUsageNotRecorded(boolean stateful, String reason) { * Returns the remaining signatures for a stateful private key, or -1 if the key is not stateful. */ static long getStatefulKeyRemaining(PrivateKey privateKey) { - if (privateKey instanceof XMSSPrivateKey xmssPrivateKey) { - return xmssPrivateKey.getUsagesRemaining(); - } else if (privateKey instanceof XMSSMTPrivateKey xmssmtPrivateKey) { - return xmssmtPrivateKey.getUsagesRemaining(); - } else if (privateKey instanceof LMSPrivateKey lmsPrivateKey) { - return lmsPrivateKey.getUsagesRemaining(); - } - return -1; + return BouncyCastlePqcCompatibility.getStatefulKeyRemaining(privateKey); Review Comment: On main this was an `instanceof` check. Now it goes through the version-gated adapter, and `claimStatefulKeyBeforeSign()` calls it for every sign, e.g. ML-DSA. Checking `privateKey.getAlgorithm()` for LMS/HSS/XMSS/XMSSMT first would keep non-stateful algorithms independent of BC-specific types. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
