Skip to content

fix: Allow quota project to be used in combination with null credentials #1688

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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