Menu ▾ ▴

#125 Audit log retention: archive, then delete, with a verifiable chain

closed
nobody
None
2026-10-23
2026-10-17
Anonymous
No

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_days in server.toml, or DOPBASE_AUDIT_RETENTION_DAYS, sets how long audit records are kept. Unset — the default — keeps the whole log, and 0 is 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_anchor stores it, dopbase audit verify picks the chain back up there and reports Pruned through: sequence N (prunedSeq in 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 generated server.toml reference tell operators to back up audit-archive/ separately.

Compatibility

  • Additive: unset retention preserves today's behavior exactly (the whole log is kept). The migration only adds the anchor table; reverting 0016 drops 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 prevHash still matches, and the sequence-gap check covers the boundary.
  • prunedSeq joins the verify response as an additive field; existing consumers and the UI ignore it.

Verification

  • cargo fmt --check
  • cargo clippy --locked --all-targets --all-features -- -D warnings
  • cargo 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, 0 and non-numeric rejection, template uncommenting), cli 212 (end-to-end: a quiet log ages out, the next audit write prunes it into the archive, and dopbase audit verify stays intact reporting the pruned sequence), main 27, constants 4, daemon 15, backups 9, users 3. The 3 pre-existing commands::export::* failures need the util-linux script binary and are unrelated to this change.
  • bun run test:docs, bun run test:github (17/17), bunx prettier --check on 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.md when public behavior changed, or explained why they are not needed.
  • [x] I updated relevan documentation inside /docs

Related

Tickets: #119
Tickets: #124
Tickets: #126
Tickets: #131

Discussion

  • Anonymous

    Anonymous - 2026-10-23

    Originally posted by: bosd

    Superseded by [#131], which merged this work (rebased onto main, with review hardening on top). Closing.

     

    Related

    Tickets: #131

  • Anonymous

    Anonymous - 2026-10-23

    Ticket changed by: bosd

    • status: open --> closed
     

Log in to post a comment.