From e06c5ab5739c91d5f630488ef82ba28795340db9 Mon Sep 17 00:00:00 2001 From: jerryfane Date: Sun, 15 Nov 2026 18:10:40 +0100 Subject: [PATCH] Dashboard Activity page: search the audit log by time, service, agent and path (152) Owner-only, read-only. A UTC minute window (default the last hour), with optional service, agent and path-contains filters; newest first, at most 500 rows. The never-rotated log is streamed and a line's time compared as text before decoding; overlong or non-JSON lines are skipped; everything is HTML-escaped. --- README.md | 2 + internal/dashboard/activity.go | 157 ++++++++++++++++++++++++++++ internal/dashboard/activity_test.go | 54 ++++++++++ internal/dashboard/dashboard.go | 1 + internal/dashboard/pages.go | 22 ++++ 5 files changed, 236 insertions(+) create mode 100644 internal/dashboard/activity.go create mode 100644 internal/dashboard/activity_test.go diff --git a/README.md b/README.md index f342e35..ed54612 100644 --- a/README.md +++ b/README.md @@ -837,6 +837,8 @@ Safety: The Keys and Agents pages are sortable tables (click a column); forms are one column. Everything works on a phone (the tables scroll sideways with the name column pinned) and follows the system's light or dark mode. +The **Activity** page (`/activity`) searches the audit log, read-only, so the owner can see who did what without a terminal or `sudo`: a UTC time window by the minute (default: the last hour; the end minute is included), and optionally a service, an agent and text the path contains. It lists each matching call (time, agent, service, method and path, status, source address, note) and dashboard event, newest first, at most 500 (narrow the search for more). It shows only what the audit log holds: no key values, no query strings. The log is streamed, never loaded whole. + The **Keys** page (`/keys`) is a table of every key the access file names or the store holds: key, API, status, the agents with access (with a dot: full or limited access), last use, calls today, calls over the last 7 days, and when the value was last added or replaced here. Filters above it show only working, failing, untested, leaked, missing or unused keys. Values are not shown in the table; a key's page can show its value on request (below). - **Show value** (a key's page) shows the stored value, or each named value, after the dashboard password, with a Copy button. It is the answer to that one request: diff --git a/internal/dashboard/activity.go b/internal/dashboard/activity.go new file mode 100644 index 0000000..5c45021 --- /dev/null +++ b/internal/dashboard/activity.go @@ -0,0 +1,157 @@ +package dashboard + +import ( + "bufio" + "encoding/json" + "errors" + "io" + "net/http" + "os" + "slices" + "strings" + "time" +) + +// Activity (Jarvis 488255): the owner looks up who called what, and when, +// from the audit log, without a terminal or sudo. Read-only. + +func registerActivity(b *Backend) { + b.Admin.Handle("GET /activity", b.activityPage) +} + +// activityLimit caps the rows shown; the newest matches are kept. +const activityLimit = 500 + +// activityFormat is the form's time format, as a datetime-local input sends it. +const activityFormat = "2006-01-02T15:04" + +type activityRow struct { + Time string `json:"time"` + Source string `json:"source"` + Role string `json:"role"` + Service string `json:"service"` + Method string `json:"method"` + Path string `json:"path"` + Status int `json:"status"` + Note string `json:"note"` + // Admin is a dashboard event (login, key added, ...). + Admin string `json:"admin"` +} + +type activityQuery struct { + From, To, Service, Role, Path string +} + +func (b *Backend) activityPage(w http.ResponseWriter, r *http.Request, sid string) { + now := b.Now().UTC() + q := activityQuery{From: r.FormValue("from"), To: r.FormValue("to"), + Service: strings.TrimSpace(r.FormValue("service")), Role: strings.TrimSpace(r.FormValue("role")), Path: strings.TrimSpace(r.FormValue("path"))} + data := map[string]any{"Q": &q} + from, to, err := activityWindow(q.From, q.To, now) + if err != nil { + data["ActivityError"] = err.Error() + b.render(w, http.StatusBadRequest, "activity", sid, data) + return + } + q.From, q.To = from.Format(activityFormat), to.Add(-time.Minute).Format(activityFormat) + rows, more, err := readActivity(b.rules().AuditLog, from, to, q) + switch { + case errors.Is(err, os.ErrNotExist) || b.rules().AuditLog == "": + data["ActivityError"] = "There is no audit log yet." + case err != nil: + data["ActivityError"] = "The audit log could not be read." + } + data["Rows"], data["More"], data["Limit"] = rows, more, activityLimit + b.render(w, http.StatusOK, "activity", sid, data) +} + +// activityWindow reads the form's UTC minutes; the end minute is included. +// The default is the last hour. +func activityWindow(fromText, toText string, now time.Time) (time.Time, time.Time, error) { + to := now.Truncate(time.Minute).Add(time.Minute) + from := to.Add(-time.Hour) + if toText != "" { + t, err := time.Parse(activityFormat, toText) + if err != nil { + return from, to, errors.New("Write the times as 2026-11-15T16:39 (UTC).") + } + to = t.Add(time.Minute) + if fromText == "" { + from = to.Add(-time.Hour) + } + } + if fromText != "" { + t, err := time.Parse(activityFormat, fromText) + if err != nil { + return from, to, errors.New("Write the times as 2026-11-15T16:39 (UTC).") + } + from = t + } + if !from.Before(to) { + return from, to, errors.New("The start must be before the end.") + } + return from, to, nil +} + +// readActivity scans the audit log for lines in [from, to) matching q, +// newest first; past activityLimit it keeps the last ones written. The log is append-only and never rotated, so it is streamed: +// a line's time is compared as text before it is decoded. +func readActivity(path string, from, to time.Time, q activityQuery) ([]activityRow, bool, error) { + f, err := os.Open(path) + if err != nil { + return nil, false, err + } + defer f.Close() + lo, hi := from.UTC().Format(time.RFC3339), to.UTC().Format(time.RFC3339) + var kept []activityRow + more := false + rd := bufio.NewReaderSize(f, 64<<10) + for { + line, err := rd.ReadSlice('\n') + if errors.Is(err, bufio.ErrBufferFull) { + // A line this long is not an audit record worth showing; skip it. + for errors.Is(err, bufio.ErrBufferFull) { + _, err = rd.ReadSlice('\n') + } + continue + } + if len(line) > 0 { + if row, ok := activityMatch(line, lo, hi, q); ok { + if len(kept) == activityLimit { + kept, more = kept[1:], true + } + kept = append(kept, row) + } + } + if err == io.EOF { + break + } + if err != nil { + return nil, false, err + } + } + // A line is written when the call ends but stamped with its start, so + // file order is not time order. + slices.SortStableFunc(kept, func(a, b activityRow) int { return strings.Compare(b.Time, a.Time) }) + return kept, more, nil +} + +func activityMatch(line []byte, lo, hi string, q activityQuery) (activityRow, bool) { + var row activityRow + s := string(line) + i := strings.Index(s, `"time":"`) + if i < 0 || len(s) < i+8+len(lo) { + return row, false + } + if t := s[i+8 : i+8+len(lo)]; t < lo || t >= hi { + return row, false + } + if json.Unmarshal(line, &row) != nil { + return row, false + } + if q.Service != "" && row.Service != q.Service || q.Role != "" && row.Role != q.Role || + q.Path != "" && !strings.Contains(row.Path, q.Path) { + return row, false + } + return row, true +} diff --git a/internal/dashboard/activity_test.go b/internal/dashboard/activity_test.go new file mode 100644 index 0000000..41470f7 --- /dev/null +++ b/internal/dashboard/activity_test.go @@ -0,0 +1,54 @@ +package dashboard + +import ( + "html" + "net/http" + "net/url" + "os" + "strings" + "testing" +) + +// Jarvis 488255: the owner finds which agent cancelled a macserve job at a +// given minute, from the dashboard, without sudo. +func TestActivityFindsTheCallerByMinuteAndPath(t *testing.T) { + e := newEnv(t) + log := strings.Join([]string{ + `{"time":"2026-11-15T16:39:53Z","source":"100.64.0.9","role":"tandem","service":"macserve-builds","method":"POST","path":"/v1/jobs/j_ce4ce15b617d/cancel","status":200,"bytes":2,"ms":40}`, + `{"time":"2026-11-15T16:39:50Z","source":"100.64.0.9","role":"herdrup-duo","service":"macserve-builds","method":"GET","path":"/v1/jobs/j_other","status":200,"bytes":2,"ms":40}`, + `{"time":"2026-11-15T16:41:00Z","source":"100.64.0.9","role":"usmle-app","service":"macserve-builds","method":"POST","path":"/v1/jobs/j_ce4ce15b617d/cancel","status":200,"bytes":2,"ms":40}`, + `{"admin":"key added","key":"X","session":"s1","time":"2026-11-15T16:39:10Z"}`, + `{"time":"2026-11-15T16:39:30Z","role":"