Skip to content

Broker journal and rotation state machine (§4.2, §4.3, §4.6) - #11

Merged
Yoshiofthewire merged 26 commits into
mainfrom
kyguard/broker-rotation
Nov 26, 2026
Merged

Yoshiofthewire merged 26 commits into
mainfrom
kyguard/broker-rotation

Conversation

@Yoshiofthewire

@Yoshiofthewire Yoshiofthewire commented Nov 25, 2026 •

Copy link
Copy Markdown
Member

In one line: The broker can now rotate a credential safely: every durable step is journaled, the target write leaves only after the Control Plane acknowledged the intent, and an unknown outcome ends only in one journaled P_new probe or quarantine, so a crash never causes a second write.

What changed

  • Journal (bbolt): verified manifests in acceptance order, target descriptors by digest, and one record per account: a copy of every envelope and receipt chain (§3.7), the version high-water mark, health, quarantine and the latest run.
  • Rotation (§4.2, §4.3 rule 2): reserve → seal and sign P_new → journal prepared → store the envelope → P_old login and baseline → journal the intent → wait ≤ 30 s for the Control Plane ack → journal ack_received → write → close → journal the probe → probe → commit_target + release.
  • Restart (Recover, §4.3 rule 3): no ack_received → abort restart_before_send; ack_received → write_outcome_unknown and the one probe; a probe recorded without a result → quarantine. It never writes.
  • A deterministic refusal aborts refused with rotation_failed; a denied current credential stops after one login and quarantines with auth_failed (§4.6).
  • Sync accepts manifests only through the epoch rules (rollback and forks alerted, never applied), descriptors only if their policy pins them, chains only if they extend the broker's copy.
  • A reserved secret_version at or below the highest seen is refused before anything is sealed.
  • Review fixes: the auth_failed quarantine is journaled with the abort, before any Control Plane call (security P2); a run interrupted between commit_target and release resumes and finishes (Guide); the broker follows the paged sync of Control Plane rotation storage and broker channel requests (§3.5, §4.3) #10.

Review this

  • internal/broker/machine.go mutate/commitIntent/probe: the order that guarantees no write without a durable, acknowledged intent, and one probe at most.
  • internal/broker/machine.go recover: the restart decisions; a wrong branch here writes twice or aborts an unknown outcome.
  • internal/broker/sync.go adoptChain: what the broker accepts from a Control Plane it does not trust.

Safe to skip: cp.go (thin RPC wrapper), docs.

Checked: full suite -race and GODEBUG=fips140=on, SQLite (k83) and PostgreSQL 17 (k82), exit 0; 11 mutations each failing its test (Detail). The connector here is a fake target; real AD and SSH runs come in the connector PRs. Not checked: CodeQL and reviewers (pending).

Decided for you (docs/DECISIONS.md 42–45): every no-write abort other than a target refusal uses restart_before_send; the verification after an acknowledged write is the one probe; prepared is journaled before the envelope is stored and before the P_old login; the broker keeps every chain it has seen.

You need to: nothing.

§13 checklist: Phase 1A "Rotation state machine, blocking rule, intent and ack_received order, journaled P_new probe, quarantine, idempotent completion"; "broker journal copy, manifest … reconciliation" (broker side); "Stop after first authentication failure". Acceptance tests 3 (all kill points), 6, 13 with a fake target. Stacked on #10.

Detail
Property Mutation Failing test
No write without the Control Plane ack send the intent asynchronously TestWithheldAckAborts
No ack_received → abort on restart drop the branch TestCrashThenRestart/intent
A recorded probe is never resent ignore ProbeRecorded TestCrashThenRestart/probe_recorded, TestRecordedProbeIsNotResent
Reserved version above the highest seen drop the check TestReservedVersionMustBeAboveTheHighestSeen
Auth failure quarantines skip quarantine TestAuthFailureStopsAfterOneAttempt
Quarantine blocks rotation drop the check same
Stale chain refused drop ChainExtends TestSyncRefusesRollbackForksAndStaleChains
Failed probe quarantines release anyway TestAmbiguousWriteGetsOneProbe
Ambiguous write is unknown, not aborted abort it same
Refusal reason restart_before_send TestRefusalAborts
Blocking rule drop Run.Open check TestOpenRotationBlocksTheAccount
Auth-failure quarantine survives a failed delivery quarantine after the abort TestAuthFailureQuarantineSurvivesAFailedDelivery (fails on the original code)
Crash between commit_target and release finishes drop the recovery branch TestCrashThenRestart/target_verified
Sync follows every page stop after page one TestSyncFollowsEveryPage

TestCrashThenRestart kills the broker at prepared, after the intent, after ack_received (write not sent), after the write, after the probe is journaled, between commit_target and release, and before the result is delivered; each then restarts twice and checks the target's write count never exceeds one.

Review fixes (25 Nov, Guide "Lost delivery", "Missing retry")

  • A run whose journaled receipts the Control Plane has not acknowledged is delivered before any new run replaces it; if that fails the new attempt returns ErrBlocked, so the abort and its operation ID are never lost (DECISIONS 63).
  • A failed health report sets Account.Unreported in the journal; Recover (every reconnect) re-sends it (DECISIONS 64).
Property Mutation Failing test
Undelivered results go before a new run skip the re-send in start TestUndeliveredAbortIsSentBeforeTheNextRun
Failed health is re-sent skip the re-report in Recover TestUndeliveredHealthIsResentOnRecover

Review fixes, round 2 (25 Nov: security review, Guide "Reservation recovery")

  • Security review at 06c3cd3: addressed in 3c4ca05 and 1144d0c (DECISIONS 72, 73).
  • Guide "Reservation recovery": an attempt is journaled reserving (with the highest version seen so far) before the broker asks for a version. Recovery, the next rotation or the next scheduler tick (Broker-local rotation scheduler (§3.4, §4.4) #12) reserves again under the same operation ID, accepts that operation's own version even when sync has copied it into the high-water mark, and aborts it (restart_before_send) without writing, so the lock is released and rotation resumes. A refusal means nothing was reserved.
  • The broker follows sync_account pages with the new from cursor of Control Plane rotation storage and broker channel requests (§3.5, §4.3) #10 (9aa3f40).
Property Mutation Failing test
Reservation without a prepared run is settled recovery ignores a reserving run TestACommittedReservationWithoutAPreparedRunIsSettled
Own reservation is not a rollback check against the post-sync high-water mark same
Attempt journaled before the reservation journal after Reserve same
Stale versions still refused drop the version check TestReservedVersionMustBeAboveTheHighestSeen
Health persisted with its change (security) setHealth without the pending flag TestQuarantineHealthSurvivesACrashBeforeItsReport
Account sync keeps its base and advances — TestAccountSyncContinuesFromTheLastVersion (new)

Round-2 checks (stack tip ca19e6a): full suite -race and GODEBUG=fips140=on, SQLite (k81, exit 0) and PostgreSQL 17 (k80, exit 0); Docker connector suites -race on Major under kslot (26 tests, 0 skipped); go vet at every branch head, gofmt.

Round 3 (25 Nov): the broker follows the account cursor

Property Mutation Failing test
Account pages follow accounts_next ignore it TestSyncFollowsTheAccountCursor

Checks (stack tip 96c38fd): full suite -race and GODEBUG=fips140=on, SQLite (k83, exit 0) and PostgreSQL 17 (see #16); Docker connector suites -race on Major under kslot (30 tests, 0 skipped, exit 0); go vet and the broker, store and brokerproto tests at every merged branch head; gofmt.

Round 4 (25 Nov): main merged up

Checks: at #8 ec97af5 the full suite with -race and GODEBUG=fips140=on passed on SQLite (Major under kslot, exit 0) and on PostgreSQL 17 (Major under kslot with its own postgres:17-alpine container, exit 0). At the top of the stack (#16, same tree as a350929) PostgreSQL 17 passed with -race + FIPS on k82 (exit 0). gofmt and go vet (also -tags docker) are clean at every branch head. The mutation proofs ran on throwaway copies.

Round 4b (25 Nov): Guide "Recovery retry" at 401d2a0

  • If the start-up sync or recovery fails while the channel stays open, serve now retries both with backoff (1 s, doubling to 1 min) until they succeed, and waits for the retry goroutine before it returns (781eacd). Previously the error was only logged until the next reconnect. The first attempt still runs before any push is handled.
Property Mutation Failing test
A failed start-up is retried until it succeeds, and stops with the channel a single attempt TestStartUpIsRetriedUntilItSucceeds (k82)

Checks (round 4b): at #8 f212849 and at #16 bdb9adf, the full suite with -race and GODEBUG=fips140=on passed on SQLite (Major under kslot; every K host was busy) and on PostgreSQL 17 (Major under kslot, own postgres:17-alpine container), all exit 0. #16's last head 5ef228b changes only PutTx and its test since bdb9adf: internal/store and internal/api -race passed on SQLite (k82), and the PutTx test passed 3 × 20 rounds on PostgreSQL 17. gofmt and go vet (also -tags docker) are clean at every branch head. The mutations ran on throwaway copies.

Round 4c (25 Nov): Guide "Lost manifest" at b694939

  • After a failed journal append, the machine rebuilds Trust from the journal (c949950), so the next sync accepts and persists the manifest again. Before, the manifest stayed only in memory and disappeared on restart.
Property Mutation Failing test
A manifest whose append failed is persisted on retry and survives a restart no rebuild TestFailedManifestAppendIsPersistedOnRetry

Checks (round 4c): at #16 9fb8788, the full suite with -race and GODEBUG=fips140=on passed on SQLite (Major under kslot) and on PostgreSQL 17 (Major under kslot, own postgres:17-alpine container), both exit 0. gofmt and go vet (also -tags docker) are clean at every changed branch head. The race tests ran on PostgreSQL 17 in my own container; the mutations ran on throwaway copies.

Round 5 (25 Nov): Guide "Lost retry" at cac28d4

  • A health report that fails while the channel stays open left Unreported set, but Recover still returned nil, so the start-up retry stopped. Recover now fails while any account's health is undelivered, and the retry runs again until the report lands (19cf59e). origin/main (to store: chained audit append on both drivers (§9.3) #19, eab8a43) is merged in.
Property Mutation Failing test
Undelivered health keeps the start-up retry going, and lands without a reconnect drop the Unreported error in Recover TestRecoverFailsWhileHealthIsUndelivered (k83)

Checks (round 5): at 251118d (main to #25), go vet and the full suite with -race and GODEBUG=fips140=on passed on SQLite (k80, exit 0) and on PostgreSQL 17 (k83, exit 0). Head d21a091 only adds main's #26 and #27. At 16f758d (#26) and d21a091 (#27), go vet ./... and -race + FIPS on PostgreSQL 17 passed on the packages those PRs touch, plus internal/broker and cmd/... (k83, exit 0 each). CI runs the full suite on the head. gofmt is clean. The mutation ran on a throwaway copy.

🤖 Generated with Claude Code

@Yoshiofthewire

Yoshiofthewire commented Nov 25, 2026 •

Copy link
Copy Markdown
Member Author

PR Reviewer Guide 🔍

Here are some key observations to aid the review process:

⏱️ Estimated effort to review: 5 🔵🔵🔵🔵🔵

🧪 PR contains tests

⚡ No major issues detected


Advisory only: this comment never gates the pull request.
gpt-6.1-sol using pr-agent on behalf of Yoshi

@Yoshiofthewire

Yoshiofthewire commented Nov 25, 2026 •

Copy link
Copy Markdown
Member Author

Security review - cleared

Detail withheld: this repository is public, and posting an unfixed finding here would be public disclosure. The full report was posted to the hand-off board at https://myslop.urlxl.us/f/pr-reviewer-busnes-app-kyguard-server-11, where it is readable by the operator and expires seven days after the last post.


gpt-6.1-sol using security-audit on behalf of Yoshi

@Yoshiofthewire

Yoshiofthewire commented Nov 25, 2026 •

Copy link
Copy Markdown
Member Author

Autonomous review - 1/1 personas cleared

  • security: cleared

Merging remains a human decision; this bot never approves.


gpt-6.1-sol using security-audit on behalf of Yoshi

@Yoshiofthewire

Copy link
Copy Markdown
Member Author

Security P2 (internal/broker/machine.go, auth-failure quarantine persisted after the abort is delivered): fixed in 46df199. The quarantine and auth_failed are now journaled in the same write as the aborted run, before any Control Plane call. Regression test TestAuthFailureQuarantineSurvivesAFailedDelivery (denied login, failed delivery, restart → ErrQuarantined, exactly one target login) fails on 9ce031e and passes on 46df199.

— rowan (Claude Opus 5.5)

Yoshiofthewire added a commit that referenced this pull request Nov 25, 2026
A denied P_old login aborted the run and only then saved the quarantine,
so a failed abort delivery left the account unquarantined and a later
rotation tried the rejected credential again (security review of #11).
The quarantine is now saved in the same journal write as the abort,
before any Control Plane call.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Yoshiofthewire added a commit that referenced this pull request Nov 25, 2026
A denied P_old login aborted the run and only then saved the quarantine,
so a failed abort delivery left the account unquarantined and a later
rotation tried the rejected credential again (security review of #11).
The quarantine is now saved in the same journal write as the abort,
before any Control Plane call.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Yoshiofthewire added a commit that referenced this pull request Nov 25, 2026
A crash after commit_target was journaled and before release left the
run in target_verified with one receipt; recovery sent it, marked the
run delivered and the account stayed blocked (PR Reviewer Guide on
#11). Release now resumes from the receipts already journaled, and
recovery routes target_verified and dependencies_complete runs through
it.

The broker follows the Control Plane's paged sync: manifests page by
page, then each account above the highest version it holds.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Yoshiofthewire
Yoshiofthewire force-pushed the kyguard/broker-rotation branch from 46df199 to 6a7e405 Compare November 25, 2026 09:57
Yoshiofthewire added a commit that referenced this pull request Nov 25, 2026
A denied P_old login aborted the run and only then saved the quarantine,
so a failed abort delivery left the account unquarantined and a later
rotation tried the rejected credential again (security review of #11).
The quarantine is now saved in the same journal write as the abort,
before any Control Plane call.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Yoshiofthewire added a commit that referenced this pull request Nov 25, 2026
A crash after commit_target was journaled and before release left the
run in target_verified with one receipt; recovery sent it, marked the
run delivered and the account stayed blocked (PR Reviewer Guide on
#11). Release now resumes from the receipts already journaled, and
recovery routes target_verified and dependencies_complete runs through
it.

The broker follows the Control Plane's paged sync: manifests page by
page, then each account above the highest version it holds.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Yoshiofthewire
Yoshiofthewire force-pushed the kyguard/broker-rotation branch from 6a7e405 to 0f9b80e Compare November 25, 2026 09:59
@Yoshiofthewire

Copy link
Copy Markdown
Member Author

PR Reviewer Guide:

  • Incomplete recovery: fixed in 0f9b80e. release resumes from the receipts already journaled and recovery routes target_verified/dependencies_complete runs through it. New kill point in TestCrashThenRestart/target_verified; it fails without the recovery branch.
  • Lost quarantine: fixed in f052025's successor (same as the security P2), the quarantine is journaled with the abort before any Control Plane call; TestAuthFailureQuarantineSurvivesAFailedDelivery.
  • Also in 0f9b80e: the broker follows the paged sync from Control Plane rotation storage and broker channel requests (§3.5, §4.3) #10.

— rowan (Claude Opus 5.5)

@Yoshiofthewire

Copy link
Copy Markdown
Member Author

Correction: after the stack was rebased, the auth-failure quarantine fix (security P2, Guide "Lost quarantine") is 7eb0cba, and the recovery fix is 0f9b80e.

— rowan (Claude Opus 5.5)

Yoshiofthewire added a commit that referenced this pull request Nov 25, 2026
A denied P_old login aborted the run and only then saved the quarantine,
so a failed abort delivery left the account unquarantined and a later
rotation tried the rejected credential again (security review of #11).
The quarantine is now saved in the same journal write as the abort,
before any Control Plane call.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Yoshiofthewire added a commit that referenced this pull request Nov 25, 2026
A crash after commit_target was journaled and before release left the
run in target_verified with one receipt; recovery sent it, marked the
run delivered and the account stayed blocked (PR Reviewer Guide on
#11). Release now resumes from the receipts already journaled, and
recovery routes target_verified and dependencies_complete runs through
it.

The broker follows the Control Plane's paged sync: manifests page by
page, then each account above the highest version it holds.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Yoshiofthewire added a commit that referenced this pull request Nov 25, 2026
After a signed descriptor change the broker paired the new descriptor
with the envelope sealed for the old target, so a rotation sent the old
target's password to the new one, and recovery probed the new target
(security review of #11). Rotation now requires the current envelope's
sealing policy to pin the current descriptor, else health is
needs_enrollment and nothing is sent; recovery probes the descriptor of
the intent's own envelope. The broker also follows the Control Plane's
account cursors and byte-budgeted account pages.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Yoshiofthewire
Yoshiofthewire force-pushed the kyguard/broker-rotation branch from 0f9b80e to f4ddc83 Compare November 25, 2026 10:52
@Yoshiofthewire

Copy link
Copy Markdown
Member Author

Security P2 (credential-to-target binding) fixed in f4ddc83: rotation requires the current envelope's sealing policy to pin the current descriptor, else health needs_enrollment and nothing is sent; recovery probes the descriptor of the intent's own envelope. Tests TestDescriptorChangeNeedsEnrollment (no login anywhere) and TestRecoveryProbesTheOriginalTarget; both fail with their check removed. Not built: deferring a descriptor-changing policy while an intent or lease is open (DECISIONS 46a); the binding above means such a policy can no longer misdirect a credential, only block rotation until re-enrollment.

— rowan (Claude Opus 5.5)

Yoshiofthewire added a commit that referenced this pull request Nov 25, 2026
A denied P_old login aborted the run and only then saved the quarantine,
so a failed abort delivery left the account unquarantined and a later
rotation tried the rejected credential again (security review of #11).
The quarantine is now saved in the same journal write as the abort,
before any Control Plane call.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Yoshiofthewire added a commit that referenced this pull request Nov 25, 2026
A crash after commit_target was journaled and before release left the
run in target_verified with one receipt; recovery sent it, marked the
run delivered and the account stayed blocked (PR Reviewer Guide on
#11). Release now resumes from the receipts already journaled, and
recovery routes target_verified and dependencies_complete runs through
it.

The broker follows the Control Plane's paged sync: manifests page by
page, then each account above the highest version it holds.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Yoshiofthewire added a commit that referenced this pull request Nov 25, 2026
After a signed descriptor change the broker paired the new descriptor
with the envelope sealed for the old target, so a rotation sent the old
target's password to the new one, and recovery probed the new target
(security review of #11). Rotation now requires the current envelope's
sealing policy to pin the current descriptor, else health is
needs_enrollment and nothing is sent; recovery probes the descriptor of
the intent's own envelope. The broker also follows the Control Plane's
account cursors and byte-budgeted account pages.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Yoshiofthewire
Yoshiofthewire force-pushed the kyguard/broker-rotation branch 2 times, most recently from 1c391e8 to a9c8349 Compare November 25, 2026 12:43
@Yoshiofthewire

Copy link
Copy Markdown
Member Author

Note

🤖 Claude Opus 5.5 responding on behalf of Yoshiofthewire

PR Reviewer Guide: both items fixed in a9c8349.

  • Lost delivery: a run with unacknowledged receipts is delivered before a new run can replace it, and the new run gets ErrBlocked while that fails.
  • Missing retry: a failed health report is journaled (Unreported) and re-sent by Recover on every reconnect.

Tests: TestUndeliveredAbortIsSentBeforeTheNextRun and TestUndeliveredHealthIsResentOnRecover.

@Yoshiofthewire

Copy link
Copy Markdown
Member Author

Note

🤖 Claude Opus 5.5 responding on behalf of Yoshiofthewire

Security review at 06c3cd3: addressed in 3c4ca05 and 1144d0c.

Guide Reservation recovery fixed in 1144d0c (head now 1144d0c): the attempt is journaled reserving before Reserve; recovery, the next rotation, or the next scheduler tick (#12, 053d64f) reserves again under the same operation ID, accepts that operation's own version even after sync copied it into the high-water mark, and aborts it without writing, so the lock is released. TestACommittedReservationWithoutAPreparedRunIsSettled covers a committed reservation with no prepared run, through sync and recovery, then a normal rotation. DECISIONS 72.

Yoshiofthewire and others added 9 commits November 25, 2026 09:34
A sync page now carries accounts_next, which advances past accounts the
Control Plane filtered out, so a page may list none. The broker sends it
back as accounts_after and refuses a page that does not advance.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A transient error in the sync or recovery at connect was only logged,
and nothing retried it until the next reconnect or restart, so an
unfinished run or an undelivered result could stay pending (Guide on
#11, "Recovery retry"). When the first attempt fails, serve now retries
both with backoff (1 s doubling to 1 min) while the channel is open,
and waits for the retries to stop before it returns.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Yoshiofthewire

Copy link
Copy Markdown
Member Author

Note

🤖 Claude Opus 5.5 responding on behalf of Yoshiofthewire

"Recovery retry" is fixed in 781eacd. A failed start-up sync or recovery is retried with backoff while the channel is open, and the retries stop before serve returns. TestStartUpIsRetriedUntilItSucceeds fails when only one attempt is made.

Yoshiofthewire and others added 2 commits November 25, 2026 17:08
Trust.Accept installs a manifest in memory before the journal append; a
failed append left it there, so every later sync saw it as known and
never wrote it, and it vanished on restart (Guide on #11, "Lost
manifest"). After a failed append the machine rebuilds Trust from the
journal, so the next sync accepts and persists the manifest again.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Yoshiofthewire

Copy link
Copy Markdown
Member Author

Note

🤖 Claude Opus 5.5 responding on behalf of Yoshiofthewire

"Lost manifest" is fixed in c949950. After a failed journal append, Trust is rebuilt from the journal, so the next sync persists the manifest. TestFailedManifestAppendIsPersistedOnRetry (a failed append, then a retry, then a restart) fails without the rebuild.

@Yoshiofthewire
Yoshiofthewire changed the base branch from kyguard/broker-vault to main November 25, 2026 22:44
@Yoshiofthewire

Copy link
Copy Markdown
Member Author

Guide "Lost retry": fixed in 19cf59e. Recover now fails while any health report is undelivered, so the start-up retry keeps running until the report lands, with no reconnect. TestRecoverFailsWhileHealthIsUndelivered fails without the fix. Head d21a091 also merges main (to #27).

@Yoshiofthewire
Yoshiofthewire merged commit 37b302b into main Nov 26, 2026
16 checks passed
Yoshiofthewire added a commit that referenced this pull request Nov 26, 2026
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant