Skip to content

Commit 01ed807

Browse files
committed
feat: Universe Domain Environment Variable Support
1 parent b177d9e commit 01ed807

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
@InternalApi
4848
@AutoValue
4949
public abstract class EndpointContext {
50+
private static final String GOOGLE_CLOUD_UNIVERSE_DOMAIN = "GOOGLE_CLOUD_UNIVERSE_DOMAIN";
5051
private static final String INVALID_UNIVERSE_DOMAIN_ERROR_TEMPLATE =
5152
"The configured universe domain (%s) does not match the universe domain found in the credentials (%s). If you haven't configured the universe domain explicitly, `googleapis.com` is the default.";
5253
public static final String UNABLE_TO_RETRIEVE_CREDENTIALS_ERROR_MESSAGE =
@@ -213,7 +214,9 @@ private String determineUniverseDomain() {
213214
if (universeDomain() != null && universeDomain().isEmpty()) {
214215
throw new IllegalArgumentException("The universe domain value cannot be empty.");
215216
}
216-
// Override with user set universe domain if provided
217+
// Override with configured universe domain if provided. If the universe domain is configured
218+
// by the user, the universe domain will either be the settings or from the env var. The
219+
// value from ClientSettings has priority.
217220
return universeDomain() != null ? universeDomain() : Credentials.GOOGLE_DEFAULT_UNIVERSE;
218221
}
219222

@@ -283,6 +286,11 @@ String mtlsEndpointResolver(
283286
}
284287

285288
public EndpointContext build() throws IOException {
289+
// If the universe domain wasn't configured explicitly in the settings, check the
290+
// environment variable for the value
291+
if (universeDomain() == null) {
292+
setUniverseDomain(System.getenv(GOOGLE_CLOUD_UNIVERSE_DOMAIN));
293+
}
286294
// The Universe Domain is used to resolve the Endpoint. It should be resolved first
287295
setResolvedUniverseDomain(determineUniverseDomain());
288296
setResolvedEndpoint(determineEndpoint());

gax-java/gax/src/test/java/com/google/api/gax/rpc/EndpointContextTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,21 @@ public void endpointContextBuild_gdchFlow_noUniverseDomain_customEndpoint() thro
340340
.isEqualTo(Credentials.GOOGLE_DEFAULT_UNIVERSE);
341341
}
342342

343+
// JUnit has limitations with mocking Environment Variable. For this test, we simply replicate the
344+
// behavior in `build()` by setting the universe domain to an env var value. Keeping this test
345+
// case scenario if a future solution for testing env vars exists.
346+
@Test
347+
public void endpointContextBuild_universeDomainEnvVarSet() throws IOException {
348+
String envVarUniverseDomain = "random.com";
349+
EndpointContext endpointContext =
350+
defaultEndpointContextBuilder
351+
.setUniverseDomain(envVarUniverseDomain)
352+
.setClientSettingsEndpoint(null)
353+
.build();
354+
Truth.assertThat(endpointContext.resolvedEndpoint()).isEqualTo("test.random.com:443");
355+
Truth.assertThat(endpointContext.resolvedUniverseDomain()).isEqualTo(envVarUniverseDomain);
356+
}
357+
343358
@Test
344359
public void hasValidUniverseDomain_gdchFlow_anyCredentials() throws IOException {
345360
Credentials noCredentials = NoCredentialsProvider.create().getCredentials();

0 commit comments

Comments
 (0)