grants: a seat's holder hears its shared worker created and deleted

The builder binds the seat's worker when the bus says it was created and again after a deletion, but no holder was
granted those advisories, so under scoped grants it would never hear them. Each holder of a seat with an accept not
pulled by kind is now granted the subscribe of CREATED and DELETED for that seat's worker. The worker the grants part
creates is SeatQueue's as returned, every shared setting copied, and a test holds it equal to SeatQueue's.

Builds: design 47 H2 H-A-468 (1), addendum, second addendum
This commit is contained in:
2026-11-22 11:37:03 +01:00
parent a85c9d4032
commit 3f25e659ba
6 changed files with 55 additions and 5 deletions
@@ -16,7 +16,7 @@ func theGrantsPartCreatesOnlyDeclaredStreams(t *testing.T) {
// alone, whatever another module's reads say; the controller's copy grants any module that declares it.
{Module: "mesh-vault", State: []Bucket{{Module: "mesh-vault", Name: "bus-accounts"}}},
{Module: "spy", Serves: []string{"spy_x"}, Reads: []string{VaultBusAccounts}},
{Module: "mesh-delivery-builder",
{Module: "mesh-delivery-builder", Holds: []Seat{{Name: "mesh-builds", Scope: "mesh", Accepts: []string{"step"}}},
State: []Bucket{{Module: "mesh-delivery-builder", Name: "steps", History: 5}},
Logs: []Log{{Module: "mesh-delivery-builder", Name: "builds"}},
Streams: []ModuleStream{{Module: "mesh-delivery-builder", Name: "BUILDASKS", Events: []string{"ask"}}}},
@@ -148,6 +148,11 @@ func theGrantsPartCreatesOnlyDeclaredStreams(t *testing.T) {
{"mesh-delivery-builder", "$JS.EVENT.ADVISORY.STREAM.CREATED.KV_mesh-grants_assertions", false},
{"mesh-delivery-builder", "$JS.EVENT.ADVISORY.STREAM.CREATED.LOG_mesh-delivery-builder_builds", false},
{"mesh-delivery-builder", "$JS.EVENT.ADVISORY.STREAM.DELETED.KV_mesh-delivery-builder_steps", false},
// H-A-468 addendum: the holder of a seat with an accept hears its shared worker created and deleted, no other's.
{"mesh-delivery-builder", "$JS.EVENT.ADVISORY.CONSUMER.CREATED.SEAT_MESH_BUILDS.SEAT_MESH_BUILDS_worker", true},
{"mesh-delivery-builder", "$JS.EVENT.ADVISORY.CONSUMER.DELETED.SEAT_MESH_BUILDS.SEAT_MESH_BUILDS_worker", true},
{"mesh-delivery-builder", "$JS.EVENT.ADVISORY.CONSUMER.CREATED.SEAT_MESH_WANTS.SEAT_MESH_WANTS_worker", false},
{"spy", "$JS.EVENT.ADVISORY.CONSUMER.CREATED.SEAT_MESH_BUILDS.SEAT_MESH_BUILDS_worker", false},
} {
if got := MaySubscribe(perms[row.who], row.subject); got != row.may {
t.Errorf("%s may hear %s: %v, want %v", row.who, row.subject, got, row.may)
@@ -156,8 +156,11 @@ func AskStreamsOf(defs map[string]seatdef.Definition) (out []jetstream.StreamCon
MaxAge: time.Duration(st.MaxAge) * time.Second, MaxMsgs: int64(st.MaxMsgs),
Duplicates: time.Duration(st.DuplicatesSeconds) * time.Second})
if !d.PulledByKind {
workers = append(workers, Consumer{Name: w.Name, Stream: w.Stream, Filters: slices.Clone(w.Filters),
AckWaitSeconds: w.AckWaitSeconds, MaxDeliver: w.MaxDeliver, MaxAckPending: w.MaxAckPending, Why: w.Why})
// SeatQueue's worker as it returns it, every setting the two types share (its in-progress is the holder's beat,
// no setting of the bus's): the grants part makes no derivation of its own (H-A-468 second addendum).
workers = append(workers, Consumer{Name: w.Name, Stream: w.Stream, Filters: w.Filters, Queue: w.Queue, Push: w.Push,
TakesOverFrom: w.TakesOverFrom, DeliverSubject: w.DeliverSubject, AckWaitSeconds: w.AckWaitSeconds,
MaxDeliver: w.MaxDeliver, MaxAckPending: w.MaxAckPending, FromNow: w.FromNow, Why: w.Why, Resettable: w.Resettable})
}
}
slices.SortFunc(out, func(a, b jetstream.StreamConfig) int { return strings.Compare(a.Name, b.Name) })
@@ -1162,6 +1162,11 @@ func permissionsFor(p Principal) (Permissions, error) {
if s.Name == meshbroker.Seat {
s.Serves = withoutActing(s.Serves)
}
// A seat with an accept not pulled by kind has its one shared worker, made by the grants part (H-A-468): its holder
// hears the bus say it was created and deleted, so it binds it when it appears and again after a deletion.
if len(s.Accepts) > 0 && !s.PulledByKind && safeSubject.MatchString(s.Name) {
sub = append(sub, seatWorkerAdvisories(s.Name)...)
}
if s.isNewTraffic() {
// Composed by SeatTrafficOf below, worker and all; only its tools are served here.
for _, t := range s.Serves {
@@ -1799,6 +1804,13 @@ func CreateConsumerGrants(consumers []ModuleConsumer) []string {
return out
}
// seatWorkerAdvisories are the bus's advisories that a seat's one shared worker was created and deleted (H-A-468
// addendum), heard by each holder of the seat.
func seatWorkerAdvisories(seat string) []string {
stream, worker := seatStreamName(seat), WorkerName(seat, "")
return []string{"$JS.EVENT.ADVISORY.CONSUMER.CREATED." + stream + "." + worker, "$JS.EVENT.ADVISORY.CONSUMER.DELETED." + stream + "." + worker}
}
// GrantsModule is the one module whose principal publishes the modules' memberships (novox/hq ADR 0547 §5): the grants
// part, which composes them from the manifests and assignments. It defines no stream and is granted no other subject here.
const GrantsModule = "mesh-grants"
@@ -48,7 +48,7 @@ accounts {
} }
{ user: "one.telegram", password: "$2a$11$tttttttttttttttttttttt", permissions: {
publish: { allow: ["$JS.ACK.EVENTS.one_telegram.>", "$JS.ACK.EVENTS.one_telegram_tools.>", "$JS.ACK.SEAT_TELEGRAM_SENDER.SEAT_TELEGRAM_SENDER_worker.>", "$JS.API.CONSUMER.CREATE.KV_mesh-grants_memberships.*.$KV.mesh-grants_memberships.one.telegram", "$JS.API.CONSUMER.DELETE.KV_mesh-grants_memberships.>", "$JS.API.CONSUMER.INFO.EVENTS.one_telegram", "$JS.API.CONSUMER.INFO.EVENTS.one_telegram_tools", "$JS.API.CONSUMER.INFO.SEAT_TELEGRAM_SENDER.SEAT_TELEGRAM_SENDER_worker", "$JS.API.CONSUMER.MSG.NEXT.EVENTS.one_telegram", "$JS.API.CONSUMER.MSG.NEXT.EVENTS.one_telegram_tools", "$JS.API.CONSUMER.MSG.NEXT.SEAT_TELEGRAM_SENDER.SEAT_TELEGRAM_SENDER_worker", "$JS.API.DIRECT.GET.KV_mesh-grants_memberships.$KV.mesh-grants_memberships.one.telegram", "$JS.API.STREAM.INFO.KV_mesh-grants_memberships", "$JS.API.STREAM.NAMES", "$JS.FC.KV_mesh-grants_memberships.>", "mesh.seat.*.accept.>", "mesh.seat.*.call.*.*.one~telegram", "mesh.seat.*.call.*.one~telegram", "mesh.seat.*.tool.>", "mesh.seat.telegram-sender.event.delivered", "mesh.seat.telegram-sender.event.failed"], deny: ["mesh.seat.node-launcher.tool.secret", "mesh.seat.node-launcher.tool.secret.*", "mesh.seat.node-launcher.call.secret.>", "mesh.seat.mesh-broker.call.apply.>", "mesh.seat.mesh-broker.call.create.>", "mesh.seat.mesh-broker.call.drop.>", "mesh.seat.mesh-broker.call.recreate.>", "mesh.seat.mesh-broker.tool.apply", "mesh.seat.mesh-broker.tool.create", "mesh.seat.mesh-broker.tool.drop", "mesh.seat.mesh-broker.tool.recreate"] }
subscribe: { allow: ["$SRV.INFO", "$SRV.INFO.telegram", "$SRV.INFO.telegram.>", "$SRV.PING", "$SRV.PING.telegram", "$SRV.PING.telegram.>", "$SRV.STATS", "$SRV.STATS.telegram", "$SRV.STATS.telegram.>", "_INBOX.one.telegram.>", "mesh.mod.telegram.call.>", "mesh.mod.telegram.tool.>", "mesh.seat.telegram-sender.accept.send"] }
subscribe: { allow: ["$JS.EVENT.ADVISORY.CONSUMER.CREATED.SEAT_TELEGRAM_SENDER.SEAT_TELEGRAM_SENDER_worker", "$JS.EVENT.ADVISORY.CONSUMER.DELETED.SEAT_TELEGRAM_SENDER.SEAT_TELEGRAM_SENDER_worker", "$SRV.INFO", "$SRV.INFO.telegram", "$SRV.INFO.telegram.>", "$SRV.PING", "$SRV.PING.telegram", "$SRV.PING.telegram.>", "$SRV.STATS", "$SRV.STATS.telegram", "$SRV.STATS.telegram.>", "_INBOX.one.telegram.>", "mesh.mod.telegram.call.>", "mesh.mod.telegram.tool.>", "mesh.seat.telegram-sender.accept.send"] }
allow_responses: { max: 1, ttl: "1m" }
} }
{ user: "two.audit", password: "$2a$11$aaaaaaaaaaaaaaaaaaaaaa", permissions: {
File diff suppressed because one or more lines are too long
@@ -1,6 +1,9 @@
package grants
import (
"os"
"path/filepath"
"reflect"
"slices"
"strings"
"testing"
@@ -8,6 +11,7 @@ import (
"github.com/nats-io/nats.go/jetstream"
sdkbc "git.novox.be/novox/mesh-sdk/go/buscompose"
"git.novox.be/novox/mesh-sdk/go/seatdef"
"mesh-grants/internal/buscompose"
@@ -66,3 +70,29 @@ func TestEachSeatThatAcceptsWorkHasItsAskStream(t *testing.T) {
t.Errorf("made %v (not %v), created %v: want only the builds seat's, the wants seat's being held", made, notMade, c.made)
}
}
// The worker the grants part creates for the seat mesh-builds is SeatQueue's, setting by setting (H-A-468 second addendum):
// the one the builder's whole test makes from SeatQueue, so the two cannot differ.
func TestTheSeatsWorkerIsSeatQueues(t *testing.T) {
raw, err := os.ReadFile(filepath.Join("..", "..", "..", "mesh-delivery-contracts", "seats", "mesh-builds", "seat.json"))
if err != nil {
t.Fatal(err)
}
def, err := seatdef.Parse(raw)
if err != nil {
t.Fatal(err)
}
_, w, ok, err := sdkbc.SeatQueue(def.Seat, def.Accepts)
if err != nil || !ok {
t.Fatalf("SeatQueue: %v %v", ok, err)
}
_, workers, refused := buscompose.AskStreamsOf(map[string]seatdef.Definition{def.Seat: def})
if len(workers) != 1 || len(refused) != 0 {
t.Fatalf("workers %+v, refused %v", workers, refused)
}
want := jetstream.ConsumerConfig{Durable: w.Name, FilterSubjects: w.Filters, AckPolicy: jetstream.AckExplicitPolicy,
AckWait: time.Duration(w.AckWaitSeconds) * time.Second, MaxDeliver: w.MaxDeliver, MaxAckPending: w.MaxAckPending, Description: w.Why}
if got := buscompose.ConsumerConfig(workers[0]); workers[0].Stream != w.Stream || !reflect.DeepEqual(got, want) {
t.Errorf("the created worker on %s = %+v, want SeatQueue's %+v on %s", workers[0].Stream, got, want, w.Stream)
}
}