Skip to content

Commit cd82235

Browse files
fix: executeSelect now use provided credentials instead of GOOGLE_APP… (#3465)
* fix: executeSelect now use provided credentials instead of GOOGLE_APPLICATION_CREDENTIALS * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md --------- Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent 95ac790 commit cd82235

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/ConnectionImpl.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import com.google.api.core.BetaApi;
2323
import com.google.api.core.InternalApi;
24+
import com.google.api.gax.core.FixedCredentialsProvider;
2425
import com.google.api.services.bigquery.model.GetQueryResultsResponse;
2526
import com.google.api.services.bigquery.model.JobConfigurationQuery;
2627
import com.google.api.services.bigquery.model.QueryParameter;
@@ -35,6 +36,7 @@
3536
import com.google.cloud.bigquery.storage.v1.ArrowRecordBatch;
3637
import com.google.cloud.bigquery.storage.v1.ArrowSchema;
3738
import com.google.cloud.bigquery.storage.v1.BigQueryReadClient;
39+
import com.google.cloud.bigquery.storage.v1.BigQueryReadSettings;
3840
import com.google.cloud.bigquery.storage.v1.CreateReadSessionRequest;
3941
import com.google.cloud.bigquery.storage.v1.DataFormat;
4042
import com.google.cloud.bigquery.storage.v1.ReadRowsRequest;
@@ -955,7 +957,12 @@ BigQueryResult highThroughPutRead(
955957

956958
try {
957959
if (bqReadClient == null) { // if the read client isn't already initialized. Not thread safe.
958-
bqReadClient = BigQueryReadClient.create();
960+
BigQueryReadSettings settings =
961+
BigQueryReadSettings.newBuilder()
962+
.setCredentialsProvider(
963+
FixedCredentialsProvider.create(bigQueryOptions.getCredentials()))
964+
.build();
965+
bqReadClient = BigQueryReadClient.create(settings);
959966
}
960967
String parent = String.format("projects/%s", destinationTable.getProject());
961968
String srcTable =

google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITBigQueryTest.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3249,6 +3249,57 @@ public void testExecuteSelectDefaultConnectionSettings() throws SQLException {
32493249
assertEquals(42, bigQueryResult.getTotalRows());
32503250
}
32513251

3252+
@Test
3253+
public void testExecuteSelectWithCredentials() throws SQLException {
3254+
// This test validate that executeSelect uses the same credential provided by the BigQuery
3255+
// object used to create the Connection client.
3256+
// This is done the following scenarios:
3257+
// 1. Validate that setting a valid credential executes the query.
3258+
// 2. Validate that setting an invalid credential causes failure.
3259+
3260+
// Scenario 1.
3261+
// Create a new bigQuery object but explicitly set the credentials.
3262+
RemoteBigQueryHelper bigqueryHelper = RemoteBigQueryHelper.create();
3263+
BigQueryOptions bigQueryOptions =
3264+
bigqueryHelper
3265+
.getOptions()
3266+
.toBuilder()
3267+
.setCredentials(bigquery.getOptions().getCredentials())
3268+
.build();
3269+
BigQuery bigQueryGoodCredentials = bigQueryOptions.getService();
3270+
3271+
ConnectionSettings connectionSettings =
3272+
ConnectionSettings.newBuilder()
3273+
.setJobTimeoutMs(10L) // Force non-fast query to use BigQueryReadClient.
3274+
.setDefaultDataset(DatasetId.of(DATASET))
3275+
.build();
3276+
Connection connectionGoodCredentials =
3277+
bigQueryGoodCredentials.createConnection(connectionSettings);
3278+
String query =
3279+
"SELECT * FROM "
3280+
+ TABLE_ID_LARGE.getTable(); // Large query result is needed to use BigQueryReadClient.
3281+
BigQueryResult bigQueryResult = connectionGoodCredentials.executeSelect(query);
3282+
assertEquals(313348, bigQueryResult.getTotalRows());
3283+
3284+
// Scenario 2.
3285+
// Create a new bigQuery object but explicitly an invalid credential.
3286+
BigQueryOptions bigQueryOptionsBadCredentials =
3287+
bigqueryHelper
3288+
.getOptions()
3289+
.toBuilder()
3290+
.setCredentials(loadCredentials(FAKE_JSON_CRED_WITH_GOOGLE_DOMAIN))
3291+
.build();
3292+
BigQuery bigQueryBadCredentials = bigQueryOptionsBadCredentials.getService();
3293+
Connection connectionBadCredentials =
3294+
bigQueryBadCredentials.createConnection(connectionSettings);
3295+
try {
3296+
connectionBadCredentials.executeSelect(query);
3297+
fail(); // this line should not be reached
3298+
} catch (BigQuerySQLException e) {
3299+
assertNotNull(e);
3300+
}
3301+
}
3302+
32523303
/* TODO(prasmish): replicate the entire test case for executeSelect */
32533304
@Test
32543305
public void testQueryTimeStamp() throws InterruptedException {

0 commit comments

Comments
 (0)