| 1 | package diagnostics |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "io" |
| 6 | "os" |
| 7 | "path/filepath" |
| 8 | ) |
| 9 | |
| 10 | |
| 11 | type safetyEvidence struct { |
| 12 | Policy string `json:"policy"` |
| 13 | CreatedAt string `json:"created_at"` |
| 14 | Checks []struct { |
| 15 | ID string `json:"id"` |
| 16 | Title string `json:"title"` |
| 17 | State string `json:"state"` |
| 18 | Critical bool `json:"critical"` |
| 19 | Evidence string `json:"evidence"` |
| 20 | Remedy string `json:"remedy,omitempty"` |
| 21 | CheckedAt string `json:"checked_at"` |
| 22 | } `json:"checks"` |
| 23 | } |
| 24 | |
| 25 | func (s *Store) safetySnapshot() ([]byte, string) { |
| 26 | f, err := privateFile(filepath.Join(filepath.Dir(s.dir), "last-safety-report.json"), os.O_RDONLY) |
| 27 | if os.IsNotExist(err) { |
| 28 | return nil, "no saved report" |
| 29 | } |
| 30 | if err != nil { |
| 31 | return nil, "saved report unavailable" |
| 32 | } |
| 33 | defer f.Close() |
| 34 | data, err := io.ReadAll(io.LimitReader(f, (128<<10)+1)) |
| 35 | var report safetyEvidence |
| 36 | if err != nil || len(data) > 128<<10 || json.Unmarshal(data, &report) != nil || report.CreatedAt == "" || len(report.Checks) == 0 { |
| 37 | return nil, "saved report invalid or oversized" |
| 38 | } |
| 39 | report.Policy, report.CreatedAt = Redact(report.Policy), Redact(report.CreatedAt) |
| 40 | for i := range report.Checks { |
| 41 | c := &report.Checks[i] |
| 42 | c.ID, c.Title, c.State = Redact(c.ID), Redact(c.Title), Redact(c.State) |
| 43 | c.Evidence, c.Remedy, c.CheckedAt = Redact(c.Evidence), Redact(c.Remedy), Redact(c.CheckedAt) |
| 44 | } |
| 45 | data, _ = json.MarshalIndent(report, "", " ") |
| 46 | return data, "saved report; compare created_at with incident time" |
| 47 | } |