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
+1 -1
View File
@@ -473,7 +473,7 @@ func (c *Composer) composeShadow(ctx context.Context, running []engcomp.Composed
c.mu.Lock()
c.wants = wants
c.mu.Unlock()
scope := frombus.Scope{Node: node, KeptAt: kept, InOwn: inOwn, Running: running, SameValue: c.sameValue, Heard: c.heardNow(), RunningManifest: c.runningFor(node)}
scope := frombus.Scope{Node: node, KeptAt: kept, InOwn: inOwn, Running: running, SameValue: c.sameValue, Heard: c.heardNow(), RunningManifest: c.runningFor(node), Filter: c.lastFilter(node)}
if input != nil {
input(scope, fps)
}
+124
View File
@@ -0,0 +1,124 @@
package selfcompose
import (
"context"
"encoding/json"
"fmt"
"time"
"git.novox.be/novox/mesh-contracts/go/seats/meshmoduleassigner"
"git.novox.be/novox/mesh-networking/modules/mesh-connectivity-contracts/go/seats/nodepacketfilter"
"github.com/novox/mesh-host/internal/compose/resolution"
)
// The compose of the node's packet filter asks the holder of the seat node-packet-filter its verb allowed, of its own node,
// within the compose-ask bound, and renders the answer's allowances beside the derived ports (design 08 control-verbs §1,
// §2; ADR 0683). An unanswered allowed is a part withheld (H-A-306): no filter file is composed and the one loaded is kept,
// said once in the journal and in the composition read (§4, H-A-321).
// DefaultComposeAskBound is how long a compose waits for an input it asks (allowed) when the setting `compose-ask-bound`
// gives nothing (Env.ComposeAskBound 0; design 08 control-verbs §1, H-A-314 (5); principle 11).
const DefaultComposeAskBound = 10 * time.Second
// AllowedPart is the part a compose of the filter asks: the seat node-packet-filter's verb allowed (H-A-321).
const AllowedPart = nodepacketfilter.Seat + "." + nodepacketfilter.VerbAllowed
// composeAskBound is the setting, or DefaultComposeAskBound.
func (c *Composer) composeAskBound() time.Duration {
if c.env.ComposeAskBound > 0 {
return c.env.ComposeAskBound
}
return DefaultComposeAskBound
}
// filterOf is what this pass's compose of the node's packet filter is given: on a converged node whose placed modules hold
// the seat node-packet-filter, the allowances allowed answered, or why it did not answer (the filter then kept as it runs).
// An adopted node renders none (§5), and a node with no holder placed composes no filter to give them to.
func (c *Composer) filterOf(ctx context.Context, own meshmoduleassigner.Assignments) resolution.Filter {
if c.env.Allowed == nil || own.Decided.Adopted || !c.holdsFilter(ctx, own) {
return resolution.Filter{}
}
at := c.now()
bound := c.composeAskBound()
raw, err := c.env.Allowed(ctx, bound)
why := ""
var answer nodepacketfilter.AllowedAnswer
switch {
case err != nil:
why = fmt.Sprintf("asked of the seat %s's verb %s on %s, which did not answer within %s: %v",
nodepacketfilter.Seat, nodepacketfilter.VerbAllowed, c.env.Node, bound, err)
case json.Unmarshal(raw, &answer) != nil:
why = fmt.Sprintf("asked of the seat %s's verb %s on %s, which answered what is not its answer",
nodepacketfilter.Seat, nodepacketfilter.VerbAllowed, c.env.Node)
default:
if bad := notAnAllowance(answer.Allowances); bad != "" {
why = fmt.Sprintf("asked of the seat %s's verb %s on %s, which answered what is not its answer: %s",
nodepacketfilter.Seat, nodepacketfilter.VerbAllowed, c.env.Node, bad)
}
}
c.mu.Lock()
said := c.allowedSaid
c.allowedSaid = why != ""
c.mu.Unlock()
if why != "" {
// Said once, and again only after a compose with an answer in between (§4).
if !said {
c.env.Say("the node composes no filter file and keeps the one loaded: the part " + AllowedPart + " is withheld, " + why)
}
return resolution.Filter{Withheld: why}
}
return resolution.Filter{Allowing: resolution.Allowing{Allowances: answer.Allowances, At: at}}
}
// holdsFilter says a module placed on the node claims the seat node-packet-filter, as the manifest of its want's build says.
func (c *Composer) holdsFilter(ctx context.Context, own meshmoduleassigner.Assignments) bool {
for _, m := range own.Modules {
if claims, known := c.Claims(ctx, m.Module, nodepacketfilter.Seat); claims && known {
return true
}
}
return false
}
// notAnAllowance names the first allowance the renderer cannot render as the seat's form says it (a key, tcp or udp, a
// port 1-65535, an until in RFC 3339); "" when every one reads.
func notAnAllowance(allowances []nodepacketfilter.Allowance) string {
for _, a := range allowances {
switch {
case a.Key == "":
return "an allowance with no key"
case a.Protocol != nodepacketfilter.AllowanceProtocolTcp && a.Protocol != nodepacketfilter.AllowanceProtocolUdp:
return fmt.Sprintf("%s has the protocol %q", a.Key, a.Protocol)
case a.Port < 1 || a.Port > 65535:
return fmt.Sprintf("%s has the port %d", a.Key, a.Port)
}
if a.Until != "" {
if _, err := time.Parse(time.RFC3339, a.Until); err != nil {
return fmt.Sprintf("%s has the until %q", a.Key, a.Until)
}
}
}
return ""
}
// LastWithheldParts is each part the last pass kept because its read did not answer, with why (H-A-321): the filter's
// allowed, named AllowedPart. Nil when none.
func (c *Composer) LastWithheldParts() map[string]string {
c.mu.Lock()
defer c.mu.Unlock()
if c.filter.Withheld == "" {
return nil
}
return map[string]string{AllowedPart: c.filter.Withheld}
}
// lastFilter is what the last pass's compose of this node's filter was given, for a candidate composed in shadow on node
// (so a candidate's filter is what the node composes); empty for another node.
func (c *Composer) lastFilter(node string) resolution.Filter {
if node != c.env.Node {
return resolution.Filter{}
}
c.mu.Lock()
defer c.mu.Unlock()
return c.filter
}
+45 -14
View File
@@ -28,6 +28,7 @@ import (
"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"
"git.novox.be/novox/mesh-sdk/go/declaration"
"github.com/novox/mesh-host/internal/buckets"
"github.com/novox/mesh-host/internal/compositions"
@@ -145,6 +146,13 @@ type Env struct {
// Set, the composer reads the catalog half by the fingerprints its wants and the runnings it heard name and nothing else of
// the indexer's or the builder's states (design 47 G §421, §628); a composer is made with it.
Entry meshcatalogclient.Call
// Allowed asks the holder of the seat node-packet-filter on this node its verb allowed, within the bound given, in the
// engine's own name: the allowances a compose of the filter renders (design 08 control-verbs §1). Nil asks nothing, and
// the filter renders none.
Allowed func(ctx context.Context, within time.Duration) (json.RawMessage, error)
// ComposeAskBound is the setting `compose-ask-bound`: how long a compose waits for allowed; DefaultComposeAskBound when 0
// (design 08 control-verbs §1, H-A-314 (5)).
ComposeAskBound time.Duration
// Registry asks the registry seats' verb versions (oci-artifact-registry, container-image-registry) in the engine's own
// name: a want naming its version is resolved to its artifacts' digests through it (design 47 G A-430a).
Registry meshcatalogclient.Call
@@ -328,6 +336,11 @@ type Composer struct {
pubSaid string
pubWrite sync.Mutex
pubKept map[string]ComposedEntry
// filter is what the last pass's compose of the packet filter was given (filterOf), which a candidate composed in shadow
// is given too; allowedSaid says its withheld allowed was said in the journal, until it answers (design 08
// control-verbs §4). Guarded by mu.
filter resolution.Filter
allowedSaid bool
}
// Pass is how one pass went, as the node-engine's verb compose answers it.
@@ -574,24 +587,37 @@ func (c *Composer) Watch(ctx context.Context, _ string) (<-chan agent.Update, er
if err != nil {
return nil, fmt.Errorf("the node composes again when a secret changes, and cannot hear the vault say so: %w", err)
}
go func() {
defer stop()
for {
select {
case <-ctx.Done():
return
case _, ok := <-heard:
if !ok {
return
}
// And the packet filter's holder saying its allowances changed on this node: the filter is composed again, asking
// allowed; the event's body is not rendered (design 08 control-verbs §3).
changed, stopChanged, err := c.env.Hear(nodepacketfilter.SubjectAllowancesChanged(c.env.Node))
if err != nil {
stop()
return nil, fmt.Errorf("the node composes its filter again when its allowances change, and cannot hear the holder of %s say so: %w",
nodepacketfilter.Seat, err)
}
for _, h := range []struct {
heard <-chan []byte
stop func()
}{{heard, stop}, {changed, stopChanged}} {
go func() {
defer h.stop()
for {
select {
case merged <- tagged{in: -1, ok: true}:
case <-ctx.Done():
return
case _, ok := <-h.heard:
if !ok {
return
}
select {
case merged <- tagged{in: -1, ok: true}:
case <-ctx.Done():
return
}
}
}
}
}()
}()
}
}
if c.env.SeatListChanged != nil {
go func() {
@@ -662,7 +688,7 @@ func (c *Composer) Watch(ctx context.Context, _ string) (<-chan agent.Update, er
}
switch {
case t.in < 0:
dirty = true // a secret changed: its new generation is asked of the vault at the compose
dirty = true // a secret or the allowances changed: asked again at the compose
case t.u.Caught:
caught[t.in] = true
if ins[t.in].bucket == buckets.Assignments {
@@ -1138,6 +1164,11 @@ func (c *Composer) compose(ctx context.Context) []agent.Update {
// (ADR 0636 §1): the module is held as it runs before anything of its build is read, so no take-in or artifact
// reference of it (the artifact store's seat is not bootstrapped) can refuse the pass (issue 1966).
scope.Held = c.heldUntilListed(ctx, r, own)
// The packet filter's allowances, asked of its holder at every compose of the filter (design 08 control-verbs §1, §4).
scope.Filter = c.filterOf(ctx, own)
c.mu.Lock()
c.filter = scope.Filter
c.mu.Unlock()
// A want whose entry the catalog half has not answered, or holds none of, is held with what runs of it (§421).
if holds := c.entryHolds(); len(holds) > 0 {
if scope.Held == nil {
+2 -1
View File
@@ -31,6 +31,7 @@ import (
"testing"
"time"
"git.novox.be/novox/mesh-networking/modules/mesh-connectivity-contracts/go/seats/nodepacketfilter"
"git.novox.be/novox/mesh-networking/modules/mesh-wireguard/go/overlay"
"git.novox.be/novox/mesh-sdk/go/catalogue"
"git.novox.be/novox/mesh-sdk/go/seatlist"
@@ -250,7 +251,7 @@ func TestTheNodeComposesItsWantsFromTheBus(t *testing.T) {
}
secretChanged := make(chan []byte, 1)
env.Hear = func(subject string) (<-chan []byte, func(), error) {
if subject == meshcatalog.SubjectCatalogRevisionAll {
if subject == meshcatalog.SubjectCatalogRevisionAll || subject == nodepacketfilter.SubjectAllowancesChanged("n1") {
return make(chan []byte), func() {}, nil
}
if subject != VaultSecretChanged {
+1 -1
View File
@@ -682,7 +682,7 @@ func (s *Shadow) answerProposal(ctx context.Context, proposal string) (nodeengin
given[node] = &without
}
a := frombus.Answer(ctx, reader{c: c}, c.env.Mesh, frombus.Scope{Node: node, KeptAt: kept, Assignments: given, InOwn: inOwn,
SameValue: c.sameValue, HeldToday: s.heldToday(heldAdded), RunningManifest: c.runningFor(node)}, fps, running, proposal, rev)
SameValue: c.sameValue, HeldToday: s.heldToday(heldAdded), RunningManifest: c.runningFor(node), Filter: c.lastFilter(node)}, fps, running, proposal, rev)
for _, module := range slices.Sorted(maps.Keys(heldAdded)) {
a.Held = append(a.Held, nodeengine.CandidateRefusal{Module: module, Why: heldAdded[module]})
}
+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