# Agent-driven key rotation

Source: https://botpassword.com/docs/rotation

**Status: partly built.** The rotation state machine and the Cloudflare and AWS IAM adapters run today. You set an item up with `bpw rotate configure` and start it with `bpw rotate start` (both ask for your master password); an agent can start one only through a grant with autopilot (`bpw grant add --autopilot`, on a grant whose only intent is `rotate`). GitHub, Stripe and generic credentials cannot be rotated through their API yet, and the other strategies, `change_password` and the confirmation sheet are planned, not built yet.

Rotation is the one intent that changes both a provider and your vault, so BotPassword runs it as a durable state machine: create the new credential, store it as a pending revision, verify it, promote it, and revoke the old one after a grace period. The agent calls `rotate` and receives `rotated` plus a revision reference. It never sees either value.

## What are the states?

| State | Meaning |
|---|---|
| `Planned` | `rotate` accepted and recorded before any provider call |
| `Created` | The provider created the new credential |
| `Stored` | The new value is written as a pending revision |
| `Verified` | The new credential authenticated |
| `Promoted` | The pending revision becomes current |
| `OldRevoked` | The grace period elapsed and the old credential was revoked |
| `Failed` | The provider refused; nothing changed |
| `NeedsAttention` | Verification or old-credential revocation failed; both are kept and reported |

The record lives in the local store and is synced, so a different trusted device can finish or report an interrupted rotation.

## What rules keep it safe?

1. **Persist before every external call.** `Planned` carries an idempotency key and is written before the provider is called. After a restart, an interrupted `Planned` queries the provider by that key or by a deterministic name such as `bpw-<rotation-id>`, so a timeout never creates a second credential.
2. **Store the new value before anything can lose it.** It becomes a pending revision immediately and is never held only in memory.
3. **Verify before promoting.** The adapter makes a non-destructive authenticated call with the new credential.
4. **Revoke the old one after a grace period.** The default is 10 minutes for personal credentials; company policy can change it. Consumers listed on the credential are notified at promotion. An old credential that cannot be revoked becomes `NeedsAttention` with exact instructions, never silently left active.
5. **No step runs twice blindly.** Each step checks provider state first or uses an idempotency key. If neither is possible, the result is `unknown_outcome`.

## What strategies exist?

Each provider adapter declares which it supports.

| Strategy | When | How |
|---|---|---|
| `api` | The provider has an API to create or roll credentials | Fully automatic, using a separate management credential the grant names |
| `delegated` | The provider offers short-lived scoped credentials (GitHub App installation tokens, AWS STS, Cloudflare scoped tokens) | Nothing long-lived to rotate; the broker mints per use |
| `browser_assisted` | Rotation exists only in the provider's dashboard | The agent drives the UI, BotPassword handles `sign_in` (built over CDP, disclosing), the agent triggers "regenerate", and `capture_secret` (planned, not built yet) reads the new value into the pending revision |
| `human_assisted` | Nothing else works | BotPassword opens the right page and captures the pasted value through the human UI, then verifies and promotes |

## What would a rotation look like?

For `cloudflare/prod-deploy` with the `api` strategy:

1. The agent calls `rotate` for the credential (partly built: it needs a grant with autopilot).
2. The broker writes a pending rotation record, creates or rolls the token through Cloudflare's API using a separate token-management credential, and stores the new value as a pending revision.
3. It verifies and promotes the new token. Any `run_with` consumer picks up the new value on its next use; a tool that fetches its login through a [credential helper](/docs/credential-helpers) (`cli_login`) does too, because the helper asks for the value each time.
4. After the grace period it revokes the old token.

For a GitHub fine-grained token, which has no creation API, the adapter uses `browser_assisted`. It also recommends moving long-lived automation to a GitHub App, where the broker mints short-lived tokens.

## Which providers come first?

Today `bpw rotate configure` accepts Cloudflare and AWS IAM; the others below are planned.

Initial adapter priorities are GitHub, Cloudflare, Stripe and generic bearer or basic, plus AWS IAM as the easiest fully automatic case and best end-to-end test. The expected strategies below are unconfirmed; each adapter's real API capabilities are verified against the provider before it is relied on.

| Provider | Expected strategies |
|---|---|
| AWS IAM | `api`, `delegated` |
| Cloudflare | `api`, `delegated` |
| GitHub | `delegated`, `browser_assisted` |
| Stripe | `browser_assisted`; restricted keys to be checked |
| Generic bearer or basic | `browser_assisted`, `human_assisted` |
| Website passwords | `change_password` (planned, not built yet), the same state machine with a site adapter |

Adapters are reviewable Rust modules behind a trait, not plugins executed at runtime. Per-provider rotation notes live under [/keys](/keys).

## How will `change_password` differ?

Planned, not built yet. It will use the same machinery for website passwords. The new password is saved as a pending revision before submission. Success promotes it, a clear failure leaves the old one current, and an ambiguous page returns `unknown_outcome` and keeps both.

## Who turns it on?

`rotate` (partly built) and `change_password` (planned, not built yet) have their own grant flags. Today an agent can rotate only under a grant with autopilot; the one-time toggle per credential needs the planned confirmation sheet. See [Grants and approvals](/docs/grants-and-approvals) and [Typed outcomes](/docs/typed-outcomes).
