Skip to content

Commit 0f7e7a3

Browse files
committed
feat: Universe Domain Environment Variable Support
1 parent c8d145b commit 0f7e7a3

File tree

3 files changed

+45
-4
lines changed

3 files changed

+45
-4
lines changed

.github/workflows/ci.yaml

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,15 @@ jobs:
2121
- name: Unit Tests
2222
run: |
2323
mvn test --batch-mode --no-transfer-progress -Dcheckstyle.skip \
24-
-Dfmt.skip -DenableTestCoverage
24+
-Dfmt.skip -DenableTestCoverage -Dsurefire.failIfNoSpecifiedTests=false \
25+
-Dtest=\!EndpointContextTest#endpointContextBuild_universeDomainEnvVarSet
26+
- run: echo "GOOGLE_CLOUD_UNIVERSE_DOMAIN=random.com" >> $GITHUB_ENV
27+
- name: EndpointContext Env Var Test
28+
run: |
29+
mvn test --batch-mode --no-transfer-progress -Dcheckstyle.skip \
30+
-Dfmt.skip -DenableTestCoverage -Dsurefire.failIfNoSpecifiedTests=false \
31+
-Dtest=EndpointContextTest#endpointContextBuild_universeDomainEnvVarSet
32+
- run: echo "GOOGLE_CLOUD_UNIVERSE_DOMAIN=" >> $GITHUB_ENV
2533
- run: bazelisk version
2634
- name: Install Maven modules
2735
run: |
@@ -63,7 +71,15 @@ jobs:
6371
- name: Unit Tests
6472
run: |
6573
mvn test --batch-mode --no-transfer-progress -Dcheckstyle.skip \
66-
-Dfmt.skip -DenableTestCoverage
74+
-Dfmt.skip -DenableTestCoverage -Dsurefire.failIfNoSpecifiedTests=false \
75+
-Dtest=\!EndpointContextTest#endpointContextBuild_universeDomainEnvVarSet
76+
- run: echo "GOOGLE_CLOUD_UNIVERSE_DOMAIN=random.com" >> $GITHUB_ENV
77+
- name: EndpointContext Env Var Test
78+
run: |
79+
mvn test --batch-mode --no-transfer-progress -Dcheckstyle.skip \
80+
-Dfmt.skip -DenableTestCoverage -Dsurefire.failIfNoSpecifiedTests=false \
81+
-Dtest=EndpointContextTest#endpointContextBuild_universeDomainEnvVarSet
82+
- run: echo "GOOGLE_CLOUD_UNIVERSE_DOMAIN=" >> $GITHUB_ENV
6783
- run: bazelisk version
6884
- name: Install Maven modules
6985
run: |
@@ -97,7 +113,13 @@ jobs:
97113
# the "jvm" system property.
98114
mvn verify --batch-mode --no-transfer-progress -Dcheckstyle.skip \
99115
-Dfmt.skip \
100-
-Djvm="${JAVA8_HOME}/bin/java"
116+
-Djvm="${JAVA8_HOME}/bin/java" -Dsurefire.failIfNoSpecifiedTests=false \
117+
-Dtest=\!EndpointContextTest#endpointContextBuild_universeDomainEnvVarSet
118+
export GOOGLE_CLOUD_UNIVERSE_DOMAIN=random.com
119+
mvn test --batch-mode --no-transfer-progress -Dcheckstyle.skip \
120+
-Dfmt.skip -DenableTestCoverage -Dsurefire.failIfNoSpecifiedTests=false \
121+
-Dtest=EndpointContextTest#endpointContextBuild_universeDomainEnvVarSet
122+
- run: echo "GOOGLE_CLOUD_UNIVERSE_DOMAIN=" >> $GITHUB_ENV
101123

102124
build-java8-gapic-generator-java:
103125
name: "build(8) for gapic-generator-java"

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

Lines changed: 8 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,8 @@ 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+
// If the universe domain is configured by the user, the universe domain will either be
218+
// from the settings or from the env var. The value from ClientSettings has priority.
217219
return universeDomain() != null ? universeDomain() : Credentials.GOOGLE_DEFAULT_UNIVERSE;
218220
}
219221

@@ -283,6 +285,11 @@ String mtlsEndpointResolver(
283285
}
284286

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

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

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

343+
@Test
344+
public void endpointContextBuild_universeDomainEnvVarSet() throws IOException {
345+
String envVarUniverseDomain = "random.com";
346+
EndpointContext endpointContext =
347+
defaultEndpointContextBuilder
348+
.setUniverseDomain(null)
349+
.setClientSettingsEndpoint(null)
350+
.build();
351+
Truth.assertThat(endpointContext.resolvedEndpoint()).isEqualTo("test.random.com:443");
352+
Truth.assertThat(endpointContext.resolvedUniverseDomain()).isEqualTo(envVarUniverseDomain);
353+
}
354+
343355
@Test
344356
public void hasValidUniverseDomain_gdchFlow_anyCredentials() throws IOException {
345357
Credentials noCredentials = NoCredentialsProvider.create().getCredentials();

0 commit comments

Comments
 (0)