Repository navigation
feat(doctor): warn when the audit anchor has no off-host copy (#3772 slice 4b) #3896
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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. |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -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://<bucket>[/<prefix>] (and/or CONTAINARIUM_AUDIT_ANCHOR_TSA_URL) on the daemon to keep a copy off this host.") | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The TSA setting in the advisory needs its trust bundle. The advisory tells operators to set Name the trust-bundle setting wherever the TSA URL is named. Both TSA settings are required together: Suggested fix
Committable suggestion
|
||||||
| _, _ = 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")) | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Preserve the daemon's configured path exactly. The daemon uses Use the raw environment value. Select
|
||||||
| if path == "" { | ||||||
| path = defaultAuditAnchorPath | ||||||
| } | ||||||
| printAnchorOffHost(w, anchorOffHostStatus(path, | ||||||
| os.Getenv("CONTAINARIUM_AUDIT_ANCHOR_BUCKET"), | ||||||
| os.Getenv("CONTAINARIUM_AUDIT_ANCHOR_TSA_URL"))) | ||||||
| } | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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()) | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Report anchor inspection failures separately from absence.
If
os.Statreturns a permission or I/O error, this returnsLocalOnly=false. Doctor then prints nothing, even when an existing anchor has no off-host configuration.Keep the silent return for non-existence. For other errors, print an advisory that the anchor status could not be checked. Keep the exit code unchanged.
Based on learnings, filesystem inspection errors must not all be classified as non-existence.
Source: Learnings