From 80129b7dd62c06af36605fdcdb8a2c52986c3a95 Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Sat, 14 Nov 2026 15:29:18 +1100 Subject: [PATCH] fix(webui): capture list narrows to the requested workspace in the store query (#1335) --- internal/contracts/state/v1/capture.pb.go | 18 +++-- internal/domain/storecontract/capture.go | 5 +- .../infrastructure/captureintake/org_test.go | 2 +- .../captureintake/receiver_test.go | 3 +- internal/infrastructure/postgres/edastore.go | 10 +-- .../postgres/postgresdb/edastore.sql.go | 9 +-- .../postgres/queries/edastore.sql | 2 +- internal/infrastructure/staterpc/captures.go | 7 +- .../staterpc/captures_workspace_test.go | 68 +++++++++++++++++++ internal/webui/api_capture.go | 4 +- internal/webui/eventtypes.go | 2 +- internal/webui/mappings.go | 2 +- proto/state/v1/capture.proto | 6 +- 13 files changed, 115 insertions(+), 23 deletions(-) create mode 100644 internal/infrastructure/staterpc/captures_workspace_test.go diff --git a/internal/contracts/state/v1/capture.pb.go b/internal/contracts/state/v1/capture.pb.go index 0e10e4c9d..4a754fcc8 100644 --- a/internal/contracts/state/v1/capture.pb.go +++ b/internal/contracts/state/v1/capture.pb.go @@ -520,8 +520,10 @@ func (x *InsertCaptureResponse) GetId() string { } type StreamCapturesRequest struct { - state protoimpl.MessageState `protogen:"open.v1"` - Limit int64 `protobuf:"varint,1,opt,name=limit,proto3" json:"limit,omitempty"` + state protoimpl.MessageState `protogen:"open.v1"` + Limit int64 `protobuf:"varint,1,opt,name=limit,proto3" json:"limit,omitempty"` + // workspace_id narrows the list to one workspace; empty lists every one. + WorkspaceId string `protobuf:"bytes,2,opt,name=workspace_id,json=workspaceId,proto3" json:"workspace_id,omitempty"` unknownFields protoimpl.UnknownFields sizeCache protoimpl.SizeCache } @@ -563,6 +565,13 @@ func (x *StreamCapturesRequest) GetLimit() int64 { return 0 } +func (x *StreamCapturesRequest) GetWorkspaceId() string { + if x != nil { + return x.WorkspaceId + } + return "" +} + // Streamed one capture per message; see StreamCaptures above. type StreamCapturesResponse struct { state protoimpl.MessageState `protogen:"open.v1"` @@ -750,9 +759,10 @@ const file_state_v1_capture_proto_rawDesc = "" + "\n" + "max_events\x18\x03 \x01(\x03R\tmaxEvents\"'\n" + "\x15InsertCaptureResponse\x12\x0e\n" + - "\x02id\x18\x01 \x01(\tR\x02id\"-\n" + + "\x02id\x18\x01 \x01(\tR\x02id\"P\n" + "\x15StreamCapturesRequest\x12\x14\n" + - "\x05limit\x18\x01 \x01(\x03R\x05limit\"K\n" + + "\x05limit\x18\x01 \x01(\x03R\x05limit\x12!\n" + + "\fworkspace_id\x18\x02 \x01(\tR\vworkspaceId\"K\n" + "\x16StreamCapturesResponse\x121\n" + "\acapture\x18\x01 \x01(\v2\x17.state.v1.CapturedEventR\acapture\"d\n" + "!StreamUndispatchedCapturesRequest\x12\x1a\n" + diff --git a/internal/domain/storecontract/capture.go b/internal/domain/storecontract/capture.go index a8b870e02..cd3cef625 100644 --- a/internal/domain/storecontract/capture.go +++ b/internal/domain/storecontract/capture.go @@ -5,12 +5,15 @@ import ( "time" "github.com/samcharles93/archie-core/internal/domain/eventtype" + "github.com/samcharles93/archie-core/internal/domain/org" ) // CaptureStore persists inbound webhook captures. type CaptureStore interface { InsertCapture(ctx context.Context, c CapturedEvent, retention time.Duration, maxEvents int) (string, error) - ListCaptures(ctx context.Context, limit int) ([]CapturedEvent, error) + // ListCaptures returns the newest captures in the acting org. A non-empty + // workspace narrows them to that workspace's sources. + ListCaptures(ctx context.Context, workspace org.WorkspaceID, limit int) ([]CapturedEvent, error) } // CaptureRefusalStore counts the events network rules refused, per source. diff --git a/internal/infrastructure/captureintake/org_test.go b/internal/infrastructure/captureintake/org_test.go index b918e1dec..e48597853 100644 --- a/internal/infrastructure/captureintake/org_test.go +++ b/internal/infrastructure/captureintake/org_test.go @@ -31,7 +31,7 @@ func (s *stubCaptures) InsertCapture(_ context.Context, _ storecontract.Captured return "capture-1", nil } -func (s *stubCaptures) ListCaptures(_ context.Context, _ int) ([]storecontract.CapturedEvent, error) { +func (s *stubCaptures) ListCaptures(_ context.Context, _ org.WorkspaceID, _ int) ([]storecontract.CapturedEvent, error) { return nil, nil } diff --git a/internal/infrastructure/captureintake/receiver_test.go b/internal/infrastructure/captureintake/receiver_test.go index 0c68b7b22..58fa611be 100644 --- a/internal/infrastructure/captureintake/receiver_test.go +++ b/internal/infrastructure/captureintake/receiver_test.go @@ -9,6 +9,7 @@ import ( "testing" "time" + "github.com/samcharles93/archie-core/internal/domain/org" "github.com/samcharles93/archie-core/internal/domain/source" "github.com/samcharles93/archie-core/internal/domain/storecontract" ) @@ -41,7 +42,7 @@ func (c *recordingCaptures) InsertCapture(_ context.Context, e storecontract.Cap return "id", nil } -func (c *recordingCaptures) ListCaptures(context.Context, int) ([]storecontract.CapturedEvent, error) { +func (c *recordingCaptures) ListCaptures(context.Context, org.WorkspaceID, int) ([]storecontract.CapturedEvent, error) { return nil, nil } diff --git a/internal/infrastructure/postgres/edastore.go b/internal/infrastructure/postgres/edastore.go index 8e5621b86..60a183e98 100644 --- a/internal/infrastructure/postgres/edastore.go +++ b/internal/infrastructure/postgres/edastore.go @@ -212,11 +212,13 @@ func captureValue(r postgresdb.Capture) storecontract.CapturedEvent { } } -// ListCaptures returns the newest captures first in the acting org. -func (s *EDA) ListCaptures(ctx context.Context, limit int) ([]storecontract.CapturedEvent, error) { +// ListCaptures returns the newest captures first in the acting org, only those +// of workspace when it is set. +func (s *EDA) ListCaptures(ctx context.Context, workspace org.WorkspaceID, limit int) ([]storecontract.CapturedEvent, error) { rows, err := s.q.ListCaptures(ctx, postgresdb.ListCapturesParams{ - OrgID: string(org.OrgFromContext(ctx)), - Limit: int32(limit), + OrgID: string(org.OrgFromContext(ctx)), + WorkspaceID: string(workspace), + Limit: int32(limit), }) if err != nil { return nil, fmt.Errorf("edastore: list captures: %w", err) diff --git a/internal/infrastructure/postgres/postgresdb/edastore.sql.go b/internal/infrastructure/postgres/postgresdb/edastore.sql.go index d588c5544..2d41f2d30 100644 --- a/internal/infrastructure/postgres/postgresdb/edastore.sql.go +++ b/internal/infrastructure/postgres/postgresdb/edastore.sql.go @@ -792,18 +792,19 @@ func (q *Queries) ListBindings(ctx context.Context, scopeOrg pgtype.Text) ([]Lis const listCaptures = `-- name: ListCaptures :many SELECT id, source, remote_addr, content_type, headers, body, authenticated, received_at, unsigned, event_type, org_id, workspace_id, delivery FROM captures -WHERE org_id = $2 +WHERE org_id = $2 AND ($3::text = '' OR workspace_id = $3) ORDER BY received_at DESC LIMIT $1 ` type ListCapturesParams struct { - Limit int32 - OrgID string + Limit int32 + OrgID string + WorkspaceID string } func (q *Queries) ListCaptures(ctx context.Context, arg ListCapturesParams) ([]Capture, error) { - rows, err := q.db.Query(ctx, listCaptures, arg.Limit, arg.OrgID) + rows, err := q.db.Query(ctx, listCaptures, arg.Limit, arg.OrgID, arg.WorkspaceID) if err != nil { return nil, err } diff --git a/internal/infrastructure/postgres/queries/edastore.sql b/internal/infrastructure/postgres/queries/edastore.sql index 59ff9e133..f87a2acf8 100644 --- a/internal/infrastructure/postgres/queries/edastore.sql +++ b/internal/infrastructure/postgres/queries/edastore.sql @@ -10,7 +10,7 @@ FROM (SELECT 1) intake LEFT JOIN sources s ON s.path = $2; -- name: ListCaptures :many SELECT id, source, remote_addr, content_type, headers, body, authenticated, received_at, unsigned, event_type, org_id, workspace_id, delivery FROM captures -WHERE org_id = @org_id +WHERE org_id = @org_id AND (@workspace_id::text = '' OR workspace_id = @workspace_id) ORDER BY received_at DESC LIMIT $1; diff --git a/internal/infrastructure/staterpc/captures.go b/internal/infrastructure/staterpc/captures.go index bc67cfc74..0e3ccb347 100644 --- a/internal/infrastructure/staterpc/captures.go +++ b/internal/infrastructure/staterpc/captures.go @@ -7,6 +7,7 @@ import ( "time" pb "github.com/samcharles93/archie-core/internal/contracts/state/v1" + "github.com/samcharles93/archie-core/internal/domain/org" "github.com/samcharles93/archie-core/internal/domain/storecontract" ) @@ -66,7 +67,7 @@ func (s *captureServer) StreamCaptures(r *pb.StreamCapturesRequest, stream pb.Ca if err != nil { return err } - captures, err := cs.ListCaptures(stream.Context(), int(r.Limit)) + captures, err := cs.ListCaptures(stream.Context(), org.WorkspaceID(r.WorkspaceId), int(r.Limit)) if err != nil { return s.logErr(stream.Context(), "StreamCaptures", err) } @@ -128,8 +129,8 @@ func (c *Client) CaptureRefusals(ctx context.Context, since time.Time) ([]storec // ListCaptures streams one capture per message; see StreamCaptures in // server.go. -func (c *Client) ListCaptures(ctx context.Context, limit int) ([]storecontract.CapturedEvent, error) { - stream, err := c.capture.StreamCaptures(ctx, &pb.StreamCapturesRequest{Limit: int64(limit)}) +func (c *Client) ListCaptures(ctx context.Context, workspace org.WorkspaceID, limit int) ([]storecontract.CapturedEvent, error) { + stream, err := c.capture.StreamCaptures(ctx, &pb.StreamCapturesRequest{Limit: int64(limit), WorkspaceId: string(workspace)}) if err != nil { return nil, unmapError(err) } diff --git a/internal/infrastructure/staterpc/captures_workspace_test.go b/internal/infrastructure/staterpc/captures_workspace_test.go new file mode 100644 index 000000000..ecb6fd6d7 --- /dev/null +++ b/internal/infrastructure/staterpc/captures_workspace_test.go @@ -0,0 +1,68 @@ +package staterpc + +import ( + "context" + "testing" + + "google.golang.org/grpc" + + pb "github.com/samcharles93/archie-core/internal/contracts/state/v1" + "github.com/samcharles93/archie-core/internal/domain/org" + "github.com/samcharles93/archie-core/internal/domain/storecontract" +) + +type workspaceCaptures struct { + storecontract.CaptureStore + byWorkspace map[org.WorkspaceID][]storecontract.CapturedEvent +} + +func (w workspaceCaptures) ListCaptures(_ context.Context, ws org.WorkspaceID, _ int) ([]storecontract.CapturedEvent, error) { + if ws != "" { + return w.byWorkspace[ws], nil + } + var all []storecontract.CapturedEvent + for _, list := range w.byWorkspace { + all = append(all, list...) + } + return all, nil +} + +type captureStream struct { + grpc.ServerStream + sent []*pb.CapturedEvent +} + +func (s *captureStream) Context() context.Context { return context.Background() } + +func (s *captureStream) Send(r *pb.StreamCapturesResponse) error { + s.sent = append(s.sent, r.Capture) + return nil +} + +// A capture list asked for one workspace carries that workspace to the store. +// Guards the dashboard's workspace-narrowed capture list showing another +// workspace's webhook bodies. +func TestStreamCapturesNarrowsToTheRequestedWorkspace(t *testing.T) { + store := workspaceCaptures{byWorkspace: map[org.WorkspaceID][]storecontract.CapturedEvent{ + "ws-a": {{ID: "a1"}}, + "ws-b": {{ID: "b1"}, {ID: "b2"}}, + }} + s := &captureServer{server: &server{deps: Deps{Captures: store}}} + for _, tt := range []struct { + name, workspace string + want int + }{ + {"one workspace", "ws-b", 2}, + {"every workspace", "", 3}, + } { + t.Run(tt.name, func(t *testing.T) { + stream := &captureStream{} + if err := s.StreamCaptures(&pb.StreamCapturesRequest{Limit: 10, WorkspaceId: tt.workspace}, stream); err != nil { + t.Fatal(err) + } + if len(stream.sent) != tt.want { + t.Fatalf("got %d captures, want %d", len(stream.sent), tt.want) + } + }) + } +} diff --git a/internal/webui/api_capture.go b/internal/webui/api_capture.go index 18d9b9fa6..c116201b9 100644 --- a/internal/webui/api_capture.go +++ b/internal/webui/api_capture.go @@ -3,6 +3,8 @@ package webui import ( "net/http" "strconv" + + "github.com/samcharles93/archie-core/internal/domain/org" ) // defaultCapturesListLimit applies when the limit parameter is missing, @@ -20,7 +22,7 @@ func (s *Server) handleCaptures(w http.ResponseWriter, r *http.Request) { if err != nil || limit <= 0 { limit = defaultCapturesListLimit } - captures, err := s.Captures.ListCaptures(r.Context(), int(limit)) + captures, err := s.Captures.ListCaptures(r.Context(), org.WorkspaceID(r.URL.Query().Get("workspace")), int(limit)) if err != nil { s.Log.ErrorContext(r.Context(), "list captures", "err", err) http.Error(w, "list captures failed", http.StatusInternalServerError) diff --git a/internal/webui/eventtypes.go b/internal/webui/eventtypes.go index 753191ff3..82344e885 100644 --- a/internal/webui/eventtypes.go +++ b/internal/webui/eventtypes.go @@ -77,7 +77,7 @@ func (a *eventTypesAPI) handleEventTypes(w http.ResponseWriter, r *http.Request) } proposals := []eventtype.Proposal{} if a.captures != nil { - captures, err := a.captures.ListCaptures(r.Context(), proposalScanLimit) + captures, err := a.captures.ListCaptures(r.Context(), "", proposalScanLimit) if err != nil { a.log.ErrorContext(r.Context(), "list captures for proposals", "err", err) http.Error(w, "list captures failed", http.StatusInternalServerError) diff --git a/internal/webui/mappings.go b/internal/webui/mappings.go index bb08620ac..bcdeeb273 100644 --- a/internal/webui/mappings.go +++ b/internal/webui/mappings.go @@ -270,7 +270,7 @@ func (a *mappingsAPI) handleMappingPreview(w http.ResponseWriter, r *http.Reques // ListCaptures, so this scans the newest window rather than adding a new store // method for a single low-volume caller. func (a *mappingsAPI) captureByID(ctx context.Context, id string) (*storecontract.CapturedEvent, error) { - captures, err := a.captures.ListCaptures(ctx, mappingCaptureScanWindow(a.captureMaxEvents)) + captures, err := a.captures.ListCaptures(ctx, "", mappingCaptureScanWindow(a.captureMaxEvents)) if err != nil { return nil, err } diff --git a/proto/state/v1/capture.proto b/proto/state/v1/capture.proto index 758c1b0fb..2ea3c63a2 100644 --- a/proto/state/v1/capture.proto +++ b/proto/state/v1/capture.proto @@ -60,7 +60,11 @@ message InsertCaptureRequest { int64 max_events = 3; } message InsertCaptureResponse { string id = 1; } -message StreamCapturesRequest { int64 limit = 1; } +message StreamCapturesRequest { + int64 limit = 1; + // workspace_id narrows the list to one workspace; empty lists every one. + string workspace_id = 2; +} // Streamed one capture per message; see StreamCaptures above. message StreamCapturesResponse { CapturedEvent capture = 1; } message StreamUndispatchedCapturesRequest {