mesh-grants: a grant waits only for what the loaded list does not allow #223
@@ -68,8 +68,11 @@ func GrantWaits(accounts []string, composed, loaded map[string]User, unknown str
|
||||
adds = append(adds, user+" (not in the loaded list)")
|
||||
continue
|
||||
}
|
||||
n := len(minus(c.Publish, l.Publish)) + len(minus(c.Subscribe, l.Subscribe))
|
||||
if c.AllowResponses && !l.AllowResponses {
|
||||
// By what a permission allows, not by its text: a subject a loaded one covers is carried (`>` covers every one), and
|
||||
// answering requests is carried where the loaded user may publish every subject (2026-11-16: every grant waited on
|
||||
// "1 permissions" that the loaded `>` already gave).
|
||||
n := len(notAllowed(c.Publish, l.Publish)) + len(notAllowed(c.Subscribe, l.Subscribe))
|
||||
if c.AllowResponses && !l.AllowResponses && len(notAllowed([]string{">"}, l.Publish)) > 0 {
|
||||
n++
|
||||
}
|
||||
if l.PublishDeny != nil {
|
||||
@@ -86,6 +89,40 @@ func GrantWaits(accounts []string, composed, loaded map[string]User, unknown str
|
||||
return "waits for the bus to load a list carrying what it adds: " + strings.Join(adds, ", ")
|
||||
}
|
||||
|
||||
// notAllowed is each subject of a that no subject of b covers.
|
||||
func notAllowed(a, b []string) []string {
|
||||
var out []string
|
||||
for _, x := range a {
|
||||
if !slices.ContainsFunc(b, func(y string) bool { return subjectCovers(y, x) }) {
|
||||
out = append(out, x)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// subjectCovers says whether the permission pattern p allows every subject the permission s allows, by the bus's wildcards:
|
||||
// `*` one token, `>` one or more tokens at the end.
|
||||
func subjectCovers(p, s string) bool {
|
||||
pt, st := strings.Split(p, "."), strings.Split(s, ".")
|
||||
for i, t := range pt {
|
||||
if t == ">" {
|
||||
return len(st) > i
|
||||
}
|
||||
if i >= len(st) {
|
||||
return false
|
||||
}
|
||||
switch {
|
||||
case t == "*":
|
||||
if st[i] == ">" {
|
||||
return false
|
||||
}
|
||||
case t != st[i]:
|
||||
return false
|
||||
}
|
||||
}
|
||||
return len(pt) == len(st)
|
||||
}
|
||||
|
||||
// **The permissions of each list the part sealed, one key per user** (issue 774, sweep 2026-10-30 item 5): the last two
|
||||
// lists, by digest, each user's permissions as sealed, never a hash: what tells which grants the list the bus loaded
|
||||
// carries, without the bus module's tools. One value holding every user of two lists outgrew the bucket's largest value
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
package grants
|
||||
|
||||
import "testing"
|
||||
|
||||
// A grant is carried by what the loaded list allows, not by its text (2026-11-16: every grant waited on a permission the
|
||||
// loaded `>` already gave).
|
||||
func TestAGrantWaitsOnlyForWhatTheLoadedListDoesNotAllow(t *testing.T) {
|
||||
all := User{User: "n.m", Publish: []string{">"}, Subscribe: []string{">"}}
|
||||
for _, c := range []struct {
|
||||
name string
|
||||
composed, loaded User
|
||||
waits bool
|
||||
}{
|
||||
{"everything covers a subject and answering", User{User: "n.m", Publish: []string{"mesh.a.b", ">"}, Subscribe: []string{"mesh.*.c"}, AllowResponses: true}, all, false},
|
||||
{"a narrower loaded subject does not cover a wider one", User{User: "n.m", Publish: []string{"mesh.>"}}, User{User: "n.m", Publish: []string{"mesh.a.>"}}, true},
|
||||
{"a star covers one token", User{User: "n.m", Subscribe: []string{"mesh.a.c"}}, User{User: "n.m", Subscribe: []string{"mesh.*.c"}}, false},
|
||||
{"answering is not carried by a narrow publish", User{User: "n.m", Publish: []string{"mesh.a"}, AllowResponses: true}, User{User: "n.m", Publish: []string{"mesh.a"}}, true},
|
||||
} {
|
||||
got := GrantWaits([]string{"n.m"}, map[string]User{"n.m": c.composed}, map[string]User{"n.m": c.loaded}, "")
|
||||
if (got != "") != c.waits {
|
||||
t.Errorf("%s: waits %q, want waiting %v", c.name, got, c.waits)
|
||||
}
|
||||
}
|
||||
for _, c := range []struct {
|
||||
p, s string
|
||||
want bool
|
||||
}{{">", "a", true}, {">", "a.>", true}, {"a.>", "a", false}, {"a.*", "a.>", false}, {"a.*", "a.*", true}, {"a.b", "a.b", true}, {"a.b", "a.b.c", false}} {
|
||||
if got := subjectCovers(c.p, c.s); got != c.want {
|
||||
t.Errorf("subjectCovers(%q, %q) = %v, want %v", c.p, c.s, got, c.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user