Skip to content

Commit

Permalink
feat: Universe Domain Environment Variable Support
Browse files Browse the repository at this point in the history
  • Loading branch information
lqiu96 committed Feb 29, 2024
1 parent c8d145b commit 0f7e7a3
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 4 deletions.
28 changes: 25 additions & 3 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,15 @@ jobs:
- name: Unit Tests
run: |
mvn test --batch-mode --no-transfer-progress -Dcheckstyle.skip \
-Dfmt.skip -DenableTestCoverage
-Dfmt.skip -DenableTestCoverage -Dsurefire.failIfNoSpecifiedTests=false \
-Dtest=\!EndpointContextTest#endpointContextBuild_universeDomainEnvVarSet
- run: echo "GOOGLE_CLOUD_UNIVERSE_DOMAIN=random.com" >> $GITHUB_ENV
- name: EndpointContext Env Var Test
run: |
mvn test --batch-mode --no-transfer-progress -Dcheckstyle.skip \
-Dfmt.skip -DenableTestCoverage -Dsurefire.failIfNoSpecifiedTests=false \
-Dtest=EndpointContextTest#endpointContextBuild_universeDomainEnvVarSet
- run: echo "GOOGLE_CLOUD_UNIVERSE_DOMAIN=" >> $GITHUB_ENV
- run: bazelisk version
- name: Install Maven modules
run: |
Expand Down Expand Up @@ -63,7 +71,15 @@ jobs:
- name: Unit Tests
run: |
mvn test --batch-mode --no-transfer-progress -Dcheckstyle.skip \
-Dfmt.skip -DenableTestCoverage
-Dfmt.skip -DenableTestCoverage -Dsurefire.failIfNoSpecifiedTests=false \
-Dtest=\!EndpointContextTest#endpointContextBuild_universeDomainEnvVarSet
- run: echo "GOOGLE_CLOUD_UNIVERSE_DOMAIN=random.com" >> $GITHUB_ENV
- name: EndpointContext Env Var Test
run: |
mvn test --batch-mode --no-transfer-progress -Dcheckstyle.skip \
-Dfmt.skip -DenableTestCoverage -Dsurefire.failIfNoSpecifiedTests=false \
-Dtest=EndpointContextTest#endpointContextBuild_universeDomainEnvVarSet
- run: echo "GOOGLE_CLOUD_UNIVERSE_DOMAIN=" >> $GITHUB_ENV
- run: bazelisk version
- name: Install Maven modules
run: |
Expand Down Expand Up @@ -97,7 +113,13 @@ jobs:
# the "jvm" system property.
mvn verify --batch-mode --no-transfer-progress -Dcheckstyle.skip \
-Dfmt.skip \
-Djvm="${JAVA8_HOME}/bin/java"
-Djvm="${JAVA8_HOME}/bin/java" -Dsurefire.failIfNoSpecifiedTests=false \
-Dtest=\!EndpointContextTest#endpointContextBuild_universeDomainEnvVarSet
export GOOGLE_CLOUD_UNIVERSE_DOMAIN=random.com
mvn test --batch-mode --no-transfer-progress -Dcheckstyle.skip \
-Dfmt.skip -DenableTestCoverage -Dsurefire.failIfNoSpecifiedTests=false \
-Dtest=EndpointContextTest#endpointContextBuild_universeDomainEnvVarSet
- run: echo "GOOGLE_CLOUD_UNIVERSE_DOMAIN=" >> $GITHUB_ENV

build-java8-gapic-generator-java:
name: "build(8) for gapic-generator-java"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
@InternalApi
@AutoValue
public abstract class EndpointContext {
private static final String GOOGLE_CLOUD_UNIVERSE_DOMAIN = "GOOGLE_CLOUD_UNIVERSE_DOMAIN";
private static final String INVALID_UNIVERSE_DOMAIN_ERROR_TEMPLATE =
"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.";
public static final String UNABLE_TO_RETRIEVE_CREDENTIALS_ERROR_MESSAGE =
Expand Down Expand Up @@ -213,7 +214,8 @@ private String determineUniverseDomain() {
if (universeDomain() != null && universeDomain().isEmpty()) {
throw new IllegalArgumentException("The universe domain value cannot be empty.");
}
// Override with user set universe domain if provided
// If the universe domain is configured by the user, the universe domain will either be
// from the settings or from the env var. The value from ClientSettings has priority.
return universeDomain() != null ? universeDomain() : Credentials.GOOGLE_DEFAULT_UNIVERSE;
}

Expand Down Expand Up @@ -283,6 +285,11 @@ String mtlsEndpointResolver(
}

public EndpointContext build() throws IOException {
// If the universe domain wasn't configured explicitly in the settings, check the
// environment variable for the value
if (universeDomain() == null) {
setUniverseDomain(System.getenv(GOOGLE_CLOUD_UNIVERSE_DOMAIN));
}
// The Universe Domain is used to resolve the Endpoint. It should be resolved first
setResolvedUniverseDomain(determineUniverseDomain());
setResolvedEndpoint(determineEndpoint());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,18 @@ public void endpointContextBuild_gdchFlow_noUniverseDomain_customEndpoint() thro
.isEqualTo(Credentials.GOOGLE_DEFAULT_UNIVERSE);
}

@Test
public void endpointContextBuild_universeDomainEnvVarSet() throws IOException {
String envVarUniverseDomain = "random.com";
EndpointContext endpointContext =
defaultEndpointContextBuilder
.setUniverseDomain(null)
.setClientSettingsEndpoint(null)
.build();
Truth.assertThat(endpointContext.resolvedEndpoint()).isEqualTo("test.random.com:443");
Truth.assertThat(endpointContext.resolvedUniverseDomain()).isEqualTo(envVarUniverseDomain);
}

@Test
public void hasValidUniverseDomain_gdchFlow_anyCredentials() throws IOException {
Credentials noCredentials = NoCredentialsProvider.create().getCredentials();
Expand Down

0 comments on commit 0f7e7a3

Please sign in to comment.