Skip to content

store: chained audit append on both drivers (§9.3) - #19

Merged
Yoshiofthewire merged 11 commits into
mainfrom
kyguard/audit-chain
Nov 25, 2026
Merged

Yoshiofthewire merged 11 commits into
mainfrom
kyguard/audit-chain

Conversation

@Yoshiofthewire

@Yoshiofthewire Yoshiofthewire commented Nov 25, 2026 •

Copy link
Copy Markdown
Member

In one line: The audit log is now a hash chain with one writer inside the caller's transaction. A failed audit write fails the action, and an edited or truncated log no longer verifies.

What changed

  • audit_records gains seq, prev_hash and hash. audit_chain_head is the anchor: it is not seeded, and an empty head means genesis.
  • One writer, appendAudit, runs inside the caller's transaction. On PostgreSQL it holds pg_advisory_xact_lock; SQLite transactions now begin immediate. It resumes the chain from the head record and writes record and head together.
  • The chain key is HKDF(encryption key, "kyguard:auditchain"). Without it, no audit write succeeds.
  • revokePasswordGrants (password change), SCIM create and adopt, SSO provisioning and role changes audit in their own transaction (store.WithAudit). No LogAudit result is discarded any more, and back-channel logout fails on a failed audit write.
  • VerifyAuditChain checks the whole log against the head.

Review this

Checked: go test -race and the FIPS profile, full suite, SQLite and PostgreSQL 17 at this head. Mutation table below.
Not checked: the external-effect half of acceptance 12 (operation_intents, outcome_unknown). It is the next PR in this stack. Until then a failed audit write after a backup run (recordRun) is only logged (DECISIONS 19), and the next PR removes that.
You need to: review. Held unmerged by rule.

§13 Phase 0 checklist: Chained audit append on both drivers, transaction rules, fixed inherited paths (§9.3), except intents. Acceptance 12, except the intent clauses.

Detail

Mutation table (each mutation makes the named test fail)

# Property Mutation Failing test
A1 Concurrent writers never fork the chain at genesis drop the PostgreSQL advisory lock TestConcurrentAuditWritersMakeOneChain (genesis race)
A3 A password change audits through the chain restore the direct insert in revokePasswordGrants TestAuditChainCoversPasswordChangesAndDetectsTampering
A4 Verification checks the head skip the head comparison TestAuditChainCoversPasswordChangesAndDetectsTampering (truncation)
A5 A failed audit write rolls back the action audit outside the transaction TestFailedAuditRollsBackTheAction

hawthorn ran these at the pre-rebase head 7c86931. Since then the branch moved only by rebasing. TestAuditWriteNeedsTheChainKey and TestSnapshotWithoutAnAuditHead cover the key floor and the unseeded head.

Checks at 8d0f1cb

  • gofmt, go vet, go build, go test -race -count=1 -p 2 ./... and the same under GODEBUG=fips140=on: SQLite pass (Major, kslot; every K host was busy); PostgreSQL 17 pass (Major, own postgres:17-alpine container). Exit 0 on both.

Stack

#1 → #3 → #7 → #17 → #18 → #19 (this).

Review fixes (hawthorn fix-auth)

  • Security P2 (338d5d5): verification rejects a stored field that differs from its canonical rendering, which the HMAC covers, instead of re-sanitizing it into a match.
  • Guide, upgrade failure: records written before migration 15 are chained when the store opens, oldest first and after any chained ones. The first append after an upgrade succeeds (DECISIONS 18a).
  • Guide, inconsistent snapshot: PostgreSQL verification reads head and records in one REPEATABLE READ snapshot.
Property Mutation Test Result
noncanonical stored text breaks the chain skip the canonical comparison TestNoncanonicalStoredFieldBreaksTheChain fails
older records are chained at open skip the adoption TestRecordsFromBeforeTheChainAreChainedAtOpen fails
verification passes while writers append read committed TestVerifyWhileWritersAppend fails on PostgreSQL

Checked at 338d5d5: race and FIPS suites on SQLite (Major) and PostgreSQL 17 (Major, own container), exit 0.

Review fixes, round 2

  • Security P2 (legacy signing) (f0d21d4): only the open that applies migration 15 chains the records it found. A row inserted unchained later is never signed, and verification keeps reporting it.
Property Mutation Test Result
a later unchained row is never signed adopt at every open TestRecordsFromBeforeTheChainAreChainedOnce fails (both drivers)
the upgrading open chains older rows never adopt same fails

Checked at f0d21d4: race and FIPS suites on SQLite (Major) and PostgreSQL 17 (k82), exit 0. #25–#28 sit on 338d5d5; the root thread holds restacks, so they do not have this commit yet.

Review fixes, round 3

  • Guide, interrupted upgrade (13abb62): the older records are chained inside migration 15's own transaction (a per-version hook in the migration runner), so schema and chain commit together; an interrupted or keyless upgrade leaves migration 15 unapplied and the next start retries both.
Property Mutation Test Result
a failed chaining never commits migration 15 ignore the hook's error TestRecordsFromBeforeTheChainAreChainedOnce fails

Checked at 13abb62: race and FIPS suites on SQLite (Major) and PostgreSQL 17 (Major, own container), exit 0.

Stack merge (hawthorn fix-auth-3): head bc26fc3 merges kyguard/routes (plain merge commits, no rebase; 9192b01 merged it first, and bc26fc3 adds a content-free merge of the pushed commit below), so this PR carries #7's fail-closed SSO sign-in (9220dd7) and every fix below it. The only conflicts were in docs/DECISIONS.md, and both sides were kept. Go vet passes at this head; CI runs the suites.

Round 4 (hawthorn): main merged; broker audit records chained

  • Merges kyguard/routes, which carries origin/main with Broker listener, CA and pairing (§4.1) #8's broker stack. migrate_postgres.go keeps both table lists, and DECISIONS keeps both sides.
  • Join fix (896dc31): Broker listener, CA and pairing (§4.1) #8's auditTx inserted audit_records directly, outside the hash chain. On this branch every broker record (bundle import, pools, pairing claims, intent sweeps) left VerifyAuditChain failing. auditTx now calls appendAudit in the caller's transaction, as its own comment anticipated.
Property Mutation Test Result
Broker audit records join the chain the old auditTx (direct insert) TestExpirePendingIntentsLeavesYoungOnes (now verifies the chain) fails ("2 records were never chained")

Checks: race + FIPS on SQLite and PostgreSQL 17, at the bottom (#18 @ bd2fb58: SQLite k81 exit 0, PostgreSQL own container exit 0) and the top (#28 @ 0cb6d83: SQLite k82 exit 0, PostgreSQL own container exit 0). Affected packages were also run at this head on k82.

🤖 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-19, 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 and others added 2 commits November 25, 2026 07:53
Implement spec §9.3: audit_records gains seq, prev_hash and hash, and
audit_chain_head (unseeded; empty means genesis) is the anchor. One
writer, appendAudit, runs inside the caller's transaction: it takes
the PostgreSQL advisory lock (SQLite transactions now begin
immediate), resumes the chain from the head record and inserts record
and head together. The key is HKDF(encryption key, kyguard:auditchain).

revokePasswordGrants, SCIM create and adopt, SSO provisioning, role
changes and SCIM adoption now audit in their own transaction through
store.WithAudit; back-channel logout fails on a failed audit write.
No LogAudit result is discarded any more.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Security review P2: verification now rejects a stored field that
differs from its canonical rendering, which the HMAC covers, instead of
re-sanitizing it into a match. PR Reviewer Guide: records written
before migration 15 are chained when the store opens, so the first
append after an upgrade succeeds; PostgreSQL verification reads head
and records in one REPEATABLE READ snapshot.

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

Copy link
Copy Markdown
Member Author

Fixed in 338d5d5. Security P2: verification rejects any stored field that differs from its canonical rendering (TestNoncanonicalStoredFieldBreaksTheChain, both drivers). Guide (upgrade failure): records written before migration 15 are chained at store open, oldest first and after any chained ones (TestRecordsFromBeforeTheChainAreChainedAtOpen). Guide (inconsistent snapshot): PostgreSQL verification uses one REPEATABLE READ snapshot (TestVerifyWhileWritersAppend fails on PostgreSQL at read committed). Each fails under its mutation; DECISIONS 18a.

Security review P2: every keyed open signed any unchained row, so a
database writer without the key could have a fabricated record
authenticated at the next start. Only the open that applies migration
15 now chains the records it found; a row that appears unchained later
stays unchained and verification reports it.

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

Copy link
Copy Markdown
Member Author

Security P2 (legacy signing) fixed in f0d21d4: migrations.RunApplied reports what an open applied, and legacy rows are chained only by the open that applies migration 15. TestRecordsFromBeforeTheChainAreChainedOnce (both drivers) inserts an unchained row after the upgrade and requires verification to refuse it; it fails if adoption runs at every open.

PR Reviewer Guide: a start interrupted after migration 15 committed but
before chaining left the older records unchained for good. The
migration runner now takes per-version hooks run in that version's
transaction, and the store chains the records there, so schema and
chain commit together; without the key such a database refuses to
migrate. The store gets its chain key at construction.

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

Copy link
Copy Markdown
Member Author

Guide (interrupted upgrade) fixed in 13abb62: migrations.RunApplied takes per-version hooks run inside that version's transaction, and the store chains the older records in migration 15's, so an interrupted or failed chaining leaves the migration unapplied and the next start retries both. Without the chain key such a database refuses to migrate. The test opens keyless first (fails, version 15 unrecorded), then with the key (chained, verifies).

Yoshiofthewire and others added 7 commits November 25, 2026 09:28
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

# Conflicts:
#	docs/DECISIONS.md
…t-chain

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Picks up routes' merge commit as pushed (62f55c3). Its tree equals the
f8e98a9 merged before, so nothing changes.

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

# Conflicts:
#	docs/DECISIONS.md
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Brings main (#8's broker tables) in. migrate_postgres.go keeps both
table lists; DECISIONS keeps both sides in number order.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
#8's auditTx inserted audit_records directly, outside the chain, so on
this branch every broker record (bundle import, pools, pairing, intent
sweeps) left VerifyAuditChain failing. auditTx now calls appendAudit, in
the caller's transaction, as its own comment anticipated.

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

Copy link
Copy Markdown
Member Author

Merger (saltmarsh-merge): closing and reopening right away so CodeQL runs now that the base is main. The branch is unchanged.

@Yoshiofthewire
Yoshiofthewire merged commit eab8a43 into main Nov 25, 2026
27 checks passed
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