oscerd commented on code in PR #27820:
URL: https://github.com/apache/camel/pull/27820#discussion_r4269289949


##########
components/camel-netty/src/test/java/org/apache/camel/component/netty/NettySSLHostnameVerificationTest.java:
##########
@@ -0,0 +1,117 @@
+/*
+ * 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.netty;
+
+import org.apache.camel.BindToRegistry;
+import org.apache.camel.CamelExecutionException;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.support.jsse.KeyManagersParameters;
+import org.apache.camel.support.jsse.KeyStoreParameters;
+import org.apache.camel.support.jsse.SSLContextParameters;
+import org.apache.camel.support.jsse.TrustManagersParameters;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.condition.DisabledIfSystemProperty;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * The producer checks that the certificate of the server matches the host it 
connects to when
+ * {@code hostnameVerification=true}.
+ * <p/>
+ * keystore-localhost.jks lists only DNS:localhost as subject alternative 
name, so it matches "localhost" but not
+ * "127.0.0.1" - which both routes below resolve to, so the same consumer 
serves them.
+ */
+@DisabledIfSystemProperty(named = "java.vendor", matches = ".*ibm.*")
+class NettySSLHostnameVerificationTest extends BaseNettyTest {
+
+    private static final String REPLY = "Bonjour";
+
+    @BindToRegistry("sslContextParameters")
+    public SSLContextParameters loadSSLContextParams() {
+        KeyStoreParameters ksp = new KeyStoreParameters();
+        
ksp.setResource(this.getClass().getClassLoader().getResource("keystore-localhost.jks").toString());
+        ksp.setPassword("changeit");
+
+        KeyManagersParameters kmp = new KeyManagersParameters();
+        kmp.setKeyPassword("changeit");
+        kmp.setKeyStore(ksp);
+
+        TrustManagersParameters tmp = new TrustManagersParameters();
+        tmp.setKeyStore(ksp);
+
+        SSLContextParameters sslContextParameters = new SSLContextParameters();
+        sslContextParameters.setKeyManagers(kmp);
+        sslContextParameters.setTrustManagers(tmp);
+        return sslContextParameters;
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() {
+        return new RouteBuilder() {
+            public void configure() {
+                
from("netty:tcp://localhost:{{port}}?sync=true&ssl=true&sslContextParameters=#sslContextParameters")
+                        .setBody(constant(REPLY));
+            }
+        };
+    }
+
+    @Test
+    void connectsWhenTheCertificateMatchesTheHost() {
+        String out = template.requestBody(
+                
"netty:tcp://localhost:{{port}}?sync=true&ssl=true&sslContextParameters=#sslContextParameters"
+                                          + "&hostnameVerification=true",
+                "Hello", String.class);
+
+        assertEquals(REPLY, out);
+    }
+
+    /**
+     * Without a peer host on the engine the JDK cannot run the check at all 
and reports "Hostname or IP address is
+     * undefined", which would make this pass for the wrong reason - so the 
message is asserted as well.
+     */
+    @Test
+    void failsWhenTheCertificateDoesNotMatchTheHost() {
+        CamelExecutionException e = assertThrows(CamelExecutionException.class,
+                () -> template.requestBody(
+                        
"netty:tcp://127.0.0.1:{{port}}?sync=true&ssl=true&sslContextParameters=#sslContextParameters"
+                                           + "&hostnameVerification=true",
+                        "Hello", String.class));
+
+        String message = rootCause(e).getMessage();
+        assertTrue(message != null && message.contains("127.0.0.1"),
+                "the host name check should have rejected the certificate, but 
got: " + message);

Review Comment:
   Declining this one, and correcting the observation below it.
   
   **On the assertion.** The guard is already explicit, in the Javadoc directly 
above the test:
   
   ```java
   /**
    * Without a peer host on the engine the JDK cannot run the check at all and 
reports "Hostname or IP address is
    * undefined", which would make this pass for the wrong reason - so the 
message is asserted as well.
    */
   ```
   
   So `!message.contains("undefined")` would restate in code what the comment 
already says in prose, and as you note it adds no coverage. It would also 
couple the test to a second JDK message string and make a compound `assertTrue` 
three clauses wide.
   
   The reason I am confident the existing assertion is load-bearing is that I 
ran the revert — restoring the no-arg `createSSLEngine()` and keeping the tests:
   
   ```
   [ERROR]   Run 1: failsWhenTheCertificateDoesNotMatchTheHost:97 the host name 
check should have rejected the certificate, but got: Hostname or IP address is 
undefined. ==> expected: <true> but was: <false>
   [ERROR]   Run 2: ... Run 3: ...
   ```
   
   and `connectsWhenTheCertificateMatchesTheHost` errored with 
`SSLHandshakeException: (certificate_unknown) Hostname or IP address is 
undefined.` That failure message is the part I care about most: it names the 
exact wrong reason, which the extra clause would not improve.
   
   **On the server-side observation — it is not unconditional.** 
`configureServerSSLOnDemand()` sets the algorithm only inside `if 
(consumer.getConfiguration().isHostnameVerification())`, and 
`hostnameVerification` is a `boolean` with no `defaultValue`, so it is `false` 
unless a route asks for it. A default consumer never touches `SSLParameters`, 
and the behaviour you describe needs the option to be set explicitly on the 
consumer.
   
   The substance of the point is right, though, and it is already on the record 
rather than needing a new follow-up: the "Left out on purpose — the server 
side" section of this PR's description, and the "Consumer side - separate 
question" section of CAMEL-25649. Short version — in server mode `HTTPS` asks 
the JDK to match the *client's* certificate against a host name the server does 
not have, so whenever a client certificate is actually presented it fails the 
handshake with the same `Hostname or IP address is undefined`; and matching a 
client certificate against the client's address is not a meaningful control 
anyway. The same block is in `camel-netty-http`'s 
`HttpServerInitializerFactory` and `HttpServerSharedInitializerFactory`.
   
   I left it rather than remove a security-looking line on my own judgement. 
Happy to split it into its own issue and fix it either way — drop the algorithm 
in server mode, or document the option as client-side only — once a committer 
says which they prefer. @gnodet @davsclaus, that is the one thing on this PR 
that wants a human call.
   
   _Claude Code on behalf of oscerd_
   



-- 
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]

Reply via email to