Skip to content

Commit 4fcf67e

Browse files
authored
GH-1499: [JDBC] Prefer an explicit trustStore over the system trust store (#1496)
## What's Changed `ArrowFlightSqlClientHandler.Builder.build()` picks the TLS trust source in the order `tlsRootCerts`, `useSystemTrustStore`, `trustStore`. `useSystemTrustStore` defaults to true, so a connection configured with `trustStore` and `trustStorePassword` but without `useSystemTrustStore=false` never reads that trust store. The server is validated against the system store instead, which is opened with the trust store's password. When that password matches the JDK `cacerts` (`changeit`), or on Windows and macOS where the system keychains need none, any certificate chaining to a system CA is accepted even though the user limited trust to their own CA, and the handshake then sends the credentials to that server. Otherwise the connection fails with "Keystore was tampered with, or password was incorrect", which is why every existing test sets `useSystemTrustStore=false` next to `trustStore`. An explicit `trustStore` now takes precedence over the system store, the same way `tlsRootCerts` already does, and the docs say `useSystemTrustStore` is ignored when either is set. Configurations that already set `useSystemTrustStore=false` are unaffected. The new test in `ConnectionTlsTest` connects with `trustStore` and the default `useSystemTrustStore` and fails without this change. `testGetAuthenticatedEncryptedConnectionWithKeyStoreBadPassword` still passes, now failing on the configured trust store rather than on `cacerts`. Closes #1499.
1 parent db524b6 commit 4fcf67e

3 files changed

Lines changed: 34 additions & 5 deletions

File tree

‎docs/source/flight_sql_jdbc_driver.rst‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,8 @@ case-sensitive. The supported parameters are:
155155

156156
* - useSystemTrustStore
157157
- true
158-
- When TLS is enabled, whether to use the system certificate store
158+
- When TLS is enabled, whether to use the system certificate store.
159+
Ignored when ``trustStore`` or ``tlsRootCerts`` is set.
159160

160161
* - retainCookies
161162
- true

‎flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/client/ArrowFlightSqlClientHandler.java‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1154,14 +1154,14 @@ public ArrowFlightSqlClientHandler build() throws SQLException {
11541154
if (tlsRootCertificatesPath != null) {
11551155
clientBuilder.trustedCertificates(
11561156
ClientAuthenticationUtils.getTlsRootCertificatesStream(tlsRootCertificatesPath));
1157-
} else if (useSystemTrustStore) {
1158-
clientBuilder.trustedCertificates(
1159-
ClientAuthenticationUtils.getCertificateInputStreamFromSystem(
1160-
trustStorePassword));
11611157
} else if (trustStorePath != null) {
11621158
clientBuilder.trustedCertificates(
11631159
ClientAuthenticationUtils.getCertificateStream(
11641160
trustStorePath, trustStorePassword));
1161+
} else if (useSystemTrustStore) {
1162+
clientBuilder.trustedCertificates(
1163+
ClientAuthenticationUtils.getCertificateInputStreamFromSystem(
1164+
trustStorePassword));
11651165
}
11661166
}
11671167

‎flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ConnectionTlsTest.java‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,34 @@ public void testGetNonAuthenticatedEncryptedConnection() throws Exception {
294294
}
295295
}
296296

297+
/**
298+
* Check that a configured trust store is used even when useSystemTrustStore is left at its
299+
* default value, rather than being silently replaced by the system trust store.
300+
*
301+
* @throws Exception on error.
302+
*/
303+
@Test
304+
public void testGetEncryptedConnectionWithTrustStoreAndDefaultSystemTrustStore()
305+
throws Exception {
306+
final Properties properties = new Properties();
307+
308+
properties.put(
309+
ArrowFlightConnectionProperty.HOST.camelName(), FLIGHT_SERVER_TEST_EXTENSION.getHost());
310+
properties.put(
311+
ArrowFlightConnectionProperty.PORT.camelName(), FLIGHT_SERVER_TEST_EXTENSION.getPort());
312+
properties.put(ArrowFlightConnectionProperty.USER.camelName(), userTest);
313+
properties.put(ArrowFlightConnectionProperty.PASSWORD.camelName(), passTest);
314+
properties.put(ArrowFlightConnectionProperty.USE_ENCRYPTION.camelName(), true);
315+
properties.put(ArrowFlightConnectionProperty.TRUST_STORE.camelName(), trustStorePath);
316+
properties.put(ArrowFlightConnectionProperty.TRUST_STORE_PASSWORD.camelName(), trustStorePass);
317+
318+
final ArrowFlightJdbcDataSource dataSource =
319+
ArrowFlightJdbcDataSource.createNewDataSource(properties);
320+
try (final Connection connection = dataSource.getConnection()) {
321+
assertTrue(connection.isValid(300));
322+
}
323+
}
324+
297325
/**
298326
* Check if an encrypted connection can be established successfully when connecting through the
299327
* DriverManager using just a connection url.

0 commit comments

Comments
 (0)