diff --git a/java-bigquery/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/ConnectionImpl.java b/java-bigquery/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/ConnectionImpl.java index 9416caf708ef..5a96f8c0dccd 100644 --- a/java-bigquery/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/ConnectionImpl.java +++ b/java-bigquery/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/ConnectionImpl.java @@ -21,6 +21,7 @@ import com.google.api.core.BetaApi; import com.google.api.core.InternalApi; import com.google.api.gax.core.FixedCredentialsProvider; +import com.google.api.gax.core.NoCredentialsProvider; import com.google.api.services.bigquery.model.GetQueryResultsResponse; import com.google.api.services.bigquery.model.JobConfigurationQuery; import com.google.api.services.bigquery.model.QueryParameter; @@ -989,18 +990,37 @@ TableDataList tableDataListRpc(TableId destinationTable, String pageToken) { } } + /** + * Builds {@link BigQueryReadSettings} for the BigQuery Storage Read API client from {@link + * BigQueryOptions}, propagating credentials and universe domain configuration. + */ + @VisibleForTesting + BigQueryReadSettings createBigQueryReadSettings() throws IOException { + // ServiceOptions leaves credentials null if none were configured and ADC lookup failed. + // Use NoCredentialsProvider in that case instead of passing null to FixedCredentialsProvider. + BigQueryReadSettings.Builder settingsBuilder = + BigQueryReadSettings.newBuilder() + .setCredentialsProvider( + bigQueryOptions.getCredentials() != null + ? FixedCredentialsProvider.create(bigQueryOptions.getCredentials()) + : NoCredentialsProvider.create()); + // Forward an explicitly configured universe domain so the Read API client resolves + // bigquerystorage.:443 and validates credentials against the same universe. + // Leave it unset when null so GAX still checks GOOGLE_CLOUD_UNIVERSE_DOMAIN before defaulting + // to googleapis.com. + if (bigQueryOptions.getUniverseDomain() != null) { + settingsBuilder.setUniverseDomain(bigQueryOptions.getUniverseDomain()); + } + return settingsBuilder.build(); + } + @VisibleForTesting BigQueryResult highThroughPutRead( TableId destinationTable, long totalRows, Schema schema, BigQueryResultStats stats) { try { if (bqReadClient == null) { // if the read client isn't already initialized. Not thread safe. - BigQueryReadSettings settings = - BigQueryReadSettings.newBuilder() - .setCredentialsProvider( - FixedCredentialsProvider.create(bigQueryOptions.getCredentials())) - .build(); - bqReadClient = BigQueryReadClient.create(settings); + bqReadClient = BigQueryReadClient.create(createBigQueryReadSettings()); } String parent = String.format("projects/%s", destinationTable.getProject()); String srcTable = diff --git a/java-bigquery/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/ConnectionImplTest.java b/java-bigquery/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/ConnectionImplTest.java index addf83fa1b88..ad0787c6948f 100644 --- a/java-bigquery/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/ConnectionImplTest.java +++ b/java-bigquery/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/ConnectionImplTest.java @@ -18,6 +18,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -28,6 +29,7 @@ import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; +import com.google.api.gax.core.NoCredentialsProvider; import com.google.api.services.bigquery.model.GetQueryResultsResponse; import com.google.api.services.bigquery.model.QueryParameter; import com.google.api.services.bigquery.model.QueryParameterType; @@ -37,10 +39,12 @@ import com.google.api.services.bigquery.model.TableDataList; import com.google.api.services.bigquery.model.TableRow; import com.google.api.services.bigquery.model.TableSchema; +import com.google.cloud.NoCredentials; import com.google.cloud.ServiceOptions; import com.google.cloud.Tuple; import com.google.cloud.bigquery.spi.BigQueryRpcFactory; import com.google.cloud.bigquery.spi.v2.HttpBigQueryRpc; +import com.google.cloud.bigquery.storage.v1.BigQueryReadSettings; import com.google.common.collect.ImmutableList; import com.google.common.util.concurrent.ListenableFuture; import java.io.IOException; @@ -817,4 +821,61 @@ void testCreateQueryRequestAndJobWithEmptyParameters() throws BigQuerySQLExcepti connection.createQueryJob(SQL_QUERY, connectionSettings, emptyParameters, emptyLabels); assertEquals(JOB, createdJob.getId()); } + + @Test + void testCreateBigQueryReadSettings_forwardsUniverseDomain() throws IOException { + BigQueryOptions customUniverseOptions = + BigQueryOptions.newBuilder() + .setProjectId(PROJECT) + .setServiceRpcFactory(rpcFactoryMock) + .setCredentials(NoCredentials.getInstance()) + .setUniverseDomain("custom-universe.com") + .build(); + ConnectionImpl customConnection = + (ConnectionImpl) customUniverseOptions.getService().createConnection(connectionSettings); + + BigQueryReadSettings readSettings = customConnection.createBigQueryReadSettings(); + assertEquals("custom-universe.com", readSettings.getUniverseDomain()); + assertEquals("bigquerystorage.custom-universe.com:443", readSettings.getEndpoint()); + assertEquals( + NoCredentials.getInstance(), readSettings.getCredentialsProvider().getCredentials()); + } + + @Test + void testCreateBigQueryReadSettings_leavesUniverseDomainUnsetWhenDefault() throws IOException { + BigQueryOptions defaultUniverseOptions = + BigQueryOptions.newBuilder() + .setProjectId(PROJECT) + .setServiceRpcFactory(rpcFactoryMock) + .setCredentials(NoCredentials.getInstance()) + .build(); + ConnectionImpl defaultConnection = + (ConnectionImpl) defaultUniverseOptions.getService().createConnection(connectionSettings); + + BigQueryReadSettings readSettings = defaultConnection.createBigQueryReadSettings(); + assertEquals("googleapis.com", readSettings.getUniverseDomain()); + assertEquals("bigquerystorage.googleapis.com:443", readSettings.getEndpoint()); + assertEquals( + NoCredentials.getInstance(), readSettings.getCredentialsProvider().getCredentials()); + } + + @Test + void testCreateBigQueryReadSettings_usesNoCredentialsProviderWhenCredentialsNull() + throws IOException { + BigQueryOptions nullCredentialsOptions = mock(BigQueryOptions.class); + when(nullCredentialsOptions.getCredentials()).thenReturn(null); + when(nullCredentialsOptions.getUniverseDomain()).thenReturn(null); + ConnectionImpl nullCredentialsConnection = + new ConnectionImpl( + connectionSettings, + nullCredentialsOptions, + bigqueryRpcMock, + BigQueryRetryConfig.newBuilder().build()); + + BigQueryReadSettings readSettings = nullCredentialsConnection.createBigQueryReadSettings(); + assertInstanceOf(NoCredentialsProvider.class, readSettings.getCredentialsProvider()); + assertNull(readSettings.getCredentialsProvider().getCredentials()); + assertEquals("googleapis.com", readSettings.getUniverseDomain()); + assertEquals("bigquerystorage.googleapis.com:443", readSettings.getEndpoint()); + } }