Back to Field Notes
Deep dive

A key ring for the hub: rotating keys without re-pairing a site

Our hub signs every agent request a customer site accepts, and until now changing its key meant re-pairing every site. We designed a key ring that rotates with no downtime, and built the first half of it on October 20.

Covers
Written
Repositories
infinary.io, infinary_controls
Reading time
4 min
#security#control-plane#infinary-controls
Contents

Why the hub has a key

Infinary’s hub is our control plane. When one of our agents works in a customer’s ERPNext site, the site doesn’t take the agent’s word for who it is. Every request carries an assertion signed by the hub, and the site checks it against the hub’s public key, which it learned when it was paired.

That design had one awkward property: the site knew exactly one hub key. Changing that key meant re-pairing every site, one at a time, with agent work failing on each site until it was done. A key you can’t change easily is a key you won’t change often. So on October 20 we designed rotation as a routine, scheduled event with no downtime, and started building it.

The design, at a high level

A ring, not a key. The hub now holds a small ring of signing keys, each with a state: next, active, retiring or retired. Each key lives in its own secret version, read by number, never by “latest”, and a record says which version is in which state.

Sites learn keys from signed key sets. Every 15 minutes, and straight after any change, a site asks the hub for the current key set. A new set must be signed by a key the site already trusts, and by every key it adds. A site never accepts a key just because the hub offered it.

Sites report what they trust. In the same exchange, each site tells the hub which keys it holds. That gives the hub the one rule the whole design rests on:

The hub signs for a site only with a key that site has reported.

That rule is where the zero downtime comes from. A new key sits in the next state until every site reports it, so no site is ever sent a signature it can’t check. Sites on an older version of our site app, which can’t take key sets, simply stay on the key they paired with until they’re updated.

A sequence diagram of one rotation. The hub creates a next key and publishes a key set signed by a key every site already trusts and by the new key. Each site checks the signatures, adds the next key, and reports the keys it now trusts. Once every site has reported it, the hub promotes the next key to active and signs with it, only for sites that reported it. After a grace period, the old key is retired and its secret version disabled.
One rotation: the hub never signs with a key a site hasn't reported.

A schedule, and a button. An hourly job walks each rotation through its steps: create a key, publish it, wait for every site (with a deadline), promote it, keep the old key through a grace period at least as long as the longest agent run, retire it, then disable its secret version. The plan is a rotation every 90 days. Staff can also “Rotate now”, with an emergency mode that retires the old key at once, and laggard and revoked sites have their own handling. Destroying a secret version stays a manual command.

The same thinking reaches the other keys: the key behind our model gateway’s tokens gets the same ring, and each site’s agent key gets its own “Rotate now”. Everything is on one Admin → Keys page, where only a short allowlist of administrators can take write actions.

What we built on October 20

Six changes merged that day:

  • the design;
  • a narrow permission for the control plane to list, disable and re-enable secret versions, but never destroy one;
  • the ring itself, with today’s key adopted as its first and only entry, so nothing changed on the wire;
  • automatic pruning of old secret versions, plus “Rotate now” for a site’s agent key;
  • a read-only Keys page in the portal;
  • on the site side, a release of our site app that trusts up to five hub keys and takes new key sets over the signed check.

The scheduled rollover itself, an end-to-end rehearsal, and the first real rotation are still ahead of us.

Things we learned building it

  • A route that doesn’t exist yet can look like “unauthorized”. An older control plane answers the new key-set request with 401, not 404, because its session check runs before routing. The site treats both as “not offered yet” and tries again later.
  • Order of commits matters in pairing. One step saved a new agent key on the site before telling the hub. If telling the hub failed, the hub was left holding a dead key until the next weekly run. The new stale-key check, merged the same day, repairs that on its own.
  • A job queued “after commit” from a script may never run. Frappe’s enqueue_after_commit waits for a commit that a bench script may already have made. We found that in the end-to-end test of the site side.

What we’d tell someone else

If rotating a key needs a migration, it won’t happen until it has to, which is the worst time. Make the system tell you which keys each party holds, and only ever sign with a key the other side has confirmed. Rotation then becomes something a scheduler can do.