# Delegate to an Auditor

A delegation lets an auditor list, read and open the objects you choose, until a date you choose, without a vault token or your recipient key. The auditor signs every request with their own key, so a copied delegation file is useless alone. You can revoke it at any time, and every step appears in your log.

Two things are separate:

- **The delegation** lets the auditor read: list the delegated objects, read their index rows and receipts, and download their sealed bytes.
- **A grant** lets the auditor open one object. You create one per object.

## Before you start

- Your vault has an owner key. Without one you can't certify a delegation root.
- A `vaults:admin` token in `admin.token`.
- Your owner key, and your recipient key file `vault.key` to make grants.

## 1. The auditor creates a key

The auditor runs, on their own machine:

```bash
octet-vault auditor keygen --out auditor.key --label "<auditor name>"
```

This writes `auditor.key`, which the auditor keeps, and `auditor.pub.json`, which the auditor sends you. It holds only public keys.

## 2. Create and certify a delegation root

Do this once. Your owner key certifies the root key, and the root key signs every delegation from then on.

```bash
octet-vault delegation root keygen --out delegation-root.key --label "<your company> delegations"
octet-vault --token-file admin.token delegation root set --root-key delegation-root.key \
    --owner-key owner.key --allow-list allow.txt
```

With the owner key on a YubiKey, pass `--owner-piv <slot>` instead of `--owner-key owner.key`. The root may sign delegations for one year by default. Set another end with `--not-after <RFC 3339 time>`.

## 3. Create the delegation

```bash
octet-vault --token-file admin.token delegation create --root-key delegation-root.key \
    --auditor auditor.pub.json --object <object-id> \
    --until <RFC 3339 time, at most 90 days ahead> \
    --out delegation.bsc --allow-list allow.txt
```

- `--object` names an object in scope. Repeat it, or separate IDs with commas.
- `--decision-ref <ref>` puts every object of that decision in scope. Narrow it by time with `--received-from` and `--received-until`.
- `--ops` sets what the auditor may do: any of `list`, `get`, `fetch` and `grant`. The default is all four. No delegation can run a [retention challenge](/docs/vaults/concepts/integrity/#proof-of-retention).

`create` registers the delegation with your vault and prints its ID. Send `delegation.bsc` to the auditor.

## 4. Grant each object

For each delegated object, seal its opening secret to the auditor:

```bash
octet-vault --token-file admin.token grant <object-id> --key vault.key \
    --auditor auditor.pub.json --allow-list allow.txt
```

It prints the grant as `<object-id>:<grant-id>`. Send those pairs to the auditor with the delegation. A grant opens only its own object, and only with the auditor's key.

## 5. The auditor reads and opens

The auditor passes the delegation and their key after the command name, in place of a token:

```bash
octet-vault list --delegation delegation.bsc --auditor-key auditor.key

octet-vault open <object-id> --key auditor.key --grant <object-id>:<grant-id> \
    --delegation delegation.bsc --auditor-key auditor.key

octet-vault verify-log <object-id> --delegation delegation.bsc --auditor-key auditor.key
```

`list` shows only the delegated objects. `open` prints a summary of the object, and with `--json` the same evidence bundle you see when you open the object with your recipient key. `verify-log` checks the object against the log and its timestamps.

The CLI asks for the auditor key's passphrase to sign the requests. `open` asks a second time, for the same file passed as `--key`, to open the grant. To avoid the prompts, set `OCTET_VAULT_AUDITOR_PASSPHRASE` and `OCTET_VAULT_KEY_PASSPHRASE`, or pass `--auditor-passphrase-file` and `--key-passphrase-file`.

:::note[zsh]
To keep the delegation flags in a variable in zsh, use an array: `A=(--delegation delegation.bsc --auditor-key auditor.key)`, then `octet-vault list $A`. A plain string variable reaches the CLI as one argument.
:::

## Narrow a delegation before handing it on

Anyone who holds a delegation can narrow it, never widen it:

```bash
octet-vault delegation attenuate delegation.bsc --ops get,fetch,grant \
    --object <object-id> --until <earlier RFC 3339 time> --out delegation-narrow.bsc
```

A delegation narrowed to objects can't list, because `list` names no object. Narrow by object only for `get`, `fetch` and `grant`. Whoever receives a narrowed delegation also needs the auditor key file it is bound to.

To inspect a delegation file, offline and without checking its signatures, run `octet-vault delegation inspect delegation.bsc`.

## Revoke

```bash
octet-vault --token-file admin.token delegation list
octet-vault --token-file admin.token delegation revoke <delegation-id> --allow-list allow.txt
```

The auditor's next request gets `401 delegation_revoked`. To cut off one narrowed copy and keep the rest, revoke by its revocation ID: `delegation revoke --revocation-id <id>`.

A grant the auditor already fetched can't be recalled: the auditor holds that object's secret. Your log records when it was fetched.

## What you see in `monitor`

| Event | When | In `monitor` |
|---|---|---|
| `delegation_root_set` | Step 2 | Expected, through `--allow-list` |
| `delegation_issued` | Step 3 | Expected, through `--allow-list` |
| `grant_created` | Step 4 | Expected, through `--allow-list` |
| `object_read` | The auditor downloads an object's sealed bytes | Logged with the delegation as the reader |
| `grant_fetched` | The auditor fetches a grant | Always an ALERT, once per fetch |
| `delegation_revoked` | You revoke | Expected, through `--allow-list` |
