Repository navigation
GH-1384: [JDBC] Add proxy connection properties to the Flight SQL JDBC driver #1538
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -148,6 +148,7 @@ private static ArrowFlightSqlClientHandler createNewClientHandler( | |
| .withConnectTimeout(config.getConnectTimeout()) | ||
| .withDriverVersion(driverVersion) | ||
| .withOAuthConfiguration(config.getOauthConfiguration()) | ||
| .withProxyDetector(config.getProxyDetector()) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: Take Could the token request use the same settings? Nimbus 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.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
| } | ||
|
|
||
|
|
||
| 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(); | ||
| } | ||
| } |
There was a problem hiding this comment.
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.