# Keys and the registry

> Optional signing: who may sign what, and how `bay enrol` adds and removes a key.

Source: https://getbay.io/docs/keys (HTML).

Keys are optional, and bay works without them. Without a registry, bay runs unsigned: every check and review record counts, and results say `trust: unsigned`. Add a registry when only enrolled keys may sign merge requests, merges and evidence. Then results say `trust: signed`, and a record signed by any other key never counts. A runtime observation counts only when a producer key signed it, so it needs a registry.

## The registry

bay keeps its keys in the registry, the Git ref `refs/bay/registry`. The registry is a chain of commits. Each commit holds up to six OpenSSH allowed-signers files: `agent`, `runner`, `producer`, `reviewer`, `approver` and `registry`. A registry without an `approver` file enrols no approver. A `removed` file lists each key taken out, with the reason and the time. `bay enrol` adds a key, and `bay enrol --remove` takes one out.

## Rules for the files

- Each line holds one key: `<principal> <keytype> <base64>`.
- Give each line exactly one principal, no options, and no `cert-authority`.
- Put each public key in exactly one file, and never enrol a key the `removed` file lists.
- bay refuses a registry of any other shape, and so does the hook on a host from `bay host`.
- The hook takes a new registry commit only as a fast-forward, signed by a key in the previous `registry` file.
- It also refuses a version that drops a line of the `removed` file.

## Reviewer keys

A reviewer key signs `review` records and a reviewer's delivery talks, and nothing else. A `review` record verifies only against the `reviewer` file. A reviewer key never signs a check record, so it never counts toward a check. A reviewer name is never also a producer, agent or approver name. bay refuses a registry that enrols a name both as a reviewer and as one of those. Set `bay.reviewerName` and `bay.reviewerKey` in Git config on the machine that writes reviews.

## Approver keys

An approver key signs only `approval` and `waiver` records, and a person's answer on a talk. An `approval` or `waiver` record verifies only against the `approver` file. An approver name is never also a producer, reviewer or agent name. bay refuses a registry that puts one name in two of these classes. An agent and a producer may share a name.

Set `bay.approverName` and `bay.approverKey` in Git config on the approver's machine. Or run `bay install --approver <name>` in the project's clone: it makes the keys, enrols the approver and sets both. Require a person's approval shows how a person approves a change.

## Sign only approvals

By default, a registry signs everything: requests, merges, change records and every evidence record. Set `signing = "approvals"` in the `[policy]` table of `.bay/policy.toml` on main to sign only approvals. Then only an `approval` or `waiver` record needs a key, one from the registry's `approver` file. Everything else runs unsigned, as without a registry, and results say `trust: approvals`.

No machine then needs an `agent`, `runner` or `producer` key. This suits a small team with no runner. bay reads the mode from main, so a change never sets the mode bay judges it by.

```toml
# .bay/policy.toml on main
[policy]
signing = "approvals"   # "all", the default, signs everything
reuse_unsigned = true   # optional: evidence reuse and batch on unsigned evidence
```

> Without a registry, bay signs nothing and no approval counts, whatever the mode says.
> `bay doctor` shows the mode in its `signing` row.

### What this mode gives up

- **Talks**: they run unsigned, and each names its actor, as a merge does. Talk rows say `signed: false`.
- **Evidence reuse and `batch`**: both stay off, unless the policy also sets `reuse_unsigned = true`. Results still say `trust: approvals`.
- **Runtime observations**: only a producer-signed record counts, so none does.
- **Live summaries and runner presence**: both run unsigned.
- **The host hook**: on a host from `bay host`, it checks signatures only on registry and evidence pushes.
- Elsewhere in these docs, "with a key registry" means the default mode, which signs everything.

## Add a key

Run `bay enrol <class> <name> <key.pub>` in a clone, on the machine that keeps the registry key. `git config bay.registryKey` names that key's private half. Never sign the registry with an agent key. bay reads the remote's registry, adds the key to its class file, signs the new version and pushes it.

The first key creates the registry. It is a `registry` key: the public half of `bay.registryKey`. A host judges no registry push until it holds one, so create the first registry yourself. bay refuses a key already enrolled, a name a rule refuses, or a remote it cannot read, and writes nothing.

```sh
git config bay.registryKey ~/.ssh/bay-registry
bay enrol registry admin ~/.ssh/bay-registry.pub   # the first key: the registry key itself
bay enrol approver alice alice.pub                 # then one line per key, by class
bay enrol agent agent-7 agent-7.pub
```

> Runner, producer, reviewer and approver names use `a-z`, `0-9` and `-`, at most 32 characters.
> Ask each person for the public key only, the `.pub` file.
> Each agent's machine sets `bay.agentKey`, and each agent runs with `BAY_ACTOR` set to its enrolled name.
> The host refuses a change that one agent signed for another actor.
> bay pushes the new version as a fast-forward, never forced, and reads it back from the remote.

### Remove a key

Run `bay enrol --remove <class> <name> --reason rotated|revoked` on the machine that keeps the registry key. bay takes every key of that name out of its class file. It adds a line for each key to the `removed` file, then signs and pushes the new version. The reason decides what happens to the records the key signed before.

```sh
bay enrol --remove agent agent-7 --reason rotated   # replaced in the ordinary course
bay enrol agent agent-7 agent-7-new.pub             # the same name, with a new key
bay enrol --remove agent agent-9 --reason revoked   # the key may be in other hands
```

| Reason | When to use it | The key's records |
|---|---|---|
| `rotated` | You replace the key in the ordinary course. | A record it signed before the removal still counts. bay goes by the time written in the record. |
| `revoked` | The key may be in someone else's hands. | No record it signed counts, old ones too, wherever bay reads it. |

> bay never removes the last `registry` key: nobody could sign the registry after it.
> Enrol another one first.
> `bay enrol` refuses a removed key under any name, before it signs or pushes anything.
> A registry that enrols a removed key again does not load, and `bay doctor` is red with `REGISTRY_UNREADABLE`.
> Repair it with `bay enrol --remove <class> <name> --reason revoked`, for the name that holds the key.
> bay refuses a name with no key in that class, and then writes nothing.

## When a change takes effect

- On a main branch from `bay host`, a removal applies to every later push. Elsewhere, use the host's branch protection.
- bay judges a request by the registry version it names. That version must exist when the agent pushes the request.
- bay's own checks read the current registry, so they apply the change after the next fetch.
- Plain `git verify-commit` keeps the old list until `bay install` runs again on that machine.
- So run `bay install` on each machine where people verify commits with plain Git.

## Keep signing keys out of agent reach

`bay doctor` has a `key-custody` row. It judges key files on Unix only, and says `not checked` elsewhere. It judges the keys that `bay.runnerKey`, `bay.producerKey`, `bay.reviewerKey`, `bay.approverKey`, `bay.registryKey` and `bay.releaseKey` name in Git config. It also judges `bay.gateProducerKey`, the key of bay's own release gate, which you do not set.

A file key this account can read is red with `KEY_EXPOSED` when agent sessions run under this account. With such a key, passwordless `sudo` on the account makes the row red too. A hardware key, such as `ed25519-sk`, stays green. bay knows one by the `sk-` type in its `.pub` file. An agent key belongs to the session account, so the row only lists it.

```sh
ssh-keygen -t ed25519-sk -f ~/.ssh/bay-runner     # signs only with a touch
```

> For keys, the row reads paths, modes, owners and public halves only.
> It never opens a private key.
> A token file beside a key turns the row red too, even beside a hardware key.

### Know who signed

The `key-custody` row lists `actors`: the agents named in this repository's session records that this account wrote. `actor_proof` says what a signature tells you about who signed. It is `key` when `bay.agentKey` names a key, and `none` when no agent key is set. It is `account` when several actors share this account and it can read their file agent key. Then any of them can sign as another: the row is `degraded`, unless an exposed key turns it red. `actor_note` names the fix: an OS account for each actor, or a hardware agent key.

### Refuse merges from a shared agent key

Set `[agent] require_actor_proof = true` in `.bay/policy.toml` on main to act on `actor_proof`. Then `bay merge` refuses with `ACTOR_UNPROVEN` when `actor_proof` is `account`, before it signs anything. It also refuses when bay cannot judge the key, such as a file agent key off Unix. With no agent key, `actor_proof` is `none`, and the setting refuses nothing.

bay reads the setting from main, so a change cannot lift it for itself. It is off by default. The `key-custody` row shows `require_actor_proof`, and `actor_refusal` when bay refuses merges on this machine.

```toml
[agent]
require_actor_proof = true   # .bay/policy.toml on main
```
