Skip to content

Verify the audit log against its hash chain - #78

Open
WiktorB2004 wants to merge 12 commits into
add-integrity-sealerfrom
add-integrity-verify
Open

WiktorB2004 wants to merge 12 commits into
add-integrity-sealerfrom
add-integrity-verify

Conversation

@WiktorB2004

Copy link
Copy Markdown
Owner

Summary

Adds verify() and acknowledge() for the hash chain, with async forms averify() and aacknowledge(). Builds on #76, so merge that first.

  • verify(engine, tables) checks every block of the chain and every row it seals, and returns a VerificationReport with a status (VALID, VALID_WITH_NOTES or FAILED), the failures and the notes. It collects every failure instead of stopping at the first one.
  • The checks live in a small verifier core with no SQLAlchemy import, and it has unit tests of its own. It rebuilds the leaves with the same functions the sealer uses.
  • The reader uses one connection and takes no lock, so a pool of one is enough. It reads the catalog, then the chain (streamed in chunks), then the horizon and xmax, then each partition in short READ ONLY transactions of keyset chunks on (xid, id). It never holds one snapshot over the whole log. If a partition is dropped during the run, as drop_expired does after its retire block, the run starts again once. If it is dropped again, it is reported as absent.
  • A row whose xid is at or above the snapshot's xmax is reported as XID_REWIND.
  • Erasures are checked per group: the stamp, the erased form, and the per-operation counts against the audit.scrubbed records. A stamp must name a transaction no older than its row and below xmax. Records and stamps from before the genesis block count as no receipt. Scrubs still running at the horizon get a note and are counted by a later run. A retired partition explains a missing record only when it is in the stream of the records' severity (system_severity).
  • A scrub's audit.scrubbed record now also counts the rows it stamped (stamped_activity_rows, and stamped_transaction_rows for scrub_actor). Rows written before integrity are changed but not stamped, so the existing counts could not be compared with the stamps on their own. This only happens with integrity on.
  • acknowledge() runs verify first, then appends an ack block, hashed and locked like the other blocks. The block pins each failure it waives as it was seen: the row for a row failure, the root and row count for a segment. A failure that is new or has changed since is reported again. A scope of only table is refused, and so is an ack that would waive nothing.
  • Detached, pending and dropped partitions are reported. So is a retire block inside the retention, when the retention is given.
  • Everything stays private: nothing is exported or documented yet. With integrity off, nothing changes.

Closes #74

Test plan

  • Tests added or updated (or N/A — say why)
  • uv run pytest passes locally, including the PostgreSQL tests in tests/db
  • Schema, SQL or partition changes checked on PostgreSQL 14 (AUDIT_TEST_PG_IMAGE=postgres:14) as well as the default image
  • ruff check, ruff format --check and mypy pass
  • Docs and docstrings updated if the public API changed (N/A: still private, no public API change)
  • CHANGELOG.md Unreleased bullet if callers should notice (N/A: private for now)

With integrity, a scrub changes rows written without integrity but does
not stamp them, so activity_rows alone cannot be compared with the
stamps. The record now also carries stamped_activity_rows (and
stamped_transaction_rows for scrub_actor). Nothing changes without
integrity.
A pure class, with no SQLAlchemy import, that checks the blocks of the
chain and the rows of each partition, rebuilt with the leaf functions of
the sealer, and reports every failure and note. Rows are judged per
incarnation, and ack blocks turn matching failures into ACKNOWLEDGED
notes.
verify() reads the chain, the catalog and then each partition in short
READ ONLY transactions, keyset-paged by (xid, id), and feeds the verifier
core. acknowledge() appends an ack block under the sealer's lock. Both
stay private.
The partition comes from the rows and the retire bounds are fixed dates,
so no test depends on the clock. A chunk size that divides a partition's
rows ends with one empty read.
A stamp must name a transaction no older than its row and below the
snapshot's xmax; anything else is SCRUB_COUNT_MISMATCH instead of being
skipped. Records and stamps from before the genesis block are no
receipts. Scrubs at or above the horizon are noted as UNSEALED_TAIL and
counted by a later run. A retired partition explains a missing record
only when it is in the stream of the records' severity, which verify now
takes as system_severity.
A row whose xid is at or above the xmax of the snapshot that read it
cannot have been written yet; it was a note in the unsealed tail. The
reader reads xmax after each chunk, so rows of transactions that started
during the run stay in the tail.
verify only reads, so one run gains nothing from excluding another, and
a session lock does not survive a pooler in transaction mode. It now runs
on one connection, so a pool of one is enough.
The horizon was read before the chain, so a seal that committed in
between looked like a rewind. Now the catalog comes first, then the
blocks, streamed in seq chunks, then the horizon and xmax, then the rows.
A partition dropped after the catalog read, as drop_expired does after
its retire block, makes the run start again once; dropped again, it is
reported as absent. A partition whose read fails leaves no trace in the
verifier.
An ack waived every failure of its kind in its scope, so a later change
in the same scope was waived too. acknowledge now runs verify first and
stores each failure it waives: the row for a failure about a row, the
rebuilt root and row count for a segment, every field for the rest. A
failure that is new or changed since is reported again. A scope of only
table is refused, and so is an ack that would waive nothing.
The async forms run verify and acknowledge on one AsyncConnection
through run_sync, as aseal does. Tested on asyncpg and psycopg, with a
partition dropped mid-run.

This branch has not been deployed

No deployments
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