From 6e8af21f2c6a966c31be34b1d17a9d0020f2b6cd Mon Sep 17 00:00:00 2001 From: hsinatfootprintai <149764663+hsinatfootprintai@users.noreply.github.com> Date: Thu, 3 Dec 2026 07:13:05 +0800 Subject: [PATCH] feat(doctor): warn when the audit anchor has no off-host copy (#3772 slice 4b) Advisory section only; never changes the exit code. Reads the same CONTAINARIUM_AUDIT_ANCHOR_* variables the daemon and verify-anchor read. Co-Authored-By: Claude Sonnet 5.5 --- changelog.d/3772.added.md | 1 + internal/cmd/doctor.go | 3 ++ internal/cmd/doctor_anchor.go | 61 +++++++++++++++++++++++ internal/cmd/doctor_anchor_test.go | 80 ++++++++++++++++++++++++++++++ 4 files changed, 145 insertions(+) create mode 100644 changelog.d/3772.added.md create mode 100644 internal/cmd/doctor_anchor.go create mode 100644 internal/cmd/doctor_anchor_test.go diff --git a/changelog.d/3772.added.md b/changelog.d/3772.added.md new file mode 100644 index 000000000..6d6b305c0 --- /dev/null +++ b/changelog.d/3772.added.md @@ -0,0 +1 @@ +`containarium doctor` now warns, in an advisory section that does not change the exit code, when the audit anchor exists only as a local file with no bucket or timestamp authority configured, so losing the host's disk loses the proof the audit chain was not rewritten. diff --git a/internal/cmd/doctor.go b/internal/cmd/doctor.go index 05442b570..11f814100 100644 --- a/internal/cmd/doctor.go +++ b/internal/cmd/doctor.go @@ -83,6 +83,9 @@ func runDoctor(cmd *cobra.Command, args []string) error { client := &http.Client{Timeout: 3 * time.Second} printDaemonReadiness(os.Stdout, policyreadiness.Run(ctx, policyreadiness.HTTPCheck(client, doctorDaemonURL))) + // Audit anchor off-host copy (#3772), advisory. + printDoctorAnchorAdvisory(os.Stdout) + if failed > 0 { return fmt.Errorf("doctor: %d required check(s) failed — this host cannot run the daemon's user management until fixed (see prd daemon-deploy-contract)", failed) } diff --git a/internal/cmd/doctor_anchor.go b/internal/cmd/doctor_anchor.go new file mode 100644 index 000000000..29f17821f --- /dev/null +++ b/internal/cmd/doctor_anchor.go @@ -0,0 +1,61 @@ +//go:build !windows && !containarium_client + +package cmd + +import ( + "fmt" + "io" + "os" + "strings" +) + +// anchorOffHost describes what the audit anchor is published to, as this +// shell's environment configures it. The daemon reads the same variables +// (CONTAINARIUM_AUDIT_ANCHOR_BUCKET and CONTAINARIUM_AUDIT_ANCHOR_TSA_URL), as +// does `audit verify-anchor`. +type anchorOffHost struct { + Path string // the local anchor file + LocalOnly bool // the file exists and nothing off this host receives the anchor +} + +// anchorOffHostStatus reports whether the audit anchor has a copy that does +// not live on this host. A local anchor file alone does not survive losing the +// disk: whoever can rewrite the audit chain can rewrite that file too, which is +// what an off-host copy (a bucket, or an RFC 3161 timestamp) prevents (#3772). +// +// An absent anchor file means anchoring has not run on this host, so there is +// nothing to warn about. +func anchorOffHostStatus(path, bucket, tsaURL string) anchorOffHost { + st := anchorOffHost{Path: path} + if _, err := os.Stat(path); err != nil { + return st + } + st.LocalOnly = strings.TrimSpace(bucket) == "" && strings.TrimSpace(tsaURL) == "" + return st +} + +// printAnchorOffHost renders the advisory. It prints nothing unless the anchor +// is local-only, and like the rest of doctor's advisory sections it never +// changes the exit code. +func printAnchorOffHost(w io.Writer, st anchorOffHost) { + if !st.LocalOnly { + return + } + _, _ = fmt.Fprintln(w) + _, _ = fmt.Fprintln(w, "Audit anchor (advisory — does not affect the exit code):") + _, _ = fmt.Fprintf(w, " ! %s is the only copy of the audit anchor — it lives on this host, so losing the disk loses the proof the audit chain was not rewritten.\n", st.Path) + _, _ = fmt.Fprintln(w, " Set CONTAINARIUM_AUDIT_ANCHOR_BUCKET=gs://[/] (and/or CONTAINARIUM_AUDIT_ANCHOR_TSA_URL) on the daemon to keep a copy off this host.") + _, _ = fmt.Fprintln(w, " (Read from this shell's environment; run doctor under the daemon's unit to see its settings.)") +} + +// printDoctorAnchorAdvisory resolves the anchor settings the way the daemon +// and `audit verify-anchor` do, then prints the advisory. +func printDoctorAnchorAdvisory(w io.Writer) { + path := strings.TrimSpace(os.Getenv("CONTAINARIUM_AUDIT_ANCHOR_PATH")) + if path == "" { + path = defaultAuditAnchorPath + } + printAnchorOffHost(w, anchorOffHostStatus(path, + os.Getenv("CONTAINARIUM_AUDIT_ANCHOR_BUCKET"), + os.Getenv("CONTAINARIUM_AUDIT_ANCHOR_TSA_URL"))) +} diff --git a/internal/cmd/doctor_anchor_test.go b/internal/cmd/doctor_anchor_test.go new file mode 100644 index 000000000..5d507cc8c --- /dev/null +++ b/internal/cmd/doctor_anchor_test.go @@ -0,0 +1,80 @@ +//go:build !windows && !containarium_client + +package cmd + +import ( + "bytes" + "os" + "path/filepath" + "strings" + "testing" +) + +func TestAnchorOffHostStatus(t *testing.T) { + dir := t.TempDir() + present := filepath.Join(dir, "anchors.jsonl") + if err := os.WriteFile(present, []byte("{}\n"), 0o600); err != nil { + t.Fatal(err) + } + missing := filepath.Join(dir, "none.jsonl") + + tests := []struct { + name string + path, bucket string + tsa string + wantLocalOnly bool + }{ + {"file only", present, "", "", true}, + {"blank settings count as unset", present, " ", " ", true}, + {"bucket configured", present, "gs://b/p", "", false}, + {"tsa configured", present, "", "https://tsa.example.com", false}, + {"no anchor file means nothing to warn about", missing, "", "", false}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := anchorOffHostStatus(tt.path, tt.bucket, tt.tsa).LocalOnly; got != tt.wantLocalOnly { + t.Errorf("LocalOnly = %v, want %v", got, tt.wantLocalOnly) + } + }) + } +} + +func TestPrintAnchorOffHost(t *testing.T) { + var out bytes.Buffer + printAnchorOffHost(&out, anchorOffHost{Path: "/x/anchors.jsonl", LocalOnly: true}) + got := out.String() + for _, want := range []string{"advisory", "does not affect the exit code", "/x/anchors.jsonl", "only copy", "CONTAINARIUM_AUDIT_ANCHOR_BUCKET"} { + if !strings.Contains(got, want) { + t.Errorf("advisory missing %q:\n%s", want, got) + } + } + + out.Reset() + printAnchorOffHost(&out, anchorOffHost{Path: "/x", LocalOnly: false}) + if out.Len() != 0 { + t.Errorf("an anchor with an off-host copy should print nothing, got:\n%s", out.String()) + } +} + +func TestPrintDoctorAnchorAdvisory_ReadsTheDaemonsVariables(t *testing.T) { + path := filepath.Join(t.TempDir(), "anchors.jsonl") + if err := os.WriteFile(path, []byte("{}\n"), 0o600); err != nil { + t.Fatal(err) + } + t.Setenv("CONTAINARIUM_AUDIT_ANCHOR_PATH", path) + t.Setenv("CONTAINARIUM_AUDIT_ANCHOR_BUCKET", "") + t.Setenv("CONTAINARIUM_AUDIT_ANCHOR_TSA_URL", "") + + var out bytes.Buffer + printDoctorAnchorAdvisory(&out) + if !strings.Contains(out.String(), path) { + t.Errorf("local-only anchor should warn, got:\n%s", out.String()) + } + + t.Setenv("CONTAINARIUM_AUDIT_ANCHOR_BUCKET", "gs://b/p") + out.Reset() + printDoctorAnchorAdvisory(&out) + if out.Len() != 0 { + t.Errorf("with a bucket configured nothing should print, got:\n%s", out.String()) + } +}