Skip to content
Dhruvie.
All writing4 min read

Regulated software · PostgreSQL · Python · Architecture

Append-only by design: audit trails you can actually trust

In regulated manufacturing, “we never update that table” is not an answer. Here is how I make history impossible to rewrite quietly: database permissions, hash chains, and rules that live where they can be tested.

By Dhruv Suthar

Sooner or later, someone reviewing a regulated system asks a simple question: can anyone change this record after the fact? If the honest answer is “only if they go around the application”, you do not have an audit trail. You have a convention.

I ran into this while prototyping a tray-tracking system for sterile manufacturing, where trays of vials move through a fixed sequence of stages and every move has to be accounted for. I designed it with FDA 21 CFR Part 11 requirements in mind. These are the design choices I would make again.

Make the database say no

The first line of defence is not application code. It is the permissions of the database role the application connects with. Migrations run as an owner role; the running application gets a role that can insert into the ledger tables but cannot update or delete anything in them.

-- The application can add history, never rewrite it
REVOKE UPDATE, DELETE, TRUNCATE ON audit_trail FROM app_user;
REVOKE UPDATE, DELETE ON tray_movement FROM app_user;

Now a bug, a careless script, or a compromised application server cannot edit history through the normal connection. The database refuses before any of your code gets a say.

Chain the records together

Permissions protect against the application. They do not protect against someone with owner access editing a row directly. For that, each audit record stores a SHA-256 hash of its own contents together with the previous record’s hash. Edit a row and its stored hash no longer matches its contents; recompute that hash too, and the link from the next row breaks.

# Each row commits to its content and to the row before it.
# Fields are joined with a separator and timestamps are written in UTC,
# so the same row always produces the same hash.
row_hash = sha256(join("|", prev_hash or "", event_type, payload, actor, created_at_utc))

A verification function walks the chain and reports the first record that does not line up. The honest way to describe this is tamper-evident, not tamper-proof: it cannot stop a determined administrator, but it makes a quiet edit hard to hide. Someone with owner access could still rewrite every hash after the row they changed; catching that needs the latest hash recorded somewhere outside the database.

Keep the rules where they can be tested

The rules that decide whether a tray may move are the heart of the system, so I kept them in a pure Python package with no Django, no database and no network code. An import-linter contract fails the build if anyone tries to import those layers into it.

def check_move(tray: Tray, target_stage: int, grants: list[Grant]) -> Decision:
    if target_stage == tray.stage + 1:
        return Decision.allow()
    grant = next((g for g in grants if g.covers(tray, target_stage)), None)
    if grant:
        return Decision.allow(consumes=grant)
    return Decision.refuse("Out of sequence", remedy="Raise a skip request")

Because the engine is pure, every interlock has a plain unit test, and property-based tests with Hypothesis generate move sequences looking for any path that skips a stage. The server is the only place a move is decided; any check on the tablet is advisory and must never be stricter than the server.

Exceptions belong in the workflow

Real processes have exceptions. Pretending they do not just pushes them onto paper. Instead, an out-of-sequence move is refused with a remedy: raise a skip request. A manager approves it, and the grant is good for that one stage, once. Events captured while a device was offline are not discarded; they are quarantined and adjudicated by a supervisor. The people who raise an exception and the people who approve it are different roles.

No passwords to protect

Sign-in goes through OpenID Connect, designed to federate to the company’s existing directory; in the prototype, Keycloak stands in for it. The application stores no passwords at all, which removes a whole class of findings from any security review, and every action in the audit trail belongs to a real, centrally managed identity.

What this does not do

None of this makes a system compliant on its own. Validation protocols, procedures and training are the customer’s quality process, and a prototype is not a validated system. Where the requirements were genuinely unresolved, such as how quantities should reconcile when trays are split or merged, the system refuses those operations rather than guessing. Putting a number nobody agreed on into a GMP record is worse than not supporting the feature yet.

  • Enforce “append-only” with database permissions, not good intentions.
  • Chain records with hashes so out-of-band edits become visible.
  • Keep business rules pure, so they can be tested exhaustively.
  • Model exceptions as approved, auditable requests.
  • Refuse what is not specified instead of inventing behaviour.

Related work

The project behind this note

Let’s work together.