You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@zookeeper.apache.org by "kezhuw (via GitHub)" <gi...@apache.org> on 2025/08/17 07:29:05 UTC

[PR] ZOOKEEPER-4958: Fix client hostname verification ignored in server if ssl.authProvider configured [zookeeper]

kezhuw opened a new pull request, #2303:
URL: https://github.com/apache/zookeeper/pull/2303

   `NettyServerCnxnFactory` uses `TrustManager` from `X509AuthenticationProvider`
   if `ssl.authProvider` is configured. But `clientHostnameVerificationEnabled`
   is explicitly set to `false` in construction of `X509AuthenticationProvider`.
   
   This cause the server skip hostname verification agaist client
   certificate.
   
   This is reproducible in case of following server configs:
   
   * zookeeper.ssl.hostnameVerification: true
   * zookeeper.ssl.clientHostnameVerification: true
   * zookeeper.fips-mode: false
   * zookeeper.ssl.authProvider: x509


-- 
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: notifications-unsubscribe@zookeeper.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] ZOOKEEPER-4958: Fix client hostname verification ignored in server if ssl.authProvider configured [zookeeper]

Posted by "anmolnar (via GitHub)" <gi...@apache.org>.
anmolnar commented on code in PR #2303:
URL: https://github.com/apache/zookeeper/pull/2303#discussion_r2363695560


##########
zookeeper-server/src/test/java/org/apache/zookeeper/server/SSLHostnameVerificationTest.java:
##########
@@ -0,0 +1,459 @@
+/*
+ * 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.zookeeper.server;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.security.Security;
+import java.time.Duration;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.Properties;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+import org.apache.zookeeper.WatchedEvent;
+import org.apache.zookeeper.Watcher;
+import org.apache.zookeeper.ZooKeeper;
+import org.apache.zookeeper.client.ZKClientConfig;
+import org.apache.zookeeper.common.ssl.Ca;
+import org.apache.zookeeper.common.ssl.Cert;
+import org.apache.zookeeper.server.embedded.ExitHandler;
+import org.apache.zookeeper.server.embedded.ZooKeeperServerEmbedded;
+import org.apache.zookeeper.test.ClientBase;
+import org.bouncycastle.jce.provider.BouncyCastleProvider;
+import org.burningwave.tools.net.HostResolutionRequestInterceptor;
+import org.burningwave.tools.net.MappedHostResolver;
+import org.junit.jupiter.api.AfterAll;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.CsvSource;
+import org.junit.jupiter.params.provider.ValueSource;
+
+public class SSLHostnameVerificationTest {
+    @BeforeAll
+    public static void setupDNSMocks() {
+        Map<String, String> hostAliases = new LinkedHashMap<>();
+
+        // avoid resolving "localhost" to ipv6 address "::1"
+        hostAliases.put("localhost", "127.0.0.1");
+        HostResolutionRequestInterceptor.INSTANCE.install(new MappedHostResolver(hostAliases));
+        HostResolutionRequestInterceptor.INSTANCE.clearCache();
+    }
+
+    @AfterAll
+    public static void clearDNSMocks() {
+        HostResolutionRequestInterceptor.INSTANCE.uninstall();
+    }
+
+    @BeforeAll
+    public static void setup() {
+        Security.addProvider(new BouncyCastleProvider());
+    }
+
+    @AfterAll
+    public static void cleanup() {
+        Security.removeProvider("BC");
+    }
+
+    Watcher.Event.KeeperState checkConnectState(String connectString, ZKClientConfig clientConfig) throws Exception {
+        Duration timeout = Duration.ofSeconds(1);
+        Watcher.Event.KeeperState state;
+        CompletableFuture<WatchedEvent> future = new CompletableFuture<>();
+        try (ZooKeeper zk = new ZooKeeper(connectString, (int) timeout.toMillis(), future::complete, clientConfig)) {
+            try {
+                WatchedEvent event = future.get(timeout.toMillis() * 2, TimeUnit.MILLISECONDS);
+                state = event.getState();
+            } catch (TimeoutException ignored) {
+                // See: ZOOKEEPER-4508, ZOOKEEPER-4921, ZOOKEEPER-4923
+                state = Watcher.Event.KeeperState.Expired;
+            }
+        }
+        return state;
+    }
+
+    @ParameterizedTest(name = "{0}, fips-mode: {1}")
+    @CsvSource({
+            "localhost, true",
+            "localhost, false",
+            "127.0.0.1, true",
+            "127.0.0.1, false",
+    })
+    public void testClientHostnameVerificationWithMismatchNames(String serverHost, boolean fipsEnabled, @TempDir Path tmpDir) throws Exception {
+        try (Ca ca = Ca.create(tmpDir)) {
+            // given: server with cert mismatching cn/dns/ip
+            Cert server1Cert = ca.signer("abc0").withDnsName("abc1").withIpAddress("192.168.0.10").sign();
+            Properties config = server1Cert.buildServerProperties(ca);
+            config.put("ssl.hostnameVerification", "false");
+            config.put("secureClientPortAddress", serverHost);
+
+            try (ZooKeeperServerEmbedded server = ZooKeeperServerEmbedded
+                    .builder()
+                    .baseDir(Files.createTempDirectory(tmpDir, "server.data"))
+                    .configuration(config)
+                    .exitHandler(ExitHandler.LOG_ONLY)
+                    .build()) {
+                server.start();
+
+                // server ready
+                assertTrue(ClientBase.waitForServerUp(server.getConnectionString(), 60000));
+
+                Cert clientCert = ca.sign("client");
+                ZKClientConfig clientConfig = clientCert.buildClientConfig(ca);
+                clientConfig.setProperty("zookeeper.sasl.client", "false");
+                clientConfig.setProperty("zookeeper.fips-mode", Boolean.toString(fipsEnabled));
+                clientConfig.setProperty("zookeeper.ssl.hostnameVerification", "true");
+
+                // when: connect using mismatched dns/ip
+                String connectionString =  server.getSecureConnectionString();
+                // then: connection rejected by us as no matching name
+                assertEquals(Watcher.Event.KeeperState.Expired, checkConnectState(server.getSecureConnectionString(), clientConfig));
+            }
+        }
+    }
+
+    @ParameterizedTest
+    @ValueSource(strings = {"localhost", "127.0.0.1"})
+    public void testClientHostnameVerificationWithMatchingCnName(String serverHost, @TempDir Path tmpDir) throws Exception {
+        try (Ca ca = Ca.create(tmpDir)) {
+            // given: server cert with cn name "localhost"
+            Cert server1Cert = ca.signer("localhost").sign();

Review Comment:
   Add IP address to the SAN as well. Do not rely on reverse DNS lookup.



-- 
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: notifications-unsubscribe@zookeeper.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] ZOOKEEPER-4958: Fix client hostname verification ignored in server if ssl.authProvider configured [zookeeper]

Posted by "kezhuw (via GitHub)" <gi...@apache.org>.
kezhuw commented on code in PR #2303:
URL: https://github.com/apache/zookeeper/pull/2303#discussion_r2529683429


##########
zookeeper-server/src/test/java/org/apache/zookeeper/server/SSLHostnameVerificationTest.java:
##########
@@ -0,0 +1,459 @@
+/*
+ * 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.zookeeper.server;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.security.Security;
+import java.time.Duration;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.Properties;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+import org.apache.zookeeper.WatchedEvent;
+import org.apache.zookeeper.Watcher;
+import org.apache.zookeeper.ZooKeeper;
+import org.apache.zookeeper.client.ZKClientConfig;
+import org.apache.zookeeper.common.ssl.Ca;
+import org.apache.zookeeper.common.ssl.Cert;
+import org.apache.zookeeper.server.embedded.ExitHandler;
+import org.apache.zookeeper.server.embedded.ZooKeeperServerEmbedded;
+import org.apache.zookeeper.test.ClientBase;
+import org.bouncycastle.jce.provider.BouncyCastleProvider;
+import org.burningwave.tools.net.HostResolutionRequestInterceptor;
+import org.burningwave.tools.net.MappedHostResolver;
+import org.junit.jupiter.api.AfterAll;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.CsvSource;
+import org.junit.jupiter.params.provider.ValueSource;
+
+public class SSLHostnameVerificationTest {
+    @BeforeAll
+    public static void setupDNSMocks() {
+        Map<String, String> hostAliases = new LinkedHashMap<>();
+
+        // avoid resolving "localhost" to ipv6 address "::1"
+        hostAliases.put("localhost", "127.0.0.1");
+        HostResolutionRequestInterceptor.INSTANCE.install(new MappedHostResolver(hostAliases));
+        HostResolutionRequestInterceptor.INSTANCE.clearCache();
+    }
+
+    @AfterAll
+    public static void clearDNSMocks() {
+        HostResolutionRequestInterceptor.INSTANCE.uninstall();
+    }
+
+    @BeforeAll
+    public static void setup() {
+        Security.addProvider(new BouncyCastleProvider());
+    }
+
+    @AfterAll
+    public static void cleanup() {
+        Security.removeProvider("BC");
+    }
+
+    Watcher.Event.KeeperState checkConnectState(String connectString, ZKClientConfig clientConfig) throws Exception {
+        Duration timeout = Duration.ofSeconds(1);
+        Watcher.Event.KeeperState state;
+        CompletableFuture<WatchedEvent> future = new CompletableFuture<>();
+        try (ZooKeeper zk = new ZooKeeper(connectString, (int) timeout.toMillis(), future::complete, clientConfig)) {
+            try {
+                WatchedEvent event = future.get(timeout.toMillis() * 2, TimeUnit.MILLISECONDS);
+                state = event.getState();
+            } catch (TimeoutException ignored) {
+                // See: ZOOKEEPER-4508, ZOOKEEPER-4921, ZOOKEEPER-4923
+                state = Watcher.Event.KeeperState.Expired;
+            }
+        }
+        return state;
+    }
+
+    @ParameterizedTest(name = "{0}, fips-mode: {1}")
+    @CsvSource({
+            "localhost, true",
+            "localhost, false",
+            "127.0.0.1, true",
+            "127.0.0.1, false",
+    })
+    public void testClientHostnameVerificationWithMismatchNames(String serverHost, boolean fipsEnabled, @TempDir Path tmpDir) throws Exception {
+        try (Ca ca = Ca.create(tmpDir)) {
+            // given: server with cert mismatching cn/dns/ip
+            Cert server1Cert = ca.signer("abc0").withDnsName("abc1").withIpAddress("192.168.0.10").sign();
+            Properties config = server1Cert.buildServerProperties(ca);
+            config.put("ssl.hostnameVerification", "false");
+            config.put("secureClientPortAddress", serverHost);
+
+            try (ZooKeeperServerEmbedded server = ZooKeeperServerEmbedded
+                    .builder()
+                    .baseDir(Files.createTempDirectory(tmpDir, "server.data"))
+                    .configuration(config)
+                    .exitHandler(ExitHandler.LOG_ONLY)
+                    .build()) {
+                server.start();
+
+                // server ready
+                assertTrue(ClientBase.waitForServerUp(server.getConnectionString(), 60000));
+
+                Cert clientCert = ca.sign("client");
+                ZKClientConfig clientConfig = clientCert.buildClientConfig(ca);
+                clientConfig.setProperty("zookeeper.sasl.client", "false");
+                clientConfig.setProperty("zookeeper.fips-mode", Boolean.toString(fipsEnabled));
+                clientConfig.setProperty("zookeeper.ssl.hostnameVerification", "true");
+
+                // when: connect using mismatched dns/ip
+                String connectionString =  server.getSecureConnectionString();
+                // then: connection rejected by us as no matching name
+                assertEquals(Watcher.Event.KeeperState.Expired, checkConnectState(server.getSecureConnectionString(), clientConfig));
+            }
+        }
+    }
+
+    @ParameterizedTest
+    @ValueSource(strings = {"localhost", "127.0.0.1"})
+    public void testClientHostnameVerificationWithMatchingCnName(String serverHost, @TempDir Path tmpDir) throws Exception {
+        try (Ca ca = Ca.create(tmpDir)) {
+            // given: server cert with cn name "localhost"
+            Cert server1Cert = ca.signer("localhost").sign();

Review Comment:
   As the name saying, this test is testing against "cn" name.
   
   Also, all tests in `SSLHostnameVerificationTest` are guarded by `setupDNSMocks`. I tested it and it turns out to be pretty stable.
   
   ```java
       @BeforeAll
       public static void setupDNSMocks() {
           Map<String, String> hostAliases = new LinkedHashMap<>();
   
           // avoid resolving "localhost" to ipv6 address "::1"
           hostAliases.put("localhost", "127.0.0.1");
           HostResolutionRequestInterceptor.INSTANCE.install(new MappedHostResolver(hostAliases));
           HostResolutionRequestInterceptor.INSTANCE.clearCache();
       }
   ```



-- 
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: notifications-unsubscribe@zookeeper.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] ZOOKEEPER-4958: Fix client hostname verification ignored in server if ssl.authProvider configured [zookeeper]

Posted by "kezhuw (via GitHub)" <gi...@apache.org>.
kezhuw commented on PR #2303:
URL: https://github.com/apache/zookeeper/pull/2303#issuecomment-3194198146

   It contains changes of #2300 and #2302 for now.


-- 
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: notifications-unsubscribe@zookeeper.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] ZOOKEEPER-4958: Fix client hostname verification ignored in server if ssl.authProvider configured [zookeeper]

Posted by "kezhuw (via GitHub)" <gi...@apache.org>.
kezhuw merged PR #2303:
URL: https://github.com/apache/zookeeper/pull/2303


-- 
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: notifications-unsubscribe@zookeeper.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org