diff --git a/modules/mesh-grants/internal/buscompose/buscompose_test.go b/modules/mesh-grants/internal/buscompose/buscompose_test.go index 642f56cd..8804b637 100644 --- a/modules/mesh-grants/internal/buscompose/buscompose_test.go +++ b/modules/mesh-grants/internal/buscompose/buscompose_test.go @@ -1641,3 +1641,38 @@ func aRuntimeWithholdsTheMarks(t *testing.T) { } } } + +// A watched seat event whose definition gives a subject with parts (mesh-catalog's `catalog-revision.`) is heard on +// each part as `*`, for a seat of any scope: the consumer's filter and the subscribe grant both, since the bare subject +// matches no event the holder says. +func TestWatchedEventSubjectTakesTheDefinitionsParts(t *testing.T) { + for _, row := range []struct { + name string + seat Seat + event string + want string + }{ + {"mesh seat, bare name", Seat{Name: "mesh-catalog", Scope: "mesh", EventSubjects: map[string]string{"catalog-revision.*": "catalog-revision."}}, + "catalog-revision", "mesh.seat.mesh-catalog.event.catalog-revision.*"}, + {"mesh seat, local name", Seat{Name: "mesh-catalog", Scope: "mesh", EventSubjects: map[string]string{"catalog-revision.*": "catalog-revision."}}, + "catalog-revision.*", "mesh.seat.mesh-catalog.event.catalog-revision.*"}, + {"several parts", Seat{Name: "mesh-pipeline-runner", Scope: "mesh", EventSubjects: map[string]string{"group-ended.*.*": "group-ended.."}}, + "group-ended", "mesh.seat.mesh-pipeline-runner.event.group-ended.*.*"}, + {"event without parts", Seat{Name: "mesh-catalog", Scope: "mesh", EventSubjects: map[string]string{"catalog-revision.*": "catalog-revision."}}, + "registration-changed", "mesh.seat.mesh-catalog.event.registration-changed"}, + {"another event sharing a prefix", Seat{Name: "mesh-catalog", Scope: "mesh", EventSubjects: map[string]string{"catalog-revision.*": "catalog-revision."}}, + "catalog", "mesh.seat.mesh-catalog.event.catalog"}, + } { + p := Principal{Kind: KindModule, Node: "novox", Module: "node-tools"} + row.seat.Emits = []string{row.event} + p.Watches = []Seat{row.seat} + consumer, ok := ConsumerFor(p) + if !ok || !slices.Contains(consumer.Filters, row.want) { + t.Errorf("%s: consumer filters %v (%v), want %s", row.name, consumer.Filters, ok, row.want) + } + perms, err := PermissionsFor(p) + if err != nil || !MaySubscribe(perms, strings.Replace(row.want, "*", "main", 1)) { + t.Errorf("%s: the watcher may not subscribe %s (%v)", row.name, row.want, err) + } + } +} diff --git a/modules/mesh-grants/internal/buscompose/busstate.go b/modules/mesh-grants/internal/buscompose/busstate.go index dcd9528b..bcd14394 100644 --- a/modules/mesh-grants/internal/buscompose/busstate.go +++ b/modules/mesh-grants/internal/buscompose/busstate.go @@ -101,10 +101,7 @@ func keeperConsumer(p Principal) (Consumer, bool) { continue } for _, e := range w.Emits { - if pattern, ok := machineEvent(w, e); ok { - e = anyMachine(pattern) - } - filters = append(filters, seatSubject(w, "event", e)) + filters = append(filters, watchedEventSubject(w, e)) } } // And the events of the seats its module uses that are said to it by name, the operator channel's `decided` for its own diff --git a/modules/mesh-grants/internal/buscompose/nats.go b/modules/mesh-grants/internal/buscompose/nats.go index 5987f3d6..df64deeb 100644 --- a/modules/mesh-grants/internal/buscompose/nats.go +++ b/modules/mesh-grants/internal/buscompose/nats.go @@ -1837,15 +1837,47 @@ func watchedEvents(watches []Seat) []string { continue } for _, e := range w.Emits { - if pattern, ok := machineEvent(w, e); ok { - e = anyMachine(pattern) - } - out = append(out, seatSubject(w, "event", e)) + out = append(out, watchedEventSubject(w, e)) } } return out } +// watchedEventSubject is the subject a watcher of the seat w hears its event e on: an event naming its holder's machine from +// every machine (machineEvent), and any other event whose definition gives a subject with parts (`catalog-revision.` +// of mesh-catalog's seat.json) on that subject with each part as `*`, whatever the seat's scope, since a consume names the +// event by its bare name and the bare subject matches no event the holder says (the same rule as mesh-sdk's +// buscompose EventSubjects). +func watchedEventSubject(w Seat, e string) string { + if pattern, ok := machineEvent(w, e); ok { + return seatSubject(w, "event", anyMachine(pattern)) + } + for local, subject := range w.EventSubjects { + rest, longer := strings.CutPrefix(local, e+".") + if local != e && !(longer && onlyParts(rest)) { + continue + } + toks := strings.Split(subject, ".") + for i, t := range toks { + if len(t) > 2 && t[0] == '<' && t[len(t)-1] == '>' { + toks[i] = "*" + } + } + return seatSubject(w, "event", strings.Join(toks, ".")) + } + return seatSubject(w, "event", e) +} + +// onlyParts says every token of rest is `*`: what a definition's local name adds to the event's bare name. +func onlyParts(rest string) bool { + for _, t := range strings.Split(rest, ".") { + if t != "*" { + return false + } + } + return true +} + // invokedSubjects is the publish side of a grant to call tools: one subject per `.`, // or the whole tool namespace for `*`. A person's authority and a module's `invokes` are both this // (novox/hq ADR 0152), so a malformed entry is refused in one place, before it could be widened into