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
57 changes: 57 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,63 @@ 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. They also apply to the
requests sent to the OAuth token endpoint, which are sent through the
proxy, authenticating with ``proxyUser`` and ``proxyPassword`` if set,
unless the token endpoint host matches ``nonProxyHosts``.

.. 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.

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 have being doing test with a local squid instance and I decided to push two commits on the branch.

  • first one aa82b27 adds the proxy using the setProxy method you suggested. This change has a limitation with the authenticated proxies. The setProxy does not have support for setting the proxyUser/proxyPassword values.

  • second one 08d0c7c adds the support for specifying the proxy credentials. It uses this class for making the proxy connection ProxyHttpRequestSender. There are more changes than the commit using the setProxy from nimbus, so I split the change on two commits in case you prefer to stay only with the first one assuming the mentioned limitation.

.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 All @@ -1309,7 +1329,7 @@ public ArrowFlightSqlClientHandler build() throws SQLException {
final ArrayList<CallOption> credentialOptions = new ArrayList<>();
// Authentication priority: OAuth > token > username/password
if (oauthConfig != null) {
OAuthTokenProvider tokenProvider = oauthConfig.createTokenProvider();
OAuthTokenProvider tokenProvider = oauthConfig.createTokenProvider(proxyDetector);
credentialOptions.add(new CredentialCallOption(new OAuthCredentialWriter(tokenProvider)));
} else if (isUsingUserPasswordAuth) {
// If the authFactory has already been used for a handshake, use the existing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,17 @@
import com.nimbusds.oauth2.sdk.TokenRequest;
import com.nimbusds.oauth2.sdk.TokenResponse;
import com.nimbusds.oauth2.sdk.auth.ClientAuthentication;
import com.nimbusds.oauth2.sdk.http.HTTPRequest;
import com.nimbusds.oauth2.sdk.http.HTTPResponse;
import com.nimbusds.oauth2.sdk.token.AccessToken;
import io.grpc.HttpConnectProxiedSocketAddress;
import io.grpc.ProxiedSocketAddress;
import io.grpc.ProxyDetector;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URI;
import java.net.URL;
import java.sql.SQLException;
import java.time.Instant;
import org.apache.arrow.util.VisibleForTesting;
Expand All @@ -47,6 +55,12 @@ public abstract class AbstractOAuthTokenProvider implements OAuthTokenProvider {

@VisibleForTesting @Nullable Scope scope;

/**
* Decides whether the token requests go through a proxy. When null, the JVM-wide proxy settings
* apply.
*/
@VisibleForTesting @Nullable ProxyDetector proxyDetector;

@Override
public String getValidToken() throws SQLException {
TokenInfo token = cachedToken;
Expand Down Expand Up @@ -74,7 +88,7 @@ public String getValidToken() throws SQLException {
protected TokenInfo fetchNewToken() throws SQLException {
try {
TokenRequest request = buildTokenRequest();
TokenResponse response = TokenResponse.parse(request.toHTTPRequest().send());
TokenResponse response = TokenResponse.parse(send(request.toHTTPRequest()));

if (!response.indicatesSuccess()) {
TokenErrorResponse errorResponse = response.toErrorResponse();
Expand All @@ -99,6 +113,36 @@ protected TokenInfo fetchNewToken() throws SQLException {
}
}

/**
* Sends the token request, honoring the configured {@link ProxyDetector} if any.
*
* @param httpRequest the HTTP request to send to the authorization server
* @return the HTTP response
* @throws IOException if the request cannot be sent
*/
private HTTPResponse send(HTTPRequest httpRequest) throws IOException {
if (proxyDetector == null) {
return httpRequest.send();
}

final URL url = httpRequest.getURL();
final int port = url.getPort() != -1 ? url.getPort() : url.getDefaultPort();
final ProxiedSocketAddress proxied =
proxyDetector.proxyFor(InetSocketAddress.createUnresolved(url.getHost(), port));
if (!(proxied instanceof HttpConnectProxiedSocketAddress)) {
// The target is not proxied: connect directly, ignoring the JVM-wide proxy settings.
httpRequest.setProxy(Proxy.NO_PROXY);
return httpRequest.send();
}

final HttpConnectProxiedSocketAddress httpProxy = (HttpConnectProxiedSocketAddress) proxied;
return httpRequest.send(
new ProxyHttpRequestSender(
(InetSocketAddress) httpProxy.getProxyAddress(),
httpProxy.getUsername(),
httpProxy.getPassword()));
}

/**
* Builds the flow-specific token request.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.apache.arrow.driver.jdbc.client.oauth;

import com.nimbusds.oauth2.sdk.GrantType;
import io.grpc.ProxyDetector;
import java.net.URI;
import java.net.URISyntaxException;
import java.sql.SQLException;
Expand Down Expand Up @@ -87,12 +88,27 @@ private void validate() throws SQLException {
* @throws SQLException if the grant type is not supported or configuration is invalid
*/
public OAuthTokenProvider createTokenProvider() throws SQLException {
return createTokenProvider(null);
}

/**
* Creates an OAuthTokenProvider based on the configured grant type, sending the token requests
* according to the given proxy detector.
*
* @param proxyDetector the proxy detector deciding how the token requests reach the token
* endpoint, or null to use the JVM-wide proxy settings
* @return the token provider
* @throws SQLException if the grant type is not supported or configuration is invalid
*/
public OAuthTokenProvider createTokenProvider(@Nullable ProxyDetector proxyDetector)
throws SQLException {
if (GrantType.CLIENT_CREDENTIALS.equals(grantType)) {
return OAuthTokenProviders.clientCredentials()
.tokenUri(tokenUri)
.clientId(clientId)
.clientSecret(clientSecret)
.scope(scope)
.proxyDetector(proxyDetector)
.build();
} else if (GrantType.TOKEN_EXCHANGE.equals(grantType)) {
OAuthTokenProviders.TokenExchangeBuilder builder =
Expand All @@ -105,7 +121,8 @@ public OAuthTokenProvider createTokenProvider() throws SQLException {
.audience(audience)
.requestedTokenType(requestedTokenType)
.scope(scope)
.resource(resource);
.resource(resource)
.proxyDetector(proxyDetector);

if (clientId != null && clientSecret != null) {
builder.clientCredentials(clientId, clientSecret);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.nimbusds.oauth2.sdk.token.TokenTypeURI;
import com.nimbusds.oauth2.sdk.token.TypelessAccessToken;
import com.nimbusds.oauth2.sdk.tokenexchange.TokenExchangeGrant;
import io.grpc.ProxyDetector;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Collections;
Expand Down Expand Up @@ -91,6 +92,7 @@ public static class ClientCredentialsBuilder {
private @Nullable String clientId;
private @Nullable String clientSecret;
private @Nullable String scope;
private @Nullable ProxyDetector proxyDetector;

ClientCredentialsBuilder() {}

Expand Down Expand Up @@ -155,6 +157,18 @@ public ClientCredentialsBuilder scope(@Nullable String scope) {
return this;
}

/**
* Sets the {@link ProxyDetector} used to decide whether the token requests are sent through a
* proxy (optional). When not set, the JVM-wide proxy settings are used.
*
* @param proxyDetector the proxy detector, or null to use the JVM-wide proxy settings
* @return this builder
*/
public ClientCredentialsBuilder proxyDetector(@Nullable ProxyDetector proxyDetector) {
this.proxyDetector = proxyDetector;
return this;
}

/**
* Builds a new ClientCredentialsTokenProvider instance.
*
Expand All @@ -171,7 +185,10 @@ public ClientCredentialsTokenProvider build() {
if (clientSecret == null) {
throw new IllegalStateException("clientSecret is required");
}
return new ClientCredentialsTokenProvider(tokenUri, clientId, clientSecret, scope);
final ClientCredentialsTokenProvider provider =
new ClientCredentialsTokenProvider(tokenUri, clientId, clientSecret, scope);
provider.proxyDetector = proxyDetector;
return provider;
}
}

Expand All @@ -187,6 +204,7 @@ public static class TokenExchangeBuilder {
private @Nullable Scope scope;
private @Nullable List<URI> resources;
private @Nullable ClientAuthentication clientAuth;
private @Nullable ProxyDetector proxyDetector;

TokenExchangeBuilder() {}

Expand Down Expand Up @@ -369,6 +387,18 @@ public TokenExchangeBuilder clientCredentials(String clientId, String clientSecr
return this;
}

/**
* Sets the {@link ProxyDetector} used to decide whether the token requests are sent through a
* proxy (optional). When not set, the JVM-wide proxy settings are used.
*
* @param proxyDetector the proxy detector, or null to use the JVM-wide proxy settings
* @return this builder
*/
public TokenExchangeBuilder proxyDetector(@Nullable ProxyDetector proxyDetector) {
this.proxyDetector = proxyDetector;
return this;
}

/**
* Builds a new TokenExchangeTokenProvider instance.
*
Expand All @@ -387,7 +417,10 @@ public TokenExchangeTokenProvider build() {
}

TokenExchangeGrant grant = createGrant();
return new TokenExchangeTokenProvider(tokenUri, grant, clientAuth, scope, resources);
final TokenExchangeTokenProvider provider =
new TokenExchangeTokenProvider(tokenUri, grant, clientAuth, scope, resources);
provider.proxyDetector = proxyDetector;
return provider;
}

private TokenExchangeGrant createGrant() {
Expand Down
Loading