diff --git a/gax-java/gax-grpc/src/main/java/com/google/api/gax/grpc/InstantiatingGrpcChannelProvider.java b/gax-java/gax-grpc/src/main/java/com/google/api/gax/grpc/InstantiatingGrpcChannelProvider.java index 51d1ee5802..d7b019a7e0 100644 --- a/gax-java/gax-grpc/src/main/java/com/google/api/gax/grpc/InstantiatingGrpcChannelProvider.java +++ b/gax-java/gax-grpc/src/main/java/com/google/api/gax/grpc/InstantiatingGrpcChannelProvider.java @@ -295,6 +295,7 @@ private void logDirectPathMisconfig() { Level.WARNING, "DirectPath is misconfigured. DirectPath is only available in a GCE environment."); } + // Case 4: Universe Domain is non-GDU if (!canUseDirectPathWithUniverseDomain()) { LOG.log( Level.WARNING, "DirectPath will only work in the the googleapis.com Universe Domain"); @@ -334,8 +335,16 @@ static boolean isOnComputeEngine() { return false; } - private boolean canUseDirectPathWithUniverseDomain() { - return endpoint.contains("googleapis.com"); + // Universe Domain configuration is currently only supported in the GDU + @VisibleForTesting + boolean canUseDirectPathWithUniverseDomain() { + // ClientContext will set an endpoint if it isn't provided to the TransportChannelProvider + // If the endpoint is null, then the endpoint hasn't been fully resolved yet (client not + // initialized). Before a call goes through with DirectPath, this validation must succeed. + if (endpoint == null) { + return true; + } + return endpoint.contains(Credentials.GOOGLE_DEFAULT_UNIVERSE); } @VisibleForTesting diff --git a/gax-java/gax-grpc/src/test/java/com/google/api/gax/grpc/InstantiatingGrpcChannelProviderTest.java b/gax-java/gax-grpc/src/test/java/com/google/api/gax/grpc/InstantiatingGrpcChannelProviderTest.java index 537a3c5176..93d6d64404 100644 --- a/gax-java/gax-grpc/src/test/java/com/google/api/gax/grpc/InstantiatingGrpcChannelProviderTest.java +++ b/gax-java/gax-grpc/src/test/java/com/google/api/gax/grpc/InstantiatingGrpcChannelProviderTest.java @@ -292,6 +292,38 @@ public void testDirectPathDisallowNullCredentials() throws IOException { assertThat(provider.isCredentialDirectPathCompatible()).isFalse(); } + @Test + public void testDirectPathWithNullEndpoint() { + InstantiatingGrpcChannelProvider provider = + InstantiatingGrpcChannelProvider.newBuilder() + .setAttemptDirectPath(true) + .setAttemptDirectPathXds() + .build(); + assertThat(provider.canUseDirectPathWithUniverseDomain()).isTrue(); + } + + @Test + public void testDirectPathWithGDUEndpoint() { + InstantiatingGrpcChannelProvider provider = + InstantiatingGrpcChannelProvider.newBuilder() + .setAttemptDirectPath(true) + .setAttemptDirectPathXds() + .setEndpoint("test.googleapis.com:443") + .build(); + assertThat(provider.canUseDirectPathWithUniverseDomain()).isTrue(); + } + + @Test + public void testDirectPathWithNonGDUEndpoint() { + InstantiatingGrpcChannelProvider provider = + InstantiatingGrpcChannelProvider.newBuilder() + .setAttemptDirectPath(true) + .setAttemptDirectPathXds() + .setEndpoint("test.random.com:443") + .build(); + assertThat(provider.canUseDirectPathWithUniverseDomain()).isFalse(); + } + @Test public void testDirectPathXdsEnabled() throws IOException { InstantiatingGrpcChannelProvider provider =