Skip to content

fix(bigquery-jdbc): scope Storage Read/Write API credentials for TPC - #14972

Open
logachev wants to merge 2 commits into
mainfrom
kirl/fix_tpc_readapi
Open

logachev wants to merge 2 commits into
mainfrom
kirl/fix_tpc_readapi

Conversation

@logachev

Copy link
Copy Markdown
Contributor

Summary

Queries routed through the BigQuery Storage Read API (HTAPI / Arrow) or Storage Write API fail with UNAUTHENTICATED when connecting to non-default universe domains (TPC / custom universeDomain) using service account or Application Default Credentials.

Root Cause

  1. BigQueryJdbcOAuthUtility.getCredentials(...) returns unscoped GoogleCredentials (createScopedRequired() == true) unless RequestGoogleDriveScope=1 or service account impersonation is configured.
  2. For the REST BigQuery client, BigQueryOptions (ServiceOptions.getScopedCredentials()) automatically scopes credentials, causing ServiceAccountCredentials in non-default universes to mint a scope-based self-signed JWT (scope claim, no aud claim).
  3. For BigQueryReadClient and BigQueryWriteClient, BigQueryConnection bypassed BigQueryReadSettings.defaultCredentialsProviderBuilder() / BigQueryWriteSettings.defaultCredentialsProviderBuilder() and wrapped the unscoped credential directly in FixedCredentialsProvider.create(...). Because createScopedRequired() remained true, ServiceAccountCredentials fell back to minting an audience-based self-signed JWT (aud set to the custom universe Storage endpoint, no scope claim), which is rejected with UNAUTHENTICATED.

Fix

  • Added BigQueryConnection.scopeStorageCredentials(Credentials, List<String>) mirroring GoogleCredentialsProvider in BigQueryReadSettings and BigQueryWriteSettings:
    • Applies default service scopes (BigQueryReadSettings.getDefaultServiceScopes() / BigQueryWriteSettings.getDefaultServiceScopes()) when createScopedRequired() is true, while preserving explicit scopes when already configured (such as RequestGoogleDriveScope=1).
    • Enables createWithUseJwtAccessWithScope(true) on ServiceAccountCredentials so self-signed JWTs include the scope claim without requiring extra OAuth token endpoint round-trips in GDU.
  • Re-enabled Read API integration tests in TPC (@Tag("disable_tpc")) that were previously disabled due to this authentication failure.

Tests

  • BigQueryConnectionTest: verifies BigQueryReadClient and BigQueryWriteClient preserve custom universeDomain settings and mint scope-based self-signed JWTs (scope claim present, aud absent), including when explicit scopes are pre-applied.
  • ITBigQueryJDBCTest: re-enabled testReadAPIPathLarge, testReadAPIPathLargeWithThresholdParameters, and testReadAPIPathLargeWithThresholdNotMet for TPC using self-contained UNNEST(GENERATE_ARRAY(...)) queries and explicit result set type assertions.
  • ITNightlyBigQueryTest: re-enabled testHTAPIWithValidDestinationTableSavesQueriesWithStandardSQL, testReadAPIPathLarge, and testReadAPIPathLargeWithThresholdParameters for TPC.

@logachev
logachev requested review from a team as code owners November 11, 2026 19:03

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces credentials scoping for BigQuery Read and Write clients in BigQueryConnection to support Trusted Partner Cloud (TPC) environments, and updates several integration tests by removing the @Tag("disable_tpc") annotation and asserting specific result set types. The review feedback suggests improving test robustness by adding null and presence checks for the Authorization header in the test helper extractBearerJwtPayload to avoid potential NullPointerException or IndexOutOfBoundsException, and using try-with-resources for jsonResultSet in ITBigQueryJDBCTest to prevent resource leaks if assertions fail.

…c/it/ITBigQueryJDBCTest.java

Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant