# Bots

> Team-owned bots, limited to groups and repositories, and their keys limited further by scope and binding.
A bot is an account that belongs to a **team**, not a person, for
automation that should keep working when people come and go. Every bot
has a role (**read**, **write** or **maintain**), which caps everything
its keys can do. A bot is never an administrator.

## Limiting a bot to groups and repositories

By default a bot reaches every repository of its team. You can limit it
to some groups and repositories (up to 50). A group covers everything
under it. This is a ceiling for all of the bot's keys:

- a key's bindings must sit inside the bot's resources when it is issued;
- on every request, branchd intersects the key's bindings with the bot's
  **current** resources, so narrowing a bot cuts its existing keys at once.

```sh
# Create a bot that may reach only one group
branchd bot create acme/deployer --role write --group acme/services

# Narrow it to one repository, or widen it back to the whole team
branchd bot update acme/deployer --repo acme/services/api
branchd bot update acme/deployer --whole-team
```

In the console, this is under the group's **Bots** settings.

## Bot keys

A bot key carries scopes and, optionally, bindings:

```sh
# The git preset: clone (and push, if the bot's role is above read)
branchd bot token acme/deployer --kind git

# Exactly the scopes a job needs, on one repository
branchd bot token acme/deployer --scopes forge:package:read --repo acme/services/api

# A level, never above the bot's role
branchd bot token acme/deployer --level write --group acme/services
```

| Flag                          | Meaning                                                                                    |
| ----------------------------- | ------------------------------------------------------------------------------------------ |
| `--kind git`                  | `forge:contents:read`, plus `forge:contents:edit` when the bot's role is write or maintain |
| `--kind api`                  | The API preset: the read level, or the write level for a write or maintain bot             |
| `--level LEVEL`               | `read`, `write` or `maintain`, never above the bot's role                                  |
| `--scopes S,...`              | Exactly these scopes                                                                       |
| `--repo PATH`, `--group PATH` | Bind the key to these, inside the bot's own resources. Repeatable.                         |
| `--name NAME`                 | A label for the key (default: its kind and the time)                                       |
| `--expires DURATION`          | How long until it stops working, as a duration such as `2160h` (90 days)                   |

Use one of `--kind`, `--level` or `--scopes`. Without `--repo` or
`--group`, a limited bot's key is bound to all of the bot's resources, and
a whole-team bot's key reaches the whole team.

In the API, `CreateBot` and `UpdateBot` take `resources[]`
(`{kind: GROUP or REPOSITORY, path}`; empty means the whole team, and
`update_mask` `resources` changes it). `CreateBotCredential` takes
`kind`, or `level` and/or `scopes`, and `bindings[]` with optional
per-binding scopes, each inside the key's own scopes.

## SSH deploy keys

A bot can also hold SSH public keys for git over SSH. This is branchd's
deploy key: a key that clones one repository or group (or pushes to it) and
nothing else, owned by the team rather than a person.

```sh
# Read and write on one repository (the bot's role must be write or maintain)
branchd bot token acme/deployer --kind ssh --key-file deploy_key.pub --repo acme/services/api

# Read only, from stdin, on a group
cat ci_key.pub | branchd bot token acme/deployer --kind ssh --key-file - --level read --group acme/services
```

- branchd stores only the public key, so nothing secret is printed. The
  command prints the key's fingerprint, as `ssh-keygen -l` shows it.
- `--level read` makes the key clone and fetch only. Without it, a write
  or maintain bot's key can also push, and branch rules still apply.
- `--repo`, `--group` and `--expires` work as for any bot key. A key can
  never reach past the bot's role or resources, and narrowing or disabling
  the bot narrows or stops its keys at once.
- A key can belong to only one person or bot. RSA keys under 3072 bits are
  refused.
- Clone with `ssh://git@branchd.run:2222/<repo>.git`.
- `branchd bot show` lists a bot's SSH keys by fingerprint, and `branchd bot
  revoke <team>/<name> <id>` removes one, after which the same key can be
  added again.

In the console, this is the **SSH deploy keys** section of a bot's page. In
the API, `CreateBotCredential` takes `kind: SSH` with `public_key`, and
optionally `level` (`read` or `write`) and `bindings[]`.

Source: https://docs.branchd.run/branchd/access/bots
