From 0f7e7a3b18e58f859492968796cbc627f2ad2faf Mon Sep 17 00:00:00 2001 From: Lawrence Qiu Date: Wed, 28 Feb 2024 22:04:41 -0500 Subject: [PATCH] feat: Universe Domain Environment Variable Support --- .github/workflows/ci.yaml | 28 +++++++++++++++++-- .../google/api/gax/rpc/EndpointContext.java | 9 +++++- .../api/gax/rpc/EndpointContextTest.java | 12 ++++++++ 3 files changed, 45 insertions(+), 4 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 5344f99886..9e870836eb 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -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: | @@ -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: | @@ -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" diff --git a/gax-java/gax/src/main/java/com/google/api/gax/rpc/EndpointContext.java b/gax-java/gax/src/main/java/com/google/api/gax/rpc/EndpointContext.java index b33a97cd0a..0c1815c5fa 100644 --- a/gax-java/gax/src/main/java/com/google/api/gax/rpc/EndpointContext.java +++ b/gax-java/gax/src/main/java/com/google/api/gax/rpc/EndpointContext.java @@ -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 = @@ -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; } @@ -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()); diff --git a/gax-java/gax/src/test/java/com/google/api/gax/rpc/EndpointContextTest.java b/gax-java/gax/src/test/java/com/google/api/gax/rpc/EndpointContextTest.java index f0dbae60f2..2422e9cc8e 100644 --- a/gax-java/gax/src/test/java/com/google/api/gax/rpc/EndpointContextTest.java +++ b/gax-java/gax/src/test/java/com/google/api/gax/rpc/EndpointContextTest.java @@ -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();