Skip to content

Commit

Permalink
fix: Allow quota project to be used in combination with null credenti…
Browse files Browse the repository at this point in the history
…als (#1688)
  • Loading branch information
irock authored May 25, 2023
1 parent 0b9976f commit cb07bd4
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 =
Expand Down

0 comments on commit cb07bd4

Please sign in to comment.