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
+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
}