fix(webui): capture list narrows to the requested workspace in the store query (#1335)

This commit is contained in:
sam committed 2026-11-14 15:29:36 +11:00
1 parent 48c28b36ba
commit 80129b7dd6
13 files changed
+115 -23

No files matched your search

+14 -4
View File
@@ -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" +
+4 -1
View File
@@ -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.
@@ -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
}
@@ -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
}
+6 -4
View File
@@ -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)
@@ -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
}
@@ -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;
+4 -3
View File
@@ -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)
}
@@ -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)
}
})
}
}
+3 -1
View File
@@ -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)
+1 -1
View File
@@ -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)
+1 -1
View File
@@ -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
}
+5 -1
View File
@@ -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 {