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

# Cloud Run

> Reach a Cloud Run service with internal ingress through a bqto Pro SOCKS5 proxy, or open it in a browser with the local web proxy that --web starts.

> [!NOTE]
>
> This needs bqto Pro.

bqto Pro reaches Cloud Run services with internal ingress.

## Connect

```sh theme={null}
bqto connect --service cloudrun --project my-project --instance europe-west1/app --web
```

`--instance` takes the region and the service name, as `REGION/NAME`.

A connection to Cloud Run goes through a SOCKS5 proxy on the bastion. bqto prints the proxy instead
of a port. It also prints the command that uses the proxy.

## Private Google Access

Cloud Run needs Private Google Access on the subnet of the bastion. bqto checks for it. To turn it
on, run:

```sh theme={null}
gcloud compute networks subnets update SUBNET --project PROJECT \
  --region REGION --enable-private-ip-google-access
```

## Open the service in a browser

A browser cannot use the SOCKS5 proxy, and it cannot add your identity token. So a service that
requires authentication answers 403.

`--web` starts a local HTTP reverse proxy that adds your gcloud identity token to each request.
The proxy asks for a secret that bqto makes for each session. The `web:` line starts with the
address to open. This address holds the secret:

```text theme={null}
web: http://127.0.0.1:8080/?bqto_token=SECRET → https://app-123.europe-west1.run.app (adds your gcloud identity token; the link holds this session's secret)
```

The first visit stores the secret in an HttpOnly, SameSite=Strict cookie. Then it loads the same
page without the secret in the address. A request that has neither the cookie nor the
`X-Bqto-Token` header gets 403. This rule also applies to websockets. Treat the secret as a password for the length of the session.

Two limits remain:

* Browsers do not separate cookies by port. Your browser also sends the cookie to any other server
  on `127.0.0.1` that it visits, and another user on the machine can run such a server. On a
  shared machine, open the proxy in a browser profile that you use for nothing else.
* The first entry in the browser history keeps `?bqto_token=SECRET`. The secret stops working
  when the session ends.

## In scripts

With `--output json`, the ready event has `web`, the address of the proxy, and `web_token`, the
secret.

With `-- CMD`, the command gets these variables:

| Variable | Value |
| - | - |
| `ALL_PROXY`, `HTTPS_PROXY` | `socks5h://127.0.0.1:PORT` |
| `NO_PROXY` | `127.0.0.1,localhost`, so that local addresses skip the proxy |
| `BQTO_WEB_URL` | With `--web`, the address of the proxy |
| `BQTO_WEB_TOKEN` | With `--web`, the secret |

Other users of the machine can read a command line. To send the secret outside the command line,
use bash or zsh:

```sh theme={null}
curl -H @<(printf 'X-Bqto-Token: %s\n' "$BQTO_WEB_TOKEN") "$BQTO_WEB_URL/"
```

The `<(...)` form needs bash or zsh. `sh` and fish do not have it.

## What it needs

| Where | Role | Used for |
| - | - | - |
| Service project | `roles/run.viewer` | Finding the Cloud Run service |
| Cloud Run service | `roles/run.invoker` | Calling a service that requires authentication |

To grant `roles/run.invoker` on one service:

```sh theme={null}
gcloud run services add-iam-policy-binding SERVICE --region=REGION --project=$PROJECT --member=$MEMBER --role=roles/run.invoker
```

When bqto Pro runs as a service account, it makes the identity token as that service account, for
the URL of the service. The account then needs `roles/run.invoker` on the service.
[Permissions](https://docs.bqto.dev/reference/permissions.md) has every role.


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