Skip to content

fix(audit): read and anchor the postgres chain the [storage] section selects - #797

Merged
vladimirrott merged 4 commits into
lacs-project:mainfrom
Georgefifth:fix/558-postgres-anchor-verify
Nov 18, 2026
Merged

vladimirrott merged 4 commits into
lacs-project:mainfrom
Georgefifth:fix/558-postgres-anchor-verify

Conversation

@Georgefifth

Copy link
Copy Markdown
Contributor

Problem

#558: with [storage] backend = "postgres" and an anchor configured, the daemon anchors the chain every 15 minutes through the AuditStore trait — but both readers disagreed with the daemon:

  • verify_configured_anchor short-circuited every Postgres config with CannotVerify { "checkpoint anchoring is implemented for the sqlite backend only" }, so audit verify exits 2 permanently.
  • run_audit_checkpoint ignored [storage] and opened a SQLite store only, so the CLI could not anchor a Postgres chain either.

A correctly configured Postgres deployment therefore got a permanent exit 2, which teaches the operator to ignore 2.

Solution

  • verify_configured_anchor reads chain rows via PostgresStore::read_chain_rows when [storage] selects postgres, and runs the same verify_against_anchor cross-check. The SQLite path is unchanged.
  • run_audit_checkpoint honours [storage] the same way, resolving the signing key through the shared store resolver (the CLI guard test forbids default_database_path in reader paths).
  • New [audit] checkpoint_db in config.toml, mapped to SYSKNIFE_CHECKPOINT_DB by apply_defaults_to_env (env still wins). This gives the daemon and every CLI reader one anchor source, which fixes the sudo sysknife audit verify case from the issue where the environment reset hid the anchor URL. The issue leaned toward config.toml for this; I followed that.
  • Docs: SECURITY.md shows the config.toml source alongside the systemd unit; configuration.md documents the mapping and all three consumers; the-audit-chain.md drops the stale "SQLite backend only" claim and corrects "Three checks run" to four (the status cross-check).

Result

Done-when from the issue, as a live-Postgres contract test in cli_smoke.rs (postgres_backend_checkpoint_anchors_and_verify_reports_truncation): seed a Postgres chain, anchor it with the real sysknife audit checkpoint binary, truncate the tail with raw SQL, expect audit_anchor.status == "truncated" and exit 1 from audit verify --json.

  • Red on main (the pre-fix binary reports CannotVerify/exit 2), green here.
  • cargo nextest run --workspace --locked: 2129 passed, 0 failed.
  • Live Postgres suites: cli_smoke --include-ignored 26/26, daemon postgres_store 13/13.
  • cargo fmt --check and cargo clippy --workspace --all-features --all-targets -- -D warnings clean.
  • New unit test pins the config mapping and env-precedence (checkpoint_db_maps_to_env_and_env_wins).

Fixes #558

…selects

The daemon anchors every 15 minutes through the AuditStore trait, so a
Postgres chain IS anchored -- but both readers disagreed with the daemon:
verify_configured_anchor short-circuited every postgres config with
CannotVerify ("sqlite backend only"), and audit checkpoint ignored
[storage] and opened a local SQLite file. A correctly configured Postgres
deployment got a permanent exit 2 from audit verify, which teaches the
operator to ignore 2.

- verify_configured_anchor reads chain rows from PostgresStore when
  [storage] selects postgres; the anchor cross-check now runs there too.
- run_audit_checkpoint honours [storage] the same way.
- New [audit] checkpoint_db in config.toml maps to SYSKNIFE_CHECKPOINT_DB
  via apply_defaults_to_env, so the daemon and CLI readers (including
  sudo runs with a reset environment) share one anchor source. An
  explicit env var still wins.
- Docs: SECURITY.md points at the config.toml source; configuration.md
  describes the mapping; the-audit-chain.md drops the stale
  SQLite-only claim and counts the fourth (status) check.
- Contract test per the issue's Done-when: anchor a postgres chain via
  the CLI, truncate the tail, expect Truncated (exit 1) from audit
  verify. Red on main, green here.

Fixes lacs-project#558

Signed-off-by: yap <[email protected]>

@vladimirrott vladimirrott left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed at 0bab5b3. The fix works and your new test proves it. One blocking item: CI never runs that test.

You took the issue's done-when literally and drove it through the real binary: seed a Postgres chain, audit checkpoint it, delete the tail with raw SQL, and expect truncated with exit 1 from audit verify --json. Giving each run its own search_path schema, and refusing any URL without sysknife_test in it, lets the test share a database with the existing contract tests.

I ran it against Postgres 17 in a podman pod with loopback only, streaming the tree in from this head:

[pristine]                       running 1 test / test result: ok. 1 passed     rc=0
[M1 runner.rs reset to main]     panicked at cli_smoke.rs:957  audit checkpoint must anchor a postgres chain
                                 stderr: audit database not found at /tmp/.tmpC666lN/daemon.sqlite   rc=101
[M2 verify returns CannotVerify  panicked at cli_smoke.rs:993  a truncated postgres chain with an anchor
    for postgres]                must fail verify with exit 1                                    rc=101
[pristine again]                 running 1 test / test result: ok. 1 passed     rc=0

M1 is your whole production hunk reverted; M2 puts the old short-circuit back in verify_configured_anchor only. Both go red at the step you would expect.

Blocking: postgres_backend_checkpoint_anchors_and_verify_reports_truncation is #[ignore], and the postgres-contract job runs cli_smoke with one test named:

cargo test -p sysknife-cli --test cli_smoke --locked
  audit_verify_exits_with_code_1_when_anchor_is_truncated -- --ignored --exact

git grep -n postgres_backend_checkpoint_anchors_and_verify_reports_truncation 0bab5b30 -- .github scripts prints nothing, so a later change could break the Postgres anchor path with every check green. Please add a step for it beside that one in .github/workflows/ci.yml, and the same pair of lines in both postgres branches of scripts/ci-local.sh (lines 410 and 461), so the local gate matches. That puts a workflow file in the diff, and I read those by hand before merging, which I'm glad to do here.

Not blocking, but I'd like the docs fixed in the same push. SECURITY.md and docs/configuration.md say that [audit] checkpoint_db in ~/.config/sysknife/config.toml gives the daemon and sudo sysknife audit verify one shared source. The packaged daemon runs as sysknife (User=sysknife in packaging/sysknife-daemon.service), whose home is /var/lib/sysknife (packaging/sysknife-sysusers.conf), and config_dir() resolves from XDG_CONFIG_HOME or HOME. So the daemon reads /var/lib/sysknife/.config/sysknife/config.toml, and a sudo run under the default env_reset reads root's. On a packaged install that is two files, and the docs should say the key belongs in both, or name the one place each reads.

Optional: the key lookup and load is now written out twice, once per branch of run_audit_checkpoint, and the two copies are identical. Loading the key before the if let would leave one.

…ates

The new contract test was #[ignore]d and no gate named it, so the postgres
anchor path could break with every check green. Add the exact-match step
beside the existing anchor exit-code contract in ci.yml and in both
postgres branches of ci-local.sh.

Also correct the config.toml docs: the packaged daemon (User=sysknife)
and a sudo CLI run under env_reset resolve config_dir() from different
HOMEs, so [audit] checkpoint_db belongs in both files -- say that
instead of implying one shared source.

Fold the duplicated audit-key lookup in run_audit_checkpoint back into
one load before the backend branch.

Signed-off-by: yap <[email protected]>
@Georgefifth

Copy link
Copy Markdown
Contributor Author

All three points addressed in 140dc77, thanks for the mutation runs and the podman verification.

Blocking — CI never runs the test: added the exact-match step beside the existing anchor contract in all three places: .github/workflows/ci.yml (Run live CLI postgres-backend anchor contract) and both postgres branches of scripts/ci-local.sh (the SYSKNIFE_TEST_POSTGRES_URL branch and the container branch). Verified locally against a live Postgres 17 with the exact CI commands — both anchor contracts and the daemon postgres_store suite pass. tests/release/postgres-contract-guard.test.sh reports the new wiring: "2 live contract file(s), each run with an ignore flag by ci.yml and by both ci-local.sh branches".

Docs — the two-config.toml problem: you're right, I overclaimed "one shared source". SECURITY.md and docs/configuration.md now name both files explicitly (daemon reads /var/lib/sysknife/.config/sysknife/config.toml, a default-env_reset sudo run reads root's) and say the key belongs in both; the AuditSection::checkpoint_db doc comment follows. The config.toml mapping still earns its keep — it decouples the anchor from whichever shell happens to invoke the CLI — but the docs no longer pretend a single file covers both processes.

Optional — duplicated key load: folded into one load_or_generate before the backend branch; only the row read stays per-backend.

Workspace suite re-run after the refactor: 2129/2129, fmt/clippy clean, test count unchanged so the baseline file stays as-is.

Resolve cli_smoke.rs (keep both the postgres-backend anchor contract and
the new REPL exit-code tests) and workspace-tests.json (regenerated on
the merged tree: 2146).

Signed-off-by: yap <[email protected]>

@vladimirrott vladimirrott left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the follow-up. You took all three points from my last review. The CI step is the one that matters most: postgres-contract now runs postgres_backend_checkpoint_anchors_and_verify_reports_truncation by exact name, in ci.yml and in both Postgres branches of scripts/ci-local.sh, so a break in the Postgres anchor path turns a required check red. The rewritten config notes are right too. A packaged daemon and a sudo CLI run read two different config.toml files, and your docs now say which.

Reviewed at 1701c234c386b21a079150a1c8c2aa7180f13c34. I ran nothing from this branch on my own machine. The live run used a pod with no network, postgres:17-alpine beside rust:1-slim, after your key-loading refactor:

=== [pristine]
running 1 test
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 30 filtered out
=== [M1-production-hunk-reverted]
assertion `left == right` failed: audit checkpoint must anchor a postgres chain
stderr: audit database not found at /tmp/.tmpvGvzkY/daemon.sqlite
rc=101
=== [M2-verify-short-circuits-postgres]
assertion `left == right` failed: a truncated postgres chain with an anchor must fail verify with exit 1
rc=101
=== [pristine-again]
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 30 filtered out

M1 puts main's runner.rs back, and M2 makes verify return CannotVerify before it reads Postgres. The CI log for this head shows the new step selecting the test, not filtering it away:

Run live CLI postgres-backend anchor contract
running 1 test
test postgres_backend_checkpoint_anchors_and_verify_reports_truncation ... ok
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 30 filtered out; finished in 0.64s

I also had my merge gate observe one guard per suite:

maintainer-merge verify-part 797 1701c234c386b21a079150a1c8c2aa7180f13c34 checkpoint_db_maps_to_env_and_env_wins 's/^                set_if_absent("SYSKNIFE_CHECKPOINT_DB", url);$/                let _ = url;/' rust
  receipt recorded for #797 at 1701c234 (observed: clean pass, mutated fail)
maintainer-merge verify-part 797 1701c234c386b21a079150a1c8c2aa7180f13c34 tests/release/postgres-contract-guard.test.sh 's/postgres_backend_checkpoint_anchors_and_verify_reports_truncation -- --ignored --exact/postgres_backend_checkpoint_anchors_and_verify_reports_truncatio -- --ignored --exact/' shell
  receipt recorded for #797 at 1701c234 (observed: clean pass, mutated fail)

The second one shows the existing postgres-contract-guard.test.sh already catching a one-character misspelling of your new step's test name, so the step cannot quietly select zero tests later.

The baseline reads 2146: main's 2145 plus your non-ignored config test. The live test is #[ignore]d, so it stays out of the count. All three commits carry a sign-off.

I'm approving. Because the PR edits .github/workflows/ci.yml, I merge it by hand instead of through my usual automated path, and that step is mine. You have nothing left to do here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

On Postgres the daemon anchors the audit chain but every reader reports the anchor unsupported and exits 2

2 participants