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

jbonofre pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow-java.git


The following commit(s) were added to refs/heads/main by this push:
     new e8f68ddd3 GH-1500: [JDBC] Harden trustStore and tlsRootCerts handling 
for blank values and missing password (#1501)
e8f68ddd3 is described below

commit e8f68ddd3b41f36910597b55e3df8bbd72e78271
Author: JB Onofré <[email protected]>
AuthorDate: Thu Nov 19 08:36:44 2026 +0000

    GH-1500: [JDBC] Harden trustStore and tlsRootCerts handling for blank 
values and missing password (#1501)
    
    ## What's Changed
    
    Follow-up to #1496 (#1499).
    
    - Treat blank `trustStore` and `tlsRootCerts` values as unset (`null`)
    in `ArrowFlightConnectionConfigImpl` and
    `ArrowFlightSqlClientHandler.Builder` (consistent with
    `hostnameOverride`), so empty query parameters or blank optional
    properties (e.g., `?trustStore=&useSystemTrustStore=true` or
    `?tlsRootCerts=&trustStore=...`) do not shadow fallback trust sources or
    fail trying to open an empty path.
    - Validate `trustStorePassword != null` in
    `ArrowFlightSqlClientHandler.Builder#build()` before calling
    `ClientAuthenticationUtils.getCertificateStream(...)`, so configuring
    `trustStore` without `trustStorePassword` throws `SQLException`
    (wrapping `IllegalArgumentException`) and closes the connection
    allocator instead of leaking an uncaught `NullPointerException`.
    - Sync Javadoc on
    `ArrowFlightSqlClientHandler.Builder#withSystemTrustStore`,
    `#withTrustStorePath`, and `#withTlsRootCertificates`, and clarify
    precedence and blank-value handling in
    `docs/source/flight_sql_jdbc_driver.rst`.
    - Tighten
    
`ConnectionTlsTest#testGetAuthenticatedEncryptedConnectionWithKeyStoreBadPassword`
    to assert the `IOException` -> `UnrecoverableKeyException` cause chain,
    and add unit/connection tests for blank `trustStore`/`tlsRootCerts` and
    missing `trustStorePassword`.
    
    ## Tests
    
    - Ran `ConnectionTlsTest`, `ConnectionTlsRootCertsTest`,
    `ConnectionTlsHostnameOverrideTest`, `ConnectionMutualTlsTest`,
    `ArrowFlightSqlClientHandlerBuilderTest`, and
    `ArrowFlightConnectionConfigImplTest` on JDK 17, 21, and 25, plus
    `spotless:check` and `checkstyle:check`.
    
    Closes #1500.
---
 docs/source/flight_sql_jdbc_driver.rst             |   9 +-
 .../jdbc/client/ArrowFlightSqlClientHandler.java   |  32 ++++--
 .../utils/ArrowFlightConnectionConfigImpl.java     |  20 +++-
 .../arrow/driver/jdbc/ConnectionTlsTest.java       | 112 +++++++++++++++++++--
 .../ArrowFlightSqlClientHandlerBuilderTest.java    |  33 ++++++
 .../utils/ArrowFlightConnectionConfigImplTest.java |  28 ++++++
 6 files changed, 209 insertions(+), 25 deletions(-)

diff --git a/docs/source/flight_sql_jdbc_driver.rst 
b/docs/source/flight_sql_jdbc_driver.rst
index 5e5a55981..fe731fcaa 100644
--- a/docs/source/flight_sql_jdbc_driver.rst
+++ b/docs/source/flight_sql_jdbc_driver.rst
@@ -107,16 +107,19 @@ case-sensitive. The supported parameters are:
 
    * - trustStore
      - null
-     - When TLS is enabled, the path to the certificate store
+     - When TLS is enabled, the path to the certificate store. Blank values
+       are treated as unset. Takes precedence over ``useSystemTrustStore``,
+       and is ignored when ``tlsRootCerts`` is set.
 
    * - trustStorePassword
      - null
-     - When TLS is enabled, the password for the certificate store
+     - When TLS is enabled, the password for the certificate store. Required
+       when ``trustStore`` is set.
 
    * - tlsRootCerts
      - null
      - Path to PEM-encoded root certificates for TLS - use this as
-       an alternative to ``trustStore``
+       an alternative to ``trustStore``. Blank values are treated as unset.
 
    * - hostnameOverride
      - null
diff --git 
a/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/client/ArrowFlightSqlClientHandler.java
 
b/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/client/ArrowFlightSqlClientHandler.java
index 74da46380..97108e599 100644
--- 
a/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/client/ArrowFlightSqlClientHandler.java
+++ 
b/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/client/ArrowFlightSqlClientHandler.java
@@ -689,7 +689,7 @@ public final class ArrowFlightSqlClientHandler implements 
AutoCloseable {
 
     @VisibleForTesting String password;
 
-    @VisibleForTesting String trustStorePath;
+    @VisibleForTesting @Nullable String trustStorePath;
 
     @VisibleForTesting String trustStorePassword;
 
@@ -703,7 +703,7 @@ public final class ArrowFlightSqlClientHandler implements 
AutoCloseable {
 
     @VisibleForTesting boolean useSystemTrustStore = true;
 
-    @VisibleForTesting String tlsRootCertificatesPath;
+    @VisibleForTesting @Nullable String tlsRootCertificatesPath;
 
     @VisibleForTesting String clientCertificatePath;
 
@@ -821,13 +821,16 @@ public final class ArrowFlightSqlClientHandler implements 
AutoCloseable {
     }
 
     /**
-     * Sets the KeyStore path for this handler.
+     * Sets the KeyStore path for this handler. Takes precedence over {@link
+     * #withSystemTrustStore(boolean)}, while {@link 
#withTlsRootCertificates(String)} takes
+     * precedence over this setting.
      *
-     * @param trustStorePath the KeyStore path.
+     * @param trustStorePath the KeyStore path, or null or blank to unset.
      * @return this instance.
      */
-    public Builder withTrustStorePath(final String trustStorePath) {
-      this.trustStorePath = trustStorePath;
+    public Builder withTrustStorePath(@Nullable final String trustStorePath) {
+      this.trustStorePath =
+          trustStorePath == null || trustStorePath.trim().isEmpty() ? null : 
trustStorePath;
       return this;
     }
 
@@ -915,7 +918,8 @@ public final class ArrowFlightSqlClientHandler implements 
AutoCloseable {
     }
 
     /**
-     * Sets whether to use the certificates from the operating system.
+     * Sets whether to use the certificates from the operating system. Ignored 
when {@link
+     * #withTrustStorePath(String)} or {@link 
#withTlsRootCertificates(String)} is set.
      *
      * @param useSystemTrustStore whether to use the system operating 
certificates.
      * @return this instance.
@@ -929,11 +933,15 @@ public final class ArrowFlightSqlClientHandler implements 
AutoCloseable {
      * Sets the TLS root certificate path as an alternative to using the 
System or other Trust
      * Store. The path must contain a valid PEM file.
      *
-     * @param tlsRootCertificatesPath the TLS root certificate path (if TLS is 
required).
+     * @param tlsRootCertificatesPath the TLS root certificate path (if TLS is 
required), or null or
+     *     blank to unset.
      * @return this instance.
      */
-    public Builder withTlsRootCertificates(final String 
tlsRootCertificatesPath) {
-      this.tlsRootCertificatesPath = tlsRootCertificatesPath;
+    public Builder withTlsRootCertificates(@Nullable final String 
tlsRootCertificatesPath) {
+      this.tlsRootCertificatesPath =
+          tlsRootCertificatesPath == null || 
tlsRootCertificatesPath.trim().isEmpty()
+              ? null
+              : tlsRootCertificatesPath;
       return this;
     }
 
@@ -1155,6 +1163,10 @@ public final class ArrowFlightSqlClientHandler 
implements AutoCloseable {
               clientBuilder.trustedCertificates(
                   
ClientAuthenticationUtils.getTlsRootCertificatesStream(tlsRootCertificatesPath));
             } else if (trustStorePath != null) {
+              if (trustStorePassword == null) {
+                throw new IllegalArgumentException(
+                    "trustStorePassword must be specified when trustStore is 
set.");
+              }
               clientBuilder.trustedCertificates(
                   ClientAuthenticationUtils.getCertificateStream(
                       trustStorePath, trustStorePassword));
diff --git 
a/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/utils/ArrowFlightConnectionConfigImpl.java
 
b/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/utils/ArrowFlightConnectionConfigImpl.java
index 32c18a560..8460474ba 100644
--- 
a/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/utils/ArrowFlightConnectionConfigImpl.java
+++ 
b/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/utils/ArrowFlightConnectionConfigImpl.java
@@ -84,10 +84,11 @@ public final class ArrowFlightConnectionConfigImpl extends 
ConnectionConfigImpl
   /**
    * Gets the KeyStore path.
    *
-   * @return the path.
+   * @return the path, or null if not set or blank.
    */
-  public String getTrustStorePath() {
-    return ArrowFlightConnectionProperty.TRUST_STORE.getString(properties);
+  public @Nullable String getTrustStorePath() {
+    final String trustStorePath = 
ArrowFlightConnectionProperty.TRUST_STORE.getString(properties);
+    return trustStorePath == null || trustStorePath.trim().isEmpty() ? null : 
trustStorePath;
   }
 
   /**
@@ -108,8 +109,17 @@ public final class ArrowFlightConnectionConfigImpl extends 
ConnectionConfigImpl
     return 
ArrowFlightConnectionProperty.USE_SYSTEM_TRUST_STORE.getBoolean(properties);
   }
 
-  public String getTlsRootCertificatesPath() {
-    return ArrowFlightConnectionProperty.TLS_ROOT_CERTS.getString(properties);
+  /**
+   * Gets the path to PEM-encoded TLS root certificates.
+   *
+   * @return the path, or null if not set or blank.
+   */
+  public @Nullable String getTlsRootCertificatesPath() {
+    final String tlsRootCertificatesPath =
+        ArrowFlightConnectionProperty.TLS_ROOT_CERTS.getString(properties);
+    return tlsRootCertificatesPath == null || 
tlsRootCertificatesPath.trim().isEmpty()
+        ? null
+        : tlsRootCertificatesPath;
   }
 
   /**
diff --git 
a/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ConnectionTlsTest.java
 
b/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ConnectionTlsTest.java
index 47e8404ef..b6fd045d0 100644
--- 
a/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ConnectionTlsTest.java
+++ 
b/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ConnectionTlsTest.java
@@ -16,13 +16,16 @@
  */
 package org.apache.arrow.driver.jdbc;
 
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
 import static org.junit.jupiter.api.Assertions.assertNotNull;
 import static org.junit.jupiter.api.Assertions.assertThrows;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 import static org.junit.jupiter.api.Assertions.fail;
 
+import java.io.IOException;
 import java.net.URLEncoder;
 import java.nio.file.Paths;
+import java.security.UnrecoverableKeyException;
 import java.sql.Connection;
 import java.sql.Driver;
 import java.sql.DriverManager;
@@ -33,6 +36,7 @@ import 
org.apache.arrow.driver.jdbc.client.ArrowFlightSqlClientHandler;
 import 
org.apache.arrow.driver.jdbc.utils.ArrowFlightConnectionConfigImpl.ArrowFlightConnectionProperty;
 import org.apache.arrow.driver.jdbc.utils.FlightSqlTestCertificates;
 import org.apache.arrow.driver.jdbc.utils.MockFlightSqlProducer;
+import org.apache.arrow.flight.FlightRuntimeException;
 import org.apache.arrow.memory.BufferAllocator;
 import org.apache.arrow.memory.RootAllocator;
 import org.apache.arrow.util.AutoCloseables;
@@ -259,13 +263,16 @@ public class ConnectionTlsTest {
     final ArrowFlightJdbcDataSource dataSource =
         ArrowFlightJdbcDataSource.createNewDataSource(properties);
 
-    assertThrows(
-        SQLException.class,
-        () -> {
-          try (final Connection ignored = dataSource.getConnection()) {
-            fail();
-          }
-        });
+    final SQLException exception =
+        assertThrows(
+            SQLException.class,
+            () -> {
+              try (final Connection ignored = dataSource.getConnection()) {
+                fail();
+              }
+            });
+    assertInstanceOf(IOException.class, exception.getCause());
+    assertInstanceOf(UnrecoverableKeyException.class, 
exception.getCause().getCause());
   }
 
   /**
@@ -322,6 +329,97 @@ public class ConnectionTlsTest {
     }
   }
 
+  /**
+   * Check that configuring a trust store without a password throws a {@link 
SQLException} rather
+   * than an uncaught {@link NullPointerException}.
+   */
+  @Test
+  public void testGetEncryptedConnectionWithTrustStoreMissingPassword() {
+    final Properties properties = new Properties();
+
+    properties.put(
+        ArrowFlightConnectionProperty.HOST.camelName(), 
FLIGHT_SERVER_TEST_EXTENSION.getHost());
+    properties.put(
+        ArrowFlightConnectionProperty.PORT.camelName(), 
FLIGHT_SERVER_TEST_EXTENSION.getPort());
+    properties.put(ArrowFlightConnectionProperty.USER.camelName(), userTest);
+    properties.put(ArrowFlightConnectionProperty.PASSWORD.camelName(), 
passTest);
+    properties.put(ArrowFlightConnectionProperty.USE_ENCRYPTION.camelName(), 
true);
+    properties.put(ArrowFlightConnectionProperty.TRUST_STORE.camelName(), 
trustStorePath);
+
+    final ArrowFlightJdbcDataSource dataSource =
+        ArrowFlightJdbcDataSource.createNewDataSource(properties);
+
+    final SQLException exception =
+        assertThrows(
+            SQLException.class,
+            () -> {
+              try (final Connection ignored = dataSource.getConnection()) {
+                fail();
+              }
+            });
+    assertInstanceOf(IllegalArgumentException.class, exception.getCause());
+  }
+
+  /**
+   * Check that a blank tlsRootCerts value is treated as unset and falls 
through to trustStore.
+   *
+   * @throws Exception on error.
+   */
+  @Test
+  public void 
testGetEncryptedConnectionWithBlankTlsRootCertsFallsBackToTrustStore()
+      throws Exception {
+    final Driver driver = new ArrowFlightJdbcDriver();
+    DriverManager.registerDriver(driver);
+
+    try (final Connection connection =
+        DriverManager.getConnection(
+            String.format(
+                "jdbc:arrow-flight-sql://localhost:%s?user=%s&password=%s"
+                    + "&useEncryption=true&%s=&%s=%s&%s=%s",
+                FLIGHT_SERVER_TEST_EXTENSION.getPort(),
+                userTest,
+                passTest,
+                ArrowFlightConnectionProperty.TLS_ROOT_CERTS.camelName(),
+                ArrowFlightConnectionProperty.TRUST_STORE.camelName(),
+                URLEncoder.encode(trustStorePath, "UTF-8"),
+                ArrowFlightConnectionProperty.TRUST_STORE_PASSWORD.camelName(),
+                URLEncoder.encode(trustStorePass, "UTF-8")))) {
+      assertTrue(connection.isValid(0));
+    }
+  }
+
+  /**
+   * Check that a blank trustStore value is treated as unset rather than 
attempting to open an empty
+   * path or requiring a trustStorePassword.
+   *
+   * @throws Exception on error.
+   */
+  @Test
+  public void testGetEncryptedConnectionWithBlankTrustStoreIsIgnored() throws 
Exception {
+    final Driver driver = new ArrowFlightJdbcDriver();
+    DriverManager.registerDriver(driver);
+
+    final SQLException exception =
+        assertThrows(
+            SQLException.class,
+            () -> {
+              try (final Connection ignored =
+                  DriverManager.getConnection(
+                      String.format(
+                          
"jdbc:arrow-flight-sql://localhost:%s?user=%s&password=%s"
+                              + 
"&useEncryption=true&useSystemTrustStore=false&%s=",
+                          FLIGHT_SERVER_TEST_EXTENSION.getPort(),
+                          userTest,
+                          passTest,
+                          
ArrowFlightConnectionProperty.TRUST_STORE.camelName()))) {
+                fail();
+              }
+            });
+    // Blank trustStore is skipped, so connection reaches TLS handshake and 
fails certificate
+    // verification instead of failing on missing trustStorePassword or an 
empty file path.
+    assertInstanceOf(FlightRuntimeException.class, exception.getCause());
+  }
+
   /**
    * Check if an encrypted connection can be established successfully when 
connecting through the
    * DriverManager using just a connection url.
diff --git 
a/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/client/ArrowFlightSqlClientHandlerBuilderTest.java
 
b/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/client/ArrowFlightSqlClientHandlerBuilderTest.java
index d7fc765d6..58e139b58 100644
--- 
a/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/client/ArrowFlightSqlClientHandlerBuilderTest.java
+++ 
b/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/client/ArrowFlightSqlClientHandlerBuilderTest.java
@@ -265,6 +265,39 @@ public class ArrowFlightSqlClientHandlerBuilderTest {
         new ArrowFlightSqlClientHandler.Builder().withHostnameOverride("  
").hostnameOverride);
   }
 
+  @Test
+  public void testBlankTrustStorePathIsUnset() {
+    assertNull(new 
ArrowFlightSqlClientHandler.Builder().withTrustStorePath(null).trustStorePath);
+    assertNull(new 
ArrowFlightSqlClientHandler.Builder().withTrustStorePath("").trustStorePath);
+    assertNull(new ArrowFlightSqlClientHandler.Builder().withTrustStorePath("  
").trustStorePath);
+    assertEquals(
+        "/path/to/truststore.jks",
+        new ArrowFlightSqlClientHandler.Builder()
+            .withTrustStorePath("/path/to/truststore.jks")
+            .trustStorePath);
+  }
+
+  @Test
+  public void testBlankTlsRootCertificatesPathIsUnset() {
+    assertNull(
+        new ArrowFlightSqlClientHandler.Builder()
+            .withTlsRootCertificates(null)
+            .tlsRootCertificatesPath);
+    assertNull(
+        new ArrowFlightSqlClientHandler.Builder()
+            .withTlsRootCertificates("")
+            .tlsRootCertificatesPath);
+    assertNull(
+        new ArrowFlightSqlClientHandler.Builder()
+            .withTlsRootCertificates("  ")
+            .tlsRootCertificatesPath);
+    assertEquals(
+        "/path/to/ca.pem",
+        new ArrowFlightSqlClientHandler.Builder()
+            .withTlsRootCertificates("/path/to/ca.pem")
+            .tlsRootCertificatesPath);
+  }
+
   @Test
   public void testHostnameOverrideIsTrimmed() {
     assertEquals(
diff --git 
a/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/utils/ArrowFlightConnectionConfigImplTest.java
 
b/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/utils/ArrowFlightConnectionConfigImplTest.java
index 216bfa3c0..923d98d10 100644
--- 
a/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/utils/ArrowFlightConnectionConfigImplTest.java
+++ 
b/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/utils/ArrowFlightConnectionConfigImplTest.java
@@ -24,6 +24,8 @@ import static 
org.apache.arrow.driver.jdbc.utils.ArrowFlightConnectionConfigImpl
 import static 
org.apache.arrow.driver.jdbc.utils.ArrowFlightConnectionConfigImpl.ArrowFlightConnectionProperty.PASSWORD;
 import static 
org.apache.arrow.driver.jdbc.utils.ArrowFlightConnectionConfigImpl.ArrowFlightConnectionProperty.PORT;
 import static 
org.apache.arrow.driver.jdbc.utils.ArrowFlightConnectionConfigImpl.ArrowFlightConnectionProperty.THREAD_POOL_SIZE;
+import static 
org.apache.arrow.driver.jdbc.utils.ArrowFlightConnectionConfigImpl.ArrowFlightConnectionProperty.TLS_ROOT_CERTS;
+import static 
org.apache.arrow.driver.jdbc.utils.ArrowFlightConnectionConfigImpl.ArrowFlightConnectionProperty.TRUST_STORE;
 import static 
org.apache.arrow.driver.jdbc.utils.ArrowFlightConnectionConfigImpl.ArrowFlightConnectionProperty.USER;
 import static 
org.apache.arrow.driver.jdbc.utils.ArrowFlightConnectionConfigImpl.ArrowFlightConnectionProperty.USE_CLIENT_CACHE;
 import static 
org.apache.arrow.driver.jdbc.utils.ArrowFlightConnectionConfigImpl.ArrowFlightConnectionProperty.USE_ENCRYPTION;
@@ -104,6 +106,20 @@ public final class ArrowFlightConnectionConfigImplTest {
     assertThat(arrowFlightConnectionConfig.getHostnameOverride(), 
is("flight.example.com"));
   }
 
+  @ParameterizedTest
+  @ValueSource(strings = {"", " "})
+  public void testBlankTrustStoreIsNull(String value) {
+    properties.put(TRUST_STORE.camelName(), value);
+    assertThat(arrowFlightConnectionConfig.getTrustStorePath(), 
is(nullValue()));
+  }
+
+  @ParameterizedTest
+  @ValueSource(strings = {"", " "})
+  public void testBlankTlsRootCertsIsNull(String value) {
+    properties.put(TLS_ROOT_CERTS.camelName(), value);
+    assertThat(arrowFlightConnectionConfig.getTlsRootCertificatesPath(), 
is(nullValue()));
+  }
+
   public static Stream<Arguments> provideParameters() {
     int port = RANDOM.nextInt(Short.toUnsignedInt(Short.MAX_VALUE));
     boolean useEncryption = RANDOM.nextBoolean();
@@ -145,6 +161,18 @@ public final class ArrowFlightConnectionConfigImplTest {
             "flight.example.com",
             (Function<ArrowFlightConnectionConfigImpl, ?>)
                 ArrowFlightConnectionConfigImpl::getHostnameOverride),
+        Arguments.of(
+            TRUST_STORE,
+            "/path/to/truststore.jks",
+            "/path/to/truststore.jks",
+            (Function<ArrowFlightConnectionConfigImpl, ?>)
+                ArrowFlightConnectionConfigImpl::getTrustStorePath),
+        Arguments.of(
+            TLS_ROOT_CERTS,
+            "/path/to/ca.pem",
+            "/path/to/ca.pem",
+            (Function<ArrowFlightConnectionConfigImpl, ?>)
+                ArrowFlightConnectionConfigImpl::getTlsRootCertificatesPath),
         Arguments.of(
             THREAD_POOL_SIZE,
             threadPoolSize,

Reply via email to