fix(events): scope mappings to source owners #2131
No files matched your search
@@ -380,11 +380,44 @@ func mappingOf(r store.Mapping) (mappingView, error) {
|
||||
return v, nil
|
||||
}
|
||||
|
||||
// visibleEventTypeIDs is the caller-visible set; mappings inherit the scope of
|
||||
// their event type's source, not the optional source hint stored on the mapping.
|
||||
func (m *Module) visibleEventTypeIDs(ctx context.Context) (map[uuid.UUID]struct{}, error) {
|
||||
rows, err := m.mappingStore.EventTypes(ctx, m.orgID, caller(ctx))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
visible := make(map[uuid.UUID]struct{}, len(rows))
|
||||
for _, row := range rows {
|
||||
visible[row.ID] = struct{}{}
|
||||
}
|
||||
return visible, nil
|
||||
}
|
||||
|
||||
func (m *Module) visibleMapping(ctx context.Context, id uuid.UUID) (store.Mapping, error) {
|
||||
row, err := m.mappingStore.Mapping(ctx, m.orgID, id)
|
||||
if err != nil {
|
||||
return store.Mapping{}, err
|
||||
}
|
||||
visible, err := m.visibleEventTypeIDs(ctx)
|
||||
if err != nil {
|
||||
return store.Mapping{}, err
|
||||
}
|
||||
if _, ok := visible[row.EventTypeID]; !ok {
|
||||
return store.Mapping{}, store.ErrNotFound
|
||||
}
|
||||
return row, nil
|
||||
}
|
||||
|
||||
func (m *Module) mappings(ctx context.Context, _ *struct{}) (*httpapi.Reply[struct {
|
||||
Mappings []mappingView `json:"mappings"`
|
||||
}], error,
|
||||
) {
|
||||
rows, err := m.store.Mappings(ctx, m.orgID)
|
||||
visible, err := m.visibleEventTypeIDs(ctx)
|
||||
if err != nil {
|
||||
return nil, huma.Error500InternalServerError("couldn't list event types", err)
|
||||
}
|
||||
rows, err := m.mappingStore.Mappings(ctx, m.orgID)
|
||||
if err != nil {
|
||||
return nil, huma.Error500InternalServerError("couldn't list mappings", err)
|
||||
}
|
||||
@@ -393,6 +426,9 @@ func (m *Module) mappings(ctx context.Context, _ *struct{}) (*httpapi.Reply[stru
|
||||
}
|
||||
out.Mappings = make([]mappingView, 0, len(rows))
|
||||
for _, r := range rows {
|
||||
if _, ok := visible[r.EventTypeID]; !ok {
|
||||
continue
|
||||
}
|
||||
v, err := mappingOf(r)
|
||||
if err != nil {
|
||||
return nil, huma.Error500InternalServerError("couldn't read mapping "+r.Name, err)
|
||||
@@ -407,7 +443,7 @@ func (m *Module) mapping(ctx context.Context, in *idInput) (*httpapi.Reply[mappi
|
||||
if err != nil {
|
||||
return nil, huma.Error400BadRequest("a mapping id is a UUID")
|
||||
}
|
||||
row, err := m.store.Mapping(ctx, m.orgID, id)
|
||||
row, err := m.visibleMapping(ctx, id)
|
||||
if errors.Is(err, store.ErrNotFound) {
|
||||
return nil, huma.Error404NotFound("no such mapping")
|
||||
}
|
||||
@@ -446,13 +482,20 @@ func (m *Module) checkedMapping(ctx context.Context, b mappingBody) (store.Mappi
|
||||
if err := candidate.Validate(); err != nil {
|
||||
return store.Mapping{}, huma.Error400BadRequest(err.Error())
|
||||
}
|
||||
stored, err := m.store.EventType(ctx, m.orgID, typeID)
|
||||
stored, err := m.mappingStore.EventType(ctx, m.orgID, typeID)
|
||||
if errors.Is(err, store.ErrNotFound) {
|
||||
return store.Mapping{}, huma.Error400BadRequest("mapping: event type not found: " + b.EventTypeID)
|
||||
}
|
||||
if err != nil {
|
||||
return store.Mapping{}, huma.Error500InternalServerError("couldn't read the event type", err)
|
||||
}
|
||||
usable, err := m.canUse(ctx, stored.SourceID)
|
||||
if err != nil {
|
||||
return store.Mapping{}, huma.Error500InternalServerError("couldn't read the source", err)
|
||||
}
|
||||
if !usable {
|
||||
return store.Mapping{}, huma.Error400BadRequest("mapping: event type not found: " + b.EventTypeID)
|
||||
}
|
||||
t, err := typeOf(stored)
|
||||
if err != nil {
|
||||
return store.Mapping{}, huma.Error500InternalServerError("couldn't read the event type", err)
|
||||
@@ -486,7 +529,7 @@ func (m *Module) createMapping(ctx context.Context, in *createMappingInput) (*ht
|
||||
return nil, err
|
||||
}
|
||||
row.ID = uuid.New()
|
||||
if err := m.store.InsertMapping(ctx, row); err != nil {
|
||||
if err := m.mappingStore.InsertMapping(ctx, row); err != nil {
|
||||
return nil, huma.Error500InternalServerError("couldn't save the mapping", err)
|
||||
}
|
||||
return m.mappingReply(ctx, row.ID)
|
||||
@@ -497,12 +540,17 @@ func (m *Module) updateMapping(ctx context.Context, in *updateMappingInput) (*ht
|
||||
if err != nil {
|
||||
return nil, huma.Error400BadRequest("a mapping id is a UUID")
|
||||
}
|
||||
if _, err := m.visibleMapping(ctx, id); errors.Is(err, store.ErrNotFound) {
|
||||
return nil, huma.Error404NotFound("no such mapping")
|
||||
} else if err != nil {
|
||||
return nil, huma.Error500InternalServerError("couldn't read the mapping", err)
|
||||
}
|
||||
row, err := m.checkedMapping(ctx, in.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
row.ID = id
|
||||
err = m.store.UpdateMapping(ctx, row)
|
||||
err = m.mappingStore.UpdateMapping(ctx, row)
|
||||
if errors.Is(err, store.ErrNotFound) {
|
||||
return nil, huma.Error404NotFound("no such mapping")
|
||||
}
|
||||
@@ -513,7 +561,10 @@ func (m *Module) updateMapping(ctx context.Context, in *updateMappingInput) (*ht
|
||||
}
|
||||
|
||||
func (m *Module) mappingReply(ctx context.Context, id uuid.UUID) (*httpapi.Reply[mappingView], error) {
|
||||
row, err := m.store.Mapping(ctx, m.orgID, id)
|
||||
row, err := m.visibleMapping(ctx, id)
|
||||
if errors.Is(err, store.ErrNotFound) {
|
||||
return nil, huma.Error404NotFound("no such mapping")
|
||||
}
|
||||
if err != nil {
|
||||
return nil, huma.Error500InternalServerError("couldn't read the mapping", err)
|
||||
}
|
||||
@@ -529,7 +580,12 @@ func (m *Module) deleteMapping(ctx context.Context, in *idInput) (*struct{}, err
|
||||
if err != nil {
|
||||
return nil, huma.Error400BadRequest("a mapping id is a UUID")
|
||||
}
|
||||
err = m.store.DeleteMapping(ctx, m.orgID, id)
|
||||
if _, err := m.visibleMapping(ctx, id); errors.Is(err, store.ErrNotFound) {
|
||||
return nil, huma.Error404NotFound("no such mapping")
|
||||
} else if err != nil {
|
||||
return nil, huma.Error500InternalServerError("couldn't read the mapping", err)
|
||||
}
|
||||
err = m.mappingStore.DeleteMapping(ctx, m.orgID, id)
|
||||
switch {
|
||||
case errors.Is(err, store.ErrNotFound):
|
||||
return nil, huma.Error404NotFound("no such mapping")
|
||||
|
||||
@@ -0,0 +1,227 @@
|
||||
package events
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"sort"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/danielgtaylor/huma/v2"
|
||||
"github.com/google/uuid"
|
||||
|
||||
"github.com/samcharles93/archie-core/modules/events/internal/mapping"
|
||||
"github.com/samcharles93/archie-core/modules/events/internal/store"
|
||||
"github.com/samcharles93/archie-core/modules/governance"
|
||||
)
|
||||
|
||||
// TestMappingsAreVisibleOnlyThroughVisibleEventTypes guards person-scoped
|
||||
// sources: mappings of Alice's private event type must not be listed or read
|
||||
// by Bob, while organisation mappings remain visible to both.
|
||||
func TestMappingsAreVisibleOnlyThroughVisibleEventTypes(t *testing.T) {
|
||||
for _, tt := range []struct {
|
||||
name string
|
||||
caller governance.IdentityID
|
||||
wantPrivateRead bool
|
||||
}{
|
||||
{name: "owner", caller: "alice", wantPrivateRead: true},
|
||||
{name: "other member", caller: "bob"},
|
||||
} {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
m, _, ids := newMappingScopeModule()
|
||||
wantList := []uuid.UUID{ids.orgMapping}
|
||||
if tt.wantPrivateRead {
|
||||
wantList = []uuid.UUID{ids.aliceMapping, ids.orgMapping}
|
||||
}
|
||||
ctx := governance.WithPrincipal(t.Context(), governance.Principal{IdentityID: tt.caller})
|
||||
listed, err := m.mappings(ctx, &struct{}{})
|
||||
if err != nil {
|
||||
t.Fatalf("mappings: %v", err)
|
||||
}
|
||||
got := make([]uuid.UUID, 0, len(listed.Body.Mappings))
|
||||
for _, row := range listed.Body.Mappings {
|
||||
got = append(got, row.ID)
|
||||
}
|
||||
if len(got) != len(wantList) {
|
||||
t.Errorf("mapping ids = %v, want %v", got, wantList)
|
||||
}
|
||||
for i, id := range wantList {
|
||||
if i < len(got) && got[i] != id {
|
||||
t.Errorf("mapping ids = %v, want %v", got, wantList)
|
||||
}
|
||||
}
|
||||
|
||||
read, err := m.mapping(ctx, &idInput{ID: ids.aliceMapping.String()})
|
||||
if tt.wantPrivateRead {
|
||||
if err != nil {
|
||||
t.Fatalf("owner mapping read: %v", err)
|
||||
}
|
||||
if read.Body.ID != ids.aliceMapping {
|
||||
t.Fatalf("mapping id = %s, want %s", read.Body.ID, ids.aliceMapping)
|
||||
}
|
||||
return
|
||||
}
|
||||
var denied *huma.ErrorModel
|
||||
if !errors.As(err, &denied) || denied.Status != 404 {
|
||||
t.Fatalf("other member mapping read error = %v, want 404", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestMappingCannotUseAnotherPersonsEventType(t *testing.T) {
|
||||
m, _, ids := newMappingScopeModule()
|
||||
ctx := governance.WithPrincipal(t.Context(), governance.Principal{IdentityID: "bob"})
|
||||
_, err := m.checkedMapping(ctx, mappingBody{
|
||||
Name: "private fields", EventTypeID: ids.aliceType.String(), Fields: mappingScopeFields(),
|
||||
})
|
||||
var denied *huma.ErrorModel
|
||||
if !errors.As(err, &denied) || denied.Status != 400 {
|
||||
t.Fatalf("mapping for another person's event type error = %v, want 400", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMappingCannotModifyAnotherPersonsMapping(t *testing.T) {
|
||||
for _, operation := range []string{"update", "delete"} {
|
||||
t.Run(operation, func(t *testing.T) {
|
||||
m, repo, ids := newMappingScopeModule()
|
||||
ctx := governance.WithPrincipal(t.Context(), governance.Principal{IdentityID: "bob"})
|
||||
var err error
|
||||
switch operation {
|
||||
case "update":
|
||||
_, err = m.updateMapping(ctx, &updateMappingInput{
|
||||
ID: ids.aliceMapping.String(),
|
||||
Body: mappingBody{
|
||||
Name: "moved fields", EventTypeID: ids.orgType.String(), Fields: mappingScopeFields(),
|
||||
},
|
||||
})
|
||||
case "delete":
|
||||
_, err = m.deleteMapping(ctx, &idInput{ID: ids.aliceMapping.String()})
|
||||
}
|
||||
var denied *huma.ErrorModel
|
||||
if !errors.As(err, &denied) || denied.Status != 404 {
|
||||
t.Fatalf("%s private mapping error = %v, want 404", operation, err)
|
||||
}
|
||||
if len(repo.writes) != 0 {
|
||||
t.Fatalf("unauthorised mapping writes = %v, want none", repo.writes)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
type mappingScopeIDs struct {
|
||||
aliceType, orgType uuid.UUID
|
||||
aliceMapping, orgMapping uuid.UUID
|
||||
}
|
||||
|
||||
func newMappingScopeModule() (*Module, *mappingScopeStore, mappingScopeIDs) {
|
||||
aliceSource, orgSource := uuid.New(), uuid.New()
|
||||
ids := mappingScopeIDs{aliceType: uuid.New(), orgType: uuid.New(), aliceMapping: uuid.New(), orgMapping: uuid.New()}
|
||||
now := time.Now()
|
||||
repo := &mappingScopeStore{
|
||||
sources: map[uuid.UUID]store.Source{
|
||||
aliceSource: {ID: aliceSource, OrgID: "org", Scope: store.ScopePerson, PersonID: sql.NullString{String: "alice", Valid: true}},
|
||||
orgSource: {ID: orgSource, OrgID: "org", Scope: store.ScopeOrg},
|
||||
},
|
||||
eventTypes: map[uuid.UUID]store.EventType{
|
||||
ids.aliceType: {ID: ids.aliceType, OrgID: "org", SourceID: aliceSource, Name: "private", Rule: []byte(`{}`), Schema: []byte(`{"id":"string"}`), CreatedAt: now, UpdatedAt: now},
|
||||
ids.orgType: {ID: ids.orgType, OrgID: "org", SourceID: orgSource, Name: "shared", Rule: []byte(`{}`), Schema: []byte(`{"id":"string"}`), CreatedAt: now, UpdatedAt: now},
|
||||
},
|
||||
mappings: map[uuid.UUID]store.Mapping{
|
||||
// SourceID is only a hint; the event type's source defines visibility.
|
||||
ids.aliceMapping: {ID: ids.aliceMapping, OrgID: "org", Name: "alice fields", SourceID: uuid.NullUUID{UUID: orgSource, Valid: true}, EventTypeID: ids.aliceType, Fields: []byte(`[]`), CreatedAt: now, UpdatedAt: now},
|
||||
ids.orgMapping: {ID: ids.orgMapping, OrgID: "org", Name: "shared fields", SourceID: uuid.NullUUID{UUID: aliceSource, Valid: true}, EventTypeID: ids.orgType, Fields: []byte(`[]`), CreatedAt: now, UpdatedAt: now},
|
||||
},
|
||||
}
|
||||
return &Module{orgID: "org", mappingStore: repo, reader: repo}, repo, ids
|
||||
}
|
||||
|
||||
func mappingScopeFields() []mapping.Field {
|
||||
return []mapping.Field{{Name: "id", Path: "id", Type: mapping.TypeString}}
|
||||
}
|
||||
|
||||
type mappingScopeStore struct {
|
||||
sources map[uuid.UUID]store.Source
|
||||
eventTypes map[uuid.UUID]store.EventType
|
||||
mappings map[uuid.UUID]store.Mapping
|
||||
writes []string
|
||||
}
|
||||
|
||||
func (s *mappingScopeStore) Mappings(_ context.Context, orgID string) ([]store.Mapping, error) {
|
||||
out := make([]store.Mapping, 0, len(s.mappings))
|
||||
for _, row := range s.mappings {
|
||||
if row.OrgID == orgID {
|
||||
out = append(out, row)
|
||||
}
|
||||
}
|
||||
sort.Slice(out, func(i, j int) bool { return out[i].Name < out[j].Name })
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (s *mappingScopeStore) Mapping(_ context.Context, orgID string, id uuid.UUID) (store.Mapping, error) {
|
||||
row, ok := s.mappings[id]
|
||||
if !ok || row.OrgID != orgID {
|
||||
return store.Mapping{}, store.ErrNotFound
|
||||
}
|
||||
return row, nil
|
||||
}
|
||||
|
||||
func (s *mappingScopeStore) EventTypes(_ context.Context, orgID, person string) ([]store.EventType, error) {
|
||||
out := make([]store.EventType, 0, len(s.eventTypes))
|
||||
for _, row := range s.eventTypes {
|
||||
if row.OrgID != orgID {
|
||||
continue
|
||||
}
|
||||
source := s.sources[row.SourceID]
|
||||
if !source.PersonID.Valid || source.PersonID.String == person {
|
||||
out = append(out, row)
|
||||
}
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (s *mappingScopeStore) EventType(_ context.Context, orgID string, id uuid.UUID) (store.EventType, error) {
|
||||
row, ok := s.eventTypes[id]
|
||||
if !ok || row.OrgID != orgID {
|
||||
return store.EventType{}, store.ErrNotFound
|
||||
}
|
||||
return row, nil
|
||||
}
|
||||
|
||||
func (s *mappingScopeStore) InsertMapping(_ context.Context, row store.Mapping) error {
|
||||
s.writes = append(s.writes, "insert")
|
||||
s.mappings[row.ID] = row
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *mappingScopeStore) UpdateMapping(_ context.Context, row store.Mapping) error {
|
||||
if _, ok := s.mappings[row.ID]; !ok {
|
||||
return store.ErrNotFound
|
||||
}
|
||||
s.writes = append(s.writes, "update")
|
||||
s.mappings[row.ID] = row
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *mappingScopeStore) DeleteMapping(_ context.Context, orgID string, id uuid.UUID) error {
|
||||
row, ok := s.mappings[id]
|
||||
if !ok || row.OrgID != orgID {
|
||||
return store.ErrNotFound
|
||||
}
|
||||
s.writes = append(s.writes, "delete")
|
||||
delete(s.mappings, id)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *mappingScopeStore) Source(_ context.Context, orgID string, id uuid.UUID) (store.Source, error) {
|
||||
source, ok := s.sources[id]
|
||||
if !ok || source.OrgID != orgID {
|
||||
return store.Source{}, store.ErrNotFound
|
||||
}
|
||||
return source, nil
|
||||
}
|
||||
|
||||
func (s *mappingScopeStore) SourceByPath(context.Context, string, string) (store.Source, error) {
|
||||
return store.Source{}, store.ErrNotFound
|
||||
}
|
||||
+30
-17
@@ -91,28 +91,41 @@ const listenRetry = 5 * time.Second
|
||||
|
||||
// Module hears events for one organisation.
|
||||
type Module struct {
|
||||
orgID string
|
||||
store *store.Store
|
||||
reader sourceReader
|
||||
inserter eventInserter
|
||||
types typeReader
|
||||
delivery governance.DeliveryAuthorizer
|
||||
proxies func(context.Context) ([]netip.Prefix, error)
|
||||
sealer Sealer
|
||||
parse func(ctx context.Context, connector string, body []byte, headers map[string]string) ([]connectors.Event, error)
|
||||
connectors Connectors
|
||||
runs Runs
|
||||
answers Answers
|
||||
links Links
|
||||
deliveries Deliveries
|
||||
log *slog.Logger
|
||||
poll time.Duration
|
||||
orgID string
|
||||
store *store.Store
|
||||
mappingStore mappingStore
|
||||
reader sourceReader
|
||||
inserter eventInserter
|
||||
types typeReader
|
||||
delivery governance.DeliveryAuthorizer
|
||||
proxies func(context.Context) ([]netip.Prefix, error)
|
||||
sealer Sealer
|
||||
parse func(ctx context.Context, connector string, body []byte, headers map[string]string) ([]connectors.Event, error)
|
||||
connectors Connectors
|
||||
runs Runs
|
||||
answers Answers
|
||||
links Links
|
||||
deliveries Deliveries
|
||||
log *slog.Logger
|
||||
poll time.Duration
|
||||
}
|
||||
|
||||
// mappingStore is the persistence surface the Events API uses for mappings and
|
||||
// their source-owned event types.
|
||||
type mappingStore interface {
|
||||
Mappings(context.Context, string) ([]store.Mapping, error)
|
||||
Mapping(context.Context, string, uuid.UUID) (store.Mapping, error)
|
||||
EventTypes(context.Context, string, string) ([]store.EventType, error)
|
||||
EventType(context.Context, string, uuid.UUID) (store.EventType, error)
|
||||
InsertMapping(context.Context, store.Mapping) error
|
||||
UpdateMapping(context.Context, store.Mapping) error
|
||||
DeleteMapping(context.Context, string, uuid.UUID) error
|
||||
}
|
||||
|
||||
// NewModule returns the events module for orgID.
|
||||
func NewModule(db *sqlx.DB, log *slog.Logger, orgID string, sealer Sealer, c Connectors, r Runs, a Answers, l Links, d Deliveries) *Module {
|
||||
st := store.New(db)
|
||||
m := &Module{orgID: orgID, store: st, reader: st, inserter: st, types: st, sealer: sealer, connectors: c, runs: r, answers: a, links: l, deliveries: d, log: log, poll: time.Second}
|
||||
m := &Module{orgID: orgID, store: st, mappingStore: st, reader: st, inserter: st, types: st, sealer: sealer, connectors: c, runs: r, answers: a, links: l, deliveries: d, log: log, poll: time.Second}
|
||||
m.parse = m.parseWith
|
||||
return m
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user