---
title: "auditctl"
description: "Verify every hash-chained audit table from the command line, and export a signed checkpoint that anchors the chains outside the database."
---

The operator's answer to "is the audit trail intact?". It walks every
hash-chained table for every organization, reports the first broken link in
each chain, and can sign the current chain heads so that history can later be
proved unchanged by somebody who does not trust the database.

```text
backend/cmd/auditctl
```

In the image it is installed as `/app/auditctl`. For what the chains are and
what they prove, see [Audit trail](/concepts/audit-trail).

## Synopsis

```bash
auditctl verify
auditctl checkpoint
auditctl verify --checkpoint FILE
auditctl verify-pack PACK.json PACK.json.sig
auditctl --version
```

| Command | Effect |
|---|---|
| `verify` | Recomputes every digest in every chain and prints one line per chain. Changes nothing |
| `checkpoint` | Verifies, then prints a signed JSON checkpoint of every chain head to standard output |
| `verify --checkpoint FILE` | Verifies, authenticates `FILE`, and checks that every head it anchors is still present with the digest that was signed |
| `verify-pack PACK SIG` | Checks a downloaded [evidence pack](/how-to/create-an-evidence-pack) against its detached signature: the SHA-256, then the HMAC-SHA256. Needs no database |

## Environment

| Variable | Needed by | Purpose |
|---|---|---|
| `DATABASE_URL` | all but `verify-pack` | A connection as the **migration owner**, the role `migrate` runs as. In the production Compose stack that is the value of `MIGRATION_DATABASE_URL` |
| `TOKEN_ENCRYPTION_KEY` | `verify-pack` | The hex key the API runs with; the pack-signing key is derived from it. A pack signed before the key was rotated reports the key id it needs |
| `AUDIT_CHECKPOINT_KEY` | `checkpoint`, `verify --checkpoint` | The signing key, at least 32 bytes. Generate one with `openssl rand -hex 32` |

The application role sees one organization at a time under row-level security,
so with it every chain would read as empty and intact. `auditctl` checks the
role before doing any work and refuses to run as one that cannot read tenant
tables.

<Warning>
Keep `AUDIT_CHECKPOINT_KEY` out of the database, out of the deployment's
environment file and off the database host. Its whole value is that a database
administrator does not have it. Pass it for the one invocation that needs it,
from wherever you keep the checkpoints.
</Warning>

## What it checks

| Table | What it records | Scope |
|---|---|---|
| `identity_audit_events` | Administrative actions: sign-ins, role changes, credentials, SSO, policy edits | One chain per organization |
| `finding_events` | Who suppressed, accepted, reopened or resolved a finding | One chain per organization |
| `verdicts` | What a policy decided about a scan | One chain per organization |
| `agent_actions` | Every authorization decision made for an AI agent | One chain per organization |
| `staff_audit_events` | Everything TRUSTIVAN staff did in the staff console | One platform-wide chain |

```text
TABLE                  ORGANIZATION                          STATUS  SEALED  REMOVED  UNSEALED  HEAD
identity_audit_events  3f0c…                                 intact  4102    0        0         4102
finding_events         3f0c…                                 intact  918     240      1377      1158
agent_actions          3f0c…                                 intact  66      0        0         66
staff_audit_events     (platform)                            intact  31      0        0         31
4 chain(s), 5117 sealed row(s) verified, 0 broken
```

- **SEALED** is the number of rows whose digest and link were recomputed and
  matched.
- **REMOVED** counts rows that were deleted lawfully (the retention sweep, or
  a cascade from a deleted finding, scan or agent) and left a removal record.
  A gap in a chain with no removal record is reported as a break.
- **UNSEALED** counts rows written before the table was chained. They are not
  covered by the chain. A checkpoint records the count, so the set cannot grow
  unnoticed.

A broken chain reads `BROKEN at row N: <reason>` and names the first row that
does not verify.

## Exit codes

| Code | Meaning |
|---|---|
| `0` | Every chain verifies, and matches the checkpoint if one was given |
| `1` | A chain is broken, the checkpoint's signature does not verify, or history differs from the checkpoint |
| `2` | The check could not run: configuration, connection, or an unreadable file |

`1` and `2` are distinct so that a scheduled job can page on `1` and retry on
`2`.

## Checkpoints

The chains use an unkeyed SHA-256 computed inside the database. Somebody who
can disable a trigger can rewrite a chain from any row forward, and `verify`
on its own will pass it. A checkpoint is the control against that.

```bash
AUDIT_CHECKPOINT_KEY=... auditctl checkpoint > checkpoint-$(date -u +%F).json
```

The file holds the head of every chain (sequence number and digest) and an
HMAC-SHA256 over the whole document. Rewriting any row at or below a
checkpointed head changes that head's digest, and producing a checkpoint that
matches the rewritten chain needs the key.

```bash
AUDIT_CHECKPOINT_KEY=... auditctl verify --checkpoint checkpoint-2026-10-13.json
```

```text
checkpoint of 2026-10-13T06:00:00Z (key 9c1f2ab04d7e3351, 4 chains):
  every anchored head is present and unchanged
```

| Line | Meaning |
|---|---|
| `FAIL … history at or below it was rewritten` | The row that was the head no longer has the signed digest |
| `FAIL … is gone` | The head row was deleted with no removal record |
| `FAIL … unsealed row(s) appeared` | Rows without a seal were inserted after the checkpoint |
| `FAIL … no deletion is on record` | An organization the checkpoint covers no longer exists and was not erased through the product |
| `note … organization erased since the checkpoint` | The organization was deleted through the product; its chains went with it |
| `note … row(s) removed since the checkpoint` | Rows left through retention or a cascade, each with a removal record |

`checkpoint` refuses to sign when any chain is broken: a checkpoint of a
tampered chain would anchor the tampering.

### What a checkpoint does and does not prove

It proves that history **up to the moment it was signed** is unchanged, to
anybody holding the file and the key. It says nothing about rows written
afterwards, so take checkpoints on a schedule ([below](#scheduled-checkpoints)):
the interval is the window in which a rewrite by a database administrator would
go undetected.

It proves nothing if it is stored where the database administrator can replace
it and the key. Store checkpoints off the host.

### Scheduled checkpoints

The worker takes checkpoints itself when `AUDIT_CHECKPOINT_DIR` is set:

| Variable | Default | |
|---|---|---|
| `AUDIT_CHECKPOINT_DIR` | unset: nothing is scheduled | The directory checkpoints are written to, as `checkpoint-<UTC time>.json`, mode `0600` |
| `AUDIT_CHECKPOINT_KEY` | none; required with the directory | At least 32 bytes. The worker refuses to start without it |
| `AUDIT_CHECKPOINT_INTERVAL` | `24h` | A Go duration, at least `1m`. The first run is at worker start |

Each run does what `auditctl verify --checkpoint <newest file>` followed by
`auditctl checkpoint` would: it verifies every chain, proves history up to the
newest checkpoint already in the directory, and only then writes a new one. It
writes nothing, and logs `periodic task failed` with the reason, when a chain
is broken, when history below the previous checkpoint changed, or when the
previous file does not authenticate with the key (it was edited, or the key
changed: move the old files aside to start a new series). It runs as the
worker's own database role, one organization at a time, so it needs no
migration-owner credentials.

The production Compose file mounts a volume, `audit_checkpoints`, at
`/audit-checkpoints` in the worker, so `AUDIT_CHECKPOINT_DIR=/audit-checkpoints`
is all it needs there; on Kubernetes, mount a volume yourself and name its path.

The trade is that the worker holds the key. A checkpoint still proves nothing
until it has left the host, so point the directory at storage the database's
administrators cannot write, or at a directory your off-host backup copies, and
verify against those copies. If the key cannot live on the worker's host, run
`auditctl checkpoint` from another host instead.

A lawful removal and a removal by somebody who also forged its removal record
look the same to `verify`. The checkpoint comparison reports how many rows
were removed since it was signed, so the number can be compared with what your
retention settings would remove.

## Run it on a deployment

```bash
docker exec -e DATABASE_URL="$MIGRATION_DATABASE_URL" <api-container> /app/auditctl verify
```

## See also

- [Audit trail](/concepts/audit-trail)
- [Tenant isolation](/security/tenant-isolation)
