diff --git a/modules/mesh-grants/grants_test.go b/modules/mesh-grants/grants_test.go index d21dce0b..1e5f6fad 100644 --- a/modules/mesh-grants/grants_test.go +++ b/modules/mesh-grants/grants_test.go @@ -5084,8 +5084,8 @@ func TestASeatNamedViewIsResolvedToItsHolder(t *testing.T) { want []string asks []string // the holder's first-view tools the view grants beside its events }{ - {"resolved to the holder on record", "mesh-delivery-assigner", []string{"seat:mesh-module-assigner.views.assignments", "mesh-vault.node-logins"}, - []string{"mesh-delivery-assigner.views.assignments", "mesh-vault.node-logins"}, []string{"mesh-delivery-assigner.assignments"}}, + {"resolved to the holder on record", "mesh-delivery-assigner", []string{"seat:mesh-module-assigner.views.assignments", "some-provider.node-logins"}, + []string{"mesh-delivery-assigner.views.assignments", "some-provider.node-logins"}, []string{"mesh-delivery-assigner.assignments"}}, {"a new holder is granted with no change of the manifest", "assigner-two", []string{"seat:mesh-module-assigner.views.seat-holders"}, []string{"assigner-two.views.seat-holders"}, []string{"assigner-two.seat-holders"}}, {"no holder on record stays as written", "", []string{"seat:mesh-module-assigner.views.assignments"}, diff --git a/modules/mesh-grants/internal/buscompose/buscompose_test.go b/modules/mesh-grants/internal/buscompose/buscompose_test.go index 9fe6a8bc..03c92c67 100644 --- a/modules/mesh-grants/internal/buscompose/buscompose_test.go +++ b/modules/mesh-grants/internal/buscompose/buscompose_test.go @@ -1658,6 +1658,8 @@ func TestWatchedEventSubjectTakesTheDefinitionsParts(t *testing.T) { "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.*.*"}, + {"a part between words", Seat{Name: "mesh-pipeline-runner", Scope: "mesh", EventSubjects: map[string]string{"milestone.*.asked": "milestone..asked"}}, + "milestone.asked", "mesh.seat.mesh-pipeline-runner.event.milestone.*.asked"}, {"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."}}, diff --git a/modules/mesh-grants/internal/buscompose/nats.go b/modules/mesh-grants/internal/buscompose/nats.go index c750dc3a..6242dbd1 100644 --- a/modules/mesh-grants/internal/buscompose/nats.go +++ b/modules/mesh-grants/internal/buscompose/nats.go @@ -1887,8 +1887,7 @@ func watchedEventSubject(w Seat, e string) string { return seatSubject(w, "event", anyMachine(pattern)) } for local, subject := range w.EventSubjects { - rest, longer := strings.CutPrefix(local, e+".") - if local != e && !(longer && onlyParts(rest)) { + if local != e && localName(local) != e { continue } toks := strings.Split(subject, ".") @@ -1902,14 +1901,16 @@ func watchedEventSubject(w Seat, e string) string { 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, ".") { +// localName is an event's name from its local form: the tokens that are not a part (`*`), so `milestone.*.asked` is +// `milestone.asked` and `catalog-revision.*` is `catalog-revision`. +func localName(local string) string { + var words []string + for _, t := range strings.Split(local, ".") { if t != "*" { - return false + words = append(words, t) } } - return true + return strings.Join(words, ".") } // invokedSubjects is the publish side of a grant to call tools: one subject per `.`, diff --git a/modules/mesh-grants/internal/grants/consumed_seat_events_test.go b/modules/mesh-grants/internal/grants/consumed_seat_events_test.go new file mode 100644 index 00000000..71d9c8b4 --- /dev/null +++ b/modules/mesh-grants/internal/grants/consumed_seat_events_test.go @@ -0,0 +1,77 @@ +package grants + +import ( + "slices" + "strings" + "testing" + + "git.novox.be/novox/mesh-sdk/go/catalogue" +) + +// A consume names a seat's event by its bare name, and the definition's local name carries the subject's parts as `*` +// (`catalog-revision.*`): it is the seat's event, on the seat's subject; one the seat does not say is refused, never +// turned into a module's event (design 47 G A-409). +func TestConsumedSeatEvents(t *testing.T) { + seats := map[string]catalogue.SeatDeclaration{ + "mesh-catalog": {Name: "mesh-catalog", Scope: "mesh", Emits: []string{"catalog-revision.*", "manifest-accepted", "owed.*.*", "milestone.*.asked"}}, + "mesh-wants": {Name: "mesh-wants", Scope: "mesh", Emits: []string{"moved"}}, + } + modules := map[string]catalogue.Manifest{"mesh-wants": {Module: "mesh-wants"}, "some-app": {Module: "some-app"}} + cases := []struct { + name string + consume string + wantWatch string + wantModule bool + wantRefusals int + }{ + {"bare name of an event with a part", "mesh-catalog.catalog-revision", "catalog-revision", false, 0}, + {"the local name", "mesh-catalog.catalog-revision.*", "catalog-revision.*", false, 0}, + {"an event without parts", "mesh-catalog.manifest-accepted", "manifest-accepted", false, 0}, + {"two parts", "mesh-catalog.owed", "owed", false, 0}, + {"a part between words", "mesh-catalog.milestone.asked", "milestone.asked", false, 0}, + {"a word of the name alone is not the event", "mesh-catalog.milestone", "", false, 1}, + {"an event the seat does not say", "mesh-catalog.no-such-event", "", false, 1}, + {"a word that only shares a prefix", "mesh-catalog.catalog", "", false, 1}, + {"a module of the seat's name says its own", "mesh-wants.something-else", "", true, 0}, + {"a module's event", "some-app.thing", "", true, 0}, + } + for _, c := range cases { + t.Run(c.name, func(t *testing.T) { + m := catalogue.Manifest{Module: "x", Consumes: []string{c.consume}} + fromModules, watches := consumedOf(m, seats, nil, modules) + if got := slices.Contains(fromModules, c.consume); got != c.wantModule { + t.Fatalf("module consume = %v (%v), want %v", got, fromModules, c.wantModule) + } + var watched string + if len(watches) > 0 { + watched = strings.Join(watches[0].Emits, ",") + } + if watched != c.wantWatch { + t.Fatalf("watch = %q, want %q", watched, c.wantWatch) + } + refused := refusedConsumes(m, seats, modules) + if len(refused) != c.wantRefusals { + t.Fatalf("refusals = %v, want %d", refused, c.wantRefusals) + } + if len(refused) > 0 && !strings.Contains(refused[0], c.consume) { + t.Fatalf("refusal %q does not name the consume", refused[0]) + } + }) + } +} + +// The refusal is in the pass's answer as a gap. +func TestRecordsSaysARefusedConsume(t *testing.T) { + m := catalogue.Manifest{Module: "reader", Consumes: []string{"mesh-catalog.no-such-event"}} + in := Inputs{Nodes: []string{"n1"}, Assigned: map[string][]string{"n1": {"reader"}}, Manifests: map[string]catalogue.Manifest{"reader": m}} + _, gaps := Records(in) + found := false + for _, g := range gaps { + if strings.Contains(g.Why, "no event no-such-event") { + found = true + } + } + if !found { + t.Fatalf("gaps %v do not say the refused consume", gaps) + } +} diff --git a/modules/mesh-grants/internal/grants/records.go b/modules/mesh-grants/internal/grants/records.go index 731ed4d7..99c424e8 100644 --- a/modules/mesh-grants/internal/grants/records.go +++ b/modules/mesh-grants/internal/grants/records.go @@ -110,7 +110,12 @@ func records(in Inputs, union bool) (buscompose.Records, []Gap) { m.Invokes = slices.Concat(m.Invokes, seatViewTools(m.Consumes, readHolders)) m.Consumes = resolveSeatViews(m.Consumes, readHolders) m.Reads = resolveSeatReads(m.Reads, readHolders) - d := declaredFor(m, seats, declarers) + for _, why := range refusedConsumes(m, seats, declared) { + if g := (Gap{Input: m.Module + " on " + node, Why: why}); !slices.Contains(gaps, g) { + gaps = append(gaps, g) + } + } + d := declaredFor(m, seats, declarers, declared) if len(in.Placed) > 0 { d.Holds = heldWherePlaced(d.Holds, holders, node, m.Module) } @@ -159,7 +164,7 @@ func records(in Inputs, union bool) (buscompose.Records, []Gap) { // What it consumes and invokes are its node account's: a seat's event also on the seat's own subject, as a module's // consumes are split (consumedOf), every consume still on its emitter's as before, and each verb it invokes in the // node's own name (issue 1966: the seat list's verb and events). - _, watches := consumedOf(m, seats, declarers) + _, watches := consumedOf(m, seats, declarers, declared) out.Engine = &buscompose.Declared{Module: m.Module, Consumes: m.Consumes, Watches: watches, Invokes: m.Invokes, State: bucketsFor(m, in.Nodes), Logs: logsOf(m), Streams: streamsOf(m)} withEventSubjects(in.EventSubjects, out.Engine) @@ -171,8 +176,8 @@ func records(in Inputs, union bool) (buscompose.Records, []Gap) { // declaredFor is mesh-controller's declaredFor (internal/inventory/busrecords.go), copied: one module's manifest as the // composer needs it. -func declaredFor(m catalogue.Manifest, seats map[string]catalogue.SeatDeclaration, declarers map[string]string) buscompose.Declared { - fromModules, watches := consumedOf(m, seats, declarers) +func declaredFor(m catalogue.Manifest, seats map[string]catalogue.SeatDeclaration, declarers map[string]string, modules map[string]catalogue.Manifest) buscompose.Declared { + fromModules, watches := consumedOf(m, seats, declarers, modules) d := buscompose.Declared{ Module: m.Module, ToolRunner: catalogue.IsToolRunner(m), Emits: m.EmitsAll(), Consumes: fromModules, Watches: watches, Serves: m.Tools, Invokes: withRunnerReads(m.Invokes), State: bucketsOf(m), Reads: m.Reads, Logs: logsOf(m), @@ -305,8 +310,9 @@ func resolveSeatReads(reads []string, holders []catalogue.Held) []string { // consumedOf splits what a manifest consumes as the composer grants it: a seat's event on the seat's own subject (its // watches), every other by its emitter's (novox/hq ADR 0118). A placed module and the node-engine's own manifest are split -// alike. -func consumedOf(m catalogue.Manifest, seats map[string]catalogue.SeatDeclaration, declarers map[string]string) ([]string, []buscompose.Seat) { +// alike. A consume naming a seat that says no such event (and no module of the seat's name, which says its own, ADR 0239) +// is neither: it is refused (refusedConsumes), never turned into a module's event (design 47 G A-409). +func consumedOf(m catalogue.Manifest, seats map[string]catalogue.SeatDeclaration, declarers map[string]string, modules map[string]catalogue.Manifest) ([]string, []buscompose.Seat) { var fromModules []string var watches []buscompose.Seat for _, c := range m.Consumes { @@ -314,18 +320,23 @@ func consumedOf(m catalogue.Manifest, seats map[string]catalogue.SeatDeclaration if named { if s, isASeat := seats[emitter]; isASeat && s.Kinded { ev := strings.TrimSuffix(event, ".*") - if catalogue.SeatSays(s.Emits, ev) { + if seatSaysEvent(s.Emits, ev) { watches = append(watches, buscompose.Seat{Name: s.Name, Scope: s.Scope, Emits: []string{ev}, Kinded: true, Proofs: s.Proofs, DeclaredBy: declarers[s.Name]}) continue } } - if s, isASeat := seats[emitter]; isASeat && catalogue.SeatSays(s.Emits, event) { - // Its scope, and whether the node-engine holds it, place an event that names its holder's machine (ADR 0618 §4). - compiled, _ := catalogue.SeatNamed(s.Name) - watches = append(watches, buscompose.Seat{Name: s.Name, Scope: s.Scope, Emits: []string{event}, - HeldByTheEngine: compiled.HeldByTheEngine}) - continue + if s, isASeat := seats[emitter]; isASeat { + if seatSaysEvent(s.Emits, event) { + // Its scope, and whether the node-engine holds it, place an event that names its holder's machine (ADR 0618 §4). + compiled, _ := catalogue.SeatNamed(s.Name) + watches = append(watches, buscompose.Seat{Name: s.Name, Scope: s.Scope, Emits: []string{event}, + HeldByTheEngine: compiled.HeldByTheEngine}) + continue + } + if _, isModule := modules[emitter]; !isModule { + continue + } } } fromModules = append(fromModules, c) @@ -333,6 +344,57 @@ func consumedOf(m catalogue.Manifest, seats map[string]catalogue.SeatDeclaration return fromModules, watches } +// seatSaysEvent is whether a seat's events (as a definition names them locally: `catalog-revision.*` for the subject +// `catalog-revision.`) say the event a consume names: the event of that name whatever parts its subject has, or +// one a declared pattern matches. +func seatSaysEvent(emits []string, event string) bool { + if catalogue.SeatSays(emits, event) { + return true + } + for _, e := range emits { + if nameOf(e) == event { + return true + } + } + return false +} + +// nameOf is an event's name from its local form: the tokens that are not a part (`*`), so `milestone.*.asked` is +// `milestone.asked` and `catalog-revision.*` is `catalog-revision`. +func nameOf(local string) string { + var words []string + for _, t := range strings.Split(local, ".") { + if t != "*" { + words = append(words, t) + } + } + return strings.Join(words, ".") +} + +// refusedConsumes are the consumes of a manifest naming a seat that says no such event, with why; a module of the seat's +// name says its own events (ADR 0239) and is not refused. The grants part says each aloud as a gap of its answer. +func refusedConsumes(m catalogue.Manifest, seats map[string]catalogue.SeatDeclaration, modules map[string]catalogue.Manifest) []string { + var out []string + for _, c := range m.Consumes { + emitter, event, named := strings.Cut(c, ".") + if !named { + continue + } + s, isASeat := seats[emitter] + if !isASeat { + continue + } + if _, isModule := modules[emitter]; isModule { + continue + } + if seatSaysEvent(s.Emits, strings.TrimSuffix(event, ".*")) { + continue + } + out = append(out, "consumes "+c+": the seat "+emitter+" says no event "+event+", so no filter is granted for it") + } + return out +} + // loadsABundle says the node's tool runtime loads a bundle of the module. The controller reads it from the resolved // manifest's Bundles, which a build derives and the indexer's manifest does not carry (the builder's build record names // the repository's manifest digest), so the same answer is derived here from `build.artifacts` by mesh-sdk's rule diff --git a/modules/mesh-grants/internal/grants/runner_reads_test.go b/modules/mesh-grants/internal/grants/runner_reads_test.go index cf3ff005..4ce25001 100644 --- a/modules/mesh-grants/internal/grants/runner_reads_test.go +++ b/modules/mesh-grants/internal/grants/runner_reads_test.go @@ -14,7 +14,7 @@ func TestEveryModuleInvokesTheRunnersReads(t *testing.T) { invokes []string want int }{{"none declared", nil, len(RunnerReads)}, {"one declared", []string{"seat:mesh-catalog.revision"}, len(RunnerReads)}, {"every verb", []string{"*"}, 1}} { - d := declaredFor(catalogue.Manifest{Module: "mesh-build-go", Invokes: c.invokes}, nil, nil) + d := declaredFor(catalogue.Manifest{Module: "mesh-build-go", Invokes: c.invokes}, nil, nil, nil) if len(d.Invokes) != c.want { t.Errorf("%s: invokes %v, want %d", c.name, d.Invokes, c.want) } diff --git a/modules/mesh-grants/internal/grants/sentobjects_test.go b/modules/mesh-grants/internal/grants/sentobjects_test.go index 0db59117..bfb9f365 100644 --- a/modules/mesh-grants/internal/grants/sentobjects_test.go +++ b/modules/mesh-grants/internal/grants/sentobjects_test.go @@ -56,7 +56,7 @@ func TestTheMarkedVerbsADeclarationNames(t *testing.T) { t.Errorf("marked = %v", got) } m := catalogue.Manifest{Module: "shop", Invokes: []string{"seat:node-login-shell.execute"}} - if got := declaredFor(m, seats, nil).MarkedVerbs; !slices.Equal(got, []string{"seat:node-login-shell.execute"}) { + if got := declaredFor(m, seats, nil, nil).MarkedVerbs; !slices.Equal(got, []string{"seat:node-login-shell.execute"}) { t.Errorf("the declaration carries %v, want the marked verb it invokes", got) } }