@@ -80,6 +80,12 @@ final class BigQueryJdbcOAuthUtility {
8080 + "Thank you for using JDBC Driver for Google BigQuery!\n "
8181 + "You may now close the window.</body></html>" ;
8282
83+ static final String BIGQUERY_SCOPE = "https://www.googleapis.com/auth/bigquery" ;
84+ static final String DRIVE_READONLY_SCOPE = "https://www.googleapis.com/auth/drive.readonly" ;
85+
86+ static final List <String > DEFAULT_SCOPES = Arrays .asList (BIGQUERY_SCOPE );
87+ static final List <String > DRIVE_SCOPES = Arrays .asList (BIGQUERY_SCOPE , DRIVE_READONLY_SCOPE );
88+
8389 private static final int USER_AUTH_TIMEOUT_MS = 120000 ;
8490 private static final BigQueryJdbcCustomLogger LOG =
8591 new BigQueryJdbcCustomLogger (BigQueryJdbcOAuthUtility .class .getName ());
@@ -119,15 +125,17 @@ static Map<String, String> parseOAuthProperties(DataSource ds, String callerClas
119125 oauthProperties .put (BigQueryJdbcUrlUtility .OAUTH_TYPE_PROPERTY_NAME , String .valueOf (authType ));
120126
121127 Integer reqGoogleDriveScope = ds .getRequestGoogleDriveScope ();
122- if ( reqGoogleDriveScope != null ){
123- Boolean reqGoogleDriveScopeBool = BigQueryJdbcUrlUtility .convertIntToBoolean (String .valueOf (reqGoogleDriveScope ), BigQueryJdbcUrlUtility .REQUEST_GOOGLE_DRIVE_SCOPE_PROPERTY_NAME );
128+ if (reqGoogleDriveScope != null ) {
129+ Boolean reqGoogleDriveScopeBool =
130+ BigQueryJdbcUrlUtility .convertIntToBoolean (
131+ String .valueOf (reqGoogleDriveScope ),
132+ BigQueryJdbcUrlUtility .REQUEST_GOOGLE_DRIVE_SCOPE_PROPERTY_NAME );
124133 oauthProperties .put (
125134 BigQueryJdbcUrlUtility .REQUEST_GOOGLE_DRIVE_SCOPE_PROPERTY_NAME ,
126135 String .valueOf (reqGoogleDriveScopeBool ));
127136 LOG .fine ("RequestGoogleDriveScope parsed." );
128137 }
129138
130-
131139 switch (authType ) {
132140 case GOOGLE_SERVICE_ACCOUNT :
133141 // For using a Google Service Account (OAuth Type 0)
@@ -245,7 +253,7 @@ static Map<String, String> parseOAuthProperties(DataSource ds, String callerClas
245253 BigQueryJdbcUrlUtility .OAUTH_SA_IMPERSONATION_SCOPES_PROPERTY_NAME ,
246254 ds .getOAuthSAImpersonationScopes () != null
247255 ? ds .getOAuthSAImpersonationScopes ()
248- : BigQueryJdbcUrlUtility . DEFAULT_OAUTH_SA_IMPERSONATION_SCOPES_VALUE );
256+ : BIGQUERY_SCOPE );
249257 oauthProperties .put (
250258 BigQueryJdbcUrlUtility .OAUTH_SA_IMPERSONATION_TOKEN_LIFETIME_PROPERTY_NAME ,
251259 ds .getOAuthSAImpersonationTokenLifetime () != null
@@ -379,11 +387,11 @@ private static GoogleCredentials getGoogleServiceAccountCredentials(
379387 builder .setUniverseDomain (
380388 overrideProperties .get (BigQueryJdbcUrlUtility .UNIVERSE_DOMAIN_OVERRIDE_PROPERTY_NAME ));
381389 }
382- if ("true" . equals ( authProperties . get ( BigQueryJdbcUrlUtility . REQUEST_GOOGLE_DRIVE_SCOPE_PROPERTY_NAME ))){
383- builder . setScopes (
384- Arrays . asList (
385- "https://www.googleapis.com/auth/bigquery" ,
386- "https://www.googleapis.com/auth/drive.readonly" ) );
390+ if ("true"
391+ . equals (
392+ authProperties . get (
393+ BigQueryJdbcUrlUtility . REQUEST_GOOGLE_DRIVE_SCOPE_PROPERTY_NAME ))) {
394+ builder . setScopes ( DRIVE_SCOPES );
387395 LOG .fine ("Added Google Drive read-only scope to Service Account builder." );
388396 }
389397 } catch (URISyntaxException | IOException e ) {
@@ -418,11 +426,12 @@ static UserAuthorizer getUserAuthorizer(
418426 userAuthorizerBuilder .setTokenServerUri (
419427 new URI (overrideProperties .get (BigQueryJdbcUrlUtility .OAUTH2_TOKEN_URI_PROPERTY_NAME )));
420428 }
421- List <String > scopes = new ArrayList <>();
422- scopes .add ("https://www.googleapis.com/auth/bigquery" );
429+ List <String > scopes = new java .util .ArrayList <>(DEFAULT_SCOPES );
423430
424- if ("true" .equals (authProperties .get (BigQueryJdbcUrlUtility .REQUEST_GOOGLE_DRIVE_SCOPE_PROPERTY_NAME ))) {
425- scopes .add ("https://www.googleapis.com/auth/drive.readonly" );
431+ if ("true"
432+ .equals (
433+ authProperties .get (BigQueryJdbcUrlUtility .REQUEST_GOOGLE_DRIVE_SCOPE_PROPERTY_NAME ))) {
434+ scopes .add (DRIVE_READONLY_SCOPE );
426435 LOG .fine ("Added Google Drive read-only scope to User Account builder." );
427436 }
428437
@@ -501,22 +510,19 @@ private static GoogleCredentials getPreGeneratedAccessTokenCredentials(
501510 }
502511
503512 LOG .info ("Connection established. Auth Method: Pre-generated Access Token." );
504- GoogleCredentials credentials = builder
505- .setAccessToken (
506- AccessToken .newBuilder ()
507- .setTokenValue (
508- authProperties .get (BigQueryJdbcUrlUtility .OAUTH_ACCESS_TOKEN_PROPERTY_NAME ))
509- .build ())
510- .build ();
511-
512-
513- if ("true" .equals (authProperties .get (BigQueryJdbcUrlUtility .REQUEST_GOOGLE_DRIVE_SCOPE_PROPERTY_NAME ))) {
514- credentials = credentials .createScoped (
515- Arrays .asList (
516- "https://www.googleapis.com/auth/bigquery" ,
517- "https://www.googleapis.com/auth/drive.readonly"
518- )
519- );
513+ GoogleCredentials credentials =
514+ builder
515+ .setAccessToken (
516+ AccessToken .newBuilder ()
517+ .setTokenValue (
518+ authProperties .get (BigQueryJdbcUrlUtility .OAUTH_ACCESS_TOKEN_PROPERTY_NAME ))
519+ .build ())
520+ .build ();
521+
522+ if ("true"
523+ .equals (
524+ authProperties .get (BigQueryJdbcUrlUtility .REQUEST_GOOGLE_DRIVE_SCOPE_PROPERTY_NAME ))) {
525+ credentials = credentials .createScoped (DRIVE_SCOPES );
520526 }
521527
522528 return credentials ;
@@ -567,19 +573,17 @@ static UserCredentials getPreGeneratedRefreshTokenCredentials(
567573
568574 UserCredentials userCredentials = userCredentialsBuilder .build ();
569575
570- if ("true" .equals (authProperties .get (BigQueryJdbcUrlUtility .REQUEST_GOOGLE_DRIVE_SCOPE_PROPERTY_NAME ))) {
571- userCredentials = (UserCredentials ) userCredentials .createScoped (
572- Arrays .asList (
573- "https://www.googleapis.com/auth/bigquery" ,
574- "https://www.googleapis.com/auth/drive.readonly"
575- )
576- );
576+ if ("true"
577+ .equals (
578+ authProperties .get (BigQueryJdbcUrlUtility .REQUEST_GOOGLE_DRIVE_SCOPE_PROPERTY_NAME ))) {
579+ userCredentials = (UserCredentials ) userCredentials .createScoped (DRIVE_SCOPES );
577580 }
578581 LOG .info ("Connection established. Auth Method: Pre-generated Refresh Token." );
579582 return userCredentials ;
580583 }
581584
582- private static GoogleCredentials getApplicationDefaultCredentials (Map <String , String > authProperties , String callerClassName ) {
585+ private static GoogleCredentials getApplicationDefaultCredentials (
586+ Map <String , String > authProperties , String callerClassName ) {
583587 LOG .finest ("++enter++\t " + callerClassName );
584588 try {
585589 GoogleCredentials credentials = GoogleCredentials .getApplicationDefault ();
@@ -595,13 +599,11 @@ private static GoogleCredentials getApplicationDefaultCredentials(Map<String, St
595599 "Connection established. Auth Method: Application Default Credentials, Principal: %s." ,
596600 principal );
597601
598- if ("true" .equals (authProperties .get (BigQueryJdbcUrlUtility .REQUEST_GOOGLE_DRIVE_SCOPE_PROPERTY_NAME ))) {
599- credentials = credentials .createScoped (
600- Arrays .asList (
601- "https://www.googleapis.com/auth/bigquery" ,
602- "https://www.googleapis.com/auth/drive.readonly"
603- )
604- );
602+ if ("true"
603+ .equals (
604+ authProperties .get (
605+ BigQueryJdbcUrlUtility .REQUEST_GOOGLE_DRIVE_SCOPE_PROPERTY_NAME ))) {
606+ credentials = credentials .createScoped (DRIVE_SCOPES );
605607 LOG .fine ("Added Google Drive read-only scope to ADC credentials." );
606608 }
607609
@@ -652,23 +654,22 @@ private static GoogleCredentials getExternalAccountAuthCredentials(
652654
653655 GoogleCredentials credentials ;
654656 if (credentialsPath != null ) {
655- credentials = ExternalAccountCredentials . fromStream (
656- Files .newInputStream (Paths .get (credentialsPath )));
657+ credentials =
658+ ExternalAccountCredentials . fromStream ( Files .newInputStream (Paths .get (credentialsPath )));
657659 } else if (jsonObject != null ) {
658- credentials = ExternalAccountCredentials .fromStream (
659- new ByteArrayInputStream (jsonObject .toString ().getBytes ()));
660+ credentials =
661+ ExternalAccountCredentials .fromStream (
662+ new ByteArrayInputStream (jsonObject .toString ().getBytes ()));
660663 } else {
661664 throw new IllegalArgumentException (
662665 "Insufficient info provided for external authentication" );
663666 }
664667
665- if ("true" .equals (authProperties .get (BigQueryJdbcUrlUtility .REQUEST_GOOGLE_DRIVE_SCOPE_PROPERTY_NAME ))) {
666- credentials = credentials .createScoped (
667- Arrays .asList (
668- "https://www.googleapis.com/auth/bigquery" ,
669- "https://www.googleapis.com/auth/drive.readonly"
670- )
671- );
668+ if ("true"
669+ .equals (
670+ authProperties .get (
671+ BigQueryJdbcUrlUtility .REQUEST_GOOGLE_DRIVE_SCOPE_PROPERTY_NAME ))) {
672+ credentials = credentials .createScoped (DRIVE_SCOPES );
672673 LOG .fine ("Added Google Drive read-only scope to External Account credentials." );
673674 }
674675
@@ -706,9 +707,11 @@ private static GoogleCredentials getServiceAccountImpersonatedCredentials(
706707 .get (BigQueryJdbcUrlUtility .OAUTH_SA_IMPERSONATION_SCOPES_PROPERTY_NAME )
707708 .split ("," )));
708709
709- if ("true" .equals (authProperties .get (BigQueryJdbcUrlUtility .REQUEST_GOOGLE_DRIVE_SCOPE_PROPERTY_NAME ))) {
710- if (!impersonationScopes .contains ("https://www.googleapis.com/auth/drive.readonly" )) {
711- impersonationScopes .add ("https://www.googleapis.com/auth/drive.readonly" );
710+ if ("true"
711+ .equals (
712+ authProperties .get (BigQueryJdbcUrlUtility .REQUEST_GOOGLE_DRIVE_SCOPE_PROPERTY_NAME ))) {
713+ if (!impersonationScopes .contains (DRIVE_READONLY_SCOPE )) {
714+ impersonationScopes .add (DRIVE_READONLY_SCOPE );
712715 LOG .fine ("Added Google Drive read-only scope to impersonation scopes." );
713716 }
714717 }
0 commit comments