> ## 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.

# IAM roles and permissions

> The IAM roles and APIs that bqto and bqto Pro need, the commands to grant them, a minimal custom role, and IAM conditions that limit the grant to bastions.

bqto acts as you. It uses your `gcloud` login and your Application Default Credentials, never a
service account of its own. This page lists what your account needs.

Two projects matter:

* Service project: where the instance lives. bqto creates the bastion here.
* Network project: the project that owns the VPC network. It is the same project, unless you
  use Shared VPC. With Shared VPC, it is the host project.

## Roles

| Where | Role | Used for |
| - | - | - |
| Service project | `roles/compute.instanceAdmin.v1` | Creating and deleting the bastion |
| Service project | `roles/iap.tunnelResourceAccessor` | Opening the IAP tunnel |
| Service project | `roles/compute.osLogin` | Logging in to the bastion with OS Login |
| Service project, as the quota project | `roles/serviceusage.serviceUsageConsumer` | Letting Google check API enablement and quota there ([below](#which-project-google-checks-apis-and-quota-in)) |
| Service project | The viewer role of the service: `roles/cloudsql.viewer`, `roles/alloydb.viewer`, `roles/redis.viewer`, or `roles/memorystore.viewer` (Valkey) | Finding the instance and its private IP |
| Network project | `roles/compute.networkViewer` | Choosing a subnet and checking the firewall |
| Network project, on the subnet | `roles/compute.networkUser` | Placing the bastion in a Shared VPC subnet |
| Organization (optional) | `roles/browser` | The list of organizations and folders in the wizard |

### Roles for bqto Pro

> [!NOTE]
>
> These roles are for bqto Pro features.

| Where | Role | Used for |
| - | - | - |
| Service project | `roles/managedkafka.viewer` or `roles/run.viewer` | Finding the Managed Kafka cluster or the Cloud Run service |
| Cloud Run service | `roles/run.invoker` | Calling a service that requires authentication |
| Image project, only with `--image` | `roles/compute.imageUser` | Creating the bastion from an image in another project |

### roles/editor is not enough

`roles/editor` does not include `iap.tunnelInstances.accessViaIAP`, so the tunnel fails. Add
`roles/iap.tunnelResourceAccessor`. bqto checks this before it creates the bastion, and prints the
command to grant it.

If the project does not exist or you cannot see it, bqto stops and suggests
`gcloud projects list`.

## APIs

bqto calls the following APIs, so you must enable them in the service project:

| API | Used by |
| - | - |
| `compute.googleapis.com` | bqto: the bastion VM, subnets, and firewall checks |
| `cloudresourcemanager.googleapis.com` | bqto: the project lookup, the IAP permission check, and the project list of the wizard |
| `sqladmin.googleapis.com`, `alloydb.googleapis.com`, `redis.googleapis.com`, or `memorystore.googleapis.com` | bqto: finding the Cloud SQL, AlloyDB, Redis, or Valkey instance |

If one of them is disabled, bqto prints the `gcloud services enable` command as its `Fix:` line.

Also enable `iap.googleapis.com` and `oslogin.googleapis.com`. gcloud uses IAP for the tunnel, and
the OS Login API for the short-lived SSH certificate. If one of them is disabled and gcloud needs
it, the tunnel fails. bqto then shows the line of gcloud under `last stderr:`.

```sh theme={null}
gcloud services enable compute.googleapis.com cloudresourcemanager.googleapis.com \
  iap.googleapis.com oslogin.googleapis.com sqladmin.googleapis.com --project=PROJECT
```

Replace `sqladmin.googleapis.com` with the API of your service.

## Which project Google checks APIs and quota in

Google checks that an API is enabled, and counts its quota, in a **quota project**. As `gcloud`
does by default, bqto uses the project of each call:

* The lookup of the service, the IAP check, and the bastion use the service project. This is also
  true for the calls on a Shared VPC host project.
* In a bqto Pro stack, each service uses its own project for its lookup. The IAP check and the
  bastion use the project of the first profile.
* The project list of the wizard uses your gcloud project (`gcloud config get project`).

That project needs the Compute Engine API, the Cloud Resource Manager API, and the API of the
service. You need `roles/serviceusage.serviceUsageConsumer` on it. Owner and Editor include it.

If that project refuses a call, bqto sends the call again with the quota project of your ADC login
(set by `gcloud auth application-default login`). The project can refuse a call because you lack
that role, or because the API is disabled there.

The second call works if that project has the API and you have the role on it. If both projects
refuse the call, the error names the project of the call and the fix.

This second call does not happen in two cases:

* `GOOGLE_CLOUD_QUOTA_PROJECT` is set: every call uses that project. It always wins.
* ADC is a service account (a key, impersonation, or the metadata server): the default of the
  Google client libraries applies, which is the quota project in the ADC file if it has one.

## How to grant them

Set the project, and who gets the roles: a developer or a service account.

```sh theme={null}
PROJECT=my-project
MEMBER=user:dev@example.com
# or: MEMBER=serviceAccount:bqto@my-project.iam.gserviceaccount.com
```

Service project. Use the viewer role of your service:

```sh theme={null}
gcloud projects add-iam-policy-binding $PROJECT --member=$MEMBER --role=roles/compute.instanceAdmin.v1 --condition=None
gcloud projects add-iam-policy-binding $PROJECT --member=$MEMBER --role=roles/iap.tunnelResourceAccessor --condition=None
gcloud projects add-iam-policy-binding $PROJECT --member=$MEMBER --role=roles/compute.osLogin --condition=None
gcloud projects add-iam-policy-binding $PROJECT --member=$MEMBER --role=roles/cloudsql.viewer --condition=None
```

`--condition=None` makes the grant unconditional. gcloud asks for this flag when the project has a
conditional grant.

Network project, which is the same project unless you use Shared VPC:

```sh theme={null}
NETWORK_PROJECT=my-host-project
gcloud projects add-iam-policy-binding $NETWORK_PROJECT --member=$MEMBER --role=roles/compute.networkViewer --condition=None
```

Shared VPC only, on the subnet where bqto places the bastion:

```sh theme={null}
gcloud compute networks subnets add-iam-policy-binding SUBNET --region=REGION --project=$NETWORK_PROJECT --member=$MEMBER --role=roles/compute.networkUser
```

bqto Pro, Cloud Run, only for a service that requires authentication:

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

bqto Pro, only with `--image`, when the image lives in a project of your own. The default Debian
image is public and needs nothing.

```sh theme={null}
gcloud projects add-iam-policy-binding IMAGE_PROJECT --member=$MEMBER --role=roles/compute.imageUser --condition=None
```

The quota project. It is the service project, unless bqto falls back to your ADC quota project or
you set `GOOGLE_CLOUD_QUOTA_PROJECT`:

```sh theme={null}
gcloud projects add-iam-policy-binding QUOTA_PROJECT --member=$MEMBER --role=roles/serviceusage.serviceUsageConsumer --condition=None
```

Organization, optional. Without it, the wizard still lists your projects, but shows folder numbers
instead of names.

```sh theme={null}
gcloud organizations add-iam-policy-binding ORG_ID --member=$MEMBER --role=roles/browser --condition=None
```

## Run as a service account

bqto checks that ADC and `gcloud` use the same account. To run as a service account without a key,
impersonate it in both. You need `roles/iam.serviceAccountTokenCreator` on it:

```sh theme={null}
SA=bqto@my-project.iam.gserviceaccount.com
gcloud config set auth/impersonate_service_account $SA
gcloud auth application-default login --impersonate-service-account $SA
```

* A new grant of that role can take a few minutes to apply. Until then, bqto says that your account
  cannot impersonate the service account.
* The `gcloud config` line applies to every gcloud command, not only bqto. To go back to your own
  account, run `gcloud config unset auth/impersonate_service_account`.
* With a key file instead, run `gcloud auth activate-service-account --key-file=KEY_FILE`, and
  point `GOOGLE_APPLICATION_CREDENTIALS` at the same file.
* Workload Identity Federation, as a GitHub Actions workflow gets it, works the same way as
  impersonation.

For Cloud Run, bqto Pro makes the identity token as that service account, for the URL of the
service, in `--web` and in the `try:` line. The account needs `roles/run.invoker` on the service.

## A minimal custom role

`roles/compute.instanceAdmin.v1` is broad. It can manage every VM in the project, and it gives sudo
on all of them through OS Login. This custom role for the service project holds only what bqto
uses. Save it as `bqto-role.yaml`, and keep the lines of the services that you connect to.

```yaml theme={null}
title: bqto user
description: What bqto needs in the service project
stage: GA
includedPermissions:
- resourcemanager.projects.get
- compute.projects.get
- compute.zones.list
- compute.networks.list
- compute.zoneOperations.get
- compute.instances.get
- compute.instances.list
- compute.instances.create
- compute.instances.delete
- compute.disks.create
- compute.instances.setMetadata
- compute.instances.setLabels
- compute.instances.setTags
- compute.instances.osLogin
- iap.tunnelInstances.accessViaIAP
# Cloud SQL
- cloudsql.instances.list
- cloudsql.instances.get
# AlloyDB
- alloydb.instances.list
- alloydb.instances.get
- alloydb.clusters.get
# Memorystore for Redis
- redis.instances.list
- redis.instances.get
# Memorystore for Valkey
- memorystore.instances.list
- memorystore.instances.get
# bqto Pro, Memorystore for Redis Cluster
- redis.clusters.list
- redis.clusters.get
# bqto Pro, Managed Kafka
- managedkafka.clusters.list
- managedkafka.clusters.get
# bqto Pro, Cloud Run
- run.services.list
- run.services.get
# bqto Pro, only with --image, when the image is in this project:
- compute.images.get
- compute.images.getFromFamily
- compute.images.useReadOnly
# Only when the network is in this project (no Shared VPC):
- compute.networks.get
- compute.networks.getEffectiveFirewalls
- compute.networks.getRegionEffectiveFirewalls
- compute.subnetworks.list
- compute.subnetworks.get
- compute.subnetworks.use
```

```sh theme={null}
gcloud iam roles create bqtoUser --project=$PROJECT --file=bqto-role.yaml
gcloud projects add-iam-policy-binding $PROJECT --member=$MEMBER --role=projects/$PROJECT/roles/bqtoUser --condition=None
```

Without Shared VPC, this role alone replaces the service project and network project roles in the
table, IAP and OS Login included. A project-level custom role can only be granted in its own
project. With Shared VPC, keep the predefined roles in the network project.

Bastions set `enable-oslogin-certificates=TRUE`. So `gcloud compute ssh` asks the OS Login API for
a short-lived certificate, instead of adding a key to your OS Login profile.
`roles/compute.osLogin` and this custom role already hold the permission that this call checks.
The bastion has no service account, so you also do not need `roles/iam.serviceAccountUser`.

## Narrowing the grant

### Only bastions

An IAM condition can limit `roles/compute.instanceAdmin.v1`, or the custom role, to VMs and disks
whose names start with `bqto-`. List every zone of the region, and cover both instances and disks,
because the boot disk takes the name of the VM. Save the condition as `bqto-condition.yaml`, with your project
ID in place of `P`:

```yaml theme={null}
title: bqto-bastions-only
expression: >-
  (resource.type == 'compute.googleapis.com/Instance' && (
    resource.name.startsWith('projects/P/zones/europe-west1-b/instances/bqto-') ||
    resource.name.startsWith('projects/P/zones/europe-west1-c/instances/bqto-') ||
    resource.name.startsWith('projects/P/zones/europe-west1-d/instances/bqto-'))) ||
  (resource.type == 'compute.googleapis.com/Disk' && (
    resource.name.startsWith('projects/P/zones/europe-west1-b/disks/bqto-') ||
    resource.name.startsWith('projects/P/zones/europe-west1-c/disks/bqto-') ||
    resource.name.startsWith('projects/P/zones/europe-west1-d/disks/bqto-'))) ||
  (resource.type != 'compute.googleapis.com/Instance' && resource.type != 'compute.googleapis.com/Disk')
```

Then grant the role with it:

```sh theme={null}
gcloud projects add-iam-policy-binding $PROJECT --member=$MEMBER --role=projects/$PROJECT/roles/bqtoUser --condition-from-file=bqto-condition.yaml
```

* A condition allows at most 12 logical operators. This one has 9, and a region with four zones
  needs 11.
* With predefined roles, put the same condition on `roles/compute.osLogin` too, so that the account can
  log in only to bastions.
* The account cannot create or log in to a VM with another name. IAP cannot filter by VM name, so a
  tunnel to such a VM still opens.

### Only SSH through IAP

IAP conditions cannot match VM names, but they can match the port. Grant
`roles/iap.tunnelResourceAccessor` with the condition `destination.port == 22`. A tunnel to any
other port of the bastion then fails with `4033: 'not authorized'`.

Google cannot check a conditional grant in advance. So bqto warns that the permission is possibly
missing, and goes on.

### No root through metadata

Creating a VM with metadata needs `compute.instances.setMetadata`. With it, anyone can set
`enable-oslogin=FALSE` and an SSH key on a bastion, and the guest agent gives that key sudo. They
can also set a `startup-script` that runs as root when the VM boots again.

The organization policy `compute.requireOsLogin` makes OS Login apply, whatever the instance
metadata says. The startup script needs a reboot. So also leave out `compute.instances.reset`,
`stop`, and `start`, as the minimal custom role does. This comes from Google's documentation and was
not tested live.

```sh theme={null}
gcloud resource-manager org-policies enable-enforce compute.requireOsLogin --project $PROJECT
```

### Only for a while

Privileged Access Manager can grant the role for the length of a session. It works with predefined
and custom roles, but not with Owner or Editor.


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