#125 Audit log retention: archive, then delete, with a verifiable chain
Originally created by: bosd
Implements the third slice of [#119]: a configurable audit retention period with a clear rule at expiry — archive, then delete.
Stacked on [#124]: this branch builds on feat/audit-export, so the PR currently shows that commit too. Once [#124] merges (or on a rebase) only the retention commit remains; review f280820 for this change. The read-only auditor role from [#119] follows in a separate PR.
Summary
-
audit_retention_daysinserver.toml, orDOPBASE_AUDIT_RETENTION_DAYS, sets how long audit records are kept. Unset — the default — keeps the whole log, and0is rejected as a misconfiguration rather than accepted as a policy that deletes everything. -
At expiry the rule is archive, then delete: expired records are written to
audit-archive/in the data directory as JSON lines (one file per contiguous sequence range, e.g.audit-000001-000042.jsonl, each line carrying every stored column including the chain fields), and only then deleted, in the same transaction, together with an anchor recording the newest removed record's position and hash. -
Verification continues from that anchor, so a pruned log still verifies as one unbroken chain: migration
0016_audit_chain_anchorstores it,dopbase audit verifypicks the chain back up there and reportsPruned through: sequence N(prunedSeqin the JSON report and API response), and appends after a full prune continue the sequence instead of restarting at genesis. -
Only the oldest contiguous run of records is pruned. A record timestamped before a retained predecessor — which a backwards clock step can produce — would leave a hole no anchor can describe, so it stays until everything before it has expired too.
-
Retention applies once at startup and then on each audit write, inside the caller's transaction, following the same opportunistic pattern as the 30-day session pruning (no scheduler exists in this codebase). A pruning failure is logged and never fails the audited operation.
-
Archives are not part of
dopbase backup snapshot(database only); the docs and the generatedserver.tomlreference tell operators to back upaudit-archive/separately.
Compatibility
- Additive: unset retention preserves today's behavior exactly (the whole log is kept). The migration only adds the anchor table; reverting
0016drops the anchor and with it the ability to verify across a past prune, while keeping the records themselves. - The chain stays sound through pruning: the anchor carries the deleted head's hash, so the first surviving record's
prevHashstill matches, and the sequence-gap check covers the boundary. prunedSeqjoins the verify response as an additive field; existing consumers and the UI ignore it.
Verification
cargo fmt --checkcargo clippy --locked --all-targets --all-features -- -D warningscargo test: services 24 (new tests cover archiving 5 expired records with chain fields while the chain keeps verifying, chaining continues after a full prune instead of restarting at genesis, idempotent re-application, and the clock-skew case where an expired record behind a retained one is kept), config 22 (file and environment parsing, precedence,0and non-numeric rejection, template uncommenting), cli 212 (end-to-end: a quiet log ages out, the next audit write prunes it into the archive, anddopbase audit verifystays intact reporting the pruned sequence), main 27, constants 4, daemon 15, backups 9, users 3. The 3 pre-existingcommands::export::*failures need the util-linuxscriptbinary and are unrelated to this change.bun run test:docs,bun run test:github(17/17),bunx prettier --checkon the changed Markdown.
Checklist
- [x] I kept this pull request focused on one problem.
- [x] I added or updated tests for behavior changes, or explained why tests are not needed.
- [x] I updated the documentation and
CHANGELOG.mdwhen public behavior changed, or explained why they are not needed. - [x] I updated relevan documentation inside
/docs
Originally posted by: bosd
Superseded by [#131], which merged this work (rebased onto main, with review hardening on top). Closing.
Related
Tickets:
#131Ticket changed by: bosd