From cb07bd405b448fbef51367d1b03d4fe9eab54504 Mon Sep 17 00:00:00 2001 From: Oskar Arvidsson Date: Thu, 25 May 2023 14:54:52 -0400 Subject: [PATCH] fix: Allow quota project to be used in combination with null credentials (#1688) --- .../com/google/api/gax/rpc/ClientContext.java | 2 +- .../google/api/gax/rpc/ClientContextTest.java | 21 +++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/gax-java/gax/src/main/java/com/google/api/gax/rpc/ClientContext.java b/gax-java/gax/src/main/java/com/google/api/gax/rpc/ClientContext.java index 45a54e5491..91e10aa7bf 100644 --- a/gax-java/gax/src/main/java/com/google/api/gax/rpc/ClientContext.java +++ b/gax-java/gax/src/main/java/com/google/api/gax/rpc/ClientContext.java @@ -167,7 +167,7 @@ public static ClientContext create(StubSettings settings) throws IOException { Credentials credentials = settings.getCredentialsProvider().getCredentials(); - if (settings.getQuotaProjectId() != null) { + if (settings.getQuotaProjectId() != null && credentials != null) { // If the quotaProjectId is set, wrap original credentials with correct quotaProjectId as // QuotaProjectIdHidingCredentials. // Ensure that a custom set quota project id takes priority over one detected by credentials. diff --git a/gax-java/gax/src/test/java/com/google/api/gax/rpc/ClientContextTest.java b/gax-java/gax/src/test/java/com/google/api/gax/rpc/ClientContextTest.java index 7fe418dd68..7b69e30feb 100644 --- a/gax-java/gax/src/test/java/com/google/api/gax/rpc/ClientContextTest.java +++ b/gax-java/gax/src/test/java/com/google/api/gax/rpc/ClientContextTest.java @@ -41,6 +41,7 @@ import com.google.api.gax.core.ExecutorProvider; import com.google.api.gax.core.FixedCredentialsProvider; import com.google.api.gax.core.FixedExecutorProvider; +import com.google.api.gax.core.NoCredentialsProvider; import com.google.api.gax.rpc.mtls.MtlsProvider; import com.google.api.gax.rpc.mtls.MtlsProvider.MtlsEndpointUsagePolicy; import com.google.api.gax.rpc.testing.FakeChannel; @@ -533,6 +534,26 @@ public Credentials getCredentials() throws IOException { assertThat(clientContext.getCredentials().getRequestMetadata(null)).isEqualTo(metaData); } + @Test + public void testQuotaProjectId_worksWithNullCredentials() throws IOException { + final String QUOTA_PROJECT_ID = "quota_project_id"; + + final InterceptingExecutor executor = new InterceptingExecutor(1); + final FakeTransportChannel transportChannel = FakeTransportChannel.create(new FakeChannel()); + final FakeTransportProvider transportProvider = + new FakeTransportProvider( + transportChannel, executor, true, null, Mockito.mock(Credentials.class)); + + final FakeClientSettings.Builder settingsBuilder = new FakeClientSettings.Builder(); + + settingsBuilder + .setTransportChannelProvider(transportProvider) + .setCredentialsProvider(NoCredentialsProvider.create()) + .setQuotaProjectId(QUOTA_PROJECT_ID); + + assertThat(ClientContext.create(settingsBuilder.build()).getCredentials()).isNull(); + } + @Test public void testUserAgentInternalOnly() throws Exception { TransportChannelProvider transportChannelProvider =