refactor(webui): extract the audit module and its export guards (#1778)
This commit is contained in:
3 files changed
+83
-63
No files matched your search
@@ -7,12 +7,14 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
"maps"
|
||||
"mime"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"google.golang.org/grpc/codes"
|
||||
@@ -21,6 +23,7 @@ import (
|
||||
controlpb "github.com/samcharles93/archie-core/internal/contracts/controlplane/v1"
|
||||
"github.com/samcharles93/archie-core/internal/domain/access"
|
||||
"github.com/samcharles93/archie-core/internal/domain/audit"
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow/task"
|
||||
"github.com/samcharles93/archie-core/internal/ratelimit"
|
||||
)
|
||||
|
||||
@@ -116,11 +119,46 @@ type auditResponse struct {
|
||||
Facets *auditFacetsView `json:"facets,omitempty"`
|
||||
}
|
||||
|
||||
// auditAPI owns the /api/audit routes. exporting admits one export per
|
||||
// identity and exportRate bounds how many each identity starts an hour; an
|
||||
// export is also bounded by its explicit range, a five-minute deadline and a
|
||||
// page cap.
|
||||
type auditAPI struct {
|
||||
audit audit.Store
|
||||
controlPlane controlpb.ControlPlaneServiceClient
|
||||
log *slog.Logger
|
||||
actorNames func(context.Context) func(string) string
|
||||
workflowDefinitions func(context.Context) ([]task.Definition, error)
|
||||
|
||||
exportRate *ratelimit.Limiter
|
||||
exportMu sync.Mutex
|
||||
exporting map[string]bool
|
||||
}
|
||||
|
||||
func newAuditAPI(s *Server) *auditAPI {
|
||||
return &auditAPI{
|
||||
audit: s.Audit,
|
||||
controlPlane: s.ControlPlane,
|
||||
log: s.Log,
|
||||
actorNames: s.actorNames,
|
||||
workflowDefinitions: s.workflowDefinitions,
|
||||
exportRate: ratelimit.New(auditExportWindow, auditExportPerHour),
|
||||
exporting: map[string]bool{},
|
||||
}
|
||||
}
|
||||
|
||||
func (a *auditAPI) logf(msg string, args ...any) { warnf(a.log, msg, args...) }
|
||||
|
||||
func (a *auditAPI) register(mux *http.ServeMux) {
|
||||
mux.HandleFunc("GET /api/audit", a.handleAudit)
|
||||
mux.HandleFunc("GET /api/audit/export", a.handleAuditExport)
|
||||
}
|
||||
|
||||
// handleAudit is GET /api/audit: one page of the org-scoped feed. The org and
|
||||
// the reader's authority come from the authenticated principal in the request
|
||||
// context; the query string never names an org.
|
||||
func (s *Server) handleAudit(w http.ResponseWriter, r *http.Request) {
|
||||
if s.Audit == nil {
|
||||
func (a *auditAPI) handleAudit(w http.ResponseWriter, r *http.Request) {
|
||||
if a.audit == nil {
|
||||
http.Error(w, "audit feed unavailable", http.StatusServiceUnavailable)
|
||||
return
|
||||
}
|
||||
@@ -133,12 +171,12 @@ func (s *Server) handleAudit(w http.ResponseWriter, r *http.Request) {
|
||||
http.Error(w, firstLine(err.Error()), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
page, err := s.Audit.ListAudit(r.Context(), filter, cursor, limit)
|
||||
page, err := a.audit.ListAudit(r.Context(), filter, cursor, limit)
|
||||
if err != nil {
|
||||
writeAuditError(w, err)
|
||||
return
|
||||
}
|
||||
labels, settings := s.auditLabels(r.Context())
|
||||
labels, settings := a.auditLabels(r.Context())
|
||||
records := make([]auditRecordView, 0, len(page.Entries))
|
||||
for _, entry := range page.Entries {
|
||||
records = append(records, auditRecord(entry, labels))
|
||||
@@ -159,8 +197,8 @@ func (s *Server) handleAudit(w http.ResponseWriter, r *http.Request) {
|
||||
// handleAuditExport is GET /api/audit/export?format=csv|json with the feed's
|
||||
// filters. It requires an explicit range of at most a year and streams rows
|
||||
// page by page.
|
||||
func (s *Server) handleAuditExport(w http.ResponseWriter, r *http.Request) {
|
||||
if s.Audit == nil {
|
||||
func (a *auditAPI) handleAuditExport(w http.ResponseWriter, r *http.Request) {
|
||||
if a.audit == nil {
|
||||
http.Error(w, "audit feed unavailable", http.StatusServiceUnavailable)
|
||||
return
|
||||
}
|
||||
@@ -185,12 +223,12 @@ func (s *Server) handleAuditExport(w http.ResponseWriter, r *http.Request) {
|
||||
identity := auditExportIdentity(r.Context())
|
||||
// Take the guard before charging the budget: a request rejected because
|
||||
// the same identity is already exporting must cost no allowance.
|
||||
if !s.beginAuditExport(identity) {
|
||||
if !a.beginAuditExport(identity) {
|
||||
http.Error(w, "an audit export is already running for this identity", http.StatusTooManyRequests)
|
||||
return
|
||||
}
|
||||
defer s.endAuditExport(identity)
|
||||
if !s.auditExportLimiter().Allow("audit-export", identity) {
|
||||
defer a.endAuditExport(identity)
|
||||
if !a.exportRate.Allow("audit-export", identity) {
|
||||
http.Error(w, "too many audit exports; try again later", http.StatusTooManyRequests)
|
||||
return
|
||||
}
|
||||
@@ -199,41 +237,41 @@ func (s *Server) handleAuditExport(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
// Count before any header is written: a match over the cap is the
|
||||
// caller's problem to narrow, not a download that ends early.
|
||||
if !s.auditExportWithinBudget(ctx, w, filter) {
|
||||
if !a.auditExportWithinBudget(ctx, w, filter) {
|
||||
return
|
||||
}
|
||||
|
||||
// Fetch the first page before setting download headers: a failure here is
|
||||
// still a normal error response, not a truncated file.
|
||||
first, err := s.Audit.ListAudit(ctx, filter, audit.Cursor{}, auditExportPageSize)
|
||||
first, err := a.audit.ListAudit(ctx, filter, audit.Cursor{}, auditExportPageSize)
|
||||
if err != nil {
|
||||
writeAuditError(w, err)
|
||||
return
|
||||
}
|
||||
writeAuditExportHeaders(w, format)
|
||||
|
||||
labels, _ := s.auditLabels(ctx)
|
||||
labels, _ := a.auditLabels(ctx)
|
||||
flush := func() {}
|
||||
if flusher, ok := w.(http.Flusher); ok {
|
||||
flush = flusher.Flush
|
||||
}
|
||||
var rows int64
|
||||
if format == "csv" {
|
||||
rows, err = s.exportAuditCSV(ctx, filter, first, labels, w, flush)
|
||||
rows, err = a.exportAuditCSV(ctx, filter, first, labels, w, flush)
|
||||
} else {
|
||||
rows, err = s.exportAuditJSON(ctx, filter, first, labels, w, flush)
|
||||
rows, err = a.exportAuditJSON(ctx, filter, first, labels, w, flush)
|
||||
}
|
||||
if err != nil {
|
||||
// Headers and status are already on the wire, so no error status is
|
||||
// left to send. Abort the connection so the client sees a failed
|
||||
// download rather than a silently short file.
|
||||
s.logf("audit export failed", "format", format, "err", err)
|
||||
a.logf("audit export failed", "format", format, "err", err)
|
||||
panic(http.ErrAbortHandler)
|
||||
}
|
||||
// The file is complete; the trail row is bookkeeping, so a failed write
|
||||
// is logged rather than turning a delivered export into a failed one.
|
||||
if err := s.Audit.RecordAuditExport(r.Context(), format, filter.Hash(), rows); err != nil {
|
||||
s.logf("record audit export", "format", format, "err", err)
|
||||
if err := a.audit.RecordAuditExport(r.Context(), format, filter.Hash(), rows); err != nil {
|
||||
a.logf("record audit export", "format", format, "err", err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -241,8 +279,8 @@ func (s *Server) handleAuditExport(w http.ResponseWriter, r *http.Request) {
|
||||
// cap, answering before any header is written so the client never receives a
|
||||
// truncated file it cannot tell from a complete one. A feed error is written
|
||||
// with the same status mapping as a page read.
|
||||
func (s *Server) auditExportWithinBudget(ctx context.Context, w http.ResponseWriter, filter audit.Filter) bool {
|
||||
count, err := s.Audit.CountAudit(ctx, filter)
|
||||
func (a *auditAPI) auditExportWithinBudget(ctx context.Context, w http.ResponseWriter, filter audit.Filter) bool {
|
||||
count, err := a.audit.CountAudit(ctx, filter)
|
||||
if err != nil {
|
||||
writeAuditError(w, err)
|
||||
return false
|
||||
@@ -301,12 +339,12 @@ func writeAuditExportHeaders(w http.ResponseWriter, format string) {
|
||||
w.Header().Set("X-Content-Type-Options", "nosniff")
|
||||
}
|
||||
|
||||
func (s *Server) exportAuditCSV(ctx context.Context, filter audit.Filter, first audit.Page, labels *auditLabels, w io.Writer, flush func()) (int64, error) {
|
||||
func (a *auditAPI) exportAuditCSV(ctx context.Context, filter audit.Filter, first audit.Page, labels *auditLabels, w io.Writer, flush func()) (int64, error) {
|
||||
writer := csv.NewWriter(w)
|
||||
if err := writer.Write(auditCSVHeader); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
rows, err := s.walkAudit(ctx, filter, first, func(entry audit.Entry) error {
|
||||
rows, err := a.walkAudit(ctx, filter, first, func(entry audit.Entry) error {
|
||||
return writer.Write(auditCSVRow(auditRecord(entry, labels)))
|
||||
}, func() {
|
||||
writer.Flush()
|
||||
@@ -319,13 +357,13 @@ func (s *Server) exportAuditCSV(ctx context.Context, filter audit.Filter, first
|
||||
return rows, err
|
||||
}
|
||||
|
||||
func (s *Server) exportAuditJSON(ctx context.Context, filter audit.Filter, first audit.Page, labels *auditLabels, w io.Writer, flush func()) (int64, error) {
|
||||
func (a *auditAPI) exportAuditJSON(ctx context.Context, filter audit.Filter, first audit.Page, labels *auditLabels, w io.Writer, flush func()) (int64, error) {
|
||||
if _, err := io.WriteString(w, "["); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
encoder := json.NewEncoder(w)
|
||||
firstRecord := true
|
||||
rows, err := s.walkAudit(ctx, filter, first, func(entry audit.Entry) error {
|
||||
rows, err := a.walkAudit(ctx, filter, first, func(entry audit.Entry) error {
|
||||
if !firstRecord {
|
||||
if _, err := io.WriteString(w, ","); err != nil {
|
||||
return err
|
||||
@@ -346,7 +384,7 @@ func (s *Server) exportAuditJSON(ctx context.Context, filter audit.Filter, first
|
||||
// walkAudit streams every entry of the window in feed order and returns how
|
||||
// many it emitted. It starts from the already-fetched first page so the caller
|
||||
// could answer an error before any byte of the download was written.
|
||||
func (s *Server) walkAudit(ctx context.Context, filter audit.Filter, first audit.Page, emit func(audit.Entry) error, afterPage func()) (int64, error) {
|
||||
func (a *auditAPI) walkAudit(ctx context.Context, filter audit.Filter, first audit.Page, emit func(audit.Entry) error, afterPage func()) (int64, error) {
|
||||
page := first
|
||||
pages := 1
|
||||
var rows int64
|
||||
@@ -371,7 +409,7 @@ func (s *Server) walkAudit(ctx context.Context, filter audit.Filter, first audit
|
||||
if !ok {
|
||||
return rows, errAuditExportCursor
|
||||
}
|
||||
next, err := s.Audit.ListAudit(ctx, filter, cursor, auditExportPageSize)
|
||||
next, err := a.audit.ListAudit(ctx, filter, cursor, auditExportPageSize)
|
||||
if err != nil {
|
||||
return rows, err
|
||||
}
|
||||
@@ -472,10 +510,10 @@ func (l *auditLabels) workflow(id string) string {
|
||||
// auditLabels builds the label resolver and the settings-kind facet in one
|
||||
// catalog read. A failed catalog read degrades labels to their ids; it never
|
||||
// fails the feed.
|
||||
func (s *Server) auditLabels(ctx context.Context) (*auditLabels, []auditSettingsKind) {
|
||||
labels := &auditLabels{actorName: s.actorNames(ctx), titles: map[string]string{}}
|
||||
func (a *auditAPI) auditLabels(ctx context.Context) (*auditLabels, []auditSettingsKind) {
|
||||
labels := &auditLabels{actorName: a.actorNames(ctx), titles: map[string]string{}}
|
||||
settings := []auditSettingsKind{}
|
||||
for _, resource := range s.auditCatalogResources(ctx) {
|
||||
for _, resource := range a.auditCatalogResources(ctx) {
|
||||
if resource.GetTitle() != "" {
|
||||
labels.titles[resource.GetKind()] = resource.GetTitle()
|
||||
}
|
||||
@@ -486,7 +524,7 @@ func (s *Server) auditLabels(ctx context.Context) (*auditLabels, []auditSettings
|
||||
}
|
||||
}
|
||||
labels.workflowFn = func() map[string]string {
|
||||
definitions, err := s.workflowDefinitions(ctx)
|
||||
definitions, err := a.workflowDefinitions(ctx)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
@@ -502,11 +540,11 @@ func (s *Server) auditLabels(ctx context.Context) (*auditLabels, []auditSettings
|
||||
// auditCatalogResources reads the control-plane catalogue once. A missing
|
||||
// control plane or a failed read leaves labels at their ids rather than
|
||||
// failing the feed.
|
||||
func (s *Server) auditCatalogResources(ctx context.Context) []*controlpb.ResourceDescriptor {
|
||||
if s.ControlPlane == nil {
|
||||
func (a *auditAPI) auditCatalogResources(ctx context.Context) []*controlpb.ResourceDescriptor {
|
||||
if a.controlPlane == nil {
|
||||
return nil
|
||||
}
|
||||
catalog, err := s.ControlPlane.Catalog(ctx, &controlpb.CatalogRequest{})
|
||||
catalog, err := a.controlPlane.Catalog(ctx, &controlpb.CatalogRequest{})
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
@@ -639,33 +677,23 @@ func auditExportIdentity(ctx context.Context) string {
|
||||
return "unknown"
|
||||
}
|
||||
|
||||
func (s *Server) auditExportLimiter() *ratelimit.Limiter {
|
||||
s.auditExportOnce.Do(func() {
|
||||
s.auditExportRate = ratelimit.New(auditExportWindow, auditExportPerHour)
|
||||
})
|
||||
return s.auditExportRate
|
||||
}
|
||||
|
||||
// beginAuditExport admits one export per identity. The guard is per identity,
|
||||
// not process-wide: one person's long export must not stop another person in
|
||||
// another org from taking their own.
|
||||
func (s *Server) beginAuditExport(identity string) bool {
|
||||
s.auditExportMu.Lock()
|
||||
defer s.auditExportMu.Unlock()
|
||||
if s.auditExporting == nil {
|
||||
s.auditExporting = make(map[string]bool)
|
||||
}
|
||||
if s.auditExporting[identity] {
|
||||
func (a *auditAPI) beginAuditExport(identity string) bool {
|
||||
a.exportMu.Lock()
|
||||
defer a.exportMu.Unlock()
|
||||
if a.exporting[identity] {
|
||||
return false
|
||||
}
|
||||
s.auditExporting[identity] = true
|
||||
a.exporting[identity] = true
|
||||
return true
|
||||
}
|
||||
|
||||
func (s *Server) endAuditExport(identity string) {
|
||||
s.auditExportMu.Lock()
|
||||
delete(s.auditExporting, identity)
|
||||
s.auditExportMu.Unlock()
|
||||
func (a *auditAPI) endAuditExport(identity string) {
|
||||
a.exportMu.Lock()
|
||||
delete(a.exporting, identity)
|
||||
a.exportMu.Unlock()
|
||||
}
|
||||
|
||||
var auditCSVHeader = []string{
|
||||
@@ -77,7 +77,7 @@ func TestAuditErrorMapping(t *testing.T) {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
srv := &Server{Audit: auditStub{err: tt.err}}
|
||||
rec := httptest.NewRecorder()
|
||||
srv.handleAudit(rec, httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/api/audit", nil))
|
||||
newAuditAPI(srv).handleAudit(rec, httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/api/audit", nil))
|
||||
if rec.Code != tt.want {
|
||||
t.Fatalf("status = %d, want %d", rec.Code, tt.want)
|
||||
}
|
||||
@@ -90,7 +90,7 @@ func TestAuditErrorMapping(t *testing.T) {
|
||||
|
||||
func TestAuditUnavailableWithoutFeed(t *testing.T) {
|
||||
rec := httptest.NewRecorder()
|
||||
(&Server{}).handleAudit(rec, httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/api/audit", nil))
|
||||
newAuditAPI(&Server{}).handleAudit(rec, httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/api/audit", nil))
|
||||
if rec.Code != http.StatusServiceUnavailable {
|
||||
t.Fatalf("status = %d, want %d", rec.Code, http.StatusServiceUnavailable)
|
||||
}
|
||||
@@ -104,7 +104,7 @@ func TestAuditExportRefusesOverTheRowCap(t *testing.T) {
|
||||
req := httptest.NewRequestWithContext(t.Context(), http.MethodGet,
|
||||
"/api/audit/export?format=json&from=2026-01-01T00:00:00Z&to=2026-01-02T00:00:00Z", nil)
|
||||
rec := httptest.NewRecorder()
|
||||
srv.handleAuditExport(rec, req)
|
||||
newAuditAPI(srv).handleAuditExport(rec, req)
|
||||
if rec.Code != http.StatusBadRequest {
|
||||
t.Fatalf("status = %d, want %d body=%s", rec.Code, http.StatusBadRequest, rec.Body.String())
|
||||
}
|
||||
@@ -118,13 +118,6 @@ type Server struct {
|
||||
// Audit is the org-scoped audit feed, behind the audit page and the live
|
||||
// tail. Nil answers 503 on every audit route.
|
||||
Audit audit.Store
|
||||
// auditExportOnce initialises auditExportRate; auditExporting and its
|
||||
// mutex admit one export per identity. An export is bounded by its
|
||||
// explicit range, a five-minute deadline and a page cap (see api_audit.go).
|
||||
auditExportOnce sync.Once
|
||||
auditExportRate *ratelimit.Limiter
|
||||
auditExportMu sync.Mutex
|
||||
auditExporting map[string]bool
|
||||
// now is the clock applyState reads staleness against. Nil means
|
||||
// time.Now; tests set it to pin a record's age.
|
||||
now func() time.Time
|
||||
@@ -289,13 +282,12 @@ func (s *Server) registerCoreRoutes(mux *http.ServeMux) {
|
||||
mux.HandleFunc("GET /api/curators", instanceOnly(s.handleCurators))
|
||||
mux.HandleFunc("GET /api/captures", s.handleCaptures)
|
||||
newSchedulesAPI(s).register(mux)
|
||||
newAuditAPI(s).register(mux)
|
||||
newAlertsAPI(s).register(mux)
|
||||
mux.HandleFunc("GET /api/channels", instanceOnly(s.handleChannels))
|
||||
mux.HandleFunc("POST /api/channels/{id}/reload", instanceOnly(s.handleChannelReload))
|
||||
mux.HandleFunc("GET /api/version", s.handleVersion)
|
||||
mux.HandleFunc("GET /api/capabilities", s.handleCapabilities)
|
||||
mux.HandleFunc("GET /api/audit", s.handleAudit)
|
||||
mux.HandleFunc("GET /api/audit/export", s.handleAuditExport)
|
||||
}
|
||||
|
||||
func (s *Server) registerTaskRoutes(mux *http.ServeMux) {
|
||||
|
||||
Reference in new issue
Block a user