Repository navigation
Store salted commitments for erasable entry data - #75
Open
WiktorB2004 wants to merge 15 commits into
Open
WiktorB2004 wants to merge 15 commits into
WiktorB2004 wants to merge 15 commits into
Conversation
The encoder is a custom format close to JCS: keys in UTF-16 order, numbers as plain decimal text without trailing zeros, and -0 as 0, so values read back from JSONB encode to the same bytes. Golden vectors freeze the encoding, the commitment preimage and the leaf hash.
With AuditTrail(integrity=Integrity()) both tables get xid (xid8, defaulting to pg_current_xact_id()), seal_commitments, seal_salts and seal_erased, plus an (xid, id) index. Without it the tables are unchanged. Reads list their columns instead of selecting the table, so they never return salts and work on schemas without the new columns.
insert_transaction and insert_activities commit to the erasable groups of each row with fresh salts, so the flush listener, log(), durable entries and the scrub record all get them, and a retried durable write draws new salts. Every activity row carries the same keys, since executemany takes its columns from the first row.
scrub and scrub_actor remove the salt of each group they erase and record the group in seal_erased with pg_current_xact_id(), the xid of their own audit.scrubbed entry. coalesce keeps the stamp on a NULL seal_erased.
It adds the columns, then the xid default in a second statement (the default is STABLE, so one statement would stamp every old row), and the (xid, id) indexes. Each attempt runs in a savepoint with a short lock_timeout and is retried after a random pause.
The DB tests recompute every commitment from the row as read back, under the sync and both async drivers. Hypothesis checks that the canonical encoding survives a JSONB round trip on asyncpg and psycopg, and that the inet text used in commitments matches PostgreSQL's output.
4 of 6 tasks
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
First step of tamper evidence: writes store a transaction id and salted commitments, and scrubs leave a pointer to their own record. Nothing is sealed yet, and the feature is private:
audit_trail.integrityis not exported or documented.audit_trail/integrity/: a canonical JSON encoder (keys in UTF-16 order, numbers as plain decimal text,-0as0), SHA-256 commitments with a 16-byte salt per group, theXid8column type, and theIntegrityoption. The encoder refuses\u0000, lone surrogates, numbers over 4,300 digits (checked before aDecimalis formatted) and values nested past the recursion limit.AuditTrail(integrity=Integrity())/build_tables(integrity=True)declarexid xid8 NOT NULL DEFAULT pg_current_xact_id(),seal_commitments,seal_saltsandseal_erasedon both tables, each with an(xid, id)index. Without the option (the default) the tables, DDL and queries stay exactly as they were.query.pyandtesting.pylist their columns instead of selecting the whole table, so they never return salts. A test keeps those lists equal to the table columns minus the integrity ones.label,changes,payloadandactor) for the flush listener,log(), durable entries and theaudit.scrubbedrecord. Every row has the same keys, because executemany takes its columns from the first row. A retried durable write draws new salts.remote_addris committed to as the text PostgreSQL stores. Addresses PostgreSQL would store as a different text (01.2.3.4,10/8,1.2.3.4.) are refused rather than guessed.on_errorlike a database error: logged and skipped under"log", raised under"raise",AuditWriteErrorfor durable andfail_closedwrites. With integrity on and the columns missing, the failure namesmigrations.add_integrity()under every policy.scrub/scrub_actorremove the salt of each erased group and setseal_erased[group]to the scrub's xid, which is also the xid of itsaudit.scrubbedrow. Rows written before integrity are not stamped.migrations.add_integrityruns theALTERs (ADD COLUMN, thenSET DEFAULTon its own) in a savepoint withlock_timeoutand jittered retries, and restoreslock_timeouteven when it fails. Old rows keepxidNULL.migrations.add_integrity_indexesthen builds the indexes online on an AUTOCOMMIT connection:ON ONLYon each parent,CREATE INDEX CONCURRENTLYon each partition, thenATTACH PARTITION. It is idempotent and rebuilds a failed partition build.tests/fixtures/integrity/vectors/.hypothesisis added as a dev dependency.Closes #69
Test plan
Xid8. DB tests cover commitments under sync, asyncpg and psycopg async sessions, durable retry salts, scrub stamps (including rows written before integrity), encoder refusals and missing columns under"log","raise"andfail_closed,add_integrity(upgrade, idempotent,lock_timeoutrestored, retry and give-up),add_integrity_indexes(valid index tree, idempotent, failed build rebuilt, lock timeout retry), executemany key behaviour, the Hypothesis JSONB round trip, andinettext against PostgreSQL for strict and loose inputsuv run pytestpasses locally, including the PostgreSQL tests intests/dbAUDIT_TEST_PG_IMAGE=postgres:14) as well as the default imageruff check,ruff format --checkandmypypassCHANGELOG.mdUnreleasedbullet if callers should notice: N/A, there is no user-facing change yet