From 138e2141c88b296944c5a84013fb78269d99aa68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?JB=20Onofr=C3=A9?= Date: Wed, 18 Nov 2026 10:57:08 +0100 Subject: [PATCH] GH-1500: [JDBC] Harden trustStore and tlsRootCerts handling for blank values and missing password Follow-up to #1496 (#1499). - Treat blank `trustStore` and `tlsRootCerts` values as unset (`null`) in `ArrowFlightConnectionConfigImpl` and `ArrowFlightSqlClientHandler.Builder` so empty query parameters or blank optional properties do not shadow fallback trust sources or fail trying to open an empty path. - Validate `trustStorePassword != null` in `ArrowFlightSqlClientHandler.Builder#build()` before loading `trustStore` so a missing password throws `SQLException` (wrapping `IllegalArgumentException`) and triggers allocator cleanup instead of leaking an uncaught `NullPointerException`. - Sync Javadoc on `withSystemTrustStore`, `withTrustStorePath`, and `withTlsRootCertificates` and clarify parameter docs in `docs/source/flight_sql_jdbc_driver.rst`. - Tighten `ConnectionTlsTest#testGetAuthenticatedEncryptedConnectionWithKeyStoreBadPassword` to assert the keystore password failure cause, and add tests for blank `trustStore`/`tlsRootCerts` and missing `trustStorePassword`. Closes #1500. --- docs/source/flight_sql_jdbc_driver.rst | 9 +- .../client/ArrowFlightSqlClientHandler.java | 32 +++-- .../ArrowFlightConnectionConfigImpl.java | 20 +++- .../arrow/driver/jdbc/ConnectionTlsTest.java | 112 ++++++++++++++++-- ...rrowFlightSqlClientHandlerBuilderTest.java | 33 ++++++ .../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 5e5a559811..fe731fcaaf 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 74da46380f..97108e599b 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 static final class Builder { @VisibleForTesting String password; - @VisibleForTesting String trustStorePath; + @VisibleForTesting @Nullable String trustStorePath; @VisibleForTesting String trustStorePassword; @@ -703,7 +703,7 @@ public static final class Builder { @VisibleForTesting boolean useSystemTrustStore = true; - @VisibleForTesting String tlsRootCertificatesPath; + @VisibleForTesting @Nullable String tlsRootCertificatesPath; @VisibleForTesting String clientCertificatePath; @@ -821,13 +821,16 @@ public Builder withPassword(final String password) { } /** - * 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 Builder withHostnameOverride(@Nullable final String hostnameOverride) { } /** - * 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 Builder withSystemTrustStore(final boolean useSystemTrustStore) { * 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 ArrowFlightSqlClientHandler build() throws SQLException { 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 32c18a5608..8460474ba8 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 String getToken() { /** * 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 boolean useSystemTrustStore() { 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 47e8404ef7..b6fd045d02 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.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 void testGetAuthenticatedEncryptedConnectionWithKeyStoreBadPassword() thr 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 void testGetEncryptedConnectionWithTrustStoreAndDefaultSystemTrustStore() } } + /** + * 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 d7fc765d64..58e139b58a 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 void testBlankHostnameOverrideIsUnset() { 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 216bfa3c06..923d98d10b 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.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 void testHostnameOverrideIsTrimmed() { 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 provideParameters() { int port = RANDOM.nextInt(Short.toUnsignedInt(Short.MAX_VALUE)); boolean useEncryption = RANDOM.nextBoolean(); @@ -145,6 +161,18 @@ public static Stream provideParameters() { "flight.example.com", (Function) ArrowFlightConnectionConfigImpl::getHostnameOverride), + Arguments.of( + TRUST_STORE, + "/path/to/truststore.jks", + "/path/to/truststore.jks", + (Function) + ArrowFlightConnectionConfigImpl::getTrustStorePath), + Arguments.of( + TLS_ROOT_CERTS, + "/path/to/ca.pem", + "/path/to/ca.pem", + (Function) + ArrowFlightConnectionConfigImpl::getTlsRootCertificatesPath), Arguments.of( THREAD_POOL_SIZE, threadPoolSize,