Grants: a held subject the composed one covers is replaced by it (H-A-470 (4))

The live registration history is held with `….registration.*`; the bus
refuses adding `….registration.>` beside it (JetStream 10052, overlapping
subjects), so df5a8895's subject never reached the live stream. A held
subject a composed one covers is now replaced by it, the messages kept and
no other subject dropped, and the assertion says the replacement.

Builds: design 47 H H-A-470 (third addendum)
This commit is contained in:
2026-11-22 13:04:06 +01:00
parent d79b9f1518
commit abaddd3ef8
7 changed files with 96 additions and 23 deletions
@@ -70,7 +70,8 @@ func (c creating) CreateDeclared(cfg jetstream.StreamConfig) error {
}
// AddSubjects adds subjects to a held stream and changes nothing else: it reads the stream's configuration, sets its
// subjects to the union and updates with the rest as it was.
// subjects to grants.WidenSubjects of what it holds and add (a held subject an added one covers replaced by it, since the
// bus refuses an overlap: design 47 H H-A-470, third addendum) and updates with the rest as it was.
func (c creating) AddSubjects(stream string, add []string) error {
ctx, cancel := context.WithTimeout(c.ctx, requestWithin)
defer cancel()
@@ -79,11 +80,7 @@ func (c creating) AddSubjects(stream string, add []string) error {
return err
}
cfg := s.CachedInfo().Config
for _, a := range add {
if !slices.Contains(cfg.Subjects, a) {
cfg.Subjects = append(cfg.Subjects, a)
}
}
cfg.Subjects, _ = grants.WidenSubjects(cfg.Subjects, add)
_, err = c.js.UpdateStream(ctx, cfg)
return err
}
@@ -614,6 +614,27 @@ func TestTheGrantsPartWhole(t *testing.T) {
if again, _ := stream.Info(ctx); !slices.Equal(again.Config.Subjects, []string{"a.>", "b.>"}) {
t.Errorf("subjects after adding a held one: %v", again.Config.Subjects)
}
// A held subject the added one covers is replaced by it, the messages kept: the bus refuses `registration.>` beside
// `registration.*` (JetStream 10052, overlapping subjects), the live registration history's case (design 47 H
// H-A-470, third addendum).
reg, err := js.CreateStream(ctx, jetstream.StreamConfig{Name: "REGHIST", Subjects: []string{"m.registration.*"}})
if err != nil {
t.Fatal(err)
}
for _, subj := range []string{"m.registration.one", "m.registration.two"} {
if _, err := js.Publish(ctx, subj, []byte("x")); err != nil {
t.Fatal(err)
}
}
if err := c.AddSubjects("REGHIST", []string{"m.registration.>"}); err != nil {
t.Fatal(err)
}
if _, err := js.Publish(ctx, "m.registration.source.novox.hq", []byte("x")); err != nil {
t.Errorf("a source-only registration's write: %v", err)
}
if now, _ := reg.Info(ctx); !slices.Equal(now.Config.Subjects, []string{"m.registration.>"}) || now.State.Msgs != 3 {
t.Errorf("the registration history after the widening: subjects %v, %d messages, want m.registration.> and 3", now.Config.Subjects, now.State.Msgs)
}
// A kept stream's source filter is brought to its definition's in place: the stream is not made again, its settings
// stay, and an equal filter or a source of another stream changes nothing (design 47 H H-A-145, §746).
if _, err := js.CreateStream(ctx, jetstream.StreamConfig{Name: "KEPTSRC", Subjects: []string{"ev.>"}}); err != nil {
+2 -7
View File
@@ -181,7 +181,7 @@ func (b *fakeBus) CreateConsumer(stream string, cfg jetstream.ConsumerConfig) er
return nil
}
// AddSubjects is the bus's addition of subjects to a held stream: the union, every other setting kept.
// AddSubjects is the bus's addition of subjects to a held stream: a covered subject replaced, every other setting kept.
func (b *fakeBus) AddSubjects(stream string, add []string) error {
b.mu.Lock()
defer b.mu.Unlock()
@@ -192,12 +192,7 @@ func (b *fakeBus) AddSubjects(stream string, add []string) error {
for _, s := range b.streams {
if s["name"] == stream {
have, _ := s["subjects"].([]string)
for _, a := range add {
if !slices.Contains(have, a) {
have = append(have, a)
}
}
s["subjects"] = have
s["subjects"], _ = grants.WidenSubjects(have, add)
return nil
}
}
@@ -62,8 +62,9 @@ const CreatesMissing = "a bucket, log or stream the manifest declares and the bu
// 08:54). A stream the bus holds already answers ErrHeld.
type Creator interface {
CreateDeclared(cfg jetstream.StreamConfig) error
// AddSubjects adds subjects to a stream the bus holds and nothing else: the stream's subjects become the union of what
// it holds and add; no subject is removed, no limit, retention or other setting changed, the stream not recreated.
// AddSubjects adds subjects to a stream the bus holds and nothing else: the stream's subjects become WidenSubjects of
// what it holds and add, a held subject an added one covers replaced by it (design 47 H H-A-470, third addendum) and
// no other removed; no limit, retention or other setting changed, the stream not recreated.
AddSubjects(stream string, add []string) error
// RaiseMaxBytes raises the size cap of a stream the bus holds to `to` when `to` is larger than the cap it holds, and
// changes nothing else (the operator, 2026-11-09 09:07: "raise only"). It answers the cap it held and whether it
@@ -208,7 +209,8 @@ func createMissing(c Creator, done *creations, a *Assertion, m catalogue.Manifes
}
}
// A stream the bus holds whose manifest names events it has no subject for gains those subjects, and only ever those: a
// subject the manifest dropped stays, limits and retention are left as they are (the operator, 2026-11-05).
// subject the manifest dropped stays, limits and retention are left as they are (the operator, 2026-11-05). A held
// subject a gained one covers is replaced by it, nothing dropped (design 47 H H-A-470, third addendum).
for i := range a.Items {
it := &a.Items[i]
if it.Held == nil || it.Kind != "stream" {
@@ -234,7 +236,14 @@ func createMissing(c Creator, done *creations, a *Assertion, m catalogue.Manifes
}
done.cleared(it.Stream)
it.Created = "subjects added: " + strings.Join(add, ", ")
made = append(made, "stream "+it.Stream+" gained "+strings.Join(add, ", "))
gained := "stream " + it.Stream + " gained " + strings.Join(add, ", ")
// A held subject an added one covers is replaced by it, and said (design 47 H H-A-470, third addendum).
if _, replaced := WidenSubjects(it.Held.Subjects, add); len(replaced) > 0 {
why := " (replacing " + strings.Join(replaced, ", ") + ", which it covers: every message kept)"
it.Created += why
gained += why
}
made = append(made, gained)
}
// A held stream, of any kind, whose manifest declares a larger size cap gets that cap, and no other limit changes: a
// cap the bus holds that is larger or equal stays (the operator, 2026-11-09 09:07). Compared once per manifest.
+25 -4
View File
@@ -93,16 +93,37 @@ func GrantWaits(accounts []string, composed, loaded map[string]User, unknown str
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) }) {
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 {
// WidenSubjects is a held stream's subjects once add is given them: each subject of add the stream lacks is added, and a
// held subject an added one covers is replaced by it, since the bus refuses a subject overlapping one the stream holds
// (JetStream 10052) and the held subject's every message still matches the added one (design 47 H H-A-470, third
// addendum). No other held subject is removed. It answers the held subjects it replaced.
func WidenSubjects(held, add []string) (subjects, replaced []string) {
for _, h := range held {
if slices.ContainsFunc(add, func(a string) bool { return a != h && SubjectCovers(a, h) }) {
replaced = append(replaced, h)
continue
}
subjects = append(subjects, h)
}
for _, a := range add {
if !slices.Contains(subjects, a) {
subjects = append(subjects, a)
}
}
return subjects, replaced
}
// 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. It also
// says whether a stream subject p takes every message a held subject s takes (design 47 H H-A-470, third addendum).
func SubjectCovers(p, s string) bool {
pt, st := strings.Split(p, "."), strings.Split(s, ".")
for i, t := range pt {
if t == ">" {
@@ -25,8 +25,8 @@ func TestAGrantWaitsOnlyForWhatTheLoadedListDoesNotAllow(t *testing.T) {
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)
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)
}
}
}
@@ -1,6 +1,8 @@
package grants
import (
"slices"
"strings"
"testing"
"git.novox.be/novox/mesh-sdk/go/catalogue"
@@ -38,3 +40,31 @@ func TestAStatesDeclaredCapReachesItsBucket(t *testing.T) {
t.Errorf("the held bucket was raised to %d (%v), want 256 MiB", r.to, raises)
}
}
// adder is a Creator that records the subjects it is asked to add to a held stream.
type adder struct {
Creator
add []string
}
func (a *adder) AddSubjects(_ string, add []string) error { a.add = add; return nil }
// A held subject the composed one covers is replaced by it, and the assertion says so (design 47 H H-A-470, third
// addendum: the bus refuses `registration.>` beside `registration.*`, JetStream 10052); no other subject is dropped.
func TestAHeldSubjectTheComposedOneCoversIsReplaced(t *testing.T) {
held, replaced := WidenSubjects([]string{"m.registration.*", "kept.subject"}, []string{"m.registration.>"})
if !slices.Equal(held, []string{"kept.subject", "m.registration.>"}) || !slices.Equal(replaced, []string{"m.registration.*"}) {
t.Errorf("widened to %v replacing %v", held, replaced)
}
if held, replaced := WidenSubjects([]string{"a.>"}, []string{"a.>", "b.*"}); !slices.Equal(held, []string{"a.>", "b.*"}) || replaced != nil {
t.Errorf("an equal or uncovered subject: %v replacing %v", held, replaced)
}
a := Assertion{Manifest: "d", Items: []Item{{Kind: "stream", Stream: "LOG_m_registration-history",
Composed: Limits{Subjects: []string{"m.registration.>"}}, Held: &Limits{Subjects: []string{"m.registration.*"}}}}}
c := &adder{}
made, _ := createMissing(c, &creations{}, &a, catalogue.Manifest{Module: "m"}, nil, false)
const said = "replacing m.registration.*, which it covers"
if !slices.Equal(c.add, []string{"m.registration.>"}) || len(made) != 1 || !strings.Contains(made[0], said) || !strings.Contains(a.Items[0].Created, said) {
t.Errorf("added %v, said %v and %q: want the replacement said", c.add, made, a.Items[0].Created)
}
}