package grants import ( "fmt" "slices" "sort" "strings" occ "git.novox.be/novox/mesh-contracts/operator-and-conversation/go/clients/operatorchannelclient" oc "git.novox.be/novox/mesh-contracts/operator-and-conversation/go/seats/operatorchannel" "mesh-grants/internal/buscompose" ) // **One refused principal never freezes the list** (issue 774, sweep 2026-10-30; met twice that day: the engine's acts, // then mesh-host_health). The composer refuses a principal's grants when they would make it a second writer of a state // (buscompose.CheckWriters): that refusal used to end the whole composition, so the list in force stood for every principal // while each change since piled up, and the next list that composed moved 52 users at once. Now each principal is composed // on its own: one refused keeps the grants it holds in the list in force (the part's own record of that list, sealed // grants), the rest of the list is composed and written as ever, and the refusal is raised as the warning // mesh-grants.principal-refused, naming each principal and why, until it composes again. A refused principal the list in // force does not carry stays out of the list: it never had those grants, and is refused them now. // RefusedCondition is the key of the warning raised while a principal's grants are refused. const RefusedCondition = "mesh-grants.principal-refused" // ComposeEach is the shadow list composed principal by principal: every user whose grants compose, and each refused one // by user with why. err is only for principals that cannot be derived at all (two users of one name). Pure. func ComposeEach(r buscompose.Records) ([]User, map[string]string, error) { principals, err := buscompose.Users(r) if err != nil { return nil, nil, err } out := make([]User, 0, len(principals)) refused := map[string]string{} for _, p := range principals { perms, err := buscompose.PermissionsFor(p) if err != nil { refused[p.Username()] = err.Error() continue } out = append(out, User{User: p.Username(), Publish: set(perms.Publish), Subscribe: set(perms.Subscribe), AllowResponses: perms.AllowResponses, PublishDeny: set(perms.PublishDeny), SubscribeDeny: set(perms.SubscribeDeny)}) } sort.Slice(out, func(i, j int) bool { return out[i].User < out[j].User }) return out, refused, nil } // keptOf is, for each refused user, its grants in the list in force as the part sealed them; the users it holds none of // (not in the list in force, or that list's grants not kept) are the second answer, sorted. func (p *Part) keptOf(refused map[string]string) (map[string]User, []string) { if len(refused) == 0 { return nil, nil } var inForce map[string]User if g, _, had := p.Sealed(); had && g.Digest != "" && p.Store != nil { inForce, _ = p.sealedGrantsOf(g.Digest) } kept := map[string]User{} var none []string for _, user := range sortedKeys(refused) { if u, ok := inForce[user]; ok { kept[user] = u continue } none = append(none, user) } return kept, none } // withKept is users with each kept user in its place, sorted. func withKept(users []User, kept map[string]User) []User { out := slices.Clone(users) for _, u := range kept { out = append(out, u) } sort.Slice(out, func(i, j int) bool { return out[i].User < out[j].User }) return out } // keptEntry is a user of the accounts block from grants kept rather than composed, in ComposeAccounts' own lines // (buscompose.AccountsEntry), so the bus reads it as it read the list in force. func keptEntry(u User, hash string) string { return buscompose.AccountsEntry(u.User, hash, buscompose.Permissions{Publish: u.Publish, PublishDeny: u.PublishDeny, Subscribe: u.Subscribe, SubscribeDeny: u.SubscribeDeny, AllowResponses: u.AllowResponses}) } // withEntries puts entries at the end of the accounts block's users, before it closes. func withEntries(list string, entries []string) (string, error) { if len(entries) == 0 { return list, nil } start := strings.Index(list, " users = [\n") if start < 0 { return "", fmt.Errorf("the composed list holds no users block to keep a refused user's grants in") } end := strings.Index(list[start:], "\n ]\n") if end < 0 { return "", fmt.Errorf("the composed list's users block does not close") } at := start + end + 1 return list[:at] + strings.Join(entries, "") + list[at:], nil } // sayPrincipalsRefused raises RefusedCondition while a principal's grants are refused, naming each with why and what // became of it, and clears it once none is; said once per set, again when it changes, and at the first pass after a start. func (p *Part) sayPrincipalsRefused(refused map[string]string, kept map[string]User) { if p.Say == nil { return } var lines []string for _, user := range sortedKeys(refused) { became := "left out of the list: the list in force does not carry it" if _, ok := kept[user]; ok { became = "keeps its grants of the list in force" } lines = append(lines, user+" ("+became+"): "+refused[user]) } why := strings.Join(lines, "; ") p.holdMu.Lock() defer p.holdMu.Unlock() if p.refusedKnown && why == p.refusedSaid { return } c := oc.Condition{Key: RefusedCondition, Subject: oc.ConditionSubject{Scope: "module", ID: "mesh-grants"}} if why == "" { c.State = occ.StateCleared } else { c.Severity, c.State = occ.SeverityWarning, occ.StateRaised c.Summary = fmt.Sprintf("%d principal(s) refused their composed grants: %s", len(refused), why) c.Headline = "A principal's bus grants are refused" needsYou(&c, "a fix of the manifest or the writers table, of what makes it a second writer", "The rest of the user list is written as ever; each refused principal keeps the grants the list in force gives it, "+ "or stays out where it holds none: "+why) c.Resolved = "every principal's grants compose again" } if p.Say(c) == nil { 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; buscompose.AnySeatSubject, principle 6, to a kept one) 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, own map[string][]string) ([]User, map[string]User) { add := func(u User, isKept bool) User { u.Publish = withMissing(u.Publish, own[u.User]) for _, alone := range []string{"ledger.", "enrol.", "person.", "provisioner."} { if strings.HasPrefix(u.User, alone) { return u } } u.Publish = withMissing(u.Publish, []string{buscompose.StreamNamesSubject}) if isKept { // A composed account has it from buscompose.PermissionsFor where its kind asks; a kept one is copied whole. u.Publish = withMissing(u.Publish, []string{buscompose.AnySeatSubject}) } return u } out := make([]User, len(users)) for i, u := range users { out[i] = add(u, false) } var withKept map[string]User if kept != nil { withKept = make(map[string]User, len(kept)) for k, u := range kept { withKept[k] = add(u, true) } } return out, withKept } // withMissing is list with each of rights it lacks, never taking one away. func withMissing(list, rights []string) []string { out := list for _, r := range rights { if !slices.Contains(out, r) { out = append(slices.Clone(out), r) } } 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 } // 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, byBindings map[string][]buscompose.Declared) map[string]User { if len(kept) == 0 || len(runnersKept) == 0 { return kept } out := make(map[string]User, len(kept)) for name, u := range kept { out[name] = u } for name := range runnersKept { u, ok := out[name] node, runner, cut := strings.Cut(name, ".") if !ok || !cut { continue } perms, err := buscompose.KeptRunnerGrants(r, node, runner, byBindings[node]) if err != nil { continue } u.Publish = set(append(slices.Clone(u.Publish), perms.Publish...)) u.Subscribe = set(append(slices.Clone(u.Subscribe), perms.Subscribe...)) u.AllowResponses = u.AllowResponses || perms.AllowResponses out[name] = u } 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 }