Repository navigation
feat(audit): verify-anchor reports an epoch break and checks its archive (#3772 slice 3a) #3891
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
8c7170e
4239adb
0245671
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 @@ | ||
| - **`audit verify-anchor` reports a recorded anchor epoch break** (#3772). A break made by `audit anchor new-epoch` is printed as a break (when, who, why, the archive and its sha256) instead of being invisible, and the run fails if the archive is missing, has changed, or does not end at the anchor the break records. A drop in the anchor with no break record still fails as before. The bucket and timestamp-token copies are not yet checked across a break. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| package audit | ||
|
|
||
| import ( | ||
| "context" | ||
| "crypto/sha256" | ||
| "encoding/hex" | ||
| "encoding/json" | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
| "testing" | ||
| "time" | ||
| ) | ||
|
|
||
| func problems(cs []BreakCheck) []string { | ||
| var out []string | ||
| for _, c := range cs { | ||
| out = append(out, c.Problem) | ||
| } | ||
| return out | ||
| } | ||
|
|
||
| // #3772 slice 3: a recorded break is evidence only while the archive it names | ||
| // is still there, unchanged, and ends where the break says it did. | ||
| func TestVerifyBreaks_IntactArchiveIsVerified(t *testing.T) { | ||
| s, _ := newEpochSink(t, 10, 20) | ||
| b, err := s.NewEpoch(epochOpts()) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| cs, err := s.VerifyBreaks() | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if len(cs) != 1 || cs[0].Problem != "" || cs[0].Break.ArchivedSHA256 != b.ArchivedSHA256 { | ||
| t.Fatalf("checks = %+v", cs) | ||
| } | ||
| } | ||
|
|
||
| func TestVerifyBreaks_NoBreaksIsNoChecks(t *testing.T) { | ||
| s, _ := newEpochSink(t, 10) | ||
| cs, err := s.VerifyBreaks() | ||
| if err != nil || len(cs) != 0 { | ||
| t.Fatalf("checks = %+v, err %v", cs, err) | ||
| } | ||
| } | ||
|
|
||
| func TestVerifyBreaks_FlagsAMissingOrChangedArchive(t *testing.T) { | ||
| cases := []struct { | ||
| name string | ||
| mut func(archive string) error | ||
| want string | ||
| }{ | ||
| {"missing", func(a string) error { return os.Remove(a) }, "missing"}, | ||
| {"edited", func(a string) error { | ||
| f, err := os.OpenFile(a, os.O_APPEND|os.O_WRONLY, 0) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| defer func() { _ = f.Close() }() | ||
| _, err = f.WriteString("{\"checkpoint\":5,\"root\":\"x\"}\n") | ||
| return err | ||
| }, "sha256"}, | ||
| } | ||
| for _, tc := range cases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| s, _ := newEpochSink(t, 10, 20) | ||
| b, err := s.NewEpoch(epochOpts()) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if err := tc.mut(b.ArchivedTo); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| cs, err := s.VerifyBreaks() | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if len(cs) != 1 || !strings.Contains(cs[0].Problem, tc.want) { | ||
| t.Fatalf("problems = %q; want one mentioning %q", problems(cs), tc.want) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // A break whose recorded last anchor is not the archive's last anchor does not | ||
| // describe that archive, even when the sha256 matches. | ||
| func TestVerifyBreaks_FlagsAWrongLastCheckpoint(t *testing.T) { | ||
| dir := t.TempDir() | ||
| archive := filepath.Join(dir, "a.archived-x") | ||
| body := []byte("{\"checkpoint\":20,\"root\":\"r20\"}\n") | ||
| if err := os.WriteFile(archive, body, 0o600); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| sum := sha256.Sum256(body) | ||
| line, _ := json.Marshal(EpochBreak{Kind: anchorKindEpochBreak, At: epochTestNow.Format(time.RFC3339Nano), | ||
| Actor: "a", Reason: "r", ArchivedTo: archive, ArchivedSHA256: hex.EncodeToString(sum[:]), | ||
| LastCheckpoint: 999, LastRoot: "r999"}) | ||
| path := filepath.Join(dir, "a") | ||
| if err := os.WriteFile(path, append(line, '\n'), 0o600); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| s, err := NewFileSink(path) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| cs, err := s.VerifyBreaks() | ||
| if err != nil || len(cs) != 1 || !strings.Contains(cs[0].Problem, "last anchor") { | ||
| t.Fatalf("checks = %+v, err %v", cs, err) | ||
| } | ||
| } | ||
|
|
||
| // Two epochs: the second archive holds the first break, so both are checked, | ||
| // and tampering with the oldest archive is still seen from the live file. | ||
| func TestVerifyBreaks_FollowsBreaksInsideArchives(t *testing.T) { | ||
| s, _ := newEpochSink(t, 10, 20) | ||
| first, err := s.NewEpoch(epochOpts()) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if err := s.PublishRoot(context.Background(), 3, "r3"); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| later := epochOpts() | ||
| later.Now = func() time.Time { return epochTestNow.Add(time.Hour) } | ||
| if _, err := s.NewEpoch(later); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| cs, err := s.VerifyBreaks() | ||
| if err != nil || len(cs) != 2 { | ||
| t.Fatalf("checks = %+v, err %v; want 2", cs, err) | ||
| } | ||
| for _, c := range cs { | ||
| if c.Problem != "" { | ||
| t.Errorf("unexpected problem %q", c.Problem) | ||
| } | ||
| } | ||
| if err := os.Remove(first.ArchivedTo); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| cs, _ = s.VerifyBreaks() | ||
| if len(cs) != 2 || !strings.Contains(strings.Join(problems(cs), "|"), "missing") { | ||
| t.Fatalf("after deleting the oldest archive: %q", problems(cs)) | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -178,3 +178,33 @@ func runAuditAnchorNewEpoch(cmd *cobra.Command, _ []string) error { | |
| req.Logger = store | ||
| return runAnchorNewEpoch(ctx, req, cmd.OutOrStdout()) | ||
| } | ||
|
|
||
| // reportAnchorBreaks prints every recorded epoch break in the anchor file, as a | ||
| // break and not as tampering, and fails when a break's archive is missing, | ||
| // changed, or does not end where the break says. A drop in the anchor with no | ||
| // break record is not touched here: the chain check still fails it. | ||
| func reportAnchorBreaks(sink *audit.FileSink, out, errOut io.Writer) error { | ||
| checks, err := sink.VerifyBreaks() | ||
| if err != nil { | ||
| return fmt.Errorf("check anchor epoch breaks: %w", err) | ||
| } | ||
| var bad int | ||
| for _, c := range checks { | ||
| b := c.Break | ||
| fmt.Fprintf(out, "\n ⚠ Anchor epoch break recorded %s by %s: %q\n", b.At, b.Actor, b.Reason) | ||
| fmt.Fprintf(out, " The earlier anchors (last: checkpoint %d) are archived at %s (sha256 %s).\n", b.LastCheckpoint, b.ArchivedTo, b.ArchivedSHA256) | ||
|
Comment on lines
+194
to
+195
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. Escape break metadata before printing it. If Quote or escape
|
||
| if c.Problem != "" { | ||
| bad++ | ||
| fmt.Fprintf(errOut, " ✗ %s\n", c.Problem) | ||
| } else { | ||
| fmt.Fprintln(out, " Archive present and unchanged. This is a recorded break, not tampering.") | ||
| } | ||
| } | ||
| if len(checks) > 0 { | ||
| fmt.Fprintln(out) | ||
| } | ||
| if bad > 0 { | ||
| return fmt.Errorf("%d anchor epoch break(s) are not backed by their archive", bad) | ||
| } | ||
| return nil | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| //go:build !containarium_client | ||
|
|
||
| package cmd | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/footprintai/containarium/internal/audit" | ||
| ) | ||
|
|
||
| func brokenSink(t *testing.T) (*audit.FileSink, audit.EpochBreak) { | ||
| t.Helper() | ||
| sink, err := audit.NewFileSink(filepath.Join(t.TempDir(), "anchors.jsonl")) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if err := sink.PublishRoot(context.Background(), 42, "abc"); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| b, err := sink.NewEpoch(audit.EpochOptions{Actor: "alice", Reason: "database restored from backup"}) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| return sink, b | ||
| } | ||
|
|
||
| // #3772 slice 3: a recorded break is reported as a break, with who/why and the | ||
| // archive's state, and does not fail the run while the archive backs it. | ||
| func TestReportAnchorBreaks_RecordedBreakIsReportedNotFailed(t *testing.T) { | ||
| sink, b := brokenSink(t) | ||
| var out, errOut bytes.Buffer | ||
| if err := reportAnchorBreaks(sink, &out, &errOut); err != nil { | ||
| t.Fatalf("err = %v; want nil for a break its archive backs", err) | ||
| } | ||
| for _, want := range []string{"epoch break", "alice", "database restored from backup", b.ArchivedTo, b.ArchivedSHA256[:12], "checkpoint 42"} { | ||
| if !strings.Contains(out.String(), want) { | ||
| t.Errorf("output lacks %q:\n%s", want, out.String()) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func TestReportAnchorBreaks_NoBreakPrintsNothing(t *testing.T) { | ||
| sink, err := audit.NewFileSink(filepath.Join(t.TempDir(), "a.jsonl")) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| var out, errOut bytes.Buffer | ||
| if err := reportAnchorBreaks(sink, &out, &errOut); err != nil || out.Len()+errOut.Len() != 0 { | ||
| t.Fatalf("err %v, output %q %q", err, out.String(), errOut.String()) | ||
| } | ||
| } | ||
|
|
||
| // Deleting the archive after a break must not pass as a clean break. | ||
| func TestReportAnchorBreaks_MissingArchiveFails(t *testing.T) { | ||
| sink, b := brokenSink(t) | ||
| if err := os.Remove(b.ArchivedTo); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| var out, errOut bytes.Buffer | ||
| err := reportAnchorBreaks(sink, &out, &errOut) | ||
| if err == nil || !strings.Contains(errOut.String(), "missing") { | ||
| t.Fatalf("err = %v, stderr %q; want a failure naming the missing archive", err, errOut.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.
Relative anchor paths make archive lookup depend on the working directory.
new-epochaccepts a relative--anchor-pathand stores the archive name relative to it.verify-anchorresolves that name against its own working directory. For example, runcontainarium audit anchor new-epoch --anchor-path anchors.jsonlfrom/srv/audit. The break then recordsanchors.jsonl.archived-<time>. A latercontainarium audit verify-anchor --anchor-path /srv/audit/anchors.jsonlrun from another directory reports that archive as missing and fails, although the file exists in/srv/audit. Verification from/srv/auditpasses. No data is lost.Make the recorded archive path resolve from any working directory. Either convert the anchor path to an absolute path when the sink is created, or reject a relative
--anchor-pathinnew-epoch. Use the same form for the live path and the recorded archive path.