From 39380c5c4d570b2311174da6e89bb27695547cf3 Mon Sep 17 00:00:00 2001 From: Riya Mehta Date: Mon, 6 Jan 2025 17:22:45 -0800 Subject: [PATCH] Path #3401. --- .../com/google/api/gax/rpc/EndpointContext.java | 8 +++++--- .../google/api/gax/rpc/EndpointContextTest.java | 14 ++++++++++++++ 2 files changed, 19 insertions(+), 3 deletions(-) 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 b8711253ff..a90a93c677 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 @@ -272,9 +272,6 @@ private String determineUniverseDomain() { /** Determines the fully resolved endpoint and universe domain values */ private String determineEndpoint() throws IOException { - if (shouldUseS2A()) { - return mtlsEndpoint(); - } MtlsProvider mtlsProvider = mtlsProvider() == null ? new MtlsProvider() : mtlsProvider(); // TransportChannelProvider's endpoint will override the ClientSettings' endpoint String customEndpoint = @@ -312,6 +309,11 @@ private String determineEndpoint() throws IOException { /** Determine if S2A can be used */ @VisibleForTesting boolean shouldUseS2A() { + // If mTLS endpoint is not available, skip S2A + if (Strings.isNullOrEmpty(mtlsEndpoint())) { + return false; + } + // If EXPERIMENTAL_GOOGLE_API_USE_S2A is not set to true, skip S2A. String s2AEnv; s2AEnv = envProvider().getenv(S2A_ENV_ENABLE_USE_S2A); 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 5561427dde..1ca034fde8 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 @@ -508,6 +508,20 @@ void shouldUseS2A_customEndpointSetViaTransportChannelProvider_returnsFalse() th Truth.assertThat(defaultEndpointContextBuilder.shouldUseS2A()).isFalse(); } + @Test + void shouldUseS2A_mtlsEndpointNull_returnsFalse() throws IOException { + EnvironmentProvider envProvider = Mockito.mock(EnvironmentProvider.class); + Mockito.when(envProvider.getenv(EndpointContext.S2A_ENV_ENABLE_USE_S2A)).thenReturn("false"); + defaultEndpointContextBuilder = + defaultEndpointContextBuilder + .setEnvProvider(envProvider) + .setClientSettingsEndpoint("") + .setTransportChannelProviderEndpoint("") + .setUsingGDCH(false) + .setMtlsEndpoint(null); + Truth.assertThat(defaultEndpointContextBuilder.shouldUseS2A()).isFalse(); + } + @Test void shouldUseS2A_mtlsEndpointEmpty_returnsFalse() throws IOException { EnvironmentProvider envProvider = Mockito.mock(EnvironmentProvider.class);