Score:0

Accessing BigQuery data from different project in same organization with a Service Account

ng flag

I'm having some serious trouble getting the official BigQuery client (in Python 3) to a different project in the same organization authorized from a Cloud Run service.

The Cloud Run service is in Project Main, and I'm trying to access a table in Project Other -- both of these projects are in the same organization.

The service account was a user-created service account created in Project Main (not the default one) and has all required permissions to be run as a service account in Project Main, and the service account's email was given only the "BigQuery User" role in Project Other.

In Python, I have a code snippet like this:

from google.cloud import bigquery

query_str = "SELECT * FROM `project_other.prod.table`"

bqclient = bigquery.Client()
df = bqclient.query(query_str).result().to_dataframe()

This runs inside the container deployed to Cloud Run in project main, and the deployment has the correct service account attached to it.

When triggering the script, I end up with an exception like the following:

raise self._exception google.api_core.exceptions.BadRequest: 400 Access Denied: Table
project_other:prod.table: User does not have permission to query table project_other:prod.table. at [4:13]

From what I understand, if the service account attached to the Cloud Run deployment has the required permissions, you don't have to do any fancy auth in the container as it's all "done for you" by the managed service.

Can someone please help me figure out why this is happening? The service account has iam.serviceAccounts.actAs in Project Main, but does it need those permissions in Project Other as well or is the BigQuery User role enough?

John Hanley avatar
cn flag
To query a table, you need the permission **bigquery.tables.getData**. The role **BigQuery User** has **bigquery.tables.list**. Try changing the role to **BigQuery Data Viewer** (roles/bigquery.dataViewer). https://cloud.google.com/bigquery/docs/access-control#bigquery
bandwagoner avatar
ng flag
@JohnHanley This solved the problem, thank you very much! Just to add another point that wasn't in my original question, in order to write to any tables, I also needed to give the service account the BigQuery Data Editor role. If you don't mind adding your comment as a solution, I'm happy to accept it.
Score:0
cn flag

To query a table, you need the permission bigquery.tables.getData. The role BigQuery User has bigquery.tables.list. Change the role to BigQuery Data Viewer (roles/bigquery.dataViewer).

BigQuery Predefined roles and permissions

To modify/write a table, you need the permission bigquery.tables.updateData. That permission is in the roles:

  • BigQuery Admin (roles/bigquery.admin)
  • BigQuery Data Owner (roles/bigquery.dataOwner)
  • BigQuery Data Editor (roles/bigquery.dataEditor) <- recommended role.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.