> ## Documentation Index
> Fetch the complete documentation index at: https://docs.jedify.com/llms.txt
> Use this file to discover all available pages before exploring further.

# BigQuery

> Connect BigQuery with no key: grant the installation's service account access, then declare the source.

Jedify reads BigQuery as the installation's own Google service account. There is no key: you grant that service account access to your data, and declare the source.

If your datasets are in another Google Cloud organization that won't grant your project's service account, see [Data in another organization](#data-in-another-organization).

## 1. Grant access

This step belongs to whoever owns the data. Jedify signs in as:

```sh theme={null}
tofu -chdir=terraform output -raw service_account_email
```

It needs four roles, and no more:

| Role | On | For |
| - | - | - |
| `roles/bigquery.jobUser` | The billing project | Running queries |
| `roles/bigquery.readSessionUser` | The billing project | Reading results |
| `roles/bigquery.resourceViewer` | The billing project | Reading the project's query history |
| `roles/bigquery.dataViewer` | Each dataset Jedify reads | Reading its tables |

**The billing project** is where Jedify's queries run and are billed, and the project whose query history it learns from. Choose the project your analysts query from: the history is then theirs. It can be one of the datasets' projects.

### With OpenTofu

If the identity that runs OpenTofu may change IAM on those projects and datasets, list the datasets in `terraform/terraform.tfvars` and apply:

```hcl theme={null}
bigquery_datasets = [
  { project = "acme-dwh", dataset = "sales" },
  { project = "acme-dwh", dataset = "marketing" },
]
```

OpenTofu grants `dataViewer` on each dataset and the other three roles on each dataset's project, so the billing project is one of them. It grants them again on every apply.

### By hand

The data's owners run these, as a user who may change IAM on the billing project and the datasets:

```sh theme={null}
export SA="acme-jedify-app@acme-jedify.iam.gserviceaccount.com"   # the service account above
export BILLING_PROJECT="acme-dwh"

for role in roles/bigquery.jobUser roles/bigquery.readSessionUser roles/bigquery.resourceViewer; do
  gcloud projects add-iam-policy-binding "$BILLING_PROJECT" \
    --member="serviceAccount:$SA" --role="$role" --condition=None
done

# Each dataset, as project.dataset:
for ds in acme-dwh.sales acme-lake.marketing; do
  bq query --use_legacy_sql=false --project_id="${ds%%.*}" \
    "GRANT \`roles/bigquery.dataViewer\` ON SCHEMA \`$ds\` TO \"serviceAccount:$SA\""
done
```

> [!WARNING]
>
> Reinstalling creates the service account again under the same address, and Google Cloud does not carry grants over to the new one. `bigquery_datasets` grants again on every apply; grants made by hand must be made again.

## 2. Declare the source

Add it to your Jedify resource, with the billing project as `projectId`, and apply:

```yaml theme={null}
spec:
  dataSources:
    - name: warehouse
      type: bigquery
      workloadIdentity:
        projectId: acme-dwh
```

### Datasets in other projects

Jedify's catalog lists the tables of projects it can list. A project where the service account holds only dataset grants usually isn't one, so its tables are missing from the catalog, although queries against them work. Name such a project in `data_projects`:

```yaml theme={null}
      workloadIdentity:
        projectId: acme-dwh
        settings: { data_projects: acme-lake }
```

`settings` takes one project. For several, use [credentials](#with-a-key) holding `{"type": "workload_identity", "project_id": "acme-dwh", "data_projects": ["acme-lake", "acme-ads"]}`.

## 3. Check it

```sh theme={null}
kubectl -n jedify get jedify jedify \
  -o jsonpath='{range .status.dataSources[*]}{.name}{"\t"}{.state}{"\t"}{.message}{"\n"}{end}'
```

Expected: `warehouse   Connected`.

## When it fails

| Message | Fix |
| - | - |
| `User does not have bigquery.jobs.create permission in project acme-dwh` | Grant the billing project's three roles, or correct `projectId`. |
| Connected, but the catalog shows no tables from a project | Add the project to `data_projects`. |
| `Permission 'iam.serviceAccounts.getAccessToken' denied` | Federation only: the pool's binding must end in `/subject/system:serviceaccount:jedify-be:jedify-be`. |
| `invalid_grant` | Federation only: the pool provider's issuer must be `tofu output -raw oidc_issuer`, and its audience `gcp-wif-jedify-be`. |

## Data in another organization

When the datasets are in another organization, its administrator can trust your cluster through a Workload Identity Federation pool instead of granting your service account. Jedify then signs in as a service account in their organization, and still no key exists.

**You**: give the application a token for Google, in your Jedify resource, and send the administrator three values:

```yaml theme={null}
spec:
  install:
    application:
      gcpWorkloadIdentity: { enabled: true }
      componentsDefaults: { gcpWorkloadIdentity: true }
```

| Value | |
| - | - |
| Issuer | `tofu -chdir=terraform output -raw oidc_issuer` |
| Audience | `gcp-wif-jedify-be` |
| Subject | `system:serviceaccount:jedify-be:jedify-be` |

**The data's administrator**: create the pool, a service account, the four grants, and a credential configuration file:

```sh theme={null}
export POOL_PROJECT="acme-lake"
export ISSUER="<the issuer you were sent>"
export PROJECT_NUMBER="$(gcloud projects describe "$POOL_PROJECT" --format='value(projectNumber)')"

gcloud iam workload-identity-pools create jedify --project="$POOL_PROJECT" --location=global
gcloud iam workload-identity-pools providers create-oidc jedify-cluster \
  --project="$POOL_PROJECT" --location=global --workload-identity-pool=jedify \
  --issuer-uri="$ISSUER" --allowed-audiences=gcp-wif-jedify-be \
  --attribute-mapping="google.subject=assertion.sub"

gcloud iam service-accounts create jedify-reader --project="$POOL_PROJECT"
gcloud iam service-accounts add-iam-policy-binding "jedify-reader@$POOL_PROJECT.iam.gserviceaccount.com" \
  --project="$POOL_PROJECT" --role=roles/iam.workloadIdentityUser \
  --member="principal://iam.googleapis.com/projects/$PROJECT_NUMBER/locations/global/workloadIdentityPools/jedify/subject/system:serviceaccount:jedify-be:jedify-be"

# Grant jedify-reader the four roles, as in step 1. Then:
gcloud iam workload-identity-pools create-cred-config \
  "projects/$PROJECT_NUMBER/locations/global/workloadIdentityPools/jedify/providers/jedify-cluster" \
  --service-account="jedify-reader@$POOL_PROJECT.iam.gserviceaccount.com" \
  --credential-source-file=/var/run/secrets/gcp-wif/token --output-file=jedify-bigquery.json
jq --arg p "acme-dwh" '. + {project_id: $p}' jedify-bigquery.json > f && mv f jedify-bigquery.json
```

The file holds no secret, so it is safe to send. Its `project_id` is the billing project.

**You**: store the file in your Secret Manager and declare the source with it:

```sh theme={null}
gcloud secrets create jedify-bigquery --data-file=jedify-bigquery.json --project=acme-jedify
```

```yaml theme={null}
spec:
  dataSources:
    - name: warehouse
      type: bigquery
      credentials:
        externalSecret:
          remoteRef: { key: jedify-bigquery }
```

## With a key

A service-account key works the same way: store the key file in Secret Manager and name it under `credentials`, as above. Add `data_projects` to the file when the catalog needs it. Prefer no key where you can: a key is a secret that can leak and has to be rotated.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.