diff --git a/modules/mesh-grants/grants_test.go b/modules/mesh-grants/grants_test.go index a12761c3..d5b648bb 100644 --- a/modules/mesh-grants/grants_test.go +++ b/modules/mesh-grants/grants_test.go @@ -5402,3 +5402,70 @@ func TestAKeptAccountGainsTheRightEveryAccountCarries(t *testing.T) { } } } + +// Design 47 G A-385: an account kept beyond a pass, a tool runner's included, is the union of the list in force and what the pass composes +// for it (here from the manifest the held runner's running build was judged on), never the list in force alone: before the catalog half is +// read, a held runner carrying a module with a state gains that state's rights (a bound ledger's state is the same kind) and loses nothing. +func TestAHeldRunnerIsWrittenAsTheUnionOfTheListInForceAndWhatIsComposed(t *testing.T) { + const runner = `{"module":"node-tools","version":"1","tool-runner":true,"own-secrets":{"broker":"/var/lib/nt/broker"}}` + const carried = `{"module":"mod-c","version":"1","tools":["t"],"state":["kept"]}` + b := fixture() + b.manifestOf("node-tools", runner) + b.manifestOf("mod-c", carried) + 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{} + 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_kept") } + if !slices.ContainsFunc(was.Publish, isLedger) { + t.Fatalf("the runner of a composes no right on the state of its carried module mod-c: %v", was.Publish) + } + // The list in force holds the runner without the ledger (written before the carried module had one), the runner's manifest is held, + // its running build's manifest is what its have names, and the catalog half is unread. + was.Publish = slices.DeleteFunc(slices.Clone(was.Publish), isLedger) + b.own[grants.StateUserList][key], _ = json.Marshal(was) + digest := meshcatalogclient.ManifestDigest([]byte(runner)) + fp := "src2:" + strings.Repeat("cd", 32) + b.put(grants.ReadHave, "a.node-tools", nodeengine.HaveByNodeModuleValue{V: keys.Version, Phase: nodeengineclient.PhaseRunning, Health: nodeengineclient.HealthOK, Fp: fp}) + b.entry(fp, digest) + b.refuse("node-tools", runner, "held for the test") + b.mu.Lock() + b.catalogWaited = []string{"the catalog half of the branch main"} + b.mu.Unlock() + b.assign("b", "mod-x") // the list changes + p.Compose() + next, _, _ := p.Sealed() + if next.Digest == first.Digest { + t.Fatal("the list did not change: the pass kept the list in force") + } + var got grants.User + if err := json.Unmarshal(b.own[grants.StateUserList][grants.SealedUserKey(next.Digest, "a.node-tools")], &got); err != nil { + t.Fatal(err) + } + if !slices.ContainsFunc(got.Publish, isLedger) { + t.Errorf("the held runner lacks its carried module's state rights: %v", got.Publish) + } + for _, s := range was.Publish { + if !slices.Contains(got.Publish, s) { + t.Errorf("the held runner lost %s", s) + } + } +} diff --git a/modules/mesh-grants/internal/grants/part.go b/modules/mesh-grants/internal/grants/part.go index cc0b997d..713ba3fa 100644 --- a/modules/mesh-grants/internal/grants/part.go +++ b/modules/mesh-grants/internal/grants/part.go @@ -369,11 +369,16 @@ func (p *Part) Compose() Result { candidates = append(candidates, user) } heldUsers := heldModuleUsers(in, candidates) - for user, why := range runnerUsersKept(listRecs, inForce) { + runnersKept := runnerUsersKept(listRecs, inForce) + for user, why := range runnersKept { heldUsers[user] = why } allRefused := mergedRefusals(refused, heldUsers) kept, _ := p.keptOf(allRefused) + // An account kept beyond the pass is the union of the list in force and what the pass composes for it, never the list in force + // 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) for _, user := range sortedKeys(refused) { res.PrincipalsRefused = append(res.PrincipalsRefused, user+": "+refused[user]) } diff --git a/modules/mesh-grants/internal/grants/records.go b/modules/mesh-grants/internal/grants/records.go index 8c55fd16..78b49ba1 100644 --- a/modules/mesh-grants/internal/grants/records.go +++ b/modules/mesh-grants/internal/grants/records.go @@ -83,7 +83,12 @@ func records(in Inputs, union bool) (buscompose.Records, []Gap) { if m, ok := in.Running[node+"."+module]; ok { return m, true } - m, ok := in.Manifests[module] + if m, ok := in.Manifests[module]; ok { + return m, true + } + // The manifest the module's running build was judged on, while its trunk manifest is held: the runner it is, the modules it + // carries and their grants are composed from it, never from the list in force alone (design 47 G A-385). + m, ok := in.Held[module] return m, ok } var here []catalogue.Manifest diff --git a/modules/mesh-grants/internal/grants/refused.go b/modules/mesh-grants/internal/grants/refused.go index 767a8c40..5742165b 100644 --- a/modules/mesh-grants/internal/grants/refused.go +++ b/modules/mesh-grants/internal/grants/refused.go @@ -178,3 +178,36 @@ func withMissing(list, rights []string) []string { } return out } + +// unionComposed is kept with each kept user that the pass also composed (composed, and not one in apart) holding the union of +// the grants of the list in force and of the composed account; the composed copy stays out of users, as a kept one's place is its own +// (withKept). It only adds. +func unionComposed(kept map[string]User, composed []User, apart map[string]string) map[string]User { + if len(kept) == 0 { + return kept + } + out := make(map[string]User, len(kept)) + for name, u := range kept { + out[name] = u + } + for _, c := range composed { + u, ok := out[c.User] + if !ok { + continue + } + if _, no := apart[c.User]; no { + continue + } + u.Publish = set(append(slices.Clone(u.Publish), c.Publish...)) + u.Subscribe = set(append(slices.Clone(u.Subscribe), c.Subscribe...)) + u.AllowResponses = u.AllowResponses || c.AllowResponses + if u.PublishDeny != nil || c.PublishDeny != nil { + u.PublishDeny = set(append(slices.Clone(u.PublishDeny), c.PublishDeny...)) + } + if u.SubscribeDeny != nil || c.SubscribeDeny != nil { + u.SubscribeDeny = set(append(slices.Clone(u.SubscribeDeny), c.SubscribeDeny...)) + } + out[c.User] = u + } + return out +}