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-unsubscr...@zookeeper.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org