Skip to content
Open
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
54 changes: 54 additions & 0 deletions docs/source/flight_sql_jdbc_driver.rst
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,60 @@ session setup. On expiration, the driver cancels pending login RPCs and throws
``connectTimeoutMs`` property separately controls the Netty socket connection
timeout.

Connecting Through a Proxy
==========================

By default, the driver uses the JVM proxy settings, such as the
``https.proxyHost`` and ``https.proxyPort`` system properties. A proxy
can instead be configured per connection with the following properties.
They are only used when ``useProxy`` is enabled, in which case they
take precedence over the JVM proxy settings.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OAuth token requests don't follow this yet. They still use the JVM proxy settings (see my comment on ArrowFlightConnection). The same applies to "To ignore the JVM proxy settings and connect directly" below. If token requests stay out of scope for this PR, please mention that limitation here.


.. list-table::
:header-rows: 1

* - Parameter
- Default
- Description

* - useProxy
- false
- Whether to use the proxy configured by the properties below instead
of the JVM proxy settings

* - proxyHost
- null
- The host of the HTTP proxy. Required unless ``nonProxyHosts`` is ``*``

* - proxyPort
- null
- The port of the HTTP proxy. Required when ``proxyHost`` is set

* - proxyUser
- null
- The username for Basic authentication with the proxy

* - proxyPassword
- null
- The password for Basic authentication with the proxy

* - nonProxyHosts
- null
- Hosts that are connected to directly, bypassing the proxy, separated
by ``|``. ``*`` can be used as a wildcard, as in ``*.example.com``,
and ``*`` alone bypasses the proxy for every host

In a connection URI, the ``|`` separator in ``nonProxyHosts`` must be
URI-encoded as ``%7C``. For example, the following URI ::

jdbc:arrow-flight-sql://flight.example.com:443/?useProxy=true&proxyHost=proxy.example.com&proxyPort=3128&nonProxyHosts=localhost%7C*.internal

connects through the proxy at ``proxy.example.com:3128``, except to
``localhost`` and the hosts in the ``internal`` domain.

To ignore the JVM proxy settings and connect directly, enable
``useProxy`` and set ``nonProxyHosts`` to ``*``.

OAuth 2.0 Authentication
========================

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ private static ArrowFlightSqlClientHandler createNewClientHandler(
.withConnectTimeout(config.getConnectTimeout())
.withDriverVersion(driverVersion)
.withOAuthConfiguration(config.getOauthConfiguration())
.withProxyDetector(config.getProxyDetector())

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The proxy detector is only installed on the gRPC channel. OAuth token requests don't go through it: AbstractOAuthTokenProvider.fetchNewToken sends them with request.toHTTPRequest().send(), which uses HttpURLConnection and therefore the JVM-wide ProxySelector.

Take useProxy=true&proxyHost=proxy.example.com&proxyPort=3128&oauth.flow=client_credentials&oauth.tokenUri=https://idp.example.com/token on a network where only the proxy can reach the Internet. The Flight connection goes through the proxy, but the token request tries to connect directly and fails with "Failed to send OAuth token request". The reverse also happens: with nonProxyHosts=*, token requests still go through a JVM-configured proxy.

Could the token request use the same settings? Nimbus HTTPRequest has setProxy(java.net.Proxy), so the token provider could build a Proxy from proxyHost/proxyPort, skipping it when the token endpoint matches nonProxyHosts. Proxy authentication is the tricky part: HttpURLConnection gets proxy credentials from an Authenticator, and the JDK disables Basic auth for HTTPS tunneling by default (jdk.http.auth.tunneling.disabledSchemes).

If you would rather leave this for a follow-up, please say in the docs that OAuth token requests still use the JVM proxy settings, and open an issue for it.

@scruz-denodo scruz-denodo Dec 2, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will take a look. It makes sense to add it also with this change.

In an scenario where the proxy is required and you configure it with the connection properties, it does not sound very good that the OAUTH request fails because the proxy is ignored for them.

.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.google.common.collect.ImmutableMap;
import io.grpc.ManagedChannel;
import io.grpc.ProxyDetector;
import io.grpc.netty.NettyChannelBuilder;
import io.netty.channel.ChannelOption;
import java.io.IOException;
Expand Down Expand Up @@ -783,6 +784,8 @@ public static final class Builder {

@VisibleForTesting @Nullable OAuthConfiguration oauthConfig;

@VisibleForTesting @Nullable ProxyDetector proxyDetector;

// These two middleware are for internal use within build() and should not be
// exposed by builder
// APIs.
Expand Down Expand Up @@ -826,6 +829,7 @@ public Builder() {}
this.oauthConfig = original.oauthConfig;
this.ownsSession = false;
this.followEndpointLocations = original.followEndpointLocations;
this.proxyDetector = original.proxyDetector;

if (original.retainCookies) {
this.cookieFactory = original.cookieFactory;
Expand Down Expand Up @@ -1212,6 +1216,18 @@ public Builder withOAuthConfiguration(final OAuthConfiguration oauthConfig) {
return this;
}

/**
* Sets the {@link ProxyDetector} for this handler, which overrides the JVM-wide proxy settings
* used by default.
*
* @param proxyDetector the proxy detector, or null to use the default one
* @return this builder instance
*/
public Builder withProxyDetector(ProxyDetector proxyDetector) {
this.proxyDetector = proxyDetector;
return this;
}

public String getCacheKey() {
return getLocation().toString();
}
Expand Down Expand Up @@ -1300,6 +1316,10 @@ public ArrowFlightSqlClientHandler build() throws SQLException {

channelBuilder.userAgent(userAgent);

if (proxyDetector != null) {
channelBuilder.proxyDetector(proxyDetector);
}

if (connectTimeout != null) {
channelBuilder.withOption(
ChannelOption.CONNECT_TIMEOUT_MILLIS, (int) connectTimeout.toMillis());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
/*
* 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.arrow.driver.jdbc.client.utils;

import io.grpc.HttpConnectProxiedSocketAddress;
import io.grpc.ProxiedSocketAddress;
import io.grpc.ProxyDetector;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.regex.Pattern;

/**
* A {@link ProxyDetector} configured from the JDBC connection properties, which takes precedence
* over the JVM-wide proxy settings used by default by gRPC.
*
* <p>Connections are tunneled through an HTTP CONNECT proxy, unless the target host matches one of
* the non-proxy host patterns. Without a proxy host, every connection is made directly.
*/
public final class FlightProxyDetector implements ProxyDetector {
private static final String MATCH_ALL_REGEX = ".*";

private final String proxyHost;
private final int proxyPort;
private final String proxyUser;
private final String proxyPassword;
private final List<Pattern> nonProxyHosts;

private FlightProxyDetector(
String proxyHost,
int proxyPort,
String proxyUser,
String proxyPassword,
List<Pattern> nonProxyHosts) {
this.proxyHost = proxyHost;
this.proxyPort = proxyPort;
this.proxyUser = proxyUser;
this.proxyPassword = proxyPassword;
this.nonProxyHosts = nonProxyHosts;
}

/**
* Creates a validated {@link FlightProxyDetector}.
*
* @param proxyHost the proxy host, or null to connect directly to every host.
* @param proxyPort the proxy port, required when {@code proxyHost} is set.
* @param proxyUser the username for proxy authentication.
* @param proxyPassword the password for proxy authentication.
* @param nonProxyHosts {@code |}-separated host patterns, with {@code *} wildcards, that bypass
* the proxy.
* @return the proxy detector.
* @throws SQLException if the configuration is invalid.
*/
public static FlightProxyDetector create(
String proxyHost,
Integer proxyPort,
String proxyUser,
String proxyPassword,
String nonProxyHosts)
throws SQLException {
final List<Pattern> nonProxyHostPatterns = parseNonProxyHosts(nonProxyHosts);
if (isNullOrEmpty(proxyHost)) {
if (!matchesAllHosts(nonProxyHostPatterns)) {
throw new SQLException(
"proxyHost is required when useProxy is enabled, unless nonProxyHosts is <*>.");
}
return new FlightProxyDetector(null, 0, null, null, nonProxyHostPatterns);
}
if (proxyPort == null) {
throw new SQLException("proxyPort is required when proxyHost is set.");
}
if (proxyPort < 1 || proxyPort > 65535) {
throw new SQLException(String.format("Invalid proxyPort <%d>.", proxyPort));
}
if (isNullOrEmpty(proxyUser) != isNullOrEmpty(proxyPassword)) {
throw new SQLException("proxyUser and proxyPassword must be set together.");
}
return new FlightProxyDetector(
proxyHost, proxyPort, proxyUser, proxyPassword, nonProxyHostPatterns);
}

@Override
public ProxiedSocketAddress proxyFor(SocketAddress targetServerAddress) throws IOException {
if (proxyHost == null || !(targetServerAddress instanceof InetSocketAddress)) {
return null;
}
final InetSocketAddress targetAddress = (InetSocketAddress) targetServerAddress;
if (isNonProxyHost(targetAddress.getHostString())) {
return null;
}
final InetSocketAddress proxyAddress = new InetSocketAddress(proxyHost, proxyPort);
if (proxyAddress.isUnresolved()) {
throw new IOException(String.format("Unable to resolve proxy host <%s>.", proxyHost));
}
final HttpConnectProxiedSocketAddress.Builder builder =
HttpConnectProxiedSocketAddress.newBuilder()
.setTargetAddress(targetAddress)
.setProxyAddress(proxyAddress);
if (proxyUser != null) {
builder.setUsername(proxyUser).setPassword(proxyPassword);
}
return builder.build();
}

private boolean isNonProxyHost(String host) {
final String normalizedHost = host.toLowerCase(Locale.ROOT);
return nonProxyHosts.stream().anyMatch(pattern -> pattern.matcher(normalizedHost).matches());
}

private static List<Pattern> parseNonProxyHosts(String nonProxyHosts) {
final List<Pattern> patterns = new ArrayList<>();
if (nonProxyHosts == null) {
return patterns;
}
for (String hostPattern : nonProxyHosts.split("\\|", -1)) {
String trimmed = hostPattern.trim().toLowerCase(Locale.ROOT);
if (trimmed.isEmpty()) {
continue;
}
// A leading dot matches any subdomain, as in ".example.com".
if (trimmed.startsWith(".")) {
trimmed = "*" + trimmed;
}
final String[] literals = trimmed.split("\\*", -1);
final StringBuilder regex = new StringBuilder();
for (int i = 0; i < literals.length; i++) {
if (i > 0) {
regex.append(".*");
}
if (!literals[i].isEmpty()) {
regex.append(Pattern.quote(literals[i]));
}
}
patterns.add(Pattern.compile(regex.toString()));
}
return patterns;
}

private static boolean matchesAllHosts(List<Pattern> patterns) {
return patterns.stream().anyMatch(pattern -> pattern.pattern().equals(MATCH_ALL_REGEX));
}

private static boolean isNullOrEmpty(String value) {
return value == null || value.isEmpty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.Properties;
import org.apache.arrow.driver.jdbc.ArrowFlightConnection;
import org.apache.arrow.driver.jdbc.client.oauth.OAuthConfiguration;
import org.apache.arrow.driver.jdbc.client.utils.FlightProxyDetector;
import org.apache.arrow.flight.CallHeaders;
import org.apache.arrow.flight.CallOption;
import org.apache.arrow.flight.FlightCallHeaders;
Expand All @@ -37,6 +38,16 @@

/** A {@link ConnectionConfig} for the {@link ArrowFlightConnection}. */
public final class ArrowFlightConnectionConfigImpl extends ConnectionConfigImpl {

// The proxy properties without a default value, which are only used when useProxy is enabled.
private static final ArrowFlightConnectionProperty[] PROXY_PROPERTIES = {
ArrowFlightConnectionProperty.PROXY_HOST,
ArrowFlightConnectionProperty.PROXY_PORT,
ArrowFlightConnectionProperty.PROXY_USER,
ArrowFlightConnectionProperty.PROXY_PASSWORD,
ArrowFlightConnectionProperty.NON_PROXY_HOSTS,
};

public ArrowFlightConnectionConfigImpl(final Properties properties) {
super(properties);
}
Expand Down Expand Up @@ -214,6 +225,36 @@ public boolean useClientCache() {
return ArrowFlightConnectionProperty.USE_CLIENT_CACHE.getBoolean(properties);
}

/**
* Returns the proxy detector configured by the proxy properties if useProxy is enabled, null
* otherwise.
*
* @return the proxy detector or null
* @throws SQLException if the proxy configuration is invalid
*/
public @Nullable FlightProxyDetector getProxyDetector() throws SQLException {
if (!ArrowFlightConnectionProperty.USE_PROXY.getBoolean(properties)) {
return null;
}

final Integer proxyPort;
try {
proxyPort = ArrowFlightConnectionProperty.PROXY_PORT.getInteger(properties);
} catch (NumberFormatException e) {
throw new SQLException(
String.format(
"Invalid proxyPort <%s>.",
ArrowFlightConnectionProperty.PROXY_PORT.getString(properties)),
e);
}
return FlightProxyDetector.create(
ArrowFlightConnectionProperty.PROXY_HOST.getString(properties),
proxyPort,
ArrowFlightConnectionProperty.PROXY_USER.getString(properties),
ArrowFlightConnectionProperty.PROXY_PASSWORD.getString(properties),
ArrowFlightConnectionProperty.NON_PROXY_HOSTS.getString(properties));
}

/**
* Gets the {@link CallOption}s from this {@link ConnectionConfig}.
*
Expand Down Expand Up @@ -302,6 +343,14 @@ public enum ArrowFlightConnectionProperty implements ConnectionProperty {
CONNECT_TIMEOUT_MILLIS("connectTimeoutMs", 10000, Type.NUMBER, false),
USE_CLIENT_CACHE("useClientCache", true, Type.BOOLEAN, false),

// Proxy configuration properties
USE_PROXY("useProxy", false, Type.BOOLEAN, false),
PROXY_HOST("proxyHost", null, Type.STRING, false),
PROXY_PORT("proxyPort", null, Type.NUMBER, false),
PROXY_USER("proxyUser", null, Type.STRING, false),
PROXY_PASSWORD("proxyPassword", null, Type.STRING, false),
NON_PROXY_HOSTS("nonProxyHosts", null, Type.STRING, false),

// OAuth configuration properties
OAUTH_FLOW("oauth.flow", null, Type.STRING, false),
OAUTH_CLIENT_ID("oauth.clientId", null, Type.STRING, false),
Expand Down
Loading
Loading