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
18 changes: 18 additions & 0 deletions docs/source/flight_sql_jdbc_driver.rst
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,24 @@ case-sensitive. The supported parameters are:
in subsequent internal connections used for retrieving streams
from separate endpoints.

* - followEndpointLocations
- true
- Whether to connect to the endpoint locations returned by the
server to retrieve query results. These connections are sent the
connection's credentials, client certificate and headers. When
false, an endpoint is only fetched over the existing connection:
if it has no locations, the ``arrow-flight-reuse-connection://?``
location, or a ``grpc``, ``grpc+tcp`` or ``grpc+tls`` location on
**HOSTNAME** and **PORT**. A ``grpc+tls`` location is only used
when TLS is enabled. Hosts are compared by name, without DNS
resolution. When TLS is enabled, a location on ``hostnameOverride``
is also used, on **PORT** unless the override includes a port.
Other locations are skipped, and the query fails with an
``SQLException`` if an endpoint has no location that can be used.
``retainCookies`` and ``retainAuth`` have no effect when this is
false. Set this to false when the server is not trusted to choose
which hosts the driver connects to and sends credentials to.

Note that URI values must be URI-encoded if they contain characters such
as !, @, $, etc.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ private static ArrowFlightSqlClientHandler createNewClientHandler(
.withCallOptions(config.toCallOption())
.withRetainCookies(config.retainCookies())
.withRetainAuth(config.retainAuth())
.withFollowEndpointLocations(config.followEndpointLocations())
.withCatalog(config.getCatalog())
.withClientCache(config.useClientCache() ? new FlightClientCache() : null)
.withConnectTimeout(config.getConnectTimeout())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ public final class ArrowFlightSqlClientHandler implements AutoCloseable {
private final FlightSqlClient sqlClient;
private final Set<CallOption> options = new HashSet<>();
private final Builder builder;
private final boolean followEndpointLocations;
private final Optional<String> catalog;
private final @Nullable FlightClientCache flightClientCache;

Expand All @@ -115,6 +116,7 @@ public final class ArrowFlightSqlClientHandler implements AutoCloseable {
this.cacheKey = Preconditions.checkNotNull(cacheKey);
this.sqlClient = Preconditions.checkNotNull(sqlClient);
this.builder = builder;
this.followEndpointLocations = builder.followEndpointLocations;
this.catalog = catalog;
this.flightClientCache = flightClientCache;
}
Expand Down Expand Up @@ -193,12 +195,26 @@ public List<CloseableEndpointStreamPair> getStreams(final FlightInfo flightInfo)
while (locations.hasNext()) {
Location location = locations.next();
final URI endpointUri = location.getUri();
if (endpointUri.getScheme().equals(LocationSchemes.REUSE_CONNECTION)) {
if (endpointUri.getScheme().equals(LocationSchemes.REUSE_CONNECTION)
|| (!followEndpointLocations && builder.isConnectionLocation(endpointUri))) {
stream =
new CloseableEndpointStreamPair(
sqlClient.getStream(endpoint.getTicket(), getOptions()), null);
break;
}
if (!followEndpointLocations) {
exceptions.add(
new SQLException(
String.format(
"Refusing endpoint location (scheme=%s, host=%s, port=%d) because "
+ "followEndpointLocations is false and it cannot be fetched over "
+ "the connection to %s.",
endpointUri.getScheme(),
endpointUri.getHost(),
endpointUri.getPort(),
builder.getLocation().getUri())));
continue;
}
final boolean encryptEndpoint =
endpointUri.getScheme().equals(LocationSchemes.GRPC_TLS);
if (builder.useEncryption && !encryptEndpoint) {
Expand Down Expand Up @@ -752,6 +768,8 @@ public static final class Builder {

@VisibleForTesting boolean retainAuth = true;

@VisibleForTesting boolean followEndpointLocations = true;

@VisibleForTesting Optional<String> catalog = Optional.empty();

// Copies (getStreams endpoint handlers) authenticate as the connection; on servers that key
Expand Down Expand Up @@ -807,6 +825,7 @@ public Builder() {}
this.catalog = original.catalog;
this.oauthConfig = original.oauthConfig;
this.ownsSession = false;
this.followEndpointLocations = original.followEndpointLocations;

if (original.retainCookies) {
this.cookieFactory = original.cookieFactory;
Expand Down Expand Up @@ -939,17 +958,50 @@ public Builder withHostnameOverride(@Nullable final String hostnameOverride) {
*/
@VisibleForTesting
@Nullable String getHostnameOverrideForEndpoint(final URI endpointUri) {
final String endpointHost = stripBrackets(endpointUri.getHost());
if (!useEncryption
|| hostnameOverride == null
|| endpointHost == null
|| endpointUri.getPort() != port
|| !endpointHost.equalsIgnoreCase(stripBrackets(host))) {
if (!useEncryption || hostnameOverride == null || !isOnHostAndPort(endpointUri, host, port)) {
return null;
}
return hostnameOverride;
}

/**
* Checks whether an endpoint location is a gRPC location that names the host and port this
* handler connects to or, when encryption is enabled, the hostname override. The override is
* matched with this handler's port, unless it includes a port of its own. A {@code grpc+tls}
* location only matches when encryption is enabled.
*
* @param endpointUri the endpoint location.
* @return whether the endpoint can be fetched over this handler's connection.
*/
@VisibleForTesting
boolean isConnectionLocation(final URI endpointUri) {
// Other schemes need another transport, such as an HTTP GET for https, and a location that
// asks for TLS must not be fetched over a plaintext connection.
final String scheme = endpointUri.getScheme();
if (!LocationSchemes.GRPC.equals(scheme)
&& !LocationSchemes.GRPC_INSECURE.equals(scheme)
&& !(useEncryption && LocationSchemes.GRPC_TLS.equals(scheme))) {
return false;
}
if (isOnHostAndPort(endpointUri, host, port)) {
return true;
}
if (!useEncryption || hostnameOverride == null) {
return false;
}
return isOnHostAndPort(endpointUri, hostnameOverride, port)
|| (endpointUri.getPort() != -1
&& hostnameOverride.equalsIgnoreCase(endpointUri.getRawAuthority()));
}

private static boolean isOnHostAndPort(
final URI endpointUri, @Nullable final String host, final int port) {
final String endpointHost = stripBrackets(endpointUri.getHost());
return endpointHost != null
&& endpointUri.getPort() == port
&& endpointHost.equalsIgnoreCase(stripBrackets(host));
}

/**
* Strips the brackets from an IPv6 literal. {@link URI#getHost()} keeps them, but a host set
* through properties may not have them.
Expand Down Expand Up @@ -1056,6 +1108,22 @@ public Builder withRetainAuth(boolean retainAuth) {
return this;
}

/**
* Sets whether getStreams() may connect to the endpoint locations returned by the server. These
* connections are sent this handler's credentials, client certificate and headers. When false,
* an endpoint is only fetched over this handler's connection: if it has no locations, the
* {@link LocationSchemes#REUSE_CONNECTION} location, or a gRPC location on this handler's host
* and port or, when encryption is enabled, on the hostname override. A {@code grpc+tls}
* location is only used when encryption is enabled. Other locations are refused.
*
* @param followEndpointLocations whether to connect to endpoint locations.
* @return this builder instance.
*/
public Builder withFollowEndpointLocations(boolean followEndpointLocations) {
this.followEndpointLocations = followEndpointLocations;
return this;
}

/**
* Adds the provided {@code factories} to the list of {@link #middlewareFactories} of this
* handler.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,14 @@ public boolean retainAuth() {
return ArrowFlightConnectionProperty.RETAIN_AUTH.getBoolean(properties);
}

/**
* Indicates if the driver may connect to the endpoint locations returned by the server to fetch
* results. When false, results are only fetched over the existing connection.
*/
public boolean followEndpointLocations() {
return ArrowFlightConnectionProperty.FOLLOW_ENDPOINT_LOCATIONS.getBoolean(properties);
}

/**
* The catalog to which a connection is made.
*
Expand Down Expand Up @@ -289,6 +297,7 @@ public enum ArrowFlightConnectionProperty implements ConnectionProperty {
TOKEN("token", null, Type.STRING, false),
RETAIN_COOKIES("retainCookies", true, Type.BOOLEAN, false),
RETAIN_AUTH("retainAuth", true, Type.BOOLEAN, false),
FOLLOW_ENDPOINT_LOCATIONS("followEndpointLocations", true, Type.BOOLEAN, false),
CATALOG("catalog", null, Type.STRING, false),
CONNECT_TIMEOUT_MILLIS("connectTimeoutMs", 10000, Type.NUMBER, false),
USE_CLIENT_CACHE("useClientCache", true, Type.BOOLEAN, false),
Expand Down
Loading
Loading