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

# BigQuery

> How to connect to a BigQuery Resource using the Formal Connector

## Overview

The Formal Connector enables secure access to BigQuery. When domain-wide delegation is enabled, it can impersonate users in your organization, ensuring proper access control and audit logging of all BigQuery operations.

### Differences with other technologies

The connection to BigQuery differs from other technologies in two important ways:

* **No Native Users**: Unlike Postgres or HTTP, Formal native users are not supported for BigQuery currently. Instead, the connector uses [Google ADC](https://cloud.google.com/docs/authentication/application-default-credentials) to connect to BigQuery.
* **Automatic Impersonation**: When domain-wide delegation is configured, the Connector will automatically impersonate detected end users for BigQuery requests.
* **BI tool end users**: An end user named in a BI tool's SQL comment (e.g. Hex or Metabase) is used for policies and impersonation only if the caller's access token has the `https://www.googleapis.com/auth/cloud-platform` scope and the `iam.serviceAccounts.getAccessToken` and `iam.serviceAccounts.signJwt` permissions on the Connector's service account, e.g. through the [Service Account Token Creator](https://cloud.google.com/iam/docs/service-account-permissions#token-creator-role) role. Otherwise the request is attributed to the caller's own Google identity.
* **Formal users only**: The Connector rejects a request unless it can attribute it to a Formal user. That user comes from a trusted BI tool comment or the caller's Google access token. Requests with invalid tokens, or from Google users not in Formal, get a `401` and never reach BigQuery.

## Setup

The steps outlined here provide a general overview of the process. For a
complete working example that you can deploy as-is in your GKE cluster, or use
as a starting point to integrate our Connector in your existing deployment
pipeline, please refer to our [Terraform example on
GitHub](https://github.com/formalco/terraform-provider-formal/tree/main/examples/deployments/gcp/gke-bigquery-connector).

### 1. Configure Domain-wide Delegation

Before creating a BigQuery resource in Formal, you'll need a service account with domain-wide delegation enabled in your Google Workspace. This step must be done by an admin user of your Google organization.

Start by creating a service account in your Google Cloud Console if you don't have one already, then enable domain-wide delegation:

* Go to your [Google Workspace Admin Console](https://admin.google.com)
* Navigate to **Security** > **Access and data control** > **API Controls** > **Manage domain-wide delegation**.
* Click **Add New**:
  * **Client ID**: Your service account's client ID
  * **OAuth Scopes**:
    * `https://www.googleapis.com/auth/bigquery`
    * `https://www.googleapis.com/auth/cloud-platform`
* Click **Authorize**

### 2. Create a BigQuery Resource

To create a BigQuery Resource in Formal, follow these steps:

1. Navigate to the Resources page.
2. Click on the **Create Resource** button.
3. Fill in the required fields:
   * **Technology**: Select `BigQuery` from the dropdown
   * **Resource Friendly Name**: Enter a name of your choice to identify this resource
   * **Hostname**: Enter `bigquery.googleapis.com`
   * **Port**: Enter `443` if your Connector has [TLS certificates](https://docs.formal.ai/docs/guides/core-concepts/connectors/tls.md) enabled, anything else otherwise (e.g. `7777`)
4. Click **Create** to complete the resource creation.

### 3. Connect to BigQuery

The last step before querying BigQuery is to deploy your Formal Connector using your service account credentials. The connector uses [Google ADC](https://cloud.google.com/docs/authentication/application-default-credentials) to detect the service account credentials and connect to BigQuery.

Once your Formal Connector is set up, you can connect to BigQuery through the Formal Connector using the `bq` command line tool:

```bash theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
bq --api ${PROTOCOL}://${HOSTNAME}:${PORT} query "SELECT 1"
```

Use `https` or `http` for `${PROTOCOL}` depending on whether your connector
has been configured for TLS or not. Replace `${HOSTNAME}` with your actual
Formal Connector hostname and `${PORT}` with your actual listener port.

Here's another example of how to connect to BigQuery through the Formal Connector using Python:

```python theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
from google.cloud import bigquery  # requires google-cloud-bigquery

PROTOCOL = "..."  # "http" or "https", depending on your connector setup
HOSTNAME = "..."  # Your connector hostname
PORT = "..."  # Your connector listener port

client = bigquery.Client(
    client_options={"api_endpoint": f"{PROTOCOL}://{HOSTNAME}:{PORT}"}
)

query = "SELECT word FROM `bigquery-public-data.samples.shakespeare` LIMIT 10"
for row in client.query(query):
    print(row.word)
```

## Policy Evaluation

Formal supports the following policy evaluation stages for BigQuery:

* **Session**: Evaluate and enforce policies at connection time
* **Request**: Evaluate and enforce policies before query execution
* **Response**: Evaluate and enforce policies after data retrieval

## Monitoring and Logging

The Connector provides detailed logs of all BigQuery operations, including:

* Query execution
* Data access patterns
* User identity
* Evaluated / triggered policies

These logs can be accessed through the Control Plane for audit and compliance purposes.

## Troubleshooting

Common issues include:

* TLS errors: Google SDK is strict when using a TLS certificate, and will fail if it isn't signed by a trusted certificate authority.
* Service account credentials: The Formal Connector doesn't use the correct service account credentials
* Permissions: The service account does not have the necessary permissions, especially domain-wide delegation

If you feel stuck, please reach out to us on Slack!


## Related topics

- [Log Forwarding](https://docs.formal.ai/docs/guides/integrations/log.md)
- [Cluster Connectors](https://docs.formal.ai/docs/guides/core-concepts/connectors/clustering.md)
- [Connector](https://docs.formal.ai/docs/changelog/connector.md)
- [Resources](https://docs.formal.ai/docs/guides/core-concepts/resources/introduction.md)
- [List log queries](https://docs.formal.ai/api-reference/corev1logsservice/list-log-queries.md)


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