Skip to content

Sign and export integrity checkpoints - #80

Open
WiktorB2004 wants to merge 15 commits into
add-integrity-retentionfrom
add-integrity-checkpoints
Open

WiktorB2004 wants to merge 15 commits into
add-integrity-retentionfrom
add-integrity-checkpoints

Conversation

@WiktorB2004

Copy link
Copy Markdown
Owner

Summary

Signing, checkpoints and forks for the integrity chain. Closes #72.

Builds on #79 (retention). Everything stays private: nothing is exported from audit_trail and there are no docs pages yet.

  • Signing hooks. A Signer / SignatureVerifier protocol; the core never imports a crypto library. Every block kind can be signed, and verify(verifier=..., require_signatures=...) reports BAD_SIGNATURE and UNKNOWN_KEY. With require_signatures, an unsigned checkpoint is refused too.
  • Ed25519 reference. Ed25519Signer / Ed25519Verifier in the new sqlalchemy-audit-trail[integrity] extra (cryptography). Keys use the C2SP vkey encoding, and a verifier holds several keys for rotation; two different keys with one key id are refused.
  • Checkpoints. export_checkpoint appends a checkpoint block and returns it as a C2SP signed note wrapping the library's own statement {v, chain, seq, block_hash, xid_to, sealed_at, incarnation} (not a tlog-checkpoint). Notes with two signatures of one key, or control characters in a key name, are malformed. verify(checkpoints=[...]) reports TRUNCATED, CHAIN_MISMATCH, or ANCHORED with an UNANCHORED_TAIL note.
  • require_replicated. After the commit, the export reads synchronous_standby_names and waits until as many standbys as it asks for (FIRST n, ANY n, plain list) have flushed the block's WAL. A configured standby that is reconnecting gets a short grace before NO_SYNC_STANDBY; any error after the commit is a ReplicationError with the seq and LSN.
  • Forks. declare_fork writes a closing seal of the old incarnation, then the fork block.
    • After pg_restore into a new cluster the fork records an id window, applied per incarnation by the sealer and the verifier. A stream with no row at the fork belongs to the new incarnation alone.
    • Seals refuse with ID_FLOOR_VIOLATION when an identity sequence is behind the floor, or, while the sequence is within id_count of it, when a stream has more rows at or below the floor than the fork counted (a bounded primary-key read).
    • Scrub stamps and records are matched within one xid space.
    • A declared fork is a FORKED note. It explains a diverging checkpoint only if the checkpoint was taken in an earlier incarnation and the fork block is signed by a known key; with a verifier, an unsigned fork is a FORKED_UNSIGNED note.
    • An undeclared rewind keeps failing as XID_REWIND.

Golden vectors cover the note format (including the spec's example), the keys, a block signature and a checkpoint. A real pg_dump / pg_restore test in two containers covers the managed-service fork path. There is no test with a real synchronous standby; the no-standby case and the standby decision are tested.

Test plan

  • Tests added or updated
  • uv run pytest passes locally, including the PostgreSQL tests in tests/db
  • 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 updated if the public API changed (no public API change: integrity is private for now; docstrings updated)
  • CHANGELOG.md Unreleased bullet (nothing public yet)

A checkpoint now states the incarnation it was taken in. A signed fork
explains a diverging checkpoint only when it starts a later incarnation, so
a checkpoint taken after the fork still catches a truncated or rewritten
tail.
…ions

A stream with no row at a windowed fork has ceiling -1 in the verifier and
in the sealer's window, so its first rows after the fork belong to the new
incarnation.
Stamps and audit.scrubbed records are keyed by xid space. A stamp on an
old row that fits both its own space and a scrub since the fork goes to
whichever space's records account for it. The running-scrub window and
the retired-receipt check apply per space.
The count reads at most id_count + 1 entries of the primary key, ordered
by id, and runs only while the identity sequence is within id_count of the
floor; past that the sequence check alone remains. The docstring records
that retention weakens the count.
require_replicated reads synchronous_standby_names and needs the n
standbys it asks for (FIRST n, ANY n, or a plain list) to flush the block.
A configured standby that is not connected is waited for briefly before
NO_SYNC_STANDBY, and any error after the commit is a ReplicationError
with the seq and LSN.

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