Skip to content

Commit f499ced

Browse files
authored
feat: Full Endpoint Resolution from EndpointContext (#2313)
* feat: Full endpoint resolution * chore: Fix lint issues * chore: Address PR comments * chore: Do not set resolved universe domain for gdch * chore: Move helper methods to the Builder class * chore: Address PR comments * chore: Add javadocs for universe domain
1 parent 4f535a7 commit f499ced

File tree

9 files changed

+489
-89
lines changed

9 files changed

+489
-89
lines changed

gax-java/gax-grpc/src/main/java/com/google/api/gax/grpc/InstantiatingGrpcChannelProvider.java

+12-1
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,10 @@ private void logDirectPathMisconfig() {
295295
Level.WARNING,
296296
"DirectPath is misconfigured. DirectPath is only available in a GCE environment.");
297297
}
298+
if (!canUseDirectPathWithUniverseDomain()) {
299+
LOG.log(
300+
Level.WARNING, "DirectPath will only work in the the googleapis.com Universe Domain");
301+
}
298302
}
299303
}
300304
}
@@ -325,6 +329,10 @@ static boolean isOnComputeEngine() {
325329
return false;
326330
}
327331

332+
private boolean canUseDirectPathWithUniverseDomain() {
333+
return endpoint.contains("googleapis.com");
334+
}
335+
328336
@VisibleForTesting
329337
ChannelCredentials createMtlsChannelCredentials() throws IOException, GeneralSecurityException {
330338
if (mtlsProvider.useMtlsClientCertificate()) {
@@ -356,7 +364,10 @@ private ManagedChannel createSingleChannel() throws IOException {
356364

357365
// Check DirectPath traffic.
358366
boolean useDirectPathXds = false;
359-
if (isDirectPathEnabled() && isNonDefaultServiceAccountAllowed() && isOnComputeEngine()) {
367+
if (isDirectPathEnabled()
368+
&& isNonDefaultServiceAccountAllowed()
369+
&& isOnComputeEngine()
370+
&& canUseDirectPathWithUniverseDomain()) {
360371
CallCredentials callCreds = MoreCallCredentials.from(credentials);
361372
ChannelCredentials channelCreds =
362373
GoogleDefaultChannelCredentials.newBuilder().callCredentials(callCreds).build();

gax-java/gax-grpc/src/test/java/com/google/api/gax/grpc/InstantiatingGrpcChannelProviderTest.java

+19
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,7 @@ public void testDirectPathXdsEnabled() throws IOException {
290290
InstantiatingGrpcChannelProvider.newBuilder()
291291
.setAttemptDirectPath(true)
292292
.setAttemptDirectPathXds()
293+
.setEndpoint("test.googleapis.com:443")
293294
.build();
294295

295296
assertThat(provider.isDirectPathXdsEnabled()).isTrue();
@@ -528,6 +529,7 @@ public void testLogDirectPathMisconfigWrongCredential() {
528529
InstantiatingGrpcChannelProvider.newBuilder()
529530
.setAttemptDirectPathXds()
530531
.setAttemptDirectPath(true)
532+
.setEndpoint("test.googleapis.com:443")
531533
.build();
532534
assertThat(logHandler.getAllMessages())
533535
.contains(
@@ -545,6 +547,7 @@ public void testLogDirectPathMisconfigNotOnGCE() {
545547
.setAttemptDirectPathXds()
546548
.setAttemptDirectPath(true)
547549
.setAllowNonDefaultServiceAccount(true)
550+
.setEndpoint("test.googleapis.com:443")
548551
.build();
549552
if (!InstantiatingGrpcChannelProvider.isOnComputeEngine()) {
550553
assertThat(logHandler.getAllMessages())
@@ -554,6 +557,22 @@ public void testLogDirectPathMisconfigNotOnGCE() {
554557
InstantiatingGrpcChannelProvider.LOG.removeHandler(logHandler);
555558
}
556559

560+
@Test
561+
public void testLogDirectPathMisconfigNotInGDU() {
562+
FakeLogHandler logHandler = new FakeLogHandler();
563+
InstantiatingGrpcChannelProvider.LOG.addHandler(logHandler);
564+
InstantiatingGrpcChannelProvider provider =
565+
InstantiatingGrpcChannelProvider.newBuilder()
566+
.setAttemptDirectPathXds()
567+
.setAttemptDirectPath(true)
568+
.setAllowNonDefaultServiceAccount(true)
569+
.setEndpoint("test.random.endpoint.com:443")
570+
.build();
571+
assertThat(logHandler.getAllMessages())
572+
.contains("DirectPath will only work in the the googleapis.com Universe Domain");
573+
InstantiatingGrpcChannelProvider.LOG.removeHandler(logHandler);
574+
}
575+
557576
private static class FakeLogHandler extends Handler {
558577
List<LogRecord> records = new ArrayList<>();
559578

gax-java/gax/clirr-ignored-differences.xml

+6
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,10 @@
2525
<className>com/google/api/gax/rpc/TransportChannelProvider</className>
2626
<method>* getEndpoint()</method>
2727
</difference>
28+
<!-- Add Universe Domain to ClientContext -->
29+
<difference>
30+
<differenceType>7013</differenceType>
31+
<className>com/google/api/gax/rpc/ClientContext*</className>
32+
<method>* *UniverseDomain*(*)</method>
33+
</difference>
2834
</differences>

gax-java/gax/src/main/java/com/google/api/gax/rpc/ClientContext.java

+22-13
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,9 @@ public abstract class ClientContext {
104104
@Nullable
105105
abstract String getServiceName();
106106

107+
@Nullable
108+
public abstract String getUniverseDomain();
109+
107110
@Nullable
108111
public abstract String getEndpoint();
109112

@@ -157,15 +160,28 @@ public static ClientContext create(StubSettings settings) throws IOException {
157160
final ScheduledExecutorService backgroundExecutor = backgroundExecutorProvider.getExecutor();
158161

159162
Credentials credentials = settings.getCredentialsProvider().getCredentials();
163+
boolean usingGDCH = credentials instanceof GdchCredentials;
164+
EndpointContext endpointContext =
165+
EndpointContext.newBuilder()
166+
.setServiceName(settings.getServiceName())
167+
.setUniverseDomain(settings.getUniverseDomain())
168+
.setClientSettingsEndpoint(settings.getEndpoint())
169+
.setTransportChannelProviderEndpoint(
170+
settings.getTransportChannelProvider().getEndpoint())
171+
.setMtlsEndpoint(settings.getMtlsEndpoint())
172+
.setSwitchToMtlsEndpointAllowed(settings.getSwitchToMtlsEndpointAllowed())
173+
.setUsingGDCH(usingGDCH)
174+
.build();
175+
String endpoint = endpointContext.resolvedEndpoint();
160176

161177
String settingsGdchApiAudience = settings.getGdchApiAudience();
162-
if (credentials instanceof GdchCredentials) {
178+
if (usingGDCH) {
163179
// We recompute the GdchCredentials with the audience
164180
String audienceString;
165181
if (!Strings.isNullOrEmpty(settingsGdchApiAudience)) {
166182
audienceString = settingsGdchApiAudience;
167-
} else if (!Strings.isNullOrEmpty(settings.getEndpoint())) {
168-
audienceString = settings.getEndpoint();
183+
} else if (!Strings.isNullOrEmpty(endpoint)) {
184+
audienceString = endpoint;
169185
} else {
170186
throw new IllegalArgumentException("Could not infer GDCH api audience from settings");
171187
}
@@ -204,16 +220,6 @@ public static ClientContext create(StubSettings settings) throws IOException {
204220
if (transportChannelProvider.needsCredentials() && credentials != null) {
205221
transportChannelProvider = transportChannelProvider.withCredentials(credentials);
206222
}
207-
EndpointContext endpointContext =
208-
EndpointContext.newBuilder()
209-
.setServiceName(settings.getServiceName())
210-
.setClientSettingsEndpoint(settings.getEndpoint())
211-
.setTransportChannelProviderEndpoint(
212-
settings.getTransportChannelProvider().getEndpoint())
213-
.setMtlsEndpoint(settings.getMtlsEndpoint())
214-
.setSwitchToMtlsEndpointAllowed(settings.getSwitchToMtlsEndpointAllowed())
215-
.build();
216-
String endpoint = endpointContext.getResolvedEndpoint();
217223
if (transportChannelProvider.needsEndpoint()) {
218224
transportChannelProvider = transportChannelProvider.withEndpoint(endpoint);
219225
}
@@ -264,6 +270,7 @@ public static ClientContext create(StubSettings settings) throws IOException {
264270
.setClock(clock)
265271
.setDefaultCallContext(defaultCallContext)
266272
.setServiceName(settings.getServiceName())
273+
.setUniverseDomain(settings.getUniverseDomain())
267274
.setEndpoint(settings.getEndpoint())
268275
.setQuotaProjectId(settings.getQuotaProjectId())
269276
.setStreamWatchdog(watchdog)
@@ -332,6 +339,8 @@ public abstract static class Builder {
332339
// Package-Private scope for internal use only. Shared between StubSettings and ClientContext
333340
abstract Builder setServiceName(String serviceName);
334341

342+
public abstract Builder setUniverseDomain(String universeDomain);
343+
335344
public abstract Builder setEndpoint(String endpoint);
336345

337346
public abstract Builder setQuotaProjectId(String QuotaProjectId);

0 commit comments

Comments
 (0)