Files
mesh-delivery/modules/mesh-grants/cmd/mesh-grants/composed_integration_test.go
T
jschoubben 611fafb58e AM-D4: the real bus delivers no marked call to a runtime denied the marks
A deny composed into the user list must hold on the server itself: the
runtime keeps its allow of the module's tools, and neither the tool's subject
nor the caller-named subject process.Call sends on first reaches it.

Builds: design 46 7 §6 (integration, the grants part's real-bus test)

Claude-Session: https://claude.ai/code/session_01NcbgfGKaxw984XyAPfLm4h
2026-11-12 06:20:21 +01:00

681 lines
27 KiB
Go

//go:build integration
package main
import (
"bytes"
"context"
"encoding/base64"
"encoding/json"
"errors"
"os"
"path/filepath"
"slices"
"strings"
"testing"
"time"
"git.novox.be/novox/mesh-delivery/modules/mesh-grants/grantsstate"
sdkbc "git.novox.be/novox/mesh-sdk/go/buscompose"
"git.novox.be/novox/mesh-sdk/go/catalogue"
"git.novox.be/novox/mesh-sdk/go/process"
"git.novox.be/novox/mesh-sdk/go/seal"
"github.com/nats-io/nats-server/v2/server"
"github.com/nats-io/nats.go"
"github.com/nats-io/nats.go/jetstream"
"mesh-grants/internal/buscompose"
grantsPart "mesh-grants/internal/grants"
)
// theBusNodeFollowsTheSealedListUnderTheComposedGrants, run by TestTheGrantsPartWhole, defends novox/hq ADR 0547 clause 7 on a real server (issue 1945): with the list this composer writes, the grants part
// creates its object bucket and its state, puts the sealed list and names it in `user-list.sealed`; the node-engine of
// the node holding mesh-broker watches that one key, reads the object by its name and opens it with its key, and may
// neither list the bucket, nor read another key of the state, nor write; another node reads nothing of either.
func theBusNodeFollowsTheSealedListUnderTheComposedGrants(t *testing.T) {
records := buscompose.Records{
Nodes: []string{"novox", "ace"},
Assigned: map[string][]buscompose.Declared{
"novox": {{Module: buscompose.GrantsModule, State: []buscompose.Bucket{{Module: buscompose.GrantsModule, Name: "user-list"}}}},
"ace": {{Module: "plex", Reads: []string{buscompose.ComposedRead}},
{Module: "mesh-delivery-view", Reads: []string{buscompose.ComposedRead}, ReadsObjects: []string{"mesh-knowledge_sections"}},
{Module: "mesh-knowledge", Objects: []buscompose.ObjectBucket{{Module: "mesh-knowledge", Name: "sections", MaxMiB: 1, Restricted: true}}}},
},
}
// The bus module and the router as the part composes them from their own manifests (testdata: the catalogue's
// nats and messenger at the registry milestone's join): the bus module's keeper process with its own account.
fromManifests, gaps := grantsPart.Records(grantsPart.Inputs{Nodes: []string{"novox"},
Assigned: map[string][]string{"novox": {"messenger", "nats"}},
Manifests: map[string]catalogue.Manifest{"nats": manifestIn(t, "nats"), "messenger": manifestIn(t, "messenger")}})
if len(gaps) != 0 {
t.Fatalf("composing nats and messenger from their manifests: %+v", gaps)
}
records.Assigned["novox"] = append(records.Assigned["novox"], fromManifests.Assigned["novox"]...)
records.BusState = fromManifests.BusState
// The node-engine's state as the part's records carry it: every node's compositions (ADR 0593 §6).
records.Engine = &buscompose.Declared{Module: buscompose.EngineModule, State: buscompose.EngineComposedBuckets(records.Nodes)}
users, err := buscompose.Users(records)
if err != nil {
t.Fatal(err)
}
for i := range users {
users[i].PasswordHash = "pw-" + users[i].Username()
}
accounts, err := buscompose.ComposeAccounts(users)
if err != nil {
t.Fatal(err)
}
dir := t.TempDir()
conf := filepath.Join(dir, "server.conf")
if err := os.WriteFile(conf, []byte("listen: 127.0.0.1:-1\njetstream { store_dir: \""+dir+"\" }\n"+accounts), 0o600); err != nil {
t.Fatal(err)
}
opts, err := server.ProcessConfigFile(conf)
if err != nil {
t.Fatalf("the composed list is not the server's: %v", err)
}
opts.NoLog, opts.NoSigs = true, true
s, err := server.NewServer(opts)
if err != nil {
t.Fatal(err)
}
go s.Start()
if !s.ReadyForConnections(30 * time.Second) {
t.Fatal("the bus did not come up")
}
t.Cleanup(s.Shutdown)
dial := func(user string) *nats.Conn {
nc, err := nats.Connect(s.ClientURL(), nats.UserInfo(user, "pw-"+user), nats.CustomInboxPrefix("_INBOX."+user))
if err != nil {
t.Fatalf("%s: %v", user, err)
}
t.Cleanup(nc.Close)
return nc
}
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
// The grants part: its state and its object bucket, the sealed list put and named.
grants, err := jetstream.New(dial("novox." + buscompose.GrantsModule))
if err != nil {
t.Fatal(err)
}
// Its state is made as the grants part makes a declared state, by its stream's name (CreateKeyValue would ask the
// account's info, which no module is granted).
if _, err := grants.CreateStream(ctx, jetstream.StreamConfig{Name: "KV_" + buscompose.GrantsUserListBucket,
Subjects: []string{"$KV." + buscompose.GrantsUserListBucket + ".>"}, MaxMsgsPerSubject: 20, AllowDirect: true,
AllowRollup: true, DenyDelete: true, Discard: jetstream.DiscardNew}); err != nil {
t.Fatalf("the grants part's state: %v", err)
}
kv, err := grants.KeyValue(ctx, buscompose.GrantsUserListBucket)
if err != nil {
t.Fatalf("the grants part binds its state: %v", err)
}
objects, err := grants.CreateObjectStore(ctx, jetstream.ObjectStoreConfig{Bucket: buscompose.GrantsUserListObjects})
if err != nil {
t.Fatalf("the grants part's object bucket: %v", err)
}
node, err := seal.GenerateKeyPair()
if err != nil {
t.Fatal(err)
}
list := []byte(accounts)
digest := grantsstate.UserListDigest(list)
object, err := grantsstate.SealUserList(list, node.PublicKey)
if err != nil {
t.Fatal(err)
}
name := grantsstate.UserListObjectName(digest)
for range 2 { // put twice: a second put of a name purges its old chunks
if _, err := objects.PutBytes(ctx, name, object); err != nil {
t.Fatalf("the grants part put its object: %v", err)
}
}
value, err := grantsstate.EncodeGrantsUserList(grantsstate.GrantsUserList{Follow: grantsstate.FollowGrants, Digest: digest,
Object: name, SealedTo: grantsstate.SealingKeyID(node.PublicKey), Node: "novox", Revision: 1, Users: len(users)})
if err != nil {
t.Fatal(err)
}
if _, err := kv.Put(ctx, buscompose.GrantsUserListKey, value); err != nil {
t.Fatal(err)
}
if _, err := kv.Put(ctx, "list", []byte(`{"v":1}`)); err != nil {
t.Fatal(err)
}
// Issue 1966: the grants part purges its sealed lists' keys under its composed grants: listed with their last operations,
// each purged with its value, its state's bytes read before and after.
purging := ownPurging{ctx: ctx, js: grants, module: buscompose.GrantsModule}
live, marked := grantsPart.SealedUserKey("sha256:d9", "a.mod-x"), grantsPart.SealedUserKey("sha256:d8", "a.mod-x")
if _, err := kv.Put(ctx, live, bytes.Repeat([]byte("x"), 4096)); err != nil {
t.Fatal(err)
}
if _, err := kv.Put(ctx, marked, []byte(`{}`)); err != nil {
t.Fatal(err)
}
if err := kv.Delete(ctx, marked); err != nil {
t.Fatal(err)
}
ops, err := purging.LastOps(grantsPart.StateUserList, grantsPart.KeySealedGrants+".>")
if err != nil || len(ops) != 2 || ops[live] || !ops[marked] {
t.Fatalf("the grants part lists its sealed keys with their last operations: %v, %v", ops, err)
}
before, err := purging.Bytes(grantsPart.StateUserList)
if err != nil {
t.Fatalf("the grants part reads its state's bytes: %v", err)
}
for _, k := range []string{live, marked} {
if err := purging.Purge(grantsPart.StateUserList, k); err != nil {
t.Fatalf("the grants part purges its own %s: %v", k, err)
}
}
if after, err := purging.Bytes(grantsPart.StateUserList); err != nil || after+2048 > before { // the value goes; a purge marker of the key stays
t.Errorf("a purge kept the value: %d bytes before, %d after (%v)", before, after, err)
}
if _, err := kv.Get(ctx, live); !errors.Is(err, jetstream.ErrKeyNotFound) {
t.Errorf("the purged %s is still read: %v", live, err)
}
// The bus node's engine: the key watched, the object by name (as mesh-host's link reads a declaration's file).
busNode := dial("node.novox")
js, err := busNode.JetStream(nats.MaxWait(5 * time.Second))
if err != nil {
t.Fatal(err)
}
keys, err := js.KeyValue(buscompose.GrantsUserListBucket)
if err != nil {
t.Fatalf("the bus node binds the grants part's state: %v", err)
}
w, err := keys.Watch(buscompose.GrantsUserListKey)
if err != nil {
t.Fatalf("the bus node watches the key: %v", err)
}
defer w.Stop()
var got grantsstate.GrantsUserList
select {
case e := <-w.Updates():
if e == nil {
t.Fatal("the watch found no value")
}
if got, err = grantsstate.DecodeGrantsUserList(e.Value()); err != nil {
t.Fatal(err)
}
case <-ctx.Done():
t.Fatal("the watch said nothing")
}
read := func(js nats.JetStreamContext, name string) ([]byte, error) {
meta, err := js.GetLastMsg(buscompose.GrantsUserListObjectStream, "$O."+buscompose.GrantsUserListObjects+".M."+
base64.URLEncoding.EncodeToString([]byte(name)), nats.DirectGet())
if err != nil {
return nil, err
}
var info struct {
NUID string `json:"nuid"`
Size int `json:"size"`
}
if err := json.Unmarshal(meta.Data, &info); err != nil {
return nil, err
}
sub, err := js.SubscribeSync("$O."+buscompose.GrantsUserListObjects+".C."+info.NUID, nats.OrderedConsumer(),
nats.BindStream(buscompose.GrantsUserListObjectStream))
if err != nil {
return nil, err
}
defer sub.Unsubscribe()
var body []byte
for len(body) < info.Size {
m, err := sub.NextMsg(5 * time.Second)
if err != nil {
return nil, err
}
body = append(body, m.Data...)
}
return body, nil
}
body, err := read(js, got.Object)
if err != nil {
t.Fatalf("the bus node reads the object by its name: %v", err)
}
opened, err := grantsstate.OpenUserList(body, got.Digest, func(b seal.Box) ([]byte, error) {
s, err := seal.Open(b, node.PrivateKey)
return []byte(s), err
})
if err != nil || !bytes.Equal(opened, list) {
t.Fatalf("the bus node opened %d bytes, %v", len(opened), err)
}
// And nothing beyond: no other key, no list of the bucket, no write.
if _, err := keys.Get("list"); err == nil {
t.Error("the bus node read another key of the grants part's state")
}
if _, err := js.StreamInfo(buscompose.GrantsUserListObjectStream); err == nil {
t.Error("the bus node read the object stream's info, which lists every name")
}
if _, err := js.Publish("$O."+buscompose.GrantsUserListObjects+".M.x", []byte("x")); err == nil {
t.Error("the bus node wrote the grants part's bucket")
}
// Another node reads nothing of it.
other, err := dial("node.ace").JetStream(nats.MaxWait(2 * time.Second))
if err != nil {
t.Fatal(err)
}
if _, err := read(other, got.Object); err == nil {
t.Error("another node read the object")
}
if _, err := other.GetLastMsg("KV_"+buscompose.GrantsUserListBucket, "$KV."+buscompose.GrantsUserListBucket+"."+buscompose.GrantsUserListKey, nats.DirectGet()); err == nil {
t.Errorf("another node read the key: %v", err)
}
// Review round 1 (running F4; design 25 § 12.7): the bus module's keeper connects on its own process account as the
// part composed it from nats' manifest, reads the operator's warrant in the router's record of its ask, and is
// refused a write outside its grants. The router's state is made as the part makes a declared state, and written by
// the router's own account, keyed `<asker>.<ask id>`.
{
asksStream := jetstream.StreamConfig{Name: "KV_messenger_asks", Subjects: []string{"$KV.messenger_asks.>"}, MaxMsgsPerSubject: 5,
AllowDirect: true, AllowRollup: true, DenyDelete: true, Discard: jetstream.DiscardNew}
if _, err := grants.CreateStream(ctx, asksStream); err != nil {
t.Fatalf("the grants part creates the router's asks: %v", err)
}
router, err := jetstream.New(dial("novox.messenger"))
if err != nil {
t.Fatal(err)
}
asks, err := router.KeyValue(ctx, "messenger_asks")
if err != nil {
t.Fatalf("the router binds its asks: %v", err)
}
if _, err := asks.Put(ctx, "nats.bus-1", []byte(`{"asker":"nats","ask":{"id":"bus-1"},"state":"answered","warrant":{"ask":"bus-1","asker":"nats","outcome":"chosen"}}`)); err != nil {
t.Fatalf("the router writes its record: %v", err)
}
keeper, err := process.Connect(process.Account{URL: s.ClientURL(), Node: "novox", Module: "nats", User: "novox.nats.keeper",
Password: "pw-novox.nats.keeper"})
if err != nil {
t.Fatalf("the keeper connects on its composed account: %v", err)
}
t.Cleanup(keeper.Close)
e, err := keeper.Ask("messenger_asks", "bus-1")
if err != nil || e == nil || !bytes.Contains(e.Value, []byte(`"outcome":"chosen"`)) {
t.Fatalf("the keeper reads its ask's warrant under its own account: %+v, %v", e, err)
}
if e, err := keeper.Ask("messenger_asks", "other"); err != nil || e != nil {
t.Errorf("the keeper reads %+v (%v) for an ask it has no record of", e, err)
}
raw := dial("novox.nats.keeper")
for _, subject := range []string{"$KV.messenger_asks.nats.bus-1", "$KV." + buscompose.GrantsUserListBucket + ".list"} {
reply, err := raw.Request(subject, []byte(`{}`), 500*time.Millisecond)
if err == nil && reply.Header.Get("Status") == "" {
t.Errorf("the keeper wrote %s, outside its grants: %s", subject, reply.Data)
}
}
if _, err := raw.Request("$JS.API.DIRECT.GET.KV_messenger_asks.$KV.messenger_asks.mesh-config.x", nil, 500*time.Millisecond); err == nil {
t.Error("the keeper read another asker's record")
}
}
// A context of its own: the refusals above each wait out their bound.
ctx, cancel = context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
// ADR 0593 §6 (D14): the grants part creates each node's compositions; ace's engine writes its own and not novox's;
// the view, a named reader, finds every node's bucket by listing and reads it, writing nothing; plex reads none.
for _, node := range records.Nodes {
cfg := buscompose.BucketStreamConfig(buscompose.EngineComposedBuckets([]string{node})[0])
if _, err := grants.CreateStream(ctx, cfg); err != nil {
t.Fatalf("the grants part creates %s: %v", cfg.Name, err)
}
}
aceEngine, _ := dial("node.ace").JetStream(nats.MaxWait(2 * time.Second))
if _, err := aceEngine.Publish("$KV.composed-ace.ace.node", []byte(`{}`)); err != nil {
t.Errorf("ace's engine cannot write its own compositions: %v", err)
}
if _, err := aceEngine.Publish("$KV.composed-novox.novox.node", []byte(`{}`)); err == nil {
t.Error("ace's engine wrote novox's compositions")
}
view, err := jetstream.New(dial("ace.mesh-delivery-view"))
if err != nil {
t.Fatal(err)
}
var found []string
for name := range view.KeyValueStoreNames(ctx).Name() {
found = append(found, name)
}
if !slices.Contains(found, "composed-ace") || !slices.Contains(found, "composed-novox") {
t.Errorf("the view finds the buckets %v", found)
}
composed, err := view.KeyValue(ctx, "composed-ace")
if err != nil {
t.Fatalf("the view binds ace's compositions: %v", err)
}
if _, err := composed.Get(ctx, "ace.node"); err != nil {
t.Errorf("the view reads ace's own composition: %v", err)
}
refusedIn := func() context.Context { // a refused publish is answered by no one: it waits out a short bound
c, stop := context.WithTimeout(ctx, 2*time.Second)
t.Cleanup(stop)
return c
}
if _, err := composed.Put(refusedIn(), "ace.node", []byte(`{}`)); err == nil {
t.Error("the view wrote a composition")
}
plex, _ := dial("ace.plex").JetStream(nats.MaxWait(2 * time.Second))
if _, err := plex.GetLastMsg("KV_composed-ace", "$KV.composed-ace.ace.node", nats.DirectGet()); err == nil {
t.Error("plex, not a named reader, read a composition")
}
// ADR 0592 §5, §9: a module's own object bucket, created by the grants part, written (and an object replaced) by its
// owner alone, read by the module its grant names, read-only, and by no other.
sections := buscompose.ObjectBucket{Module: "mesh-knowledge", Name: "sections", MaxMiB: 1, Restricted: true}
if _, err := grants.CreateStream(ctx, buscompose.ObjectsStreamConfig(sections)); err != nil {
t.Fatalf("the grants part creates %s: %v", sections.Stream(), err)
}
owner, err := jetstream.New(dial("ace.mesh-knowledge"))
if err != nil {
t.Fatal(err)
}
ownStore, err := owner.ObjectStore(ctx, sections.Bucket())
if err != nil {
t.Fatalf("the owner binds its object bucket: %v", err)
}
for range 2 { // put twice: a second put of a name purges its old chunks
if _, err := ownStore.PutBytes(ctx, "s1", []byte("body")); err != nil {
t.Fatalf("the owner puts an object: %v", err)
}
}
readStore, err := view.ObjectStore(ctx, sections.Bucket())
if err != nil {
t.Fatalf("the granted reader binds the bucket: %v", err)
}
if got, err := readStore.GetBytes(ctx, "s1"); err != nil || string(got) != "body" {
t.Errorf("the granted reader reads %q, %v", got, err)
}
if _, err := readStore.PutBytes(refusedIn(), "s2", []byte("x")); err == nil {
t.Error("the granted reader wrote the bucket")
}
if _, err := plex.GetLastMsg(sections.Stream(), "$O."+sections.Bucket()+".M.x", nats.DirectGet()); err == nil {
t.Error("a module the grant does not name read the bucket")
}
}
// manifestIn is a module's manifest as testdata keeps it.
func manifestIn(t *testing.T, module string) catalogue.Manifest {
t.Helper()
raw, err := os.ReadFile(filepath.Join("testdata", module+".module.json"))
if err != nil {
t.Fatal(err)
}
m, err := catalogue.Decode(raw)
if err != nil {
t.Fatalf("%s's manifest: %v", module, err)
}
return m
}
// composedBus is a server loading the accounts composed from records, its conf file rewritten and reloaded by recompose.
type composedBus struct {
t *testing.T
s *server.Server
conf string
}
func startComposedBus(t *testing.T, records buscompose.Records) *composedBus {
dir := t.TempDir()
b := &composedBus{t: t, conf: filepath.Join(dir, "server.conf")}
b.write(records, dir)
opts, err := server.ProcessConfigFile(b.conf)
if err != nil {
t.Fatalf("the composed list is not the server's: %v", err)
}
opts.NoLog, opts.NoSigs = true, true
if b.s, err = server.NewServer(opts); err != nil {
t.Fatal(err)
}
go b.s.Start()
if !b.s.ReadyForConnections(30 * time.Second) {
t.Fatal("the bus did not come up")
}
t.Cleanup(b.s.Shutdown)
return b
}
func (b *composedBus) write(records buscompose.Records, dir string) {
users, err := buscompose.Users(records)
if err != nil {
b.t.Fatal(err)
}
for i := range users {
users[i].PasswordHash = "pw-" + users[i].Username()
}
accounts, err := buscompose.ComposeAccounts(users)
if err != nil {
b.t.Fatal(err)
}
if err := os.WriteFile(b.conf, []byte("listen: 127.0.0.1:-1\njetstream { store_dir: \""+dir+"\" }\n"+accounts), 0o600); err != nil {
b.t.Fatal(err)
}
}
// recompose writes the accounts composed from records again and has the server load them.
func (b *composedBus) recompose(records buscompose.Records) {
b.write(records, filepath.Dir(b.conf))
if err := b.s.Reload(); err != nil {
b.t.Fatalf("the server does not load the composed list again: %v", err)
}
}
// dial connects as a composed user; the server's refusals of its subscriptions and publishes arrive on refusals.
func (b *composedBus) dial(user string) (*nats.Conn, chan string) {
refusals := make(chan string, 16)
nc, err := nats.Connect(b.s.ClientURL(), nats.UserInfo(user, "pw-"+user), nats.CustomInboxPrefix("_INBOX."+user),
nats.ErrorHandler(func(_ *nats.Conn, _ *nats.Subscription, err error) { refusals <- err.Error() }))
if err != nil {
b.t.Fatalf("%s: %v", user, err)
}
b.t.Cleanup(nc.Close)
return nc, refusals
}
// refusedWithin waits, within a bound, for the server's refusal naming subject.
func refusedWithin(refusals chan string, subject string) bool {
bound := time.NewTimer(5 * time.Second)
defer bound.Stop()
for {
select {
case e := <-refusals:
if strings.Contains(e, "Permissions Violation") && strings.Contains(e, subject) {
return true
}
case <-bound.C:
return false
}
}
}
// theToolRunnerHearsEveryEventOnTheBus, run by TestTheGrantsPartWhole, defends design 34 every-event §9 and §27 on a
// real server (ADR 0670 §1, §3, ADR 0671 §1, §2): the tool runner's account, as composed, hears a module's event, a
// module stream's and a restricted stream's whose grant value names no tool runner, still after that value changes and
// the accounts are composed again; it publishes none of them, hears no accept or proof, and gets no durable consumer
// and no acknowledgement on the restricted stream; another module hears no event it does not consume, and a module
// consuming the restricted stream with no entry in its grant value gets no consumer and no read.
func theToolRunnerHearsEveryEventOnTheBus(t *testing.T) {
const ledger = "mesh.stream.shop.event.booked"
records := func(auditorEntry []string) buscompose.Records {
return buscompose.Records{Nodes: []string{"a"}, Assigned: map[string][]buscompose.Declared{"a": {
{Module: buscompose.GrantsModule},
{Module: "node-tools", ToolRunner: true},
{Module: "mesh-delivery-view", Emits: []string{"delivered"}},
{Module: "plex"},
{Module: "shop", Streams: []buscompose.ModuleStream{
{Module: "shop", Name: "SHOP", Events: []string{"opened"}, MaxAgeDays: 1, MaxMiB: 1},
{Module: "shop", Name: "LEDGER", Events: []string{"booked"}, MaxAgeDays: 1, MaxMiB: 1, Restricted: true}}},
{Module: "auditor", RunsAs: "auditor", Consumes: []string{"shop.booked"},
Hears: []buscompose.Heard{{Stream: "LEDGER", Emitter: "shop", Filters: auditorEntry, Restricted: true}}},
}}}
}
bus := startComposedBus(t, records(nil))
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
refusedIn := func() context.Context {
c, stop := context.WithTimeout(ctx, 2*time.Second)
t.Cleanup(stop)
return c
}
grantsConn, _ := bus.dial("a." + buscompose.GrantsModule)
grants, err := jetstream.New(grantsConn)
if err != nil {
t.Fatal(err)
}
for _, st := range []jetstream.StreamConfig{{Name: "SHOP", Subjects: []string{"mesh.stream.shop.event.opened"}},
{Name: "LEDGER", Subjects: []string{ledger}}} {
if _, err := grants.CreateStream(ctx, st); err != nil {
t.Fatalf("the grants part creates the declared stream %s: %v", st.Name, err)
}
}
runner, runnerRefused := bus.dial("a.node-tools")
heard := make(chan *nats.Msg, 16)
for _, s := range sdkbc.EveryEvent() {
if _, err := runner.ChanSubscribe(s, heard); err != nil {
t.Fatal(err)
}
}
if err := runner.Flush(); err != nil {
t.Fatal(err)
}
hears := func(subject, from string) {
t.Helper()
select {
case m := <-heard:
if m.Subject != subject {
t.Errorf("the tool runner heard %s, want %s published by %s", m.Subject, subject, from)
}
case <-time.After(5 * time.Second):
t.Errorf("the tool runner heard nothing, want %s published by %s", subject, from)
}
}
view, _ := bus.dial("a.mesh-delivery-view")
shop, _ := bus.dial("a.shop")
for _, e := range []struct {
conn *nats.Conn
subject, from string
}{
{view, "mesh.mod.mesh-delivery-view.event.delivered", "the view"},
{shop, "mesh.stream.shop.event.opened", "a module's stream"},
{shop, ledger, "a restricted stream whose grant names no tool runner"}} {
if err := e.conn.Publish(e.subject, []byte(`{}`)); err != nil {
t.Fatal(err)
}
if err := e.conn.Flush(); err != nil {
t.Fatal(err)
}
hears(e.subject, e.from)
}
// It publishes no event, and hears no accept nor proof (they are never events).
if err := runner.Publish("mesh.mod.mesh-delivery-view.event.delivered", []byte(`{}`)); err != nil {
t.Fatal(err)
}
if !refusedWithin(runnerRefused, "mesh.mod.mesh-delivery-view.event.delivered") {
t.Error("the tool runner published the view's event")
}
for _, s := range []string{"mesh.seat.channel.accept.show.k", "mesh.seat.intake.proof.code.k"} {
if _, err := runner.SubscribeSync(s); err != nil {
t.Fatal(err)
}
if !refusedWithin(runnerRefused, s) {
t.Errorf("the tool runner subscribed %s", s)
}
}
// No durable consumer, no acknowledgement on the restricted stream.
runnerJS, err := jetstream.New(runner)
if err != nil {
t.Fatal(err)
}
if _, err := runnerJS.CreateConsumer(refusedIn(), "LEDGER", jetstream.ConsumerConfig{Durable: "node-tools"}); err == nil {
t.Error("the tool runner made a durable consumer on the restricted stream")
}
if err := runner.Publish("$JS.ACK.LEDGER.node-tools.1.1.1.1.0", nil); err != nil {
t.Fatal(err)
}
if !refusedWithin(runnerRefused, "$JS.ACK.LEDGER") {
t.Error("the tool runner acknowledged on the restricted stream")
}
// Another module hears no event it does not consume.
plex, plexRefused := bus.dial("a.plex")
if _, err := plex.SubscribeSync("mesh.mod.mesh-delivery-view.event.delivered"); err != nil {
t.Fatal(err)
}
if !refusedWithin(plexRefused, "mesh.mod.mesh-delivery-view.event.delivered") {
t.Error("plex subscribed an event it does not consume")
}
// A consumer of the restricted stream with no entry in its grant value gets no consumer and no read.
auditor, _ := bus.dial("a.auditor")
auditorJS, err := jetstream.New(auditor)
if err != nil {
t.Fatal(err)
}
if _, err := auditorJS.CreateConsumer(refusedIn(), "LEDGER", jetstream.ConsumerConfig{Durable: "auditor"}); err == nil {
t.Error("a consumer with no entry made a durable consumer on the restricted stream")
}
if _, err := auditorJS.Stream(refusedIn(), "LEDGER"); err == nil {
t.Error("a consumer with no entry reads the restricted stream")
}
// The grant value changes and the accounts are composed again: the tool runner still hears the restricted stream.
bus.recompose(records([]string{ledger}))
if err := shop.Publish(ledger, []byte(`{}`)); err != nil {
t.Fatal(err)
}
if err := shop.Flush(); err != nil {
t.Fatal(err)
}
hears(ledger, "the restricted stream after its grant value changed")
}
// aRuntimeDeniedTheMarksHearsNoMarkedCall, run by TestTheGrantsPartWhole, defends design 46 approval mark §6 on a real
// server (AM-D4): a runtime account composed with the deny, its node's tool runner stating no approval-mark/1, holds
// `mesh.mod.<module>.tool.>` and is delivered no call on a marked tool's subject nor on the caller-named subject
// process.Call sends on first; a call of an unmarked tool still reaches it.
func aRuntimeDeniedTheMarksHearsNoMarkedCall(t *testing.T) {
records := buscompose.Records{Nodes: []string{"a"}, Assigned: map[string][]buscompose.Declared{"a": {
{Module: "node-tools", ToolRunner: true},
{Module: "shell", Serves: []string{"execute", "read"}, Marked: []string{"execute"}},
{Module: "asker", Invokes: []string{"shell.execute", "shell.read"}},
}}, Readings: map[string]buscompose.RunnerReading{"a": {Capabilities: []string{"bus-state-calls"}, Fingerprint: "sha256:runner"}},
Running: map[string]string{"a.node-tools": "sha256:runner"}}
bus := startComposedBus(t, records)
runner, _ := bus.dial("a.node-tools")
heard := make(chan *nats.Msg, 16)
for _, s := range []string{"mesh.mod.shell.tool.>", "mesh.mod.shell.call.>"} {
if _, err := runner.ChanSubscribe(s, heard); err != nil {
t.Fatal(err)
}
}
if err := runner.Flush(); err != nil {
t.Fatal(err)
}
asker, askerRefused := bus.dial("a.asker")
// The caller-named subject is process.Call's first: mesh.mod.<module>.call.<tool>.<caller's token>.
for _, subject := range []string{"mesh.mod.shell.tool.execute", "mesh.mod.shell.call.execute.asker~a", "mesh.mod.shell.tool.read"} {
if err := asker.Publish(subject, []byte(`{}`)); err != nil {
t.Fatal(err)
}
}
if err := asker.Flush(); err != nil {
t.Fatal(err)
}
select {
case e := <-askerRefused:
t.Fatalf("the asker may not call: %s", e)
default:
}
select {
case m := <-heard:
if m.Subject != "mesh.mod.shell.tool.read" {
t.Errorf("the runtime denied the marks was delivered %s", m.Subject)
}
case <-time.After(5 * time.Second):
t.Fatal("the unmarked call did not reach the runtime")
}
if err := runner.Flush(); err != nil {
t.Fatal(err)
}
select {
case m := <-heard:
t.Errorf("the runtime denied the marks was delivered %s", m.Subject)
default:
}
}