Repository navigation
Commit e9095db
authored
fix(auth): the audit sealer only builds on checkpoints that verify, and signs on schedule under load (#369)
The sealer had several gaps:
- It trusted whatever checkpoint was newest in the database, without
checking its signature, and `/audit/checkpoints/latest` served it the
same way.
- Under sustained traffic it only checked for a due checkpoint once a
batch came back short, so a backlog postponed signing.
- Histories listed events by `sequence`, which the chain doesn't cover,
so anyone with the schema owner's rights could reorder displayed history
without breaking verification.
- The canonical event had no format tag, so any future field would break
every existing hash.
Closes #251.
Stacked on #361 (#250): its migration is 0036, after #361's 0035. It
merges after #361, and GitHub then retargets it to `main`.
## What changes
- **Checkpoints that don't verify:**
- The sealer builds on the newest checkpoint the environment's audit
keys verify, or on the anchor if that's newer. It also trusts its own
key, so an uncommitted key can't make it distrust and re-sign its own
checkpoints.
- Someone with auth's database role but not the key can plant
checkpoints the database can't check. The sealer logs them, counts them
in `piquel.auth.audit.untrusted_checkpoints`, and passes over them.
- It looks past at most 1,000. Past that, as removing a key that signed
for days would leave, it recomputes from the anchor and signs above them
rather than failing.
- If a planted checkpoint holds the head's position, the sealer stops
before recomputing anything.
- `/audit/checkpoints/latest` serves only a checkpoint that verifies.
The list endpoint still serves every stored one, for readers to verify
themselves.
- **Under load:**
- The sealer checks whether a checkpoint is due after every batch, and
the interval counts only from a trusted checkpoint.
- A failed checkpoint no longer ends the pass: sealing goes on. The next
try waits one interval, timed on a monotonic clock from when the attempt
ended, so a broken chain isn't recomputed on every pass.
- **Order:**
- Histories list events by `(txid, sequence)`, the order they're sealed
in, through a new index.
- `audit verify` and the sealer report a new `out_of_order` problem when
that order disagrees with the chain, including across the checkpoint the
sealer starts from.
- `sequence` alone legitimately disagrees with sealing order when
transactions overlap, so it isn't compared on its own.
- A verification that finds `out_of_order` is recorded in payload
version 2 of `audit_chain_verified`, which older releases read as
unknown.
- **Format tag:**
- `domain_events.chain_format` (migration 0036) records each sealed
event's canonical format. Format 1 is today's untagged form; format 2
only adds `"format": 2`.
- This release verifies both but keeps sealing format 1, so every deploy
and rollback stays verifiable. A one-line follow-up starts sealing
format 2.
- The column accepts only 1 and 2, so a forged value reads as a changed
event at its position.
- **Errors:** note verification no longer reports OpenSSL failures as an
invalid signature, and an `after` past 2^63−1 gets a 400 instead of a
500.
## Left as is, on purpose
- **Repeated planting:** the database role can seal events itself.
Someone who keeps sealing one and planting a checkpoint at the new head
can keep postponing signing while their access lasts. The sealer logs
and counts every plant, and `audit verify` reports them. Closing this
would mean letting several checkpoints share a position, a primary key
change; listed under Limits.
- **Moving the database:** the order check and the history order assume
transaction IDs only increase. A logical dump restored into a new
cluster breaks that, so the docs say to move the database with
`pg_upgrade` or a physical copy.
- **Follow-ups:**
- caching `/latest` while planted checkpoints are present;
- finding the trusted checkpoint once per pass rather than once per
batch;
- the admin dashboard's `/audit?after=` overflow.
## Deploying and rolling back
- **Deploy:** migration 0036 adds the column, which PostgreSQL 11+ adds
without rewriting the table, and builds one index on `domain_events`.
The build blocks writes to that table while it runs; with no real users
yet, that's brief.
- **Beside the previous release:** it keeps working beside this one:
- its events get the default format 1, and it seals in format 1 without
touching the column;
- it lists histories by `sequence`, through the old index, which stays
until no release needs it (a pending contract).
Existing sealed events are all format 1 and were sealed in `(txid,
sequence)` order since #159, so they verify unchanged, `out_of_order`
included.
- **During the rollout:** a request that pages with `before` across an
old and a new replica can briefly skip or repeat events, since the two
releases order history differently. Nothing is stored wrong.
- **Rolling back:** the previous image runs on this schema. Everything
this release seals is format 1, so the previous release still verifies
the whole chain and keeps signing. As with any migration, its `migrate`
job refuses a database with a migration it doesn't know (sqlx's
`VersionMissing`), so a rollback runs the previous image without that
job.
## Proof
- `forged_checkpoints_are_passed_over_and_not_served`: a checkpoint
signed by another key, dated ahead, is never served and postpones
nothing.
- `forged_checkpoints_are_never_built_on`: a forged checkpoint over a
tampered event isn't built on.
- `checkpoints_never_sign_events_out_of_order`, across the starting
checkpoint.
- `signing_resumes_above_too_many_untrusted_checkpoints`: with 1,001
planted checkpoints, the sealer recomputes and signs above them.
- `sealing_goes_on_when_a_checkpoint_fails`.
- `signing_resumes_from_the_anchor_when_no_checkpoint_verifies`: after a
prune, with no checkpoint that verifies, signing resumes from the
anchor.
- `a_failed_checkpoint_is_retried_after_the_interval`.
- `checkpoints_keep_their_interval_while_batches_stay_full`.
- `the_chain_format_is_per_event`.
- `verification_checks_the_recorded_order`.
- `checkpoints_are_published`, which also covers the 400.
- Garbage signatures read as invalid, not as provider failures.
- On the final head, rebased onto `main` with #361: `just db test`
passes, 389 of 389, as do the query cache check, clippy and fmt.
Reviewed with review-loop: 5 rounds with 2× GPT 6.1 Sol and 3× Claude
Opus 5.5, all on xhigh, together with #361. Sol hit its usage limit in
rounds 2–4, and both Sol reviewers reviewed the final heads in round 5.
Verdict: approved by all five.
> [!NOTE]
> Written by Claude Opus 5.5 in Claude Code and reviewed by GPT 6.1 Sol
and Claude Opus 5.5 (via T3 Code)
🤖 Generated with [Claude Code](https://claude.com/claude-code)1 parent 2755f40 commit e9095db
30 files changed
Lines changed: 1079 additions & 202 deletions
File tree
- apps/auth-service/src
- audit
- config
- jobs
- crates
- common/src
- events
- crypto/src
- database
- .sqlx
- migrations
- src
- connection
- repositories
- audit
- events
- docs
- architecture
- security
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
23 | 23 | | |
24 | 24 | | |
25 | 25 | | |
| 26 | + | |
26 | 27 | | |
27 | 28 | | |
28 | 29 | | |
| |||
82 | 83 | | |
83 | 84 | | |
84 | 85 | | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
85 | 97 | | |
86 | 98 | | |
87 | 99 | | |
| |||
91 | 103 | | |
92 | 104 | | |
93 | 105 | | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
94 | 110 | | |
95 | 111 | | |
96 | 112 | | |
| |||
116 | 132 | | |
117 | 133 | | |
118 | 134 | | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
119 | 141 | | |
120 | 142 | | |
121 | 143 | | |
| |||
233 | 255 | | |
234 | 256 | | |
235 | 257 | | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
236 | 273 | | |
237 | 274 | | |
238 | 275 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
4 | | - | |
| 4 | + | |
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
| |||
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
| 19 | + | |
19 | 20 | | |
20 | 21 | | |
21 | 22 | | |
22 | 23 | | |
23 | 24 | | |
24 | 25 | | |
25 | 26 | | |
| 27 | + | |
| 28 | + | |
26 | 29 | | |
27 | 30 | | |
28 | 31 | | |
29 | 32 | | |
30 | 33 | | |
31 | | - | |
| 34 | + | |
| 35 | + | |
32 | 36 | | |
33 | 37 | | |
34 | 38 | | |
| |||
69 | 73 | | |
70 | 74 | | |
71 | 75 | | |
72 | | - | |
73 | | - | |
74 | | - | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
75 | 80 | | |
76 | 81 | | |
77 | 82 | | |
| |||
82 | 87 | | |
83 | 88 | | |
84 | 89 | | |
85 | | - | |
| 90 | + | |
| 91 | + | |
86 | 92 | | |
87 | 93 | | |
88 | | - | |
89 | | - | |
90 | | - | |
91 | | - | |
92 | | - | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
93 | 102 | | |
94 | 103 | | |
95 | 104 | | |
| |||
105 | 114 | | |
106 | 115 | | |
107 | 116 | | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
108 | 122 | | |
109 | 123 | | |
110 | 124 | | |
| |||
0 commit comments