Skip to content

Commit 6299510

Browse files
committed
fix(bqjdbc): support Service Account Impersonation in ADC
1 parent 66ded32 commit 6299510

3 files changed

Lines changed: 46 additions & 3 deletions

File tree

java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryJdbcOAuthUtility.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,8 @@ static Map<String, String> parseOAuthProperties(DataSource ds, String callerClas
232232

233233
if (authType == AuthType.GOOGLE_SERVICE_ACCOUNT
234234
|| authType == AuthType.GOOGLE_USER_ACCOUNT
235-
|| authType == AuthType.PRE_GENERATED_TOKEN) {
235+
|| authType == AuthType.PRE_GENERATED_TOKEN
236+
|| authType == AuthType.APPLICATION_DEFAULT_CREDENTIALS) {
236237
oauthProperties.put(
237238
BigQueryJdbcUrlUtility.OAUTH_SA_IMPERSONATION_EMAIL_PROPERTY_NAME,
238239
ds.getOAuthSAImpersonationEmail());
@@ -284,8 +285,6 @@ static GoogleCredentials getCredentials(
284285
getPreGeneratedTokensCredentials(authProperties, overrideProperties, callerClassName);
285286
break;
286287
case APPLICATION_DEFAULT_CREDENTIALS:
287-
// This auth method doesn't support service account impersonation
288-
289288
credentials = getApplicationDefaultCredentials(callerClassName);
290289
break;
291290
case EXTERNAL_ACCOUNT_AUTH:

java-bigquery/google-cloud-bigquery-jdbc/src/test/java/com/google/cloud/bigquery/jdbc/BigQueryJdbcOAuthUtilityTest.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,39 @@ public void testParseUserImpersonationNonDefault() {
377377
result.get(BigQueryJdbcUrlUtility.OAUTH_SA_IMPERSONATION_TOKEN_LIFETIME_PROPERTY_NAME));
378378
}
379379

380+
@Test
381+
public void testParseUserImpersonationForADC() {
382+
Map<String, String> result =
383+
BigQueryJdbcOAuthUtility.parseOAuthProperties(
384+
DataSource.fromUrl(
385+
"jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;"
386+
+ "OAuthType=3;ProjectId=MyBigQueryProject;"
387+
+ "ServiceAccountImpersonationEmail=impersonated@email.com;"),
388+
"");
389+
390+
assertEquals("APPLICATION_DEFAULT_CREDENTIALS", result.get("OAuthType"));
391+
assertEquals(
392+
"impersonated@email.com",
393+
result.get(BigQueryJdbcUrlUtility.OAUTH_SA_IMPERSONATION_EMAIL_PROPERTY_NAME));
394+
}
395+
396+
@Test
397+
public void testGetServiceAccountImpersonatedCredentialsForADC() {
398+
Map<String, String> authProperties =
399+
BigQueryJdbcOAuthUtility.parseOAuthProperties(
400+
DataSource.fromUrl(
401+
"jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;"
402+
+ "OAuthType=3;ProjectId=MyBigQueryProject;"
403+
+ "ServiceAccountImpersonationEmail=impersonated@email.com;"),
404+
"");
405+
406+
GoogleCredentials credentials =
407+
BigQueryJdbcOAuthUtility.getCredentials(
408+
authProperties, java.util.Collections.EMPTY_MAP, false, null);
409+
410+
assertThat(credentials).isInstanceOf(ImpersonatedCredentials.class);
411+
}
412+
380413
@Test
381414
public void testGetServiceAccountImpersonatedCredentials() {
382415
Map<String, String> authProperties =

java-bigquery/google-cloud-bigquery-jdbc/src/test/java/com/google/cloud/bigquery/jdbc/it/ITAuthTests.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,4 +371,15 @@ public void testServiceAccountAuthenticationWithChainedImpersonation()
371371
.toString();
372372
validateConnection(connection_uri);
373373
}
374+
375+
@Test
376+
public void testADCAuthenticationWithImpersonation() throws IOException, SQLException {
377+
final JsonObject authJson = getAuthJson();
378+
379+
String connection_uri =
380+
getBaseUri(3, authJson.get("project_id").getAsString())
381+
.append("ServiceAccountImpersonationEmail", authJson.get("client_email").getAsString())
382+
.toString();
383+
validateConnection(connection_uri);
384+
}
374385
}

0 commit comments

Comments
 (0)