diff --git a/modules/mesh-grants/grants_test.go b/modules/mesh-grants/grants_test.go index c5404958..1cdbcba6 100644 --- a/modules/mesh-grants/grants_test.go +++ b/modules/mesh-grants/grants_test.go @@ -5597,3 +5597,72 @@ func TestARunnerKeptAsItRunsGainsWhatItsCarriedModulesBindingsGive(t *testing.T) } } } + +// A-386 live: the module a runner carries has no manifest the pass can read (the gate's), only the bindings kept for it: the kept runner still +// gains the ledger's rights from them. +func TestAKeptRunnerCarriesAModuleKnownOnlyByItsBindings(t *testing.T) { + const runner = `{"module":"node-tools","version":"1","tool-runner":true,"own-secrets":{"broker":"/var/lib/nt/broker"}}` + b := fixture() + b.manifestOf("node-tools", runner) + b.assign("a", "node-tools") + b.assign("a", "mod-c") + busKey, err := seal.GenerateKeyPair() + if err != nil { + t.Fatal(err) + } + b.put(grants.ReadHave, "a.nats", nodeengine.HaveByNodeModuleValue{V: keys.Version, Phase: nodeengineclient.PhaseRunning, Health: nodeengineclient.HealthOK, + UserList: &nodeengine.UserListWritten{Source: grantsstate.GrantsUserListSource, Revision: 8, Wanted: "sha256:aa", Digest: "sha256:aa", Users: 5, + Key: busKey.PublicKey, Follows: grantsstate.FollowGrants}}) + for _, u := range shadowOf(t, b) { + b.put(grants.ReadAccounts, u.User, map[string]any{"user": u.User, "hash": "$2a$10$x", "generation": 1, "fingerprint": "sha256:1"}) + } + p := partOn(b) + p.Bus = grants.BusAddress{URL: "nats://a.mesh:4222", Fingerprint: "ab"} + p.Objects = &fakeObjects{} + binding := membership.BusBinding{Local: "records", Storage: "ledger", Template: "ledger", Stream: "mod-c_records", History: "mod-c_records", + Subject: "mesh.ledger.mod-c.records.history", State: "mod-c_records_state", Snapshot: "mesh.ledger.mod-c.records.snapshot"} + p.Compose() // the pass that learns the bus module is the broker + body, _ := json.Marshal(grants.BrokerEvent{V: 1, Module: "mod-c", Local: "records", Storage: "ledger", Template: "ledger", Binding: binding}) + if _, err := p.HearBroker("mesh.seat.mesh-broker.event.created", body, p.Brokers()); err != nil { + t.Fatal(err) + } + p.Compose() + first, _, _ := p.Sealed() + key := grants.SealedUserKey(first.Digest, "a.node-tools") + var was grants.User + if err := json.Unmarshal(b.own[grants.StateUserList][key], &was); err != nil { + t.Fatal(err) + } + isLedger := func(s string) bool { return strings.Contains(s, "mod-c_records") } + was.Publish = append(slices.DeleteFunc(slices.Clone(was.Publish), isLedger), "mesh.control.a.tools-alive") + b.own[grants.StateUserList][key], _ = json.Marshal(was) + b.manifestOf("node-tools", strings.Replace(runner, `"tool-runner":true,`, "", 1)) + b.assign("b", "mod-x") + p.Compose() + sealed, _, _ := p.Sealed() + if sealed.Digest == first.Digest { + t.Fatal("the list did not change") + } + var got grants.User + if err := json.Unmarshal(b.own[grants.StateUserList][grants.SealedUserKey(sealed.Digest, "a.node-tools")], &got); err != nil { + t.Fatal(err) + } + for _, want := range []string{ + "$JS.API.STREAM.INFO.KV_mod-c_records_state", "$JS.API.DIRECT.GET.KV_mod-c_records_state.>", "$KV.mod-c_records_state.>", + "$JS.API.CONSUMER.CREATE.KV_mod-c_records_state.>", "$JS.API.DIRECT.GET.mod-c_records.>", "$JS.API.CONSUMER.CREATE.mod-c_records.>", + "$JS.API.DIRECT.GET.mod-c_records_snapshot.>"} { + if !slices.Contains(got.Publish, want) { + t.Errorf("the runner of a lacks %s", want) + } + } + for _, s := range was.Publish { + if !slices.Contains(got.Publish, s) { + t.Errorf("the kept runner lost %s", s) + } + } + for _, no := range []string{"mesh.ledger.mod-c.records.history.>", "mesh.ledger.mod-c.records.snapshot.>"} { + if slices.Contains(got.Publish, no) { + t.Errorf("the runner may publish on the ledger's %s", no) + } + } +} diff --git a/modules/mesh-grants/internal/buscompose/users.go b/modules/mesh-grants/internal/buscompose/users.go index 8be05795..3cca1178 100644 --- a/modules/mesh-grants/internal/buscompose/users.go +++ b/modules/mesh-grants/internal/buscompose/users.go @@ -726,11 +726,12 @@ func newEventStreams(kept []KeptEvent) []string { // KeptRunnerGrants are the grants a tool runner that a pass keeps as it runs (no placed manifest says it is one, ADR 0601 §4) is composed // as a carrying runner for: what the modules placed on its node that a runner carries, with their bindings and events, give it (design 47 -// G A-386), never as a plain module. runner is the module the kept user is named for. -func KeptRunnerGrants(r Records, node, runner string) (Permissions, error) { +// G A-386), never as a plain module. runner is the module the kept user is named for; byBindings are the modules placed on the node +// whose manifest the pass cannot read, declared by the bindings kept for them alone (they carry nothing else). +func KeptRunnerGrants(r Records, node, runner string, byBindings []Declared) (Permissions, error) { logs := DeclaredLogs(r) var carried []Declared - for _, d := range r.Assigned[node] { + for _, d := range append(slices.Clone(r.Assigned[node]), byBindings...) { if CarriedByRunner(d) { d = withReadLogs(withAnnouncedReads(d, r.Announced), logs) d.Hears = ComposedHears(d) diff --git a/modules/mesh-grants/internal/grants/part.go b/modules/mesh-grants/internal/grants/part.go index 752573a4..c1a6d7fe 100644 --- a/modules/mesh-grants/internal/grants/part.go +++ b/modules/mesh-grants/internal/grants/part.go @@ -379,7 +379,7 @@ func (p *Part) Compose() Result { // alone (design 47 G A-385); a runner kept for what it runs, whose manifest does not say it is one, is never composed as a plain module // (ADR 0601 §4). kept = unionComposed(kept, users, runnersKept) - kept = unionKeptRunners(kept, listRecs, runnersKept) + kept = unionKeptRunners(kept, listRecs, runnersKept, declaredByBindings(in, listRecs)) for _, user := range sortedKeys(refused) { res.PrincipalsRefused = append(res.PrincipalsRefused, user+": "+refused[user]) } diff --git a/modules/mesh-grants/internal/grants/refused.go b/modules/mesh-grants/internal/grants/refused.go index af4c4b4a..0d2f2a9c 100644 --- a/modules/mesh-grants/internal/grants/refused.go +++ b/modules/mesh-grants/internal/grants/refused.go @@ -215,7 +215,7 @@ func unionComposed(kept map[string]User, composed []User, apart map[string]strin // unionKeptRunners is kept with each tool runner kept as it runs (runnersKept) holding the union of the list in force and the grants of a // carrying runner on its node, composed from the modules the pass places there with their bindings and events (design 47 G A-386); a // runner that cannot be composed so stays as the list in force holds it. It only adds. -func unionKeptRunners(kept map[string]User, r buscompose.Records, runnersKept map[string]string) map[string]User { +func unionKeptRunners(kept map[string]User, r buscompose.Records, runnersKept map[string]string, byBindings map[string][]buscompose.Declared) map[string]User { if len(kept) == 0 || len(runnersKept) == 0 { return kept } @@ -229,7 +229,7 @@ func unionKeptRunners(kept map[string]User, r buscompose.Records, runnersKept ma if !ok || !cut { continue } - perms, err := buscompose.KeptRunnerGrants(r, node, runner) + perms, err := buscompose.KeptRunnerGrants(r, node, runner, byBindings[node]) if err != nil { continue } @@ -240,3 +240,18 @@ func unionKeptRunners(kept map[string]User, r buscompose.Records, runnersKept ma } return out } + +// declaredByBindings are, by node, the modules placed there that the records leave out for want of a manifest and that have bindings kept: each +// declared by its bindings alone, with no account of its own, so a tool runner carries it and reads what it is bound to (design 47 G A-386). +func declaredByBindings(in Inputs, r buscompose.Records) map[string][]buscompose.Declared { + out := map[string][]buscompose.Declared{} + for node, modules := range in.Assigned { + for _, module := range modules { + if len(in.Bindings[module]) == 0 || slices.ContainsFunc(r.Assigned[node], func(d buscompose.Declared) bool { return d.Module == module }) { + continue + } + out[node] = append(out[node], buscompose.Declared{Module: module, NoAccount: true, Bindings: in.Bindings[module]}) + } + } + return out +}