diff --git a/modules/mesh-grants/grants_test.go b/modules/mesh-grants/grants_test.go index e3114892..a12761c3 100644 --- a/modules/mesh-grants/grants_test.go +++ b/modules/mesh-grants/grants_test.go @@ -5346,3 +5346,59 @@ func TestWithheldIsAnsweredWhileTheListIsAnObject(t *testing.T) { t.Errorf("withheld from the tools = %+v, %v", got, err) } } + +// Design 47 G A-383: a right every account carries (the streams' names, A-382) is written to every account of a list, one kept or held +// from the list in force included: a held module's account of a pass before the catalog half is read gains it and loses nothing. +func TestAKeptAccountGainsTheRightEveryAccountCarries(t *testing.T) { + const names = "$JS.API.STREAM.NAMES" + b := fixture() + 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() + if first.Digest == "" { + t.Fatal("no list in force sealed") + } + // The list in force lacks the right for b.mod-y (written before the rule). + key := grants.SealedUserKey(first.Digest, "b.mod-y") + var u grants.User + if err := json.Unmarshal(b.own[grants.StateUserList][key], &u); err != nil { + t.Fatal(err) + } + u.Publish = slices.DeleteFunc(slices.Clone(u.Publish), func(s string) bool { return s == names }) + b.own[grants.StateUserList][key], _ = json.Marshal(u) + // Its manifest is now held, the catalog half is unread, and the list changes (mod-x runs on b too). + b.refuse("mod-y", yManifest, "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") + 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, "b.mod-y")], &got); err != nil { + t.Fatal(err) + } + if !slices.Contains(got.Publish, names) { + t.Errorf("the kept account b.mod-y lacks %s: %v", names, got.Publish) + } + for _, s := range u.Publish { + if !slices.Contains(got.Publish, s) { + t.Errorf("the kept account lost %s", s) + } + } +} diff --git a/modules/mesh-grants/internal/grants/part.go b/modules/mesh-grants/internal/grants/part.go index 94230b68..4e0717a5 100644 --- a/modules/mesh-grants/internal/grants/part.go +++ b/modules/mesh-grants/internal/grants/part.go @@ -399,6 +399,9 @@ func (p *Part) Compose() Result { res.KeptLoaded = sortedKeys(union) } } + // A right every account carries is written to every account of the list, one kept or held from the list in force included + // (design 47 G A-383); the pass only adds. + users, kept = withEveryAccountsRights(users, kept) users = WithHashes(users, in.Hashes) st.Digest, st.Users = Digest(users), len(users) st.Compare = Compare(users, loaded, Placed(in)).Excusing(ExcusedBy(MultiHeld(listRecs))) diff --git a/modules/mesh-grants/internal/grants/refused.go b/modules/mesh-grants/internal/grants/refused.go index 7ea2c0a9..f06049bf 100644 --- a/modules/mesh-grants/internal/grants/refused.go +++ b/modules/mesh-grants/internal/grants/refused.go @@ -137,3 +137,32 @@ func (p *Part) sayPrincipalsRefused(refused map[string]string, kept map[string]U p.refusedSaid, p.refusedKnown = why, true } } + +// withEveryAccountsRights gives each account a right every account of its kind carries (buscompose.StreamNamesSubject, design 47 G +// A-382) that it lacks, in users and in kept: a user kept from the list in force or held is copied whole, so it would never gain it. An +// account of a ledger, an enrolling node, a person or a provisioner publishes on a few subjects alone and gets none. It only adds. +func withEveryAccountsRights(users []User, kept map[string]User) ([]User, map[string]User) { + add := func(u User) User { + for _, alone := range []string{"ledger.", "enrol.", "person.", "provisioner."} { + if strings.HasPrefix(u.User, alone) { + return u + } + } + if !slices.Contains(u.Publish, buscompose.StreamNamesSubject) { + u.Publish = append(slices.Clone(u.Publish), buscompose.StreamNamesSubject) + } + return u + } + out := make([]User, len(users)) + for i, u := range users { + out[i] = add(u) + } + var withKept map[string]User + if kept != nil { + withKept = make(map[string]User, len(kept)) + for k, u := range kept { + withKept[k] = add(u) + } + } + return out, withKept +}