This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/main by this push:
     new bb0a6f6af0ec CAMEL-22751: camel-grpc - a producer with 
negotiationType=TLS only needs a client key and certificate for mutual TLS 
(#30887)
bb0a6f6af0ec is described below

commit bb0a6f6af0ecdd8899fe811d9592eca9ff399eb7
Author: Claus Ibsen <[email protected]>
AuthorDate: Thu Nov 26 19:15:05 2026 +0100

    CAMEL-22751: camel-grpc - a producer with negotiationType=TLS only needs a 
client key and certificate for mutual TLS (#30887)
    
    The producer required keyCertChainResource and keyResource whenever
    negotiationType=TLS, so it could not call a TLS server that does not ask
    for a client certificate (for example a server that authenticates with an
    API key header), and GOOGLE authentication, which requires TLS, also
    forced a client certificate. The client key and certificate are now
    configured only when both are set (mutual TLS); setting only one of them
    fails with a clear error. The server is verified with
    trustCertCollectionResource when set, else with the default trust store
    of the JVM. The consumer (server) side is unchanged: it still needs its
    own key and certificate.
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
    Signed-off-by: Claus Ibsen <[email protected]>
---
 .../org/apache/camel/catalog/components/grpc.json  |   6 +-
 .../apache/camel/catalog/docs/grpc-component.adoc  |  48 +++++++-
 .../org/apache/camel/component/grpc/grpc.json      |   6 +-
 .../camel-grpc/src/main/docs/grpc-component.adoc   |  48 +++++++-
 .../camel/component/grpc/GrpcConfiguration.java    |  11 +-
 .../apache/camel/component/grpc/GrpcProducer.java  |  55 ++++++---
 .../grpc/GrpcProducerConfigurationTest.java        |  38 ++++++
 .../component/grpc/GrpcProducerServerTlsTest.java  | 128 +++++++++++++++++++++
 .../endpoint/dsl/GrpcEndpointBuilderFactory.java   |  45 ++++++--
 9 files changed, 343 insertions(+), 42 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 fc679bb2d467..fdb4b4934d46 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
@@ -81,11 +81,11 @@
     "jwtIssuer": { "index": 37, "kind": "parameter", "displayName": "Jwt 
Issuer", "group": "security", "label": "security", "required": false, "type": 
"string", "javaType": "java.lang.String", "deprecated": false, "autowired": 
false, "secret": false, "configurationClass": 
"org.apache.camel.component.grpc.GrpcConfiguration", "configurationField": 
"configuration", "description": "JSON Web Token issuer" },
     "jwtSecret": { "index": 38, "kind": "parameter", "displayName": "Jwt 
Secret", "group": "security", "label": "security", "required": false, "type": 
"string", "javaType": "java.lang.String", "deprecated": false, "autowired": 
false, "secret": true, "security": "secret", "configurationClass": 
"org.apache.camel.component.grpc.GrpcConfiguration", "configurationField": 
"configuration", "description": "JSON Web Token secret" },
     "jwtSubject": { "index": 39, "kind": "parameter", "displayName": "Jwt 
Subject", "group": "security", "label": "security", "required": false, "type": 
"string", "javaType": "java.lang.String", "deprecated": false, "autowired": 
false, "secret": false, "configurationClass": 
"org.apache.camel.component.grpc.GrpcConfiguration", "configurationField": 
"configuration", "description": "JSON Web Token subject" },
-    "keyCertChainResource": { "index": 40, "kind": "parameter", "displayName": 
"Key Cert Chain 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 X.509 certificate chain file resource in 
PEM format link" },
+    "keyCertChainResource": { "index": 40, "kind": "parameter", "displayName": 
"Key Cert Chain 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 X.509 certificate chain file resource in 
PEM format l [...]
     "keyPassword": { "index": 41, "kind": "parameter", "displayName": "Key 
Password", "group": "security", "label": "security", "required": false, "type": 
"string", "javaType": "java.lang.String", "deprecated": false, "autowired": 
false, "secret": true, "security": "secret", "configurationClass": 
"org.apache.camel.component.grpc.GrpcConfiguration", "configurationField": 
"configuration", "description": "The PKCS#8 private key file password" },
-    "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 PKCS#8 private key file resource in PEM 
format link" },
+    "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 PKCS#8 private key file resource in PEM 
format link. On a consumer, this  [...]
     "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": "Identifies the security neg [...]
     "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": "Service Account key file in JSON format 
resource link  [...]
-    "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 trusted certificates collection file re 
[...]
+    "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 trusted certificates collection file re 
[...]
   }
 }
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 0387c2613597..d1468890c948 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
@@ -58,10 +58,10 @@ To enable these features, the following component 
properties combinations must b
 |=======================================================================
 |Num.|Option |Parameter|Value|Required/Optional
 |1|*SSL/TLS*|negotiationType|TLS|Required
-|||keyCertChainResource||Required
-|||keyResource||Required
+|||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
+|||trustCertCollectionResource||Optional. 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
@@ -517,6 +517,48 @@ YAML::
 ----
 ====
 
+gRPC service producer with TLS, where only the server is authenticated. The 
server certificate is verified with
+`trustCertCollectionResource`, or with the default trust store of the JVM when 
that option is not set. Add
+`keyCertChainResource` and `keyResource` only when the server requires a 
client certificate (mutual TLS).
+
+[tabs]
+====
+Java::
++
+[source,java]
+----
+from("direct:grpc-tls")
+    
.to("grpc://localhost:1101/org.apache.camel.component.grpc.PingPong?method=pingSyncSync&synchronous=true&negotiationType=TLS&trustCertCollectionResource=file:src/test/resources/certs/ca.pem");
+----
+
+XML::
++
+[source,xml]
+----
+<route>
+  <from uri="direct:grpc-tls"/>
+  <to 
uri="grpc://localhost:1101/org.apache.camel.component.grpc.PingPong?method=pingSyncSync&amp;synchronous=true&amp;negotiationType=TLS&amp;trustCertCollectionResource=file:src/test/resources/certs/ca.pem"/>
+</route>
+----
+
+YAML::
++
+[source,yaml]
+----
+- route:
+    from:
+      uri: direct:grpc-tls
+      steps:
+        - to:
+            uri: grpc://localhost:1101/org.apache.camel.component.grpc.PingPong
+            parameters:
+              method: pingSyncSync
+              synchronous: true
+              negotiationType: TLS
+              trustCertCollectionResource: 
"file:src/test/resources/certs/ca.pem"
+----
+====
+
 gRPC service producer with custom JSON Web Token (JWT) implementation 
authentication
 
 [tabs]
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 fc679bb2d467..fdb4b4934d46 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
@@ -81,11 +81,11 @@
     "jwtIssuer": { "index": 37, "kind": "parameter", "displayName": "Jwt 
Issuer", "group": "security", "label": "security", "required": false, "type": 
"string", "javaType": "java.lang.String", "deprecated": false, "autowired": 
false, "secret": false, "configurationClass": 
"org.apache.camel.component.grpc.GrpcConfiguration", "configurationField": 
"configuration", "description": "JSON Web Token issuer" },
     "jwtSecret": { "index": 38, "kind": "parameter", "displayName": "Jwt 
Secret", "group": "security", "label": "security", "required": false, "type": 
"string", "javaType": "java.lang.String", "deprecated": false, "autowired": 
false, "secret": true, "security": "secret", "configurationClass": 
"org.apache.camel.component.grpc.GrpcConfiguration", "configurationField": 
"configuration", "description": "JSON Web Token secret" },
     "jwtSubject": { "index": 39, "kind": "parameter", "displayName": "Jwt 
Subject", "group": "security", "label": "security", "required": false, "type": 
"string", "javaType": "java.lang.String", "deprecated": false, "autowired": 
false, "secret": false, "configurationClass": 
"org.apache.camel.component.grpc.GrpcConfiguration", "configurationField": 
"configuration", "description": "JSON Web Token subject" },
-    "keyCertChainResource": { "index": 40, "kind": "parameter", "displayName": 
"Key Cert Chain 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 X.509 certificate chain file resource in 
PEM format link" },
+    "keyCertChainResource": { "index": 40, "kind": "parameter", "displayName": 
"Key Cert Chain 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 X.509 certificate chain file resource in 
PEM format l [...]
     "keyPassword": { "index": 41, "kind": "parameter", "displayName": "Key 
Password", "group": "security", "label": "security", "required": false, "type": 
"string", "javaType": "java.lang.String", "deprecated": false, "autowired": 
false, "secret": true, "security": "secret", "configurationClass": 
"org.apache.camel.component.grpc.GrpcConfiguration", "configurationField": 
"configuration", "description": "The PKCS#8 private key file password" },
-    "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 PKCS#8 private key file resource in PEM 
format link" },
+    "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 PKCS#8 private key file resource in PEM 
format link. On a consumer, this  [...]
     "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": "Identifies the security neg [...]
     "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": "Service Account key file in JSON format 
resource link  [...]
-    "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 trusted certificates collection file re 
[...]
+    "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 trusted certificates collection file re 
[...]
   }
 }
diff --git a/components/camel-grpc/src/main/docs/grpc-component.adoc 
b/components/camel-grpc/src/main/docs/grpc-component.adoc
index 0387c2613597..d1468890c948 100644
--- a/components/camel-grpc/src/main/docs/grpc-component.adoc
+++ b/components/camel-grpc/src/main/docs/grpc-component.adoc
@@ -58,10 +58,10 @@ To enable these features, the following component 
properties combinations must b
 |=======================================================================
 |Num.|Option |Parameter|Value|Required/Optional
 |1|*SSL/TLS*|negotiationType|TLS|Required
-|||keyCertChainResource||Required
-|||keyResource||Required
+|||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
+|||trustCertCollectionResource||Optional. 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
@@ -517,6 +517,48 @@ YAML::
 ----
 ====
 
+gRPC service producer with TLS, where only the server is authenticated. The 
server certificate is verified with
+`trustCertCollectionResource`, or with the default trust store of the JVM when 
that option is not set. Add
+`keyCertChainResource` and `keyResource` only when the server requires a 
client certificate (mutual TLS).
+
+[tabs]
+====
+Java::
++
+[source,java]
+----
+from("direct:grpc-tls")
+    
.to("grpc://localhost:1101/org.apache.camel.component.grpc.PingPong?method=pingSyncSync&synchronous=true&negotiationType=TLS&trustCertCollectionResource=file:src/test/resources/certs/ca.pem");
+----
+
+XML::
++
+[source,xml]
+----
+<route>
+  <from uri="direct:grpc-tls"/>
+  <to 
uri="grpc://localhost:1101/org.apache.camel.component.grpc.PingPong?method=pingSyncSync&amp;synchronous=true&amp;negotiationType=TLS&amp;trustCertCollectionResource=file:src/test/resources/certs/ca.pem"/>
+</route>
+----
+
+YAML::
++
+[source,yaml]
+----
+- route:
+    from:
+      uri: direct:grpc-tls
+      steps:
+        - to:
+            uri: grpc://localhost:1101/org.apache.camel.component.grpc.PingPong
+            parameters:
+              method: pingSyncSync
+              synchronous: true
+              negotiationType: TLS
+              trustCertCollectionResource: 
"file:src/test/resources/certs/ca.pem"
+----
+====
+
 gRPC service producer with custom JSON Web Token (JWT) implementation 
authentication
 
 [tabs]
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 455a8efce291..078afdd83f4a 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
@@ -303,7 +303,9 @@ public class GrpcConfiguration {
     }
 
     /**
-     * The X.509 certificate chain file resource in PEM format link
+     * The X.509 certificate chain file resource in PEM format link. On a 
consumer, this is the certificate of the
+     * server and is required for TLS. On a producer, this is the certificate 
of the client, set together with
+     * keyResource only for mutual TLS (when the server requires a client 
certificate).
      */
     public void setKeyCertChainResource(String keyCertChainResource) {
         this.keyCertChainResource = keyCertChainResource;
@@ -314,7 +316,9 @@ public class GrpcConfiguration {
     }
 
     /**
-     * The PKCS#8 private key file resource in PEM format link
+     * The PKCS#8 private key file resource in PEM format link. On a consumer, 
this is the key of the server and is
+     * required for TLS. On a producer, this is the key of the client, set 
together with keyCertChainResource only for
+     * mutual TLS (when the server requires a client certificate).
      */
     public void setKeyResource(String keyResource) {
         this.keyResource = keyResource;
@@ -336,7 +340,8 @@ public class GrpcConfiguration {
     }
 
     /**
-     * The trusted certificates collection file resource in PEM format for 
verifying the remote endpoint's certificate
+     * The trusted certificates collection file resource in PEM format for 
verifying the remote endpoint's certificate.
+     * When not set, a producer verifies the server with the default trust 
store of the JVM.
      */
     public void setTrustCertCollectionResource(String 
trustCertCollectionResource) {
         this.trustCertCollectionResource = trustCertCollectionResource;
diff --git 
a/components/camel-grpc/src/main/java/org/apache/camel/component/grpc/GrpcProducer.java
 
b/components/camel-grpc/src/main/java/org/apache/camel/component/grpc/GrpcProducer.java
index 1dad46292d45..453382efc009 100644
--- 
a/components/camel-grpc/src/main/java/org/apache/camel/component/grpc/GrpcProducer.java
+++ 
b/components/camel-grpc/src/main/java/org/apache/camel/component/grpc/GrpcProducer.java
@@ -16,6 +16,8 @@
  */
 package org.apache.camel.component.grpc;
 
+import java.io.InputStream;
+
 import com.google.auth.Credentials;
 import com.google.auth.oauth2.GoogleCredentials;
 import io.grpc.CallCredentials;
@@ -25,6 +27,7 @@ import io.grpc.netty.GrpcSslContexts;
 import io.grpc.netty.NegotiationType;
 import io.grpc.netty.NettyChannelBuilder;
 import io.grpc.stub.StreamObserver;
+import io.netty.handler.ssl.SslContext;
 import io.netty.handler.ssl.SslContextBuilder;
 import org.apache.camel.AsyncCallback;
 import org.apache.camel.Exchange;
@@ -146,24 +149,7 @@ public class GrpcProducer extends DefaultAsyncProducer {
             throw new IllegalArgumentException("No connection properties (host 
or port) specified");
         }
         if (configuration.getNegotiationType() == NegotiationType.TLS) {
-            ObjectHelper.notNull(configuration.getKeyCertChainResource(), 
"keyCertChainResource");
-            ObjectHelper.notNull(configuration.getKeyResource(), 
"keyResource");
-
-            SslContextBuilder sslContextBuilder = GrpcSslContexts.forClient()
-                    .keyManager(
-                            
ResourceHelper.resolveResourceAsInputStream(endpoint.getCamelContext(),
-                                    configuration.getKeyCertChainResource()),
-                            
ResourceHelper.resolveResourceAsInputStream(endpoint.getCamelContext(),
-                                    configuration.getKeyResource()),
-                            configuration.getKeyPassword());
-
-            if 
(ObjectHelper.isNotEmpty(configuration.getTrustCertCollectionResource())) {
-                sslContextBuilder
-                        = 
sslContextBuilder.trustManager(ResourceHelper.resolveResourceAsInputStream(endpoint.getCamelContext(),
-                                
configuration.getTrustCertCollectionResource()));
-            }
-
-            channelBuilder = 
channelBuilder.sslContext(GrpcSslContexts.configure(sslContextBuilder).build());
+            channelBuilder = channelBuilder.sslContext(createSslContext());
         }
 
         channel = 
channelBuilder.negotiationType(configuration.getNegotiationType())
@@ -173,4 +159,37 @@ public class GrpcProducer extends DefaultAsyncProducer {
                 .intercept(configuration.getClientInterceptors())
                 .build();
     }
+
+    /**
+     * Creates the client SSL context. The server is always verified, with 
trustCertCollectionResource when set, else
+     * with the default trust store of the JVM. The client authenticates with 
its own certificate (mutual TLS) only when
+     * both keyCertChainResource and keyResource are set.
+     */
+    private SslContext createSslContext() throws Exception {
+        boolean hasCertChain = 
ObjectHelper.isNotEmpty(configuration.getKeyCertChainResource());
+        boolean hasKey = 
ObjectHelper.isNotEmpty(configuration.getKeyResource());
+        if (hasCertChain != hasKey) {
+            throw new IllegalArgumentException(
+                    "Both keyCertChainResource and keyResource must be set to 
authenticate the client with its own"
+                                               + " certificate (mutual TLS), 
or none of them to only verify the server, but "
+                                               + (hasCertChain ? "keyResource" 
: "keyCertChainResource") + " is missing");
+        }
+
+        SslContextBuilder sslContextBuilder = GrpcSslContexts.forClient();
+        if (hasCertChain) {
+            try (InputStream certChain = 
ResourceHelper.resolveMandatoryResourceAsInputStream(endpoint.getCamelContext(),
+                    configuration.getKeyCertChainResource());
+                 InputStream key = 
ResourceHelper.resolveMandatoryResourceAsInputStream(endpoint.getCamelContext(),
+                         configuration.getKeyResource())) {
+                sslContextBuilder.keyManager(certChain, key, 
configuration.getKeyPassword());
+            }
+        }
+        if 
(ObjectHelper.isNotEmpty(configuration.getTrustCertCollectionResource())) {
+            try (InputStream trustCerts = 
ResourceHelper.resolveMandatoryResourceAsInputStream(endpoint.getCamelContext(),
+                    configuration.getTrustCertCollectionResource())) {
+                sslContextBuilder.trustManager(trustCerts);
+            }
+        }
+        return GrpcSslContexts.configure(sslContextBuilder).build();
+    }
 }
diff --git 
a/components/camel-grpc/src/test/java/org/apache/camel/component/grpc/GrpcProducerConfigurationTest.java
 
b/components/camel-grpc/src/test/java/org/apache/camel/component/grpc/GrpcProducerConfigurationTest.java
index 6cf2fa272ffe..a51807014818 100644
--- 
a/components/camel-grpc/src/test/java/org/apache/camel/component/grpc/GrpcProducerConfigurationTest.java
+++ 
b/components/camel-grpc/src/test/java/org/apache/camel/component/grpc/GrpcProducerConfigurationTest.java
@@ -23,6 +23,7 @@ import org.junit.jupiter.api.Test;
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
 
 class GrpcProducerConfigurationTest extends CamelTestSupport {
     @Test
@@ -57,4 +58,41 @@ class GrpcProducerConfigurationTest extends CamelTestSupport 
{
         IllegalArgumentException e = 
assertThrows(IllegalArgumentException.class, producer::start);
         assertEquals("serviceAccountResource must be specified", 
e.getMessage());
     }
+
+    @Test
+    void googleAuthenticationWithServerOnlyTls() throws Exception {
+        // GOOGLE authentication needs TLS, but not a client certificate: the 
producer must get past the TLS setup
+        // without keyCertChainResource and keyResource, and only then report 
the missing serviceAccountResource
+        Endpoint endpoint = 
context.getEndpoint("grpc://localhost:1101/org.apache.camel.component.grpc.PingPong"
+                                                + 
"?authenticationType=GOOGLE&negotiationType=TLS");
+
+        Producer producer = endpoint.createProducer();
+
+        IllegalArgumentException e = 
assertThrows(IllegalArgumentException.class, producer::start);
+        assertEquals("serviceAccountResource must be specified", 
e.getMessage());
+    }
+
+    @Test
+    void tlsWithClientCertificateWithoutKey() throws Exception {
+        Endpoint endpoint = 
context.getEndpoint("grpc://localhost:1101/org.apache.camel.component.grpc.PingPong"
+                                                + "?negotiationType=TLS"
+                                                + 
"&keyCertChainResource=file:src/test/resources/certs/client.pem");
+
+        Producer producer = endpoint.createProducer();
+
+        IllegalArgumentException e = 
assertThrows(IllegalArgumentException.class, producer::start);
+        assertTrue(e.getMessage().contains("keyResource"), e.getMessage());
+    }
+
+    @Test
+    void tlsWithClientKeyWithoutCertificate() throws Exception {
+        Endpoint endpoint = 
context.getEndpoint("grpc://localhost:1101/org.apache.camel.component.grpc.PingPong"
+                                                + "?negotiationType=TLS"
+                                                + 
"&keyResource=file:src/test/resources/certs/client.key");
+
+        Producer producer = endpoint.createProducer();
+
+        IllegalArgumentException e = 
assertThrows(IllegalArgumentException.class, producer::start);
+        assertTrue(e.getMessage().contains("keyCertChainResource"), 
e.getMessage());
+    }
 }
diff --git 
a/components/camel-grpc/src/test/java/org/apache/camel/component/grpc/GrpcProducerServerTlsTest.java
 
b/components/camel-grpc/src/test/java/org/apache/camel/component/grpc/GrpcProducerServerTlsTest.java
new file mode 100644
index 000000000000..94aef5c502bf
--- /dev/null
+++ 
b/components/camel-grpc/src/test/java/org/apache/camel/component/grpc/GrpcProducerServerTlsTest.java
@@ -0,0 +1,128 @@
+/*
+ * 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.grpc;
+
+import java.io.File;
+
+import javax.net.ssl.SSLHandshakeException;
+
+import io.grpc.Server;
+import io.grpc.Status;
+import io.grpc.StatusRuntimeException;
+import io.grpc.netty.GrpcSslContexts;
+import io.grpc.netty.NettyServerBuilder;
+import io.grpc.stub.StreamObserver;
+import io.netty.handler.ssl.ClientAuth;
+import io.netty.handler.ssl.SslContext;
+import org.apache.camel.CamelExecutionException;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.test.junit6.CamelTestSupport;
+import org.apache.camel.util.ObjectHelper;
+import org.junit.jupiter.api.AfterAll;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+/**
+ * A producer calling a TLS server that does not ask for a client certificate: 
the client only verifies the server, so
+ * no client key is configured.
+ */
+class GrpcProducerServerTlsTest extends CamelTestSupport {
+
+    private static final String PING_NAME = "PING";
+
+    private static Server grpcServer;
+
+    @BeforeAll
+    static void startGrpcServer() throws Exception {
+        SslContext sslContext = GrpcSslContexts
+                .forServer(new File("src/test/resources/certs/server.pem"), 
new File("src/test/resources/certs/server.key"))
+                .clientAuth(ClientAuth.NONE)
+                .build();
+
+        grpcServer = NettyServerBuilder.forPort(0)
+                .sslContext(sslContext)
+                .addService(new PingPongImpl())
+                .build()
+                .start();
+    }
+
+    @AfterAll
+    static void stopGrpcServer() {
+        if (grpcServer != null) {
+            grpcServer.shutdownNow();
+        }
+    }
+
+    @Test
+    void tlsWithTrustedServerCertificateOnly() {
+        PingRequest pingRequest = 
PingRequest.newBuilder().setPingName(PING_NAME).setPingId(1).build();
+
+        Object pongResponse = template.requestBody("direct:grpc-tls", 
pingRequest);
+
+        PongResponse pong = assertInstanceOf(PongResponse.class, pongResponse);
+        assertEquals(1, pong.getPongId());
+        assertEquals(PING_NAME + "PONG", pong.getPongName());
+    }
+
+    @Test
+    void tlsWithDefaultTrustRejectsUnknownServerCertificate() {
+        // without trustCertCollectionResource the default trust store of the 
JVM is used, which does not trust
+        // the test CA, so the TLS handshake fails (the server certificate is 
verified, not trusted blindly)
+        PingRequest pingRequest = 
PingRequest.newBuilder().setPingName(PING_NAME).setPingId(1).build();
+
+        CamelExecutionException e = assertThrows(CamelExecutionException.class,
+                () -> template.requestBody("direct:grpc-tls-default-trust", 
pingRequest));
+
+        StatusRuntimeException status = 
ObjectHelper.getException(StatusRuntimeException.class, e);
+        assertNotNull(status, "Expected a StatusRuntimeException in the cause 
chain");
+        assertEquals(Status.Code.UNAVAILABLE, status.getStatus().getCode());
+        assertNotNull(ObjectHelper.getException(SSLHandshakeException.class, 
e),
+                "Expected the TLS handshake to fail as the server certificate 
is not trusted");
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() {
+        return new RouteBuilder() {
+            @Override
+            public void configure() {
+                from("direct:grpc-tls")
+                        .to("grpc://localhost:" + grpcServer.getPort()
+                            + 
"/org.apache.camel.component.grpc.PingPong?method=pingSyncSync&synchronous=true"
+                            + 
"&negotiationType=TLS&trustCertCollectionResource=file:src/test/resources/certs/ca.pem");
+
+                from("direct:grpc-tls-default-trust")
+                        .to("grpc://localhost:" + grpcServer.getPort()
+                            + 
"/org.apache.camel.component.grpc.PingPong?method=pingSyncSync&synchronous=true"
+                            + "&negotiationType=TLS");
+            }
+        };
+    }
+
+    static class PingPongImpl extends PingPongGrpc.PingPongImplBase {
+        @Override
+        public void pingSyncSync(PingRequest request, 
StreamObserver<PongResponse> responseObserver) {
+            
responseObserver.onNext(PongResponse.newBuilder().setPongName(request.getPingName()
 + "PONG")
+                    .setPongId(request.getPingId()).build());
+            responseObserver.onCompleted();
+        }
+    }
+}
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 fd24bfec15c5..da9eb65a9f71 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
@@ -850,7 +850,11 @@ public interface GrpcEndpointBuilderFactory {
             return this;
         }
         /**
-         * The X.509 certificate chain file resource in PEM format link.
+         * The X.509 certificate chain file resource in PEM format link. On a
+         * consumer, this is the certificate of the server and is required for
+         * TLS. On a producer, this is the certificate of the client, set
+         * together with keyResource only for mutual TLS (when the server
+         * requires a client certificate).
          * 
          * This option can also be loaded from an existing file, by prefixing
          * with file: or classpath: followed by the location of the file.
@@ -881,7 +885,11 @@ public interface GrpcEndpointBuilderFactory {
             return this;
         }
         /**
-         * The PKCS#8 private key file resource in PEM format link.
+         * The PKCS#8 private key file resource in PEM format link. On a
+         * consumer, this is the key of the server and is required for TLS. On 
a
+         * producer, this is the key of the client, set together with
+         * keyCertChainResource only for mutual TLS (when the server requires a
+         * client certificate).
          * 
          * This option can also be loaded from an existing file, by prefixing
          * with file: or classpath: followed by the location of the file.
@@ -950,7 +958,8 @@ public interface GrpcEndpointBuilderFactory {
         }
         /**
          * The trusted certificates collection file resource in PEM format for
-         * verifying the remote endpoint's certificate.
+         * verifying the remote endpoint's certificate. When not set, a 
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.
@@ -1552,7 +1561,11 @@ public interface GrpcEndpointBuilderFactory {
             return this;
         }
         /**
-         * The X.509 certificate chain file resource in PEM format link.
+         * The X.509 certificate chain file resource in PEM format link. On a
+         * consumer, this is the certificate of the server and is required for
+         * TLS. On a producer, this is the certificate of the client, set
+         * together with keyResource only for mutual TLS (when the server
+         * requires a client certificate).
          * 
          * This option can also be loaded from an existing file, by prefixing
          * with file: or classpath: followed by the location of the file.
@@ -1583,7 +1596,11 @@ public interface GrpcEndpointBuilderFactory {
             return this;
         }
         /**
-         * The PKCS#8 private key file resource in PEM format link.
+         * The PKCS#8 private key file resource in PEM format link. On a
+         * consumer, this is the key of the server and is required for TLS. On 
a
+         * producer, this is the key of the client, set together with
+         * keyCertChainResource only for mutual TLS (when the server requires a
+         * client certificate).
          * 
          * This option can also be loaded from an existing file, by prefixing
          * with file: or classpath: followed by the location of the file.
@@ -1652,7 +1669,8 @@ public interface GrpcEndpointBuilderFactory {
         }
         /**
          * The trusted certificates collection file resource in PEM format for
-         * verifying the remote endpoint's certificate.
+         * verifying the remote endpoint's certificate. When not set, a 
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.
@@ -1998,7 +2016,11 @@ public interface GrpcEndpointBuilderFactory {
             return this;
         }
         /**
-         * The X.509 certificate chain file resource in PEM format link.
+         * The X.509 certificate chain file resource in PEM format link. On a
+         * consumer, this is the certificate of the server and is required for
+         * TLS. On a producer, this is the certificate of the client, set
+         * together with keyResource only for mutual TLS (when the server
+         * requires a client certificate).
          * 
          * This option can also be loaded from an existing file, by prefixing
          * with file: or classpath: followed by the location of the file.
@@ -2029,7 +2051,11 @@ public interface GrpcEndpointBuilderFactory {
             return this;
         }
         /**
-         * The PKCS#8 private key file resource in PEM format link.
+         * The PKCS#8 private key file resource in PEM format link. On a
+         * consumer, this is the key of the server and is required for TLS. On 
a
+         * producer, this is the key of the client, set together with
+         * keyCertChainResource only for mutual TLS (when the server requires a
+         * client certificate).
          * 
          * This option can also be loaded from an existing file, by prefixing
          * with file: or classpath: followed by the location of the file.
@@ -2098,7 +2124,8 @@ public interface GrpcEndpointBuilderFactory {
         }
         /**
          * The trusted certificates collection file resource in PEM format for
-         * verifying the remote endpoint's certificate.
+         * verifying the remote endpoint's certificate. When not set, a 
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.

Reply via email to