// Audit log read-back support. // // The logger is append-only JSONL on disk. The management plane (SSWFlow TDS) // needs to *read* those events, so this file adds a bounded query that reads // the log directory back without loading unbounded history into memory. package audit import ( "bufio" "encoding/json" "os" "path/filepath" "sort" "strings" "time" "security-connector/common/types" ) // DefaultQueryLimit caps a query that does not specify one. const DefaultQueryLimit = 200 // MaxQueryLimit is the hard ceiling regardless of what the caller asks for. const MaxQueryLimit = 2000 // QueryFilter narrows an audit query. Zero values mean "no constraint". type QueryFilter struct { DeviceID string EventType types.AuditEventType Result string Since time.Time Until time.Time Limit int } // normalize clamps the limit into a sane range. func (f *QueryFilter) normalize() { if f.Limit <= 0 { f.Limit = DefaultQueryLimit } if f.Limit > MaxQueryLimit { f.Limit = MaxQueryLimit } } // matches reports whether an event satisfies the filter. func (f *QueryFilter) matches(e *types.AuditEvent) bool { if f.DeviceID != "" && e.DeviceID != f.DeviceID { return false } if f.EventType != "" && e.EventType != f.EventType { return false } if f.Result != "" && e.Result != f.Result { return false } if !f.Since.IsZero() && e.Timestamp.Before(f.Since) { return false } if !f.Until.IsZero() && e.Timestamp.After(f.Until) { return false } return true } // QueryResult is the response envelope for a bounded audit query. type QueryResult struct { Events []*types.AuditEvent `json:"events"` Total int `json:"total"` Truncated bool `json:"truncated"` Files []string `json:"files"` } // LogDir returns the directory the logger writes into. func (l *Logger) LogDir() string { return filepath.Dir(l.filePath) } // Query reads audit events back from disk, newest first. // // It flushes the current file first so freshly written events are visible, then // walks the daily JSONL files in reverse chronological order and stops as soon // as the limit is satisfied. Malformed lines are skipped rather than failing the // whole query — an audit reader must never be blocked by one bad record. func (l *Logger) Query(filter QueryFilter) (*QueryResult, error) { filter.normalize() // Make sure everything buffered in the OS is on disk before reading. l.flush() dir := l.LogDir() entries, err := os.ReadDir(dir) if err != nil { return nil, err } var files []string for _, e := range entries { if e.IsDir() { continue } name := e.Name() if !strings.HasPrefix(name, "audit_") || !strings.HasSuffix(name, ".jsonl") { continue } files = append(files, name) } // Names embed the date, so lexical descending == newest first. sort.Sort(sort.Reverse(sort.StringSlice(files))) result := &QueryResult{Events: make([]*types.AuditEvent, 0, filter.Limit)} for _, name := range files { events, err := readEventsReverse(filepath.Join(dir, name), &filter, filter.Limit-len(result.Events)) if err != nil { // A single unreadable file must not kill the query. continue } if len(events) > 0 { result.Files = append(result.Files, name) result.Events = append(result.Events, events...) } if len(result.Events) >= filter.Limit { result.Truncated = true break } } result.Total = len(result.Events) return result, nil } // readEventsReverse reads one JSONL file and returns up to max matching events, // newest first. Files are daily-rotated so reading one fully is bounded. func readEventsReverse(path string, filter *QueryFilter, max int) ([]*types.AuditEvent, error) { if max <= 0 { return nil, nil } f, err := os.Open(path) if err != nil { return nil, err } defer f.Close() var matched []*types.AuditEvent scanner := bufio.NewScanner(f) // Audit lines carry base64 quotes/details and can exceed the 64KB default. scanner.Buffer(make([]byte, 0, 64*1024), 4*1024*1024) for scanner.Scan() { line := strings.TrimSpace(scanner.Text()) if line == "" { continue } var e types.AuditEvent if err := json.Unmarshal([]byte(line), &e); err != nil { continue // skip malformed record } if !filter.matches(&e) { continue } ev := e matched = append(matched, &ev) } if err := scanner.Err(); err != nil { return nil, err } // Reverse into newest-first order and trim to max. for i, j := 0, len(matched)-1; i < j; i, j = i+1, j-1 { matched[i], matched[j] = matched[j], matched[i] } if len(matched) > max { matched = matched[:max] } return matched, nil } // Stats summarizes the audit log for a management dashboard. type Stats struct { Total int `json:"total"` ByType map[string]int `json:"by_type"` ByResult map[string]int `json:"by_result"` Devices int `json:"devices"` OldestTime *time.Time `json:"oldest_time,omitempty"` NewestTime *time.Time `json:"newest_time,omitempty"` } // Stats computes aggregate counters over the most recent MaxQueryLimit events. func (l *Logger) Stats() (*Stats, error) { res, err := l.Query(QueryFilter{Limit: MaxQueryLimit}) if err != nil { return nil, err } s := &Stats{ ByType: map[string]int{}, ByResult: map[string]int{}, } devices := map[string]struct{}{} for _, e := range res.Events { s.Total++ s.ByType[string(e.EventType)]++ if e.Result != "" { s.ByResult[e.Result]++ } if e.DeviceID != "" { devices[e.DeviceID] = struct{}{} } t := e.Timestamp if s.NewestTime == nil || t.After(*s.NewestTime) { nt := t s.NewestTime = &nt } if s.OldestTime == nil || t.Before(*s.OldestTime) { ot := t s.OldestTime = &ot } } s.Devices = len(devices) return s, nil }