Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions docs/source/flight_sql_jdbc_driver.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,7 @@ public static final class Builder {

@VisibleForTesting String password;

@VisibleForTesting String trustStorePath;
@VisibleForTesting @Nullable String trustStorePath;

@VisibleForTesting String trustStorePassword;

Expand All @@ -703,7 +703,7 @@ public static final class Builder {

@VisibleForTesting boolean useSystemTrustStore = true;

@VisibleForTesting String tlsRootCertificatesPath;
@VisibleForTesting @Nullable String tlsRootCertificatesPath;

@VisibleForTesting String clientCertificatePath;

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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.
Expand All @@ -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;
}

Expand Down Expand Up @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand All @@ -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;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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());
}

/**
Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Arguments> provideParameters() {
int port = RANDOM.nextInt(Short.toUnsignedInt(Short.MAX_VALUE));
boolean useEncryption = RANDOM.nextBoolean();
Expand Down Expand Up @@ -145,6 +161,18 @@ public static Stream<Arguments> provideParameters() {
"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,
Expand Down
Loading