Feature(Go): four local MCP read tools and a shared audit query package

Implement milestone M2 of the minimal MCP plan: the tools that answer from
the server's own state instead of opening a device channel.

- Extract the audit-log scanner from web into package auditquery so the
  admin page and get_audit_log share one implementation. The web handler
  keeps its 500-row cap, the MCP tool uses the protocol's 1000-row cap.
  Query gains json tags for the web snake_case wire format (device_id,
  warn_only) and reports TotalAll before filters.
- Add package buildinfo and inject Version/Commit with -X from build.ps1
  and the Makefile; go-winres refreshes Windows resources from the git tag
  when available. An unversioned build reports "", and only initialize
  substitutes "dev".
- list_hosts: online/offline pools, include_offline merge, every
  documented filter, limit/offset paging with total/totalAll/hasMore, and
  schema-named host fields (authStatus/tls/remark are never fabricated).
- get_host_detail: single id or a batch of up to 50, online lookup then
  the last history record, capabilities from protocol.SupportsCap plus the
  Windows/registry/virtual-desktop rules; v2 is not listed.
- get_server_status: version, uptime, online/offline counts, port and
  bind. sessions stays absent until the MCP session table lands in M3.
- get_audit_log: case-insensitive event substring, keyword, paging;
  hasMore turns false at the MaxOffset clamp so paging cannot loop.

Wire hub, history, audit path and listen address through mcp.Config from
cmd/main.go. Tests cover pools/filters/paging, capability bits, batch
failures, audit paging and the unversioned build.

Co-Authored-By: deepseek-flash
This commit is contained in:
yuanyuanxianganddeepseek-flash committed 2026-11-12 20:55:18 +01:00
1 parent 3f1957a9c9
commit 774c0d2545
16 files changed
+1952 -449

No files matched your search

+287
View File
@@ -0,0 +1,287 @@
// Package auditquery scans the server's JSON-lines audit file.
//
// The server writes its own audit records to logs/audit.log (see
// logger.AuditAt); records are already redacted at write time, so returning
// raw fields to an authenticated viewer is safe as long as we never widen what
// is written.
//
// Only the CURRENT audit file is searched. Rotated files (audit-*.log[.gz])
// are deliberately out of scope: they need gzip support and a separate index,
// and both the web UI and the MCP tool exist for recent-window triage ("what
// happened just now").
//
// The package is shared by web/audit_query.go (admin audit page) and the MCP
// get_audit_log tool, which is why it has no dependency on either.
package auditquery
import (
"bufio"
"encoding/json"
"os"
"strings"
"time"
)
const (
// DefaultLimit is used when the caller asks for no limit (0 or negative).
DefaultLimit = 100
// MaxLimit caps how many records one query may return.
MaxLimit = 1000
// MaxOffset caps how far a query may page into the result set.
MaxOffset = 10000
// scanBudget bounds how much of the audit file a single query reads. The
// tail is read when the file is larger, so the newest records (what
// operators actually triage) are always covered and a multi-GB log cannot
// stall the query goroutine.
scanBudget = 32 << 20 // 32 MB
)
// Query is one audit-log search. All filters are optional and combined with
// AND. Limit/Offset are clamped by Scan; callers that report the effective
// values back should clamp first with ClampLimit/ClampOffset.
type Query struct {
From string `json:"from"` // RFC3339 ("2026-10-18T10:00:00Z"), optional
To string `json:"to"` // RFC3339, optional
Event string `json:"event"` // exact ("web.login.failure"), prefix ("web." / "web.*"), or ""
Actor string `json:"actor"` // exact, case-insensitive
IP string `json:"ip"` // exact
DeviceID string `json:"device_id"`
Keyword string `json:"keyword"` // case-insensitive substring of the raw record
WarnOnly bool `json:"warn_only"` // only level=warn records
// EventContains is a case-insensitive substring match on the event column.
// The web UI uses Event's exact/prefix forms; the MCP get_audit_log tool
// exposes a "type contains" filter, which maps here.
EventContains string `json:"event_contains"`
Limit int `json:"limit"`
Offset int `json:"offset"` // skip the newest N matching records
}
// Result is the outcome of scanning the audit file.
type Result struct {
Events []map[string]any // newest-first
Total int // matching records in the scanned window
TotalAll int // audit records in the scanned window before filters
FileExists bool
FileSize int64
ScannedBytes int64
Truncated bool // the file was larger than the scan budget
}
// ClampLimit bounds n to [1, max]. A non-positive n means DefaultLimit; a
// non-positive max means MaxLimit. Callers with a tighter UI cap (the web
// audit page uses 500) pass it as max.
func ClampLimit(n, max int) int {
if max <= 0 {
max = MaxLimit
}
if n <= 0 {
return DefaultLimit
}
if n > max {
return max
}
return n
}
// ClampOffset bounds n to [0, MaxOffset].
func ClampOffset(n int) int {
if n < 0 {
return 0
}
if n > MaxOffset {
return MaxOffset
}
return n
}
// Scan scans path for audit records matching q. The returned events are the
// newest Limit records after skipping Offset, in newest-first order. A missing
// file is not an error: no audit record has been written yet (the logger
// creates the file lazily), so the result is empty with FileExists=false.
func Scan(path string, q Query) (Result, error) {
q.Limit = ClampLimit(q.Limit, MaxLimit)
q.Offset = ClampOffset(q.Offset)
res := Result{Events: []map[string]any{}}
f, err := os.Open(path)
if err != nil {
if os.IsNotExist(err) {
return res, nil
}
return res, err
}
defer f.Close()
info, err := f.Stat()
if err != nil {
return res, err
}
res.FileExists = true
res.FileSize = info.Size()
// For very large files, scan only the tail; the newest matches live there.
start := int64(0)
if info.Size() > scanBudget {
start = info.Size() - scanBudget
if _, err := f.Seek(start, 0); err != nil {
return res, err
}
res.Truncated = true
}
from, hasFrom := parseTime(q.From)
to, hasTo := parseTime(q.To)
keep := q.Offset + q.Limit
ring := make([]map[string]any, keep)
filled := 0
sc := bufio.NewScanner(f)
// Sanitized values are capped at 4096 bytes each; a record with many
// fields stays well under 1 MB. Allow headroom rather than aborting the
// whole query on one long line.
sc.Buffer(make([]byte, 64*1024), 4*1024*1024)
first := true
for sc.Scan() {
line := sc.Text()
if start > 0 && first {
// Seeking into the middle of the file lands mid-line. Drop that
// partial record.
first = false
continue
}
first = false
res.ScannedBytes += int64(len(line)) + 1
line = strings.TrimSpace(line)
if line == "" {
continue
}
var rec map[string]any
if err := json.Unmarshal([]byte(line), &rec); err != nil {
continue // tolerate a torn line instead of failing the page
}
if _, ok := rec["event"].(string); !ok {
continue
}
// Only audit records belong in the result. The dedicated file contains
// nothing else, but a hand-edited or mixed file must not surface
// ordinary operational lines here.
if rec["audit"] != true {
continue
}
res.TotalAll++
if !recordMatches(rec, line, q, from, hasFrom, to, hasTo) {
continue
}
// Ring buffer of the last `keep` matches (file order is chronological).
ring[res.Total%keep] = rec
if filled < keep {
filled++
}
res.Total++
}
if err := sc.Err(); err != nil {
return res, err
}
// Newest first, skipping Offset. The match with 0-based index i lives at
// ring[i%keep] as long as i >= Total-filled.
for i := res.Total - 1 - q.Offset; i >= 0 && i >= res.Total-filled; i-- {
res.Events = append(res.Events, ring[i%keep])
if len(res.Events) >= q.Limit {
break
}
}
return res, nil
}
// recordMatches applies the non-time and time filters to one record. line is
// the raw JSON line, used for the keyword substring search.
func recordMatches(rec map[string]any, line string, q Query, from time.Time, hasFrom bool, to time.Time, hasTo bool) bool {
if q.Event != "" && !eventFilterMatches(q.Event, StringField(rec, "event")) {
return false
}
if q.EventContains != "" && !strings.Contains(strings.ToLower(StringField(rec, "event")), strings.ToLower(q.EventContains)) {
return false
}
if q.Actor != "" && !strings.EqualFold(StringField(rec, "actor"), q.Actor) {
return false
}
if q.IP != "" && StringField(rec, "ip") != q.IP {
return false
}
if q.DeviceID != "" && StringField(rec, "device_id") != q.DeviceID {
return false
}
if q.WarnOnly && StringField(rec, "level") != "warn" {
return false
}
if q.Keyword != "" && !strings.Contains(strings.ToLower(line), strings.ToLower(q.Keyword)) {
return false
}
if hasFrom || hasTo {
t, ok := parseTime(StringField(rec, "time"))
if !ok {
return false // cannot prove it is in range
}
if hasFrom && t.Before(from) {
return false
}
if hasTo && t.After(to) {
return false
}
}
return true
}
// eventFilterMatches supports three forms so the UI stays simple:
//
// "web.login.failure" exact
// "web.*" prefix (trailing "*")
// "web." prefix (trailing ".")
func eventFilterMatches(filter, event string) bool {
switch {
case strings.HasSuffix(filter, "*"):
return strings.HasPrefix(event, strings.TrimSuffix(filter, "*"))
case strings.HasSuffix(filter, "."):
return strings.HasPrefix(event, filter)
default:
return event == filter
}
}
// StringField returns rec[key] when it is a string, else "".
func StringField(rec map[string]any, key string) string {
s, _ := rec[key].(string)
return s
}
// parseTime accepts the formats operators and browsers realistically send.
// Layouts without an explicit zone are parsed as UTC; the frontend always sends
// Date.toISOString(), so the RFC3339 path is what production uses.
func parseTime(s string) (time.Time, bool) {
s = strings.TrimSpace(s)
if s == "" {
return time.Time{}, false
}
layouts := []string{
time.RFC3339Nano,
time.RFC3339,
"2006-01-02T15:04:05",
"2006-01-02T15:04",
"2006-01-02 15:04:05",
"2006-01-02",
}
for _, layout := range layouts {
if t, err := time.Parse(layout, s); err == nil {
return t, true
}
}
return time.Time{}, false
}
+248
View File
@@ -0,0 +1,248 @@
package auditquery
import (
"encoding/json"
"os"
"path/filepath"
"strings"
"testing"
"time"
)
// writeAuditFixture writes JSON-lines audit records (oldest first) and returns
// the file path. Times are UTC RFC3339 so the tests are timezone-independent.
func writeAuditFixture(t *testing.T, records []map[string]any) string {
t.Helper()
path := filepath.Join(t.TempDir(), "audit.log")
var b strings.Builder
for _, rec := range records {
line, err := json.Marshal(rec)
if err != nil {
t.Fatalf("marshal fixture: %v", err)
}
b.Write(line)
b.WriteByte('\n')
}
if err := os.WriteFile(path, []byte(b.String()), 0o644); err != nil {
t.Fatalf("write fixture: %v", err)
}
return path
}
func auditFixtureRecords() []map[string]any {
at := func(hhmm string) string {
tm, err := time.Parse("2006-01-02T15:04", "2026-10-18T"+hhmm)
if err != nil {
panic(err)
}
return tm.Format(time.RFC3339)
}
return []map[string]any{
{"level": "info", "time": at("10:00"), "audit": true, "event": "web.login.success", "actor": "admin", "ip": "203.0.113.1"},
{"level": "warn", "time": at("10:01"), "audit": true, "event": "web.login.failure", "actor": "alice", "ip": "203.0.113.2", "reason": "invalid_credentials"},
{"level": "info", "time": at("10:02"), "audit": true, "event": "remote.screen.start", "actor": "admin", "role": "admin", "ip": "203.0.113.1", "device_id": "dev1"},
{"level": "info", "time": at("10:03"), "audit": true, "event": "admin.user.delete", "actor": "admin", "ip": "203.0.113.1", "target": "bob"},
}
}
func runAuditQuery(t *testing.T, path string, q Query) Result {
t.Helper()
q.Limit = ClampLimit(q.Limit, MaxLimit)
q.Offset = ClampOffset(q.Offset)
res, err := Scan(path, q)
if err != nil {
t.Fatalf("Scan: %v", err)
}
return res
}
func eventNames(res Result) []string {
out := make([]string, 0, len(res.Events))
for _, e := range res.Events {
out = append(out, StringField(e, "event"))
}
return out
}
func TestNoFilterNewestFirst(t *testing.T) {
path := writeAuditFixture(t, auditFixtureRecords())
res := runAuditQuery(t, path, Query{Limit: 100})
if res.Total != 4 || len(res.Events) != 4 {
t.Fatalf("total=%d events=%d, want 4/4", res.Total, len(res.Events))
}
if res.TotalAll != 4 {
t.Fatalf("totalAll=%d, want 4", res.TotalAll)
}
got := eventNames(res)
want := []string{"admin.user.delete", "remote.screen.start", "web.login.failure", "web.login.success"}
if strings.Join(got, ",") != strings.Join(want, ",") {
t.Fatalf("events = %v, want %v", got, want)
}
}
func TestEventFilters(t *testing.T) {
path := writeAuditFixture(t, auditFixtureRecords())
cases := []struct {
filter string
want []string
}{
{"web.", []string{"web.login.failure", "web.login.success"}},
{"web.*", []string{"web.login.failure", "web.login.success"}},
{"web.login.failure", []string{"web.login.failure"}},
{"remote.*", []string{"remote.screen.start"}},
}
for _, tc := range cases {
res := runAuditQuery(t, path, Query{Event: tc.filter, Limit: 100})
got := eventNames(res)
if strings.Join(got, ",") != strings.Join(tc.want, ",") {
t.Errorf("filter %q → %v, want %v", tc.filter, got, tc.want)
}
}
}
func TestEventContainsFilter(t *testing.T) {
path := writeAuditFixture(t, auditFixtureRecords())
res := runAuditQuery(t, path, Query{EventContains: "LOGIN", Limit: 100})
got := eventNames(res)
want := []string{"web.login.failure", "web.login.success"}
if strings.Join(got, ",") != strings.Join(want, ",") {
t.Fatalf("events = %v, want %v", got, want)
}
// Filters run before paging: total reflects matches, totalAll the window.
if res.Total != 2 || res.TotalAll != 4 {
t.Fatalf("total=%d totalAll=%d, want 2/4", res.Total, res.TotalAll)
}
}
func TestFieldFilters(t *testing.T) {
path := writeAuditFixture(t, auditFixtureRecords())
from, _ := time.Parse(time.RFC3339, "2026-10-18T10:01:00Z")
to, _ := time.Parse(time.RFC3339, "2026-10-18T10:02:00Z")
cases := []struct {
name string
q Query
want []string
}{
{"actor case-insensitive", Query{Actor: "ALICE", Limit: 100}, []string{"web.login.failure"}},
{"ip", Query{IP: "203.0.113.2", Limit: 100}, []string{"web.login.failure"}},
{"device", Query{DeviceID: "dev1", Limit: 100}, []string{"remote.screen.start"}},
{"warn only", Query{WarnOnly: true, Limit: 100}, []string{"web.login.failure"}},
{"keyword target", Query{Keyword: "BOB", Limit: 100}, []string{"admin.user.delete"}},
{"time range", Query{
From: from.Format(time.RFC3339), To: to.Format(time.RFC3339), Limit: 100,
}, []string{"remote.screen.start", "web.login.failure"}},
}
for _, tc := range cases {
res := runAuditQuery(t, path, tc.q)
got := eventNames(res)
if strings.Join(got, ",") != strings.Join(tc.want, ",") {
t.Errorf("%s → %v, want %v", tc.name, got, tc.want)
}
}
}
func TestOffsetAndLimit(t *testing.T) {
path := writeAuditFixture(t, auditFixtureRecords())
res := runAuditQuery(t, path, Query{Limit: 2, Offset: 1})
if res.Total != 4 {
t.Fatalf("total = %d, want 4", res.Total)
}
got := eventNames(res)
want := []string{"remote.screen.start", "web.login.failure"}
if strings.Join(got, ",") != strings.Join(want, ",") {
t.Fatalf("events = %v, want %v", got, want)
}
// Offsetting past the end yields no events but keeps the total.
res = runAuditQuery(t, path, Query{Limit: 2, Offset: 4})
if res.Total != 4 || len(res.Events) != 0 {
t.Fatalf("offset past end: total=%d events=%v", res.Total, eventNames(res))
}
}
func TestSkipsNonAuditAndTornLines(t *testing.T) {
path := filepath.Join(t.TempDir(), "audit.log")
content := `{"event":"device.online","actor":"x"}
not json at all
{"level":"info","time":"2026-10-18T10:00:00Z","audit":true,"event":"web.login.success","actor":"admin"}`
if err := os.WriteFile(path, []byte(content), 0o644); err != nil {
t.Fatal(err)
}
res := runAuditQuery(t, path, Query{Limit: 100})
if res.Total != 1 || len(res.Events) != 1 {
t.Fatalf("total=%d events=%v, want the single valid record", res.Total, eventNames(res))
}
// Non-audit and torn lines do not count toward totalAll either.
if res.TotalAll != 1 {
t.Fatalf("totalAll=%d, want 1", res.TotalAll)
}
}
func TestMissingFile(t *testing.T) {
res := runAuditQuery(t, filepath.Join(t.TempDir(), "does-not-exist.log"), Query{Limit: 10})
if res.FileExists {
t.Error("FileExists = true for a missing file")
}
if res.Total != 0 || len(res.Events) != 0 {
t.Errorf("total=%d events=%v, want empty", res.Total, eventNames(res))
}
if res.Events == nil {
t.Error("Events is nil; JSON would encode null instead of []")
}
}
func TestClampsPaging(t *testing.T) {
if got := ClampLimit(0, MaxLimit); got != DefaultLimit {
t.Errorf("limit(0) = %d, want default %d", got, DefaultLimit)
}
if got := ClampLimit(-3, MaxLimit); got != DefaultLimit {
t.Errorf("limit(-3) = %d, want default %d", got, DefaultLimit)
}
if got := ClampLimit(9999, MaxLimit); got != MaxLimit {
t.Errorf("limit(9999) = %d, want max %d", got, MaxLimit)
}
if got := ClampLimit(9999, 500); got != 500 {
t.Errorf("limit(9999, max 500) = %d, want 500", got)
}
if got := ClampLimit(50, MaxLimit); got != 50 {
t.Errorf("limit(50) = %d, want 50", got)
}
if got := ClampLimit(0, 0); got != DefaultLimit {
t.Errorf("limit(0, max 0) = %d, want default %d", got, DefaultLimit)
}
if got := ClampOffset(-5); got != 0 {
t.Errorf("offset(-5) = %d, want 0", got)
}
if got := ClampOffset(1 << 30); got != MaxOffset {
t.Errorf("offset(huge) = %d, want max %d", got, MaxOffset)
}
}
// The web audit page sends snake_case keys. Query is decoded from that wire
// format in web/audit_query.go, so the tags must keep those names — otherwise
// device_id / warn_only silently stop filtering.
func TestQueryDecodesWebWireFormat(t *testing.T) {
body := `{"from":"2026-11-01T00:00:00Z","to":"2026-11-12T00:00:00Z",` +
`"event":"web.","actor":"admin","ip":"1.2.3.4","device_id":"1001",` +
`"keyword":"k","warn_only":true,"limit":5,"offset":2}`
var q Query
if err := json.Unmarshal([]byte(body), &q); err != nil {
t.Fatalf("unmarshal: %v", err)
}
if q.From != "2026-11-01T00:00:00Z" || q.To != "2026-11-12T00:00:00Z" {
t.Fatalf("time range = %q..%q", q.From, q.To)
}
if q.Event != "web." || q.Actor != "admin" || q.IP != "1.2.3.4" {
t.Fatalf("filters = %+v", q)
}
if q.DeviceID != "1001" {
t.Fatalf("device_id = %q, want 1001", q.DeviceID)
}
if q.Keyword != "k" || !q.WarnOnly {
t.Fatalf("keyword/warn_only = %q/%v", q.Keyword, q.WarnOnly)
}
if q.Limit != 5 || q.Offset != 2 {
t.Fatalf("limit/offset = %d/%d", q.Limit, q.Offset)
}
}