google-auth-library-python: Permission denied while getting drive credentials: ADC with impersonation

I consider myself pretty familiar with the various google auth flows available via the python SDK - and how this interacts with gcloud-generated credentials.

We are using the bq SDK in the typical way; client = bigquery.Client() and we make use of ADC so our code is interoperable between dev and prod. Our code interacts with external tables that are sourced from sheets on google drive. We know that we need to provide the necessary scopes (and of course, permission to the underlying sheets).

The following works fine for a user identity with the necessary permissions: gcloud auth application-default login --scopes=https://www.googleapis.com/auth/drive,https://www.googleapis.com/auth/cloud-platform

However, the following does not: gcloud auth application-default login --scopes=https://www.googleapis.com/auth/drive,https://www.googleapis.com/auth/cloud-platform --impersonate-service-account=hand-of-god@mlg-apollo-data-prod.iam.gserviceaccount.com

We receive google.api_core.exceptions.Forbidden: 403 Access Denied: BigQuery BigQuery: Permission denied while getting Drive credentials.

I can replicate the same issue with my user credential if 1 of the following 2 things are true:

  1. I don’t pass google drive scopes.
  2. I don’t have access to the underlying file.

The service account that I am impersonating definitely has access to the file and I can see the BigQuery job failure with non-descript error message (a feature request has been raised for this with the BigQuery REST API team). My suspicion is that when impersonating a service account, the scopes (that are presumably buried in the credential) are not passed through / correctly read by the SDK (WHEN the ADC was generated using SA impersonation only). Maybe a similar issue is happening with my above note when the project cannot be inferred from the environment.

See below screenshot proof of correct permissions being in place: image

Very grateful for some direction here…

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Reactions: 2
  • Comments: 19 (5 by maintainers)

Most upvoted comments

For anyone else facing this, here’s the exact code that worked for me:

from google.cloud import bigquery
client = bigquery.Client(client_options={
    "scopes": ['https://www.googleapis.com/auth/drive', 'https://www.googleapis.com/auth/cloud-platform']
})
results = client.query_and_wait(sql)

While I understand the nuance here (and how this might not be a “bug” per se), it certainly can be unexpected behavior that costs manhours. I think it’s a deeper issue with how GCP handles permissions on the server side and can’t really be addressed in the language SDKs (other than with one-off hacks as described here). My rationale: The authentication between BQ to GDrive happens on the server side. It should be up to the server side to look up the service account’s permissions, see that it can access a GSheet-backed table, know that it’s allowed to talk to Gdrive, and make the connection. The way it works now: the service account making the API call into BigQuery API needs to somehow “know” that a table is Gsheet-backed, so that it can include the right access scopes. That feels problematic to me. It’s a leaky abstraction at best.

But it sounds like GCP isn’t interested in addressing it. I guess I get it: that’s a really big change with huge implications. So for now at least we have GoogleSearch to help the next poor dev find this GH issue and get past this odd behavior.

@EricSeastrand - Man, I’ve lost almost a day with these authorizations. Thanks for sharing. You’ve got a beer from me 🍺🍺🍺