Ask the packet filter's holder allowed at every compose of the filter

The node-engine stays the one renderer of the filter file: at each compose of
a converged node's filter it asks the holder of node-packet-filter its verb
allowed within the setting compose-ask-bound (10 s) and renders the answer.
When allowed does not answer, or answers what is not its answer, no filter file
is composed: the one loaded is kept as it runs, the holder's module is not held
(an old holder must stay replaceable), and the part withheld is said once in
the journal until an answer comes between. The node hears allowances-changed
on its own node's subject and composes again. The manifest invokes
seat:node-packet-filter.allowed and consumes node-packet-filter.allowances-changed.

Builds: design 08 control-verbs §1, §3, §4, §5, §7, §8; ADR 0683; H-A-305, H-A-307 (1), (2), H-A-314 (3), (5), (7), H-A-315 (2), (6), H-A-321
This commit is contained in:
2026-11-21 22:32:36 +01:00
parent 2a29c458ce
commit dc3765bb7c
18 changed files with 486 additions and 30 deletions
+115
View File
@@ -6,6 +6,7 @@ import (
"encoding/hex"
"encoding/json"
"errors"
"runtime"
"strings"
"sync"
"testing"
@@ -13,9 +14,11 @@ import (
"github.com/novox/mesh-host/internal/agent"
"git.novox.be/novox/mesh-contracts/go/seats/meshmoduleassigner"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshcatalogclient"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshwants"
"git.novox.be/novox/mesh-networking/modules/mesh-connectivity-contracts/go/seats/nodepacketfilter"
)
// Design 47 G A-430, A-430a (the lead's answer A-430b): a want naming its version is resolved to its artifacts'
@@ -134,6 +137,118 @@ func TestAModulesClaimsAreReadFromTheEntryOfItsWantsBuild(t *testing.T) {
if claims, known := c.Claims(context.Background(), "ghost", "mesh-broker"); claims || known {
t.Errorf("a module with no want has no manifest read: claims %v, known %v", claims, known)
}
// Design 08 control-verbs §1, §4, §5: the compose of the filter asks the holder of node-packet-filter its verb allowed
// within the compose-ask bound; an unanswered allowed is a part withheld, said once until an answer comes between.
t.Run("the filter asks allowed of its holder", func(t *testing.T) {
now := time.Date(2026, 11, 21, 20, 0, 0, 0, time.UTC)
var said []string
var asked []time.Duration
answer, failure := json.RawMessage(`{"allowances":[{"key":"allow.tcp.8000","port":8000,"protocol":"tcp","at":"a1","by":"op","why":"w"}]}`), error(nil)
env := Env{Node: "desk", Say: func(line string) { said = append(said, line) }, Now: func() time.Time { return now },
Allowed: func(_ context.Context, within time.Duration) (json.RawMessage, error) {
asked = append(asked, within)
return answer, failure
}}
c := New(env)
c.keptEnt = map[string]keptEntry{}
c.wants["nft"] = meshwants.WantsByWantNodeModuleValue{Fp: fp}
c.keptEnt[fp] = keptEntry{entry: meshcatalog.CatalogEntry{Module: "nft", Fp: fp,
Manifest: map[string]any{"module": "nft", "version": "1", "claims": []any{map[string]any{"name": "node-packet-filter"}}}}}
own := meshmoduleassigner.Assignments{Node: "desk", Modules: []meshmoduleassigner.PlacedModule{{Module: "nft"}}}
got := c.filterOf(context.Background(), own)
if len(asked) != 1 || asked[0] != DefaultComposeAskBound || got.Withheld != "" || len(got.Allowing.Allowances) != 1 ||
got.Allowing.Allowances[0].Key != "allow.tcp.8000" || !got.Allowing.At.Equal(now) {
t.Errorf("asked %v, given %+v; want allowed asked once within 10s and its allowance given at the compose's time", asked, got)
}
failure = errors.New("nats: no responders available for request")
for range 2 {
if got = c.filterOf(context.Background(), own); !strings.Contains(got.Withheld, "node-packet-filter's verb allowed on desk, which did not answer within 10s: nats: no responders") {
t.Errorf("an unanswered allowed gives %+v, want the part withheld naming the holder asked", got)
}
}
answer, failure = json.RawMessage(`{"allowances":[{"key":"allow.tcp.8000","port":8000,"protocol":"tcp","until":"tomorrow"}]}`), nil
if got = c.filterOf(context.Background(), own); !strings.Contains(got.Withheld, `answered what is not its answer: allow.tcp.8000 has the until "tomorrow"`) {
t.Errorf("an answer that is not allowed's gives %+v, want the part withheld", got)
}
if n := strings.Count(strings.Join(said, "\n"), "is withheld"); n != 1 {
t.Errorf("the withheld part is said %d times over three unanswered composes, want once:\n%s", n, strings.Join(said, "\n"))
}
answer = json.RawMessage(`{"allowances":[]}`)
c.filterOf(context.Background(), own)
failure = errors.New("nats: timeout")
c.filterOf(context.Background(), own)
if n := strings.Count(strings.Join(said, "\n"), "is withheld"); n != 2 {
t.Errorf("the withheld part is said %d times, want again after a compose with an answer between", n)
}
asked = nil
c.env.ComposeAskBound = 3 * time.Second
if c.filterOf(context.Background(), own); len(asked) != 1 || asked[0] != 3*time.Second {
t.Errorf("asked within %v, want the setting compose-ask-bound", asked)
}
asked = nil
own.Decided.Adopted = true
if got = c.filterOf(context.Background(), own); len(asked) != 0 || got.Withheld != "" || len(got.Allowing.Allowances) != 0 {
t.Errorf("an adopted node asked %d times and is given %+v, want none asked and none rendered", len(asked), got)
}
own.Decided.Adopted = false
own.Modules = []meshmoduleassigner.PlacedModule{{Module: "ghost"}}
if c.filterOf(context.Background(), own); len(asked) != 0 {
t.Errorf("a node with no holder of node-packet-filter placed asked allowed %d times", len(asked))
}
})
// Design 08 control-verbs §3, §8: the node hears allowances-changed on its own node's subject, never another's, and
// composes again on it.
t.Run("an allowances-changed of its node composes again", func(t *testing.T) {
b := newFakeBus()
env := b.env()
var mu sync.Mutex
heard := map[string]chan []byte{}
env.Hear = func(subject string) (<-chan []byte, func(), error) {
mu.Lock()
defer mu.Unlock()
heard[subject] = make(chan []byte)
return heard[subject], func() {}, nil
}
c := New(env)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
out, err := c.Watch(ctx, "n1.>")
if err != nil {
t.Fatal(err)
}
go func() {
for range out {
}
}()
for _, in := range c.inputs() {
b.send(in.bucket, agent.Update{Caught: true})
}
passAt := func() time.Time { c.mu.Lock(); defer c.mu.Unlock(); return c.last.At }
for deadline := time.Now().Add(10 * time.Second); passAt().IsZero(); runtime.Gosched() {
if time.Now().After(deadline) {
t.Fatal("no first pass")
}
}
first := passAt()
mu.Lock()
own, other := heard[nodepacketfilter.SubjectAllowancesChanged("n1")], heard[nodepacketfilter.SubjectAllowancesChanged("n2")]
mu.Unlock()
if own == nil || other != nil {
t.Fatalf("heard %v, want the own node's allowances-changed and no other's", heard)
}
select {
case own <- []byte(`{"node":"n1","change":"allowed"}`):
case <-time.After(10 * time.Second):
t.Fatal("nothing hears the node's allowances-changed")
}
for deadline := time.Now().Add(10 * time.Second); passAt().Equal(first); runtime.Gosched() {
if time.Now().After(deadline) {
t.Fatal("an allowances-changed of the node composed nothing again")
}
}
})
}
// Design 47 H, the lead's answer H-A-146 (A-430a: the bytes are fetched by the digest the versions answer gives): a resolution