This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch CAMEL-27724-grpc-consumer-trust in repository https://gitbox.apache.org/repos/asf/camel.git
commit 218ba080187b142ec5ca1372527185d9a4cbf08f Author: Claus Ibsen <[email protected]> AuthorDate: Fri Nov 27 19:45:10 2026 +0100 CAMEL-27724: camel-grpc - a TLS consumer without trustCertCollectionResource accepts client certificates of any CA in the JVM trust store With negotiationType=TLS the consumer requires a client certificate, but set trusted certificates only when trustCertCollectionResource was set. Without it, Netty verified the clients with the default trust store of the JVM, so a client certificate of any CA in that store was accepted. Such a consumer now fails to start with a clear message. Documented in the option, the component docs and the upgrade guide. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Claus Ibsen <[email protected]> --- .../org/apache/camel/catalog/components/grpc.json | 2 +- .../apache/camel/catalog/docs/grpc-component.adoc | 2 +- .../org/apache/camel/component/grpc/grpc.json | 2 +- .../camel-grpc/src/main/docs/grpc-component.adoc | 2 +- .../camel/component/grpc/GrpcConfiguration.java | 5 +- .../apache/camel/component/grpc/GrpcConsumer.java | 16 +++-- .../grpc/GrpcConsumerConfigurationTest.java | 13 ++++ .../component/grpc/GrpcConsumerSecurityTest.java | 78 ++++++++++++++++++++++ .../ROOT/pages/camel-4x-upgrade-guide-4_24.adoc | 8 +++ .../endpoint/dsl/GrpcEndpointBuilderFactory.java | 18 +++-- 10 files changed, 127 insertions(+), 19 deletions(-) diff --git a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/grpc.json b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/grpc.json index b3a2a5589e16..e7404fa9fbaf 100644 --- a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/grpc.json +++ b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/grpc.json @@ -86,6 +86,6 @@ "keyResource": { "index": 42, "kind": "parameter", "displayName": "Key Resource", "group": "security", "label": "security", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "autowired": false, "secret": false, "supportFileReference": true, "configurationClass": "org.apache.camel.component.grpc.GrpcConfiguration", "configurationField": "configuration", "description": "The location of the PKCS#8 private key in PEM format of keyCertChainResource. [...] "negotiationType": { "index": 43, "kind": "parameter", "displayName": "Negotiation Type", "group": "security", "label": "security", "required": false, "type": "enum", "javaType": "io.grpc.netty.NegotiationType", "enum": [ "PLAINTEXT", "TLS" ], "deprecated": false, "autowired": false, "secret": false, "defaultValue": "PLAINTEXT", "configurationClass": "org.apache.camel.component.grpc.GrpcConfiguration", "configurationField": "configuration", "description": "The transport security of t [...] "serviceAccountResource": { "index": 44, "kind": "parameter", "displayName": "Service Account Resource", "group": "security", "label": "security", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "autowired": false, "secret": false, "supportFileReference": true, "configurationClass": "org.apache.camel.component.grpc.GrpcConfiguration", "configurationField": "configuration", "description": "The location of the Google Cloud service account key f [...] - "trustCertCollectionResource": { "index": 45, "kind": "parameter", "displayName": "Trust Cert Collection Resource", "group": "security", "label": "security", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "autowired": false, "secret": false, "supportFileReference": true, "configurationClass": "org.apache.camel.component.grpc.GrpcConfiguration", "configurationField": "configuration", "description": "The location of the trusted certificates in [...] + "trustCertCollectionResource": { "index": 45, "kind": "parameter", "displayName": "Trust Cert Collection Resource", "group": "security", "label": "security", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "autowired": false, "secret": false, "supportFileReference": true, "configurationClass": "org.apache.camel.component.grpc.GrpcConfiguration", "configurationField": "configuration", "description": "The location of the trusted certificates in [...] } } diff --git a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/grpc-component.adoc b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/grpc-component.adoc index 9912f0bb304d..bb99eb9e32da 100644 --- a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/grpc-component.adoc +++ b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/grpc-component.adoc @@ -61,7 +61,7 @@ To enable these features, the following component properties combinations must b |||keyCertChainResource||Required on a consumer. Optional on a producer: only for mutual TLS, together with keyResource |||keyResource||Required on a consumer. Optional on a producer: only for mutual TLS, together with keyCertChainResource |||keyPassword||Optional -|||trustCertCollectionResource||Optional. When not set, a producer verifies the server with the default trust store of the JVM +|||trustCertCollectionResource||Required on a consumer: the CA certificates whose client certificates the consumer accepts. Optional on a producer: when not set, a producer verifies the server with the default trust store of the JVM |2|*Token-based authentication with Google API*|authenticationType|GOOGLE|Required |||negotiationType|TLS|Required |||serviceAccountResource||Required diff --git a/components/camel-grpc/src/generated/resources/META-INF/org/apache/camel/component/grpc/grpc.json b/components/camel-grpc/src/generated/resources/META-INF/org/apache/camel/component/grpc/grpc.json index b3a2a5589e16..e7404fa9fbaf 100644 --- a/components/camel-grpc/src/generated/resources/META-INF/org/apache/camel/component/grpc/grpc.json +++ b/components/camel-grpc/src/generated/resources/META-INF/org/apache/camel/component/grpc/grpc.json @@ -86,6 +86,6 @@ "keyResource": { "index": 42, "kind": "parameter", "displayName": "Key Resource", "group": "security", "label": "security", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "autowired": false, "secret": false, "supportFileReference": true, "configurationClass": "org.apache.camel.component.grpc.GrpcConfiguration", "configurationField": "configuration", "description": "The location of the PKCS#8 private key in PEM format of keyCertChainResource. [...] "negotiationType": { "index": 43, "kind": "parameter", "displayName": "Negotiation Type", "group": "security", "label": "security", "required": false, "type": "enum", "javaType": "io.grpc.netty.NegotiationType", "enum": [ "PLAINTEXT", "TLS" ], "deprecated": false, "autowired": false, "secret": false, "defaultValue": "PLAINTEXT", "configurationClass": "org.apache.camel.component.grpc.GrpcConfiguration", "configurationField": "configuration", "description": "The transport security of t [...] "serviceAccountResource": { "index": 44, "kind": "parameter", "displayName": "Service Account Resource", "group": "security", "label": "security", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "autowired": false, "secret": false, "supportFileReference": true, "configurationClass": "org.apache.camel.component.grpc.GrpcConfiguration", "configurationField": "configuration", "description": "The location of the Google Cloud service account key f [...] - "trustCertCollectionResource": { "index": 45, "kind": "parameter", "displayName": "Trust Cert Collection Resource", "group": "security", "label": "security", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "autowired": false, "secret": false, "supportFileReference": true, "configurationClass": "org.apache.camel.component.grpc.GrpcConfiguration", "configurationField": "configuration", "description": "The location of the trusted certificates in [...] + "trustCertCollectionResource": { "index": 45, "kind": "parameter", "displayName": "Trust Cert Collection Resource", "group": "security", "label": "security", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "autowired": false, "secret": false, "supportFileReference": true, "configurationClass": "org.apache.camel.component.grpc.GrpcConfiguration", "configurationField": "configuration", "description": "The location of the trusted certificates in [...] } } diff --git a/components/camel-grpc/src/main/docs/grpc-component.adoc b/components/camel-grpc/src/main/docs/grpc-component.adoc index 9912f0bb304d..bb99eb9e32da 100644 --- a/components/camel-grpc/src/main/docs/grpc-component.adoc +++ b/components/camel-grpc/src/main/docs/grpc-component.adoc @@ -61,7 +61,7 @@ To enable these features, the following component properties combinations must b |||keyCertChainResource||Required on a consumer. Optional on a producer: only for mutual TLS, together with keyResource |||keyResource||Required on a consumer. Optional on a producer: only for mutual TLS, together with keyCertChainResource |||keyPassword||Optional -|||trustCertCollectionResource||Optional. When not set, a producer verifies the server with the default trust store of the JVM +|||trustCertCollectionResource||Required on a consumer: the CA certificates whose client certificates the consumer accepts. Optional on a producer: when not set, a producer verifies the server with the default trust store of the JVM |2|*Token-based authentication with Google API*|authenticationType|GOOGLE|Required |||negotiationType|TLS|Required |||serviceAccountResource||Required diff --git a/components/camel-grpc/src/main/java/org/apache/camel/component/grpc/GrpcConfiguration.java b/components/camel-grpc/src/main/java/org/apache/camel/component/grpc/GrpcConfiguration.java index f2fd034e19ae..dd1efa3b72f2 100644 --- a/components/camel-grpc/src/main/java/org/apache/camel/component/grpc/GrpcConfiguration.java +++ b/components/camel-grpc/src/main/java/org/apache/camel/component/grpc/GrpcConfiguration.java @@ -116,8 +116,9 @@ public class GrpcConfiguration { @UriParam(label = "security", description = "The location of the trusted certificates in PEM format that verify the certificate of the other" - + " side: of the server for the producer, of the clients for the consumer. If not set, the producer" - + " verifies the server with the default trust store of the JVM.") + + " side: of the server for the producer, of the clients for the consumer. For the consumer, these" + + " are the CA certificates whose client certificates the consumer accepts, required with TLS. If" + + " not set, the producer verifies the server with the default trust store of the JVM.") @Metadata(supportFileReference = true) private String trustCertCollectionResource; diff --git a/components/camel-grpc/src/main/java/org/apache/camel/component/grpc/GrpcConsumer.java b/components/camel-grpc/src/main/java/org/apache/camel/component/grpc/GrpcConsumer.java index 252ca9a74305..d189897e46d1 100644 --- a/components/camel-grpc/src/main/java/org/apache/camel/component/grpc/GrpcConsumer.java +++ b/components/camel-grpc/src/main/java/org/apache/camel/component/grpc/GrpcConsumer.java @@ -119,6 +119,12 @@ public class GrpcConsumer extends DefaultConsumer { if (configuration.getNegotiationType() == NegotiationType.TLS) { ObjectHelper.notNull(configuration.getKeyCertChainResource(), "keyCertChainResource"); ObjectHelper.notNull(configuration.getKeyResource(), "keyResource"); + if (ObjectHelper.isEmpty(configuration.getTrustCertCollectionResource())) { + // without trusted certificates, Netty verifies the clients with the default trust store of the JVM + throw new IllegalArgumentException( + "trustCertCollectionResource must be set with negotiationType TLS: it names the CA certificates" + + " whose client certificates the consumer accepts"); + } SslContextBuilder sslContextBuilder = SslContextBuilder @@ -128,13 +134,9 @@ public class GrpcConsumer extends DefaultConsumer { ResourceHelper.resolveResourceAsInputStream(endpoint.getCamelContext(), configuration.getKeyResource()), configuration.getKeyPassword()) - .clientAuth(ClientAuth.REQUIRE); - - if (ObjectHelper.isNotEmpty(configuration.getTrustCertCollectionResource())) { - sslContextBuilder - = sslContextBuilder.trustManager(ResourceHelper.resolveResourceAsInputStream(endpoint.getCamelContext(), - configuration.getTrustCertCollectionResource())); - } + .clientAuth(ClientAuth.REQUIRE) + .trustManager(ResourceHelper.resolveResourceAsInputStream(endpoint.getCamelContext(), + configuration.getTrustCertCollectionResource())); serverBuilder = serverBuilder.sslContext(GrpcSslContexts.configure(sslContextBuilder).build()); } diff --git a/components/camel-grpc/src/test/java/org/apache/camel/component/grpc/GrpcConsumerConfigurationTest.java b/components/camel-grpc/src/test/java/org/apache/camel/component/grpc/GrpcConsumerConfigurationTest.java index 170d022eecb0..79dfae2c6908 100644 --- a/components/camel-grpc/src/test/java/org/apache/camel/component/grpc/GrpcConsumerConfigurationTest.java +++ b/components/camel-grpc/src/test/java/org/apache/camel/component/grpc/GrpcConsumerConfigurationTest.java @@ -22,6 +22,7 @@ import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertInstanceOf; import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; class GrpcConsumerConfigurationTest extends CamelTestSupport { @Test @@ -69,4 +70,16 @@ class GrpcConsumerConfigurationTest extends CamelTestSupport { "grpc:localhost:8080/org.apache.camel.component.grpc.PingPong?maxRstFramesPerWindow=100")); assertInstanceOf(IllegalArgumentException.class, exception.getCause()); } + + @Test + void tlsWithoutTrustCertCollectionResourceFailsToStart() { + FailedToCreateConsumerException exception = assertThrows(FailedToCreateConsumerException.class, + () -> consumer.receive( + "grpc:localhost:0/org.apache.camel.component.grpc.PingPong?negotiationType=TLS" + + "&keyCertChainResource=file:src/test/resources/certs/server.pem" + + "&keyResource=file:src/test/resources/certs/server.key", + 5000)); + IllegalArgumentException cause = assertInstanceOf(IllegalArgumentException.class, exception.getCause()); + assertTrue(cause.getMessage().contains("trustCertCollectionResource"), cause.getMessage()); + } } diff --git a/components/camel-grpc/src/test/java/org/apache/camel/component/grpc/GrpcConsumerSecurityTest.java b/components/camel-grpc/src/test/java/org/apache/camel/component/grpc/GrpcConsumerSecurityTest.java index d75bef4f9707..417101da1b91 100644 --- a/components/camel-grpc/src/test/java/org/apache/camel/component/grpc/GrpcConsumerSecurityTest.java +++ b/components/camel-grpc/src/test/java/org/apache/camel/component/grpc/GrpcConsumerSecurityTest.java @@ -17,12 +17,18 @@ package org.apache.camel.component.grpc; import java.io.File; +import java.io.InputStream; +import java.nio.file.Files; +import java.nio.file.Path; +import java.security.KeyStore; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; +import javax.net.ssl.KeyManagerFactory; import javax.net.ssl.SSLException; import io.grpc.ManagedChannel; +import io.grpc.StatusRuntimeException; import io.grpc.netty.GrpcSslContexts; import io.grpc.netty.NettyChannelBuilder; import io.grpc.stub.StreamObserver; @@ -38,11 +44,13 @@ import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Assumptions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; public class GrpcConsumerSecurityTest extends GrpcTestSupport { @@ -135,6 +143,76 @@ public class GrpcConsumerSecurityTest extends GrpcTestSupport { assertEquals(GRPC_TEST_PING_VALUE + GRPC_TEST_PONG_VALUE, pongResponse.getPongName()); } + @Test + public void testClientCertificateOfOwnCaIsAccepted() { + PingRequest pingRequest + = PingRequest.newBuilder().setPingName(GRPC_TEST_PING_VALUE).setPingId(GRPC_TEST_PING_ID).build(); + + PongResponse pongResponse = PingPongGrpc.newBlockingStub(tlsChannel).withDeadlineAfter(20, TimeUnit.SECONDS) + .pingSyncSync(pingRequest); + + assertEquals(GRPC_TEST_PING_VALUE + GRPC_TEST_PONG_VALUE, pongResponse.getPongName()); + } + + @Test + public void testClientCertificateOfOtherCaIsRejected(@TempDir Path tempDir) throws Exception { + // a self-signed client certificate, so it is not issued by the CA in trustCertCollectionResource + KeyManagerFactory otherClientKeys = createSelfSignedClientKeys(tempDir); + SslContext sslContext = GrpcSslContexts.forClient() + .keyManager(otherClientKeys) + .trustManager(new File("src/test/resources/certs/ca.pem")) + .build(); + ManagedChannel otherCaChannel = NettyChannelBuilder.forAddress("localhost", getRoutePort("grpc-tls")) + .sslContext(sslContext) + .build(); + try { + PingRequest pingRequest + = PingRequest.newBuilder().setPingName(GRPC_TEST_PING_VALUE).setPingId(GRPC_TEST_PING_ID).build(); + PingPongGrpc.PingPongBlockingStub stub + = PingPongGrpc.newBlockingStub(otherCaChannel).withDeadlineAfter(20, TimeUnit.SECONDS); + + assertThrows(StatusRuntimeException.class, () -> stub.pingSyncSync(pingRequest)); + + MockEndpoint mockEndpoint = getMockEndpoint("mock:tls-enable"); + mockEndpoint.expectedMessageCount(0); + mockEndpoint.assertIsSatisfied(); + } finally { + otherCaChannel.shutdownNow().awaitTermination(5, TimeUnit.SECONDS); + } + } + + private static KeyManagerFactory createSelfSignedClientKeys(Path dir) throws Exception { + Path keytool = Path.of(System.getProperty("java.home"), "bin", "keytool"); + Assumptions.assumeTrue(Files.isExecutable(keytool), "keytool not found"); + Path keystore = dir.resolve("other-client.p12"); + char[] password = "changeit".toCharArray(); + Process process = new ProcessBuilder( + keytool.toString(), "-genkeypair", "-alias", "client", "-keyalg", "RSA", "-keysize", "2048", + "-dname", "CN=localhost", "-validity", "2", "-storetype", "PKCS12", + "-keystore", keystore.toString(), "-storepass", "changeit", "-keypass", "changeit") + .redirectErrorStream(true) + .redirectOutput(dir.resolve("keytool.log").toFile()) + .start(); + assertTrue(process.waitFor(60, TimeUnit.SECONDS), "keytool did not finish"); + assertEquals(0, process.exitValue(), () -> "keytool failed: " + readLog(dir.resolve("keytool.log"))); + + KeyStore ks = KeyStore.getInstance("PKCS12"); + try (InputStream is = Files.newInputStream(keystore)) { + ks.load(is, password); + } + KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); + kmf.init(ks, password); + return kmf; + } + + private static String readLog(Path log) { + try { + return Files.readString(log); + } catch (Exception e) { + return e.getMessage(); + } + } + @Test public void testWithCorrectJWT() throws Exception { LOG.info("gRPC pingAsyncSync method async test with correct JWT start"); diff --git a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc index 842cdec20280..ecfbfaebbe7b 100644 --- a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc +++ b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc @@ -12423,6 +12423,14 @@ With `mimeType` `PAGEABLE`, `PRINTABLE` or `RENDERABLE_IMAGE`, the producer now `java.awt.print.Pageable`, `java.awt.print.Printable` or `java.awt.image.renderable.RenderableImage`, as these doc flavors need, instead of a stream. With `sendToPrinter=false`, the body is still written to a file as bytes. +=== camel-grpc - a TLS consumer requires trustCertCollectionResource + +A `grpc` consumer with `negotiationType=TLS` now fails to start with an `IllegalArgumentException` when +`trustCertCollectionResource` is not set. The consumer requires a certificate from each client, and without this option +it accepted a client certificate issued by any CA in the default trust store of the JVM (`cacerts`, or +`javax.net.ssl.trustStore`). Set `trustCertCollectionResource` to the CA certificates (PEM) whose client certificates +the consumer accepts. + === Kamelets - removed Kamelets The Kamelet catalog (camel-kamelets 4.24) removes Kamelets that could not work on Camel 4.23 or 4.24: diff --git a/dsl/camel-endpointdsl/src/generated/java/org/apache/camel/builder/endpoint/dsl/GrpcEndpointBuilderFactory.java b/dsl/camel-endpointdsl/src/generated/java/org/apache/camel/builder/endpoint/dsl/GrpcEndpointBuilderFactory.java index 09e7bbf76932..89e5576417fd 100644 --- a/dsl/camel-endpointdsl/src/generated/java/org/apache/camel/builder/endpoint/dsl/GrpcEndpointBuilderFactory.java +++ b/dsl/camel-endpointdsl/src/generated/java/org/apache/camel/builder/endpoint/dsl/GrpcEndpointBuilderFactory.java @@ -986,8 +986,10 @@ public interface GrpcEndpointBuilderFactory { /** * The location of the trusted certificates in PEM format that verify * the certificate of the other side: of the server for the producer, of - * the clients for the consumer. If not set, the producer verifies the - * server with the default trust store of the JVM. + * the clients for the consumer. For the consumer, these are the CA + * certificates whose client certificates the consumer accepts, required + * with TLS. If not set, the producer verifies the server with the + * default trust store of the JVM. * * This option can also be loaded from an existing file, by prefixing * with file: or classpath: followed by the location of the file. @@ -1757,8 +1759,10 @@ public interface GrpcEndpointBuilderFactory { /** * The location of the trusted certificates in PEM format that verify * the certificate of the other side: of the server for the producer, of - * the clients for the consumer. If not set, the producer verifies the - * server with the default trust store of the JVM. + * the clients for the consumer. For the consumer, these are the CA + * certificates whose client certificates the consumer accepts, required + * with TLS. If not set, the producer verifies the server with the + * default trust store of the JVM. * * This option can also be loaded from an existing file, by prefixing * with file: or classpath: followed by the location of the file. @@ -2261,8 +2265,10 @@ public interface GrpcEndpointBuilderFactory { /** * The location of the trusted certificates in PEM format that verify * the certificate of the other side: of the server for the producer, of - * the clients for the consumer. If not set, the producer verifies the - * server with the default trust store of the JVM. + * the clients for the consumer. For the consumer, these are the CA + * certificates whose client certificates the consumer accepts, required + * with TLS. If not set, the producer verifies the server with the + * default trust store of the JVM. * * This option can also be loaded from an existing file, by prefixing * with file: or classpath: followed by the location of the file.
