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

xiangfu0 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/pinot.git


The following commit(s) were added to refs/heads/master by this push:
     new c834ee0ed82 Verify broker hostname when the Java client uses a custom 
SSLContext (#20136)
c834ee0ed82 is described below

commit c834ee0ed8229a7df4b9a32dcbf892bb1142fb21
Author: Xiang Fu <[email protected]>
AuthorDate: Wed Nov 18 15:25:38 2026 -0800

    Verify broker hostname when the Java client uses a custom SSLContext 
(#20136)
    
    * Verify broker hostname when the Java client uses a custom SSLContext
    
    DefaultSslContextProvider installs its own AsyncHttpClient SslEngineFactory 
when an
    SSLContext is supplied. That replaces AHC's DefaultSslEngineFactory, which 
is where AHC sets
    the HTTPS endpoint identification algorithm, so the JDK SSLEngine accepted 
a trusted
    certificate issued for any host. Set the algorithm on the engine unless the 
AHC config
    disables it (disableHttpsEndpointIdentificationAlgorithm).
    
    * Add real TLS handshake test for Java client hostname verification
    
    Starts an in-process HTTPS server whose self-signed certificate is valid 
only for
    dns:localhost and connects through DefaultSslContextProvider with a client 
that trusts exactly
    that certificate: localhost succeeds, 127.0.0.1 fails with a 
CertificateException, and the same
    request succeeds when the AsyncHttpClient config disables HTTPS endpoint 
identification.
    
    * Clarify certificate matching in DefaultSslContextProvider docs
    
    Hostnames can still match the certificate CN when it has no DNS SAN; only 
IP addresses strictly
    need an IP SAN. Note that AsyncHttpClient reads the opt-out property once 
per JVM.
    
    ---------
    
    Co-authored-by: intentlab-ai[bot] 
<intentlab-ai[bot]@users.noreply.github.com>
---
 .../pinot/client/DefaultSslContextProvider.java    |  16 +++
 .../DefaultSslContextProviderHandshakeTest.java    | 128 +++++++++++++++++++++
 .../pinot/client/SslContextProviderTest.java       |  18 +++
 .../src/test/resources/tls/localhost-only.p12      | Bin 0 -> 2606 bytes
 4 files changed, 162 insertions(+)

diff --git 
a/pinot-clients/pinot-java-client/src/main/java/org/apache/pinot/client/DefaultSslContextProvider.java
 
b/pinot-clients/pinot-java-client/src/main/java/org/apache/pinot/client/DefaultSslContextProvider.java
index b2588504714..238221d8055 100644
--- 
a/pinot-clients/pinot-java-client/src/main/java/org/apache/pinot/client/DefaultSslContextProvider.java
+++ 
b/pinot-clients/pinot-java-client/src/main/java/org/apache/pinot/client/DefaultSslContextProvider.java
@@ -22,10 +22,18 @@ import java.util.List;
 import javax.annotation.Nullable;
 import javax.net.ssl.SSLContext;
 import javax.net.ssl.SSLEngine;
+import javax.net.ssl.SSLParameters;
 import org.asynchttpclient.DefaultAsyncHttpClientConfig;
 
 
 /// Default SSL context provider that uses the JDK/BCJSSE stack and disables 
OpenSSL.
+///
+/// When an [SSLContext] is supplied, every engine verifies that the broker 
certificate matches the host the client
+/// dials (HTTPS endpoint identification): a hostname must match a DNS SAN, or 
the CN when the certificate has no DNS
+/// SAN, and an IP address must match an IP SAN. Brokers register their IP 
address unless configured to use a
+/// hostname. Verification is skipped only when the AsyncHttpClient config 
disables it, e.g. with the JVM-wide system
+/// property 
`org.asynchttpclient.disableHttpsEndpointIdentificationAlgorithm=true`, which 
AsyncHttpClient reads once
+/// per JVM.
 public class DefaultSslContextProvider implements SslContextProvider {
 
   @Override
@@ -45,6 +53,14 @@ public class DefaultSslContextProvider implements 
SslContextProvider {
       builder.setSslEngineFactory((config, peerHost, peerPort) -> {
         SSLEngine engine = sslContext.createSSLEngine(peerHost, peerPort);
         engine.setUseClientMode(true);
+        // A JDK SSLEngine verifies the peer hostname only when an endpoint 
identification algorithm is set. This
+        // factory replaces AsyncHttpClient's own engine factory, so apply the 
same check it would, honoring
+        // disableHttpsEndpointIdentificationAlgorithm.
+        if (!config.isDisableHttpsEndpointIdentificationAlgorithm()) {
+          SSLParameters sslParameters = engine.getSSLParameters();
+          sslParameters.setEndpointIdentificationAlgorithm("HTTPS");
+          engine.setSSLParameters(sslParameters);
+        }
         if (enabledProtocols.length > 0) {
           engine.setEnabledProtocols(enabledProtocols);
         }
diff --git 
a/pinot-clients/pinot-java-client/src/test/java/org/apache/pinot/client/DefaultSslContextProviderHandshakeTest.java
 
b/pinot-clients/pinot-java-client/src/test/java/org/apache/pinot/client/DefaultSslContextProviderHandshakeTest.java
new file mode 100644
index 00000000000..2c2d8dd6328
--- /dev/null
+++ 
b/pinot-clients/pinot-java-client/src/test/java/org/apache/pinot/client/DefaultSslContextProviderHandshakeTest.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.pinot.client;
+
+import com.sun.net.httpserver.HttpsConfigurator;
+import com.sun.net.httpserver.HttpsServer;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.InetSocketAddress;
+import java.nio.charset.StandardCharsets;
+import java.security.KeyStore;
+import java.security.cert.CertificateException;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeUnit;
+import javax.net.ssl.KeyManagerFactory;
+import javax.net.ssl.SSLContext;
+import javax.net.ssl.TrustManagerFactory;
+import org.apache.commons.lang3.exception.ExceptionUtils;
+import org.asynchttpclient.AsyncHttpClient;
+import org.asynchttpclient.DefaultAsyncHttpClientConfig;
+import org.asynchttpclient.Dsl;
+import org.testng.annotations.AfterClass;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Test;
+
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertTrue;
+import static org.testng.Assert.expectThrows;
+
+
+/// Real TLS handshakes through [DefaultSslContextProvider] against an 
in-process HTTPS server.
+///
+/// The server certificate (`tls/localhost-only.p12`, password `changeit`) is 
self-signed with `SAN=dns:localhost` and
+/// no IP SAN, and the client trusts exactly that certificate, so only 
hostname verification can reject a connection.
+/// It was generated with:
+/// `keytool -genkeypair -alias localhost -keyalg RSA -keysize 2048 -sigalg 
SHA256withRSA -dname CN=localhost
+/// -ext SAN=dns:localhost -validity 36500 -storetype PKCS12 -keystore 
localhost-only.p12 -storepass changeit`
+public class DefaultSslContextProviderHandshakeTest {
+  private static final String KEYSTORE_RESOURCE = "tls/localhost-only.p12";
+  private static final char[] KEYSTORE_PASSWORD = "changeit".toCharArray();
+
+  private HttpsServer _server;
+  private SSLContext _clientSslContext;
+
+  @BeforeClass
+  public void setUp()
+      throws Exception {
+    KeyStore keyStore = KeyStore.getInstance("PKCS12");
+    try (InputStream inputStream = 
getClass().getClassLoader().getResourceAsStream(KEYSTORE_RESOURCE)) {
+      keyStore.load(inputStream, KEYSTORE_PASSWORD);
+    }
+    KeyManagerFactory keyManagerFactory = 
KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
+    keyManagerFactory.init(keyStore, KEYSTORE_PASSWORD);
+    SSLContext serverSslContext = SSLContext.getInstance("TLS");
+    serverSslContext.init(keyManagerFactory.getKeyManagers(), null, null);
+
+    // Bind the wildcard address so both "localhost" (IPv4 or IPv6) and 
"127.0.0.1" reach the server.
+    _server = HttpsServer.create(new InetSocketAddress(0), 0);
+    _server.setHttpsConfigurator(new HttpsConfigurator(serverSslContext));
+    _server.createContext("/", exchange -> {
+      byte[] body = "ok".getBytes(StandardCharsets.UTF_8);
+      exchange.sendResponseHeaders(200, body.length);
+      try (OutputStream outputStream = exchange.getResponseBody()) {
+        outputStream.write(body);
+      }
+    });
+    _server.start();
+
+    TrustManagerFactory trustManagerFactory =
+        
TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
+    trustManagerFactory.init(keyStore);
+    _clientSslContext = SSLContext.getInstance("TLS");
+    _clientSslContext.init(null, trustManagerFactory.getTrustManagers(), null);
+  }
+
+  @AfterClass(alwaysRun = true)
+  public void tearDown() {
+    if (_server != null) {
+      _server.stop(0);
+    }
+  }
+
+  @Test
+  public void testMatchingHostnameSucceeds()
+      throws Exception {
+    assertEquals(getStatusCode(Dsl.config(), "localhost"), 200);
+  }
+
+  @Test
+  public void testMismatchedHostnameFails() {
+    ExecutionException e = expectThrows(ExecutionException.class, () -> 
getStatusCode(Dsl.config(), "127.0.0.1"));
+    assertTrue(ExceptionUtils.indexOfThrowable(e, CertificateException.class) 
>= 0, ExceptionUtils.getStackTrace(e));
+  }
+
+  @Test
+  public void testDisabledEndpointIdentificationAcceptsMismatchedHostname()
+      throws Exception {
+    DefaultAsyncHttpClientConfig.Builder builder = 
Dsl.config().setDisableHttpsEndpointIdentificationAlgorithm(true);
+    assertEquals(getStatusCode(builder, "127.0.0.1"), 200);
+  }
+
+  private int getStatusCode(DefaultAsyncHttpClientConfig.Builder builder, 
String host)
+      throws Exception {
+    new DefaultSslContextProvider().configure(builder, _clientSslContext, 
TlsProtocols.defaultProtocols(false));
+    try (AsyncHttpClient client = Dsl.asyncHttpClient(builder.build())) {
+      return client.prepareGet("https://"; + host + ":" + 
_server.getAddress().getPort() + "/")
+          .execute()
+          .get(30, TimeUnit.SECONDS)
+          .getStatusCode();
+    }
+  }
+}
diff --git 
a/pinot-clients/pinot-java-client/src/test/java/org/apache/pinot/client/SslContextProviderTest.java
 
b/pinot-clients/pinot-java-client/src/test/java/org/apache/pinot/client/SslContextProviderTest.java
index 318bd7c2f81..a753bf12556 100644
--- 
a/pinot-clients/pinot-java-client/src/test/java/org/apache/pinot/client/SslContextProviderTest.java
+++ 
b/pinot-clients/pinot-java-client/src/test/java/org/apache/pinot/client/SslContextProviderTest.java
@@ -43,6 +43,7 @@ import org.testng.annotations.Test;
 import static org.testng.Assert.assertEquals;
 import static org.testng.Assert.assertFalse;
 import static org.testng.Assert.assertNotNull;
+import static org.testng.Assert.assertNull;
 import static org.testng.Assert.assertTrue;
 
 
@@ -87,6 +88,23 @@ public class SslContextProviderTest {
     SSLEngine engine = engineFactory.newSslEngine(config, "localhost", 443);
     assertTrue(engine.getUseClientMode());
     assertEquals(engine.getEnabledProtocols(), new String[] {protocol});
+    
assertEquals(engine.getSSLParameters().getEndpointIdentificationAlgorithm(), 
"HTTPS");
+  }
+
+  @Test
+  public void testDefaultProviderHonorsDisabledHostnameVerification()
+      throws Exception {
+    DefaultAsyncHttpClientConfig.Builder builder = 
Dsl.config().setDisableHttpsEndpointIdentificationAlgorithm(true);
+
+    SSLContext sslContext = SSLContext.getInstance("TLS");
+    sslContext.init(null, null, null);
+
+    SslContextProvider provider = new DefaultSslContextProvider();
+    provider.configure(builder, sslContext, 
tlsProtocolsWith(selectSupportedProtocol(sslContext)));
+
+    DefaultAsyncHttpClientConfig config = builder.build();
+    SSLEngine engine = config.getSslEngineFactory().newSslEngine(config, 
"localhost", 443);
+    assertNull(engine.getSSLParameters().getEndpointIdentificationAlgorithm());
   }
 
   @Test
diff --git 
a/pinot-clients/pinot-java-client/src/test/resources/tls/localhost-only.p12 
b/pinot-clients/pinot-java-client/src/test/resources/tls/localhost-only.p12
new file mode 100644
index 00000000000..54851d3fc47
Binary files /dev/null and 
b/pinot-clients/pinot-java-client/src/test/resources/tls/localhost-only.p12 
differ


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to