Files
mesh-host/internal/selfcompose/filter.go
T
jschoubben 546e535375 Serve the node-engine seat's converged, and name a part a compose kept
The holder of node-packet-filter asks the node's convergence state before it
allows a port, and never assumes converged: the engine answers it from its
record, adopted or converged, since the mode last changed (now recorded with
its time), and the found firewall's kind when adopted; a record with no mode is
refused. The composition read of the filter's holder names, under
withheld-parts, the part node-packet-filter.allowed with why when the last
compose kept the filter file because allowed did not answer.

Builds: design 33 control-verbs §10, §14; design 08 control-verbs §4, §8; ADR 0683, ADR 0168; H-A-316, H-A-321
2026-11-21 22:32:36 +01:00

128 lines
5.2 KiB
Go

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 {
c.mu.Lock()
c.filterHolder = m.Module
c.mu.Unlock()
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 of module because its read did not answer, with why (H-A-321): of the
// holder of node-packet-filter, the filter's allowed, named AllowedPart. Nil when none.
func (c *Composer) LastWithheldParts(module string) map[string]string {
c.mu.Lock()
defer c.mu.Unlock()
if c.filter.Withheld == "" || module != c.filterHolder {
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
}