Skip to content
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ public final class GrpcStorageOptions extends StorageOptions
private static final String GCS_SCOPE = "https://www.googleapis.com/auth/devstorage.full_control";
private static final Set<String> SCOPES = ImmutableSet.of(GCS_SCOPE);
private static final String DEFAULT_HOST = "https://storage.googleapis.com";
private static final String DEFAULT_HOST_DIRECT_PATH = "https://storage-direct.googleapis.com";
private static final String DEFAULT_HOST_NO_SCHEME = "storage.googleapis.com";
private static final String DEFAULT_HOST_DIRECT_PATH_NO_SCHEME = "storage-direct.googleapis.com";
// If true, disable the bound-token-by-default feature for DirectPath.
private static final boolean DIRECT_PATH_BOUND_TOKEN_DISABLED =
Boolean.parseBoolean(
Expand All @@ -142,6 +145,7 @@ public final class GrpcStorageOptions extends StorageOptions
private final GrpcRetryAlgorithmManager retryAlgorithmManager;
private final java.time.Duration terminationAwaitDuration;
private final boolean attemptDirectPath;
private final boolean attemptDirectPathXdsOverInterconnect;
private final boolean enableGrpcClientMetrics;

private final boolean grpcClientMetricsManuallyEnabled;
Expand All @@ -160,6 +164,7 @@ private GrpcStorageOptions(Builder builder, GrpcStorageDefaults serviceDefaults)
builder.terminationAwaitDuration,
serviceDefaults.getTerminationAwaitDurationJavaTime());
this.attemptDirectPath = builder.attemptDirectPath;
this.attemptDirectPathXdsOverInterconnect = builder.attemptDirectPathXdsOverInterconnect;
this.enableGrpcClientMetrics = builder.enableGrpcClientMetrics;
this.grpcClientMetricsManuallyEnabled = builder.grpcMetricsManuallyEnabled;
this.grpcInterceptorProvider = builder.grpcInterceptorProvider;
Expand Down Expand Up @@ -230,6 +235,21 @@ private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundE
*/
private Tuple<StorageSettings, Opts<UserProject>> resolveSettingsAndOpts() throws IOException {
String endpoint = getHost();
if (attemptDirectPathXdsOverInterconnect) {
if (endpoint.startsWith(DEFAULT_HOST)
&& (endpoint.length() == DEFAULT_HOST.length()
|| endpoint.charAt(DEFAULT_HOST.length()) == ':'
|| endpoint.charAt(DEFAULT_HOST.length()) == '/')) {
endpoint = DEFAULT_HOST_DIRECT_PATH + endpoint.substring(DEFAULT_HOST.length());
} else if (endpoint.startsWith(DEFAULT_HOST_NO_SCHEME)
&& (endpoint.length() == DEFAULT_HOST_NO_SCHEME.length()
|| endpoint.charAt(DEFAULT_HOST_NO_SCHEME.length()) == ':'
|| endpoint.charAt(DEFAULT_HOST_NO_SCHEME.length()) == '/')) {
endpoint =
DEFAULT_HOST_DIRECT_PATH_NO_SCHEME
+ endpoint.substring(DEFAULT_HOST_NO_SCHEME.length());
}
}
Comment on lines +238 to +252

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

The current host rewriting logic only performs exact string equality checks. If a user configures the host with an explicit port (e.g., https://storage.googleapis.com:443) or a trailing slash, the endpoint will not be rewritten to the DirectPath endpoint, causing the feature to be silently bypassed. We should make the prefix check more robust to handle ports and trailing slashes while avoiding false positives (like storage.googleapis.com.example.com).

Suggested change
if (attemptDirectPathXdsOverInterconnect) {
if ("https://storage.googleapis.com".equals(endpoint)) {
endpoint = "https://storage.direct.googleapis.com";
} else if ("storage.googleapis.com".equals(endpoint)) {
endpoint = "storage.direct.googleapis.com";
}
}
if (attemptDirectPathXdsOverInterconnect) {
if (endpoint.startsWith("https://storage.googleapis.com")
&& (endpoint.length() == 30 || endpoint.charAt(30) == ':' || endpoint.charAt(30) == '/')) {
endpoint = "https://storage.direct.googleapis.com" + endpoint.substring(30);
} else if (endpoint.startsWith("storage.googleapis.com")
&& (endpoint.length() == 22 || endpoint.charAt(22) == ':')) {
endpoint = "storage.direct.googleapis.com" + endpoint.substring(22);
}
}

URI uri = URI.create(endpoint);
String scheme = uri.getScheme();
int port = uri.getPort();
Expand Down Expand Up @@ -322,7 +342,8 @@ private Tuple<StorageSettings, Opts<UserProject>> resolveSettingsAndOpts() throw
InstantiatingGrpcChannelProvider.newBuilder()
.setEndpoint(endpoint)
.setAllowNonDefaultServiceAccount(true)
.setAttemptDirectPath(attemptDirectPath);
.setAttemptDirectPath(attemptDirectPath || attemptDirectPathXdsOverInterconnect)
.setAttemptDirectPathXdsOverInterconnect(attemptDirectPathXdsOverInterconnect);

if (!DIRECT_PATH_BOUND_TOKEN_DISABLED) {
channelProviderBuilder.setAllowHardBoundTokenTypes(
Expand All @@ -337,6 +358,18 @@ private Tuple<StorageSettings, Opts<UserProject>> resolveSettingsAndOpts() throw
channelProviderBuilder.setAttemptDirectPathXds();
}

if (attemptDirectPathXdsOverInterconnect) {
com.google.api.core.ApiFunction<ManagedChannelBuilder, ManagedChannelBuilder>
existingConfigurator = channelProviderBuilder.getChannelConfigurator();
channelProviderBuilder.setChannelConfigurator(
channelBuilder -> {
if (existingConfigurator != null) {
channelBuilder = existingConfigurator.apply(channelBuilder);
}
return channelBuilder.overrideAuthority("storage.googleapis.com");
});
}

if (scheme.equals("http")) {
channelProviderBuilder.setChannelConfigurator(ManagedChannelBuilder::usePlaintext);
}
Expand Down Expand Up @@ -428,6 +461,7 @@ public int hashCode() {
retryAlgorithmManager,
terminationAwaitDuration,
attemptDirectPath,
attemptDirectPathXdsOverInterconnect,
enableGrpcClientMetrics,
grpcInterceptorProvider,
blobWriteSessionConfig,
Expand All @@ -445,6 +479,7 @@ public boolean equals(Object o) {
}
GrpcStorageOptions that = (GrpcStorageOptions) o;
return attemptDirectPath == that.attemptDirectPath
&& attemptDirectPathXdsOverInterconnect == that.attemptDirectPathXdsOverInterconnect
&& enableGrpcClientMetrics == that.enableGrpcClientMetrics
&& Objects.equals(retryAlgorithmManager, that.retryAlgorithmManager)
&& Objects.equals(terminationAwaitDuration, that.terminationAwaitDuration)
Expand Down Expand Up @@ -494,6 +529,7 @@ public static final class Builder extends StorageOptions.Builder {
private StorageRetryStrategy storageRetryStrategy;
private java.time.Duration terminationAwaitDuration;
private boolean attemptDirectPath = GrpcStorageDefaults.INSTANCE.isAttemptDirectPath();
private boolean attemptDirectPathXdsOverInterconnect = false;
private boolean enableGrpcClientMetrics =
GrpcStorageDefaults.INSTANCE.isEnableGrpcClientMetrics();
private GrpcInterceptorProvider grpcInterceptorProvider =
Expand All @@ -512,6 +548,7 @@ public static final class Builder extends StorageOptions.Builder {
this.storageRetryStrategy = gso.getRetryAlgorithmManager().retryStrategy;
this.terminationAwaitDuration = gso.getTerminationAwaitDuration();
this.attemptDirectPath = gso.attemptDirectPath;
this.attemptDirectPathXdsOverInterconnect = gso.attemptDirectPathXdsOverInterconnect;
this.enableGrpcClientMetrics = gso.enableGrpcClientMetrics;
this.grpcInterceptorProvider = gso.grpcInterceptorProvider;
this.blobWriteSessionConfig = gso.blobWriteSessionConfig;
Expand Down Expand Up @@ -556,6 +593,18 @@ public GrpcStorageOptions.Builder setAttemptDirectPath(boolean attemptDirectPath
return this;
}

/**
* Option for whether this client should attempt to use DirectPath over Interconnect (on-premise
* xDS name resolution).
*
* @since 2.45.0
*/
public GrpcStorageOptions.Builder setAttemptDirectPathXdsOverInterconnect(
boolean attemptDirectPathXdsOverInterconnect) {
this.attemptDirectPathXdsOverInterconnect = attemptDirectPathXdsOverInterconnect;
return this;
}

/**
* Option for whether this client should emit internal gRPC client internal metrics to Cloud
* Monitoring. To disable metric reporting, set this to false. True by default. Emitting metrics
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,37 @@ public void grpc() throws Exception {
() -> assertThat(rebuilt.hashCode()).isEqualTo(base.hashCode()));
}

@Test
public void grpc_attemptDirectPathXdsOverInterconnect() throws Exception {
com.google.auth.Credentials mockCreds = com.google.cloud.NoCredentials.getInstance();
GrpcStorageOptions options =
GrpcStorageOptions.grpc()
.setCredentials(mockCreds)
.setAttemptDirectPathXdsOverInterconnect(true)
.build();

GrpcStorageOptions rebuilt = options.toBuilder().build();
assertAll(
() -> assertThat(rebuilt).isEqualTo(options),
() -> assertThat(rebuilt.hashCode()).isEqualTo(options.hashCode()));

com.google.storage.v2.StorageSettings settings = options.getStorageSettings();
assertThat(settings.getEndpoint()).isEqualTo("storage-direct.googleapis.com:443");

com.google.api.gax.rpc.TransportChannelProvider tcp = settings.getTransportChannelProvider();
assertThat(tcp).isInstanceOf(com.google.api.gax.grpc.InstantiatingGrpcChannelProvider.class);
com.google.api.gax.grpc.InstantiatingGrpcChannelProvider provider =
(com.google.api.gax.grpc.InstantiatingGrpcChannelProvider) tcp;

// Verify attemptDirectPathXdsOverInterconnect is set to true on the provider using reflection
java.lang.reflect.Field field =
com.google.api.gax.grpc.InstantiatingGrpcChannelProvider.class.getDeclaredField(
"attemptDirectPathXdsOverInterconnect");
field.setAccessible(true);
Boolean value = (Boolean) field.get(provider);
assertThat(value).isTrue();
}

@Test
public void useJwtAccessWithScope_defaultsToFalse() {
HttpStorageOptions httpOptions = HttpStorageOptions.http().build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,32 @@ public void clientShouldConstructCleanly_directPath() throws Exception {
doTest(options);
}

@Test
public void clientShouldConstructCleanly_directPathXdsOverInterconnect() throws Exception {
StorageOptions options =
StorageOptions.grpc()
.setCredentials(credentials)
.setAttemptDirectPathXdsOverInterconnect(true)
.setEnableGrpcClientMetrics(false)
.build();
doTest(options);
}

@Test
public void clientShouldWork_directPathXdsOverInterconnect() throws Exception {
assumeTrue(
"Environment cannot resolve storage.direct.googleapis.com", canResolveDirectPathAddress());
StorageOptions options =
StorageOptions.grpc()
.setCredentials(credentials)
.setAttemptDirectPathXdsOverInterconnect(true)
.setEnableGrpcClientMetrics(false)
.build();
try (Storage storage = options.getService()) {
storage.list(Storage.BucketListOption.pageSize(1));
}
}

@Test
public void lackOfProjectIdDoesNotPreventConstruction_http() throws Exception {
StorageOptions options = StorageOptions.http().setCredentials(credentials).build();
Expand All @@ -105,4 +131,13 @@ private static void doTest(StorageOptions options) throws Exception {
//noinspection EmptyTryBlock
try (Storage ignore = options.getService()) {}
}

private static boolean canResolveDirectPathAddress() {
try {
java.net.InetAddress.getAllByName("storage.direct.googleapis.com");
return true;
} catch (java.net.UnknownHostException e) {
return false;
}
}
}
2 changes: 1 addition & 1 deletion sdk-platform-java/gax-java/gax-grpc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<!-- These tests require an Env Var to be set. Use -PenvVarTest to ONLY run these tests -->
<test>!InstantiatingGrpcChannelProviderTest#testLogDirectPathMisconfig_AttemptDirectPathNotSetAndAttemptDirectPathXdsSetViaEnv_warns,!InstantiatingGrpcChannelProviderTest#canUseDirectPath_directPathEnvVarNotSet_attemptDirectPathIsTrue,InstantiatingGrpcChannelProviderTest#testLogDirectPathMisconfigWrongCredential</test>
<test>!InstantiatingGrpcChannelProviderTest#testLogDirectPathMisconfig_AttemptDirectPathNotSetAndAttemptDirectPathXdsSetViaEnv_warns,!InstantiatingGrpcChannelProviderTest#canUseDirectPath_directPathEnvVarNotSet_attemptDirectPathIsTrue</test>
<!-- <test>!InstantiatingGrpcChannelProviderTest#testLogDirectPathMisconfig_AttemptDirectPathNotSetAndAttemptDirectPathXdsSetViaEnv_warns</test> -->
</configuration>
</plugin>
Expand Down
Loading
Loading