Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
157 changes: 157 additions & 0 deletions internal/dashboard/activity.go
Original file line number Diff line number Diff line change
@@ -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
}
54 changes: 54 additions & 0 deletions internal/dashboard/activity_test.go
Original file line number Diff line number Diff line change
@@ -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":"<script>","service":"macserve-builds","method":"GET","path":"/v1/j_ce4ce15b<b>","status":404}`,
`{"time":"2026-11-15T16:39:31Z","note":"` + strings.Repeat("x", 70<<10) + `"}`,
`not json "time":"2026-11-15T16:39:40Z"`,
`{"time":"2026-11-15T15:00:00Z","role":"tandem","service":"macserve-builds","method":"POST","path":"/v1/jobs/j_ce4ce15b617d/cancel","status":200}`,
}, "\n") + "\n"
if err := os.WriteFile(e.proxy.Config().AuditLog, []byte(log), 0o600); err != nil {
t.Fatal(err)
}
q := url.Values{"from": {"2026-11-15T16:39"}, "to": {"2026-11-15T16:39"}, "service": {"macserve-builds"}, "path": {"j_ce4ce15b"}}
w := e.request("GET", "/activity?"+q.Encode(), nil)
body := html.UnescapeString(w.Body.String())
if w.Code != http.StatusOK || !strings.Contains(body, "<td>tandem</td>") || !strings.Contains(body, "POST /v1/jobs/j_ce4ce15b617d/cancel") {
t.Fatalf("the 16:39:53 cancel by tandem is not listed (%d):\n%s", w.Code, body)
}
for _, not := range []string{"herdrup-duo", "usmle-app", "15:00:00", "key added", "xxxxxxxx"} {
if strings.Contains(body, not) {
t.Errorf("listed %q, outside the minute, service or path", not)
}
}
if strings.Contains(w.Body.String(), "<script>") || strings.Contains(w.Body.String(), "<b>") {
t.Error("audit text reached the page unescaped")
}
// Newest first, and the end minute is included.
if i, j := strings.Index(body, "16:39:53"), strings.Index(body, "16:39:30"); i < 0 || j < 0 || i > j {
t.Errorf("rows not newest first")
}
// Dashboard events show with no filter; a bad time is a 400 with a hint.
if body := e.request("GET", "/activity?from=2026-11-15T16:39&to=2026-11-15T16:39", nil).Body.String(); !strings.Contains(body, "key added") {
t.Error("dashboard event missing without filters")
}
if w := e.request("GET", "/activity?to=yesterday", nil); w.Code != http.StatusBadRequest || !strings.Contains(w.Body.String(), "2026-11-15T16:39") {
t.Errorf("bad time: %d", w.Code)
}
}
1 change: 1 addition & 0 deletions internal/dashboard/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ func Register(b *Backend) {
a.Handle("POST /keys/{name}/connect", b.connectKey)
registerServices(b)
registerProjects(b)
registerActivity(b)
registerAccess(b)
}

Expand Down
22 changes: 22 additions & 0 deletions internal/dashboard/pages.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var pages = template.Must(template.New("pages").Parse(`
<a href="/keys" class="nav{{if or (eq .Page "keys") (eq .Page "key") (eq .Page "newkey") (eq .Page "service")}} on{{end}}">Keys<span class="n">{{.Nav.Keys}}</span></a>
<a href="/access" class="nav{{if or (eq .Page "access") (eq .Page "cell") (eq .Page "agent") (eq .Page "newagent") (eq .Page "token")}} on{{end}}">Agents<span class="n">{{.Nav.Agents}}</span></a>
<a href="/projects" class="nav{{if or (eq .Page "projects") (eq .Page "project")}} on{{end}}">Projects<span class="n">{{.Nav.Projects}}</span></a>
<a href="/activity" class="nav{{if eq .Page "activity"}} on{{end}}">Activity</a>
<a href="/access#requests" class="nav">Requests<span class="n{{if .PendingCount}} hot{{end}}">{{.PendingCount}}</span></a>
</nav>
{{with .Nav.ProjectList}}<p class="sec">Projects</p><nav class="sidenav projects" aria-label="Projects">{{range .}}<a href="/projects/{{.Name}}" class="nav"><i class="dot {{.Dot}}"></i>{{.Name}}<span class="n">{{.Services}}</span></a>{{end}}</nav>{{end}}
Expand Down Expand Up @@ -312,6 +313,27 @@ Sign in with Apple: <code>{"service":"NAME","auth":"sign-in-with-apple-sign","si
{{else}}<div class="card"><p class="mute">This is a {{.Kind}} service. It carries settings this form does not show, so it is changed in access.json, not here.</p></div>{{end}}
{{template "bottom"}}{{end}}

{{define "activity"}}{{template "top" .}}
<div class="bar"><h1>Activity</h1></div>
<form method="get" action="/activity" class="card"><div class="two">
<label>From (UTC)<input type="datetime-local" name="from" value="{{.Q.From}}"></label>
<label>To (UTC, minute included)<input type="datetime-local" name="to" value="{{.Q.To}}"></label>
</div><div class="two">
<label>Service<input name="service" value="{{.Q.Service}}" autocapitalize="none" spellcheck="false" placeholder="any"></label>
<label>Agent<input name="role" value="{{.Q.Role}}" autocapitalize="none" spellcheck="false" placeholder="any"></label>
</div>
<label>Path contains<input name="path" value="{{.Q.Path}}" autocapitalize="none" spellcheck="false" placeholder="e.g. /v1/jobs/j_ce4ce15b"></label>
<div class="actions"><button>Show</button></div></form>
{{with .ActivityError}}<p class="error">{{.}}</p>{{end}}
{{if .Rows}}{{if .More}}<p class="hint">Showing the newest {{.Limit}}; narrow the search to see older ones.</p>{{end}}
<div class="tablewrap"><table class="t"><thead><tr><th>Time (UTC)</th><th>Agent</th><th>Service</th><th>Call</th><th class="num">Status</th><th>From</th><th>Note</th></tr></thead><tbody>
{{range .Rows}}<tr><td class="mono">{{.Time}}</td><td>{{if .Admin}}<span class="mute">dashboard</span>{{else}}{{.Role}}{{end}}</td><td>{{.Service}}</td>
<td class="mono">{{if .Admin}}{{.Admin}}{{else}}{{.Method}} {{.Path}}{{end}}</td><td class="num">{{if .Status}}{{.Status}}{{end}}</td><td class="mono mute">{{.Source}}</td><td class="mute">{{.Note}}</td></tr>{{end}}
</tbody></table></div>
{{else if not .ActivityError}}<p class="mute">Nothing in this window.</p>{{end}}
<p class="hint">From the audit log: every call an agent made through the keyring and every dashboard change. Paths are shown without their query string; key values are never recorded.</p>
{{template "bottom"}}{{end}}

{{define "projects"}}{{template "top" .}}
<div class="bar"><h1>Projects <span class="count">{{len .Projects}}</span></h1></div>
{{if .Projects}}<div class="tablewrap"><table class="t"><thead><tr><th>Project</th><th>State</th><th class="num">Services</th><th>Ends</th><th>Keys</th></tr></thead><tbody>
Expand Down
Loading