gnodet-bot commented on code in PR #30350:
URL: https://github.com/apache/camel/pull/30350#discussion_r4621651500


##########
components/camel-pulsar/src/test/java/org/apache/camel/component/pulsar/PulsarHostnameVerificationTest.java:
##########
@@ -0,0 +1,141 @@
+/*
+ * 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.pulsar;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.InetSocketAddress;
+import java.net.URL;
+import java.nio.file.Path;
+import java.security.KeyStore;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.TimeUnit;
+
+import javax.net.ssl.KeyManagerFactory;
+import javax.net.ssl.SSLContext;
+import javax.net.ssl.SSLServerSocket;
+import javax.net.ssl.SSLSocket;
+
+import org.apache.camel.test.junit6.CamelTestSupport;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * Connects with pulsar+ssl:// to localhost, to a local server whose 
certificate is issued either for localhost or for
+ * another host name, to check that the host name in the certificate is 
checked.
+ */
+public class PulsarHostnameVerificationTest extends CamelTestSupport {
+
+    private static final String RESOURCES = 
"org/apache/camel/component/pulsar/hostname/";
+    private static final String PASSWORD = "changeit";
+    private static final long TIMEOUT_SECONDS = 20;
+
+    private final ExecutorService executor = 
Executors.newSingleThreadExecutor();
+    private SSLServerSocket server;
+
+    @AfterEach
+    void stopServer() throws IOException {
+        if (server != null) {
+            server.close();
+        }
+        executor.shutdownNow();
+    }
+
+    @Test
+    void certificateForAnotherHostIsRefused() throws Exception {
+        CompletableFuture<Boolean> command = startServer("other.p12");
+
+        connect("");
+
+        assertFalse(command.get(TIMEOUT_SECONDS, TimeUnit.SECONDS),
+                "the certificate names another host, so the connection must 
fail");
+    }
+
+    @Test
+    void certificateForTheHostIsAccepted() throws Exception {
+        CompletableFuture<Boolean> command = startServer("server.p12");
+
+        connect("");
+
+        assertTrue(command.get(TIMEOUT_SECONDS, TimeUnit.SECONDS), "the 
certificate names the host");
+    }
+
+    @Test
+    void hostnameVerificationCanBeTurnedOff() throws Exception {
+        CompletableFuture<Boolean> command = startServer("other.p12");
+
+        connect("&hostnameVerification=false");
+
+        assertTrue(command.get(TIMEOUT_SECONDS, TimeUnit.SECONDS),
+                "without the host name check, a trusted certificate is 
accepted for any host");
+    }
+
+    private void connect(String options) throws Exception {
+        URL ca = getClass().getClassLoader().getResource(RESOURCES + "ca.pem");
+        PulsarEndpoint endpoint = 
context.getEndpoint("pulsar:persistent://public/default/hostname?serviceUrl=RAW(pulsar+ssl://"
+                                                      + "localhost:" + 
server.getLocalPort() + ")"
+                                                      + 
"&tlsTrustCertsFilePath=" + Path.of(ca.toURI())
+                                                      + options,
+                PulsarEndpoint.class);
+        endpoint.start();
+        // the client connects when it is first used; the server is not a 
broker, so this does not complete
+        
endpoint.getPulsarClient().getPartitionsForTopic("persistent://public/default/hostname");

Review Comment:
   ⚠️ **Still open from previous review.** `getPartitionsForTopic()` returns a 
`CompletableFuture` whose result is never captured, and the Pulsar client is 
not closed after the assertion. If the TLS handshake fails (expected in 
`certificateForAnotherHostIsRefused`), the Pulsar client may internally retry 
connecting until the server socket is closed in `@AfterEach`. This is harmless 
today but can slow teardown or produce background noise in test logs.
   
   Consider adding an explicit `endpoint.stop()` or 
`endpoint.getPulsarClient().close()` call after the assertion in the 
`connect()` method.



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