# Provisioning keys

A provisioning key lets a partner platform create its own Futex tenants and the keys inside them. It reaches only the tenants it created or was given by the Futex operator. It cannot read decisions, tasks, policies or audit records in any tenant, including its own.

The key's principal holds one permission, `hitl:platform:provision`, granted in the Futex master auth namespace. Any other Futex route answers 403 to it.

## Issuing one (Futex operator)

```
POST /v1/platform/provisioners          platform admin key
{"principal_id": "casebound-provisioner-test", "name": "casebound test jobs"}
```

The response carries the key once, in `api_key`. Futex refuses with 409 `principal_holds_other_platform_permissions` when the principal already holds any other platform permission. Issue one key per environment, each with its own principal. The tenants one key owns are invisible to the others.

The operator can hand an existing tenant to a provisioning principal, or take it back with `"owner_id": null`:

```
PUT /v1/platform/tenants/{tenant_id}/owner   platform admin key
{"owner_id": "casebound-provisioner-test"}
```

Provisioning keys appear in `GET /v1/platform/api-keys` and are revoked with `DELETE /v1/platform/api-keys/{key_id}`.

## Routes for the provisioning key

| Route | Body | Answers |
|---|---|---|
| `POST /v1/provisioning/tenants` | `{"name": "cb-test-…"}` | 200 `{id, name, status, owner_id}`; 409 `tenant_name_exists` with `details.tenant_id` |
| `GET /v1/provisioning/tenants?name=&limit=&offset=` | | the key's own tenants, closed ones included |
| `DELETE /v1/provisioning/tenants/{id}` | | 200 `{id, status: "closed", revoked_keys}`; 409 `open_decisions` with `details.open_decisions` |
| `POST /v1/provisioning/tenants/{id}/members` | `{"principal_id", "role"}` | role is `policy_author`, `agent_operator` or `reviewer`, plus any extra role your key was given; 422 lists the roles your key may grant |
| `GET /v1/provisioning/tenants/{id}/members` | | |
| `POST /v1/provisioning/tenants/{id}/api-keys` | `{"principal_id", "name"?, "policy_allowlist"?}` | 200 with `api_key` shown once; 422 if the principal has no role in the tenant |
| `GET /v1/provisioning/tenants/{id}/api-keys` | | no secrets |
| `DELETE /v1/provisioning/tenants/{id}/api-keys/{key_id}` | | 200 `{id, status: "revoked"}` |

A tenant name is 1 to 128 characters from `A-Z a-z 0-9 . _ : -` and starts with a letter or digit. Names are unique per key among tenants that are not closed. A repeated create answers 409 with the existing tenant id, which is how a caller recovers from a lost response.

A tenant the key does not own answers 404, the same as a tenant that does not exist. Adding members and minting keys in a closed tenant answers 409 `tenant_not_active`.

Errors use the Futex envelope `{"error": {"code", "message", "details", "request_id"}}`.

## Setting up one customer

A platform usually gives each of its own customers one Futex tenant, so that Futex enforces
the separation between them. Inside one tenant, any key can read and cancel any decision by
id, and webhook subscriptions and audit reads cover the whole tenant.

1. `POST /v1/provisioning/tenants` with a name you can derive again, such as
   `<platform>-<env>-<customer-slug>`. A 409 `tenant_name_exists` carries the existing id in
   `error.details.tenant_id`. Treat it as success.
2. Add one member per job, for example `svc-<platform>-<customer>-admin` as `policy_author` and
   `svc-<platform>-<customer>-runtime` as `agent_operator`.
3. Mint one key per member and store it before doing anything else. The `api_key` value is in
   that response only. If it is lost, revoke the key and mint another.
4. Create the customer's policies with the `policy_author` key.
5. Register the customer's webhook with the runtime key. Subscriptions belong to a tenant, so
   each customer needs its own.

Issue a separate provisioning key for each environment (development, production). The tenants
of one key are invisible to the others.

## Changing a policy

`PUT /v1/policies/{id}` needs `tenant_admin`, which a provisioning key cannot grant. A
`policy_author` key changes a policy by creating a new one, sending new decisions to the new
id, and retiring the old one with `POST /v1/policies/{id}/retire`. New decisions on a retired
policy answer 409, and decisions already open finish under the definition they were raised
with. When the set of approvers changes, that is a new policy, because assignees are part of
the definition.

## Extra roles for a provisioning key

Every provisioning key can grant `policy_author`, `agent_operator` and `reviewer`. The Futex
operator can also let a key grant any of these, one by one:

- `attesting_operator`: raise decisions that name the person who asked (`requester`), so that
  Futex stops that person approving their own request.
- `delegated_actor`: act on a task for a named person with `on_behalf_of`, when people approve
  inside your own screens.
- `auditor`: read the tenant's audit trail.

Ask the Futex agent on AgentBus for the roles you need. The operator sets them with
`PUT /v1/platform/provisioners/{principal_id}/roles {"grantable_roles": [...]}`, and the answer
lists every role the key may now grant. A change reaches a key after about a minute, because Futex caches a key's permissions for 60 seconds. No
provisioning key can grant `tenant_admin`.

## Closing a tenant

`DELETE /v1/provisioning/tenants/{id}` refuses while the tenant has decisions in `pending`, `in_review`, `escalated` or `approved_pending_clearance`. Cancel them first with the tenant's runtime key. The call then sets the tenant to `closed` and revokes its active keys in one transaction, and a closed tenant's keys are refused at login. Futex keeps the decisions and the audit trail under the retention policy. A repeated call answers 200 with `revoked_keys: 0`.

A decision request that reaches Futex while a close is in progress waits for the close to finish. If the close commits, the request is refused with 403 `Tenant is not active`. A close that meets a decision still being written waits for it and then answers 409 `open_decisions`.

## Audit

Futex records the provisioning principal as the actor on `tenant.created`, `tenant.closed`, `member.added`, `api_key.created` and `api_key.revoked` in the tenant's own trail. Issuing a provisioning key writes `provisioner_key.created` to the platform trail, an operator ownership change writes `tenant.owner_changed`, and a change to a key's extra roles writes `provisioner.roles_changed`.
