Files
mesh-catalog/modules/systemd-resolved/cmd/resolver-tools/main.go
T
jochen 9f5614b35e systemd-resolved: routes answers the uplink's names and the mesh's fallback (issue 960)
routes (bus and the machine socket) gains uplink_names {names, link,
refused}, the applied list's names resolved holds on that link now, never
the file's, and mesh_resolvers {silent, since} from the guard's fallback
state. The guard writes that state at each of ADR 0688's two turns, and
once more at its own first round, since it goes at every boot.

Builds: design 08 sign-in-page §2, §8, §9
2026-11-24 21:26:30 +01:00

108 lines
4.6 KiB
Go

// systemd-resolved's tools bundle (novox/hq ADR 0247): the node-resolver seat's verbs, and the module's
// guard.
//
// Started with no arguments it is served by the node's runtime as the operator account, over MCP on stdio
// through the Go SDK (ADR 0188, ADR 0193): `routes`, `route` and `unroute` on the mesh, for the operator
// to read and correct. Started as `resolver-tools guard` it is the module's long-running process, as root:
// it keeps the machine's resolver file the module's own and serves the same verbs on the machine, to the
// modules there (guard.go). stdout is the MCP channel; everything else is said on stderr.
package main
import (
"context"
"fmt"
"os"
"os/signal"
"syscall"
stdio "git.novox.be/novox/mesh-sdk/go"
)
// Seat is the role this module holds.
const Seat = "node-resolver"
func main() {
if len(os.Args) > 1 {
if os.Args[1] != "guard" || len(os.Args) != 2 {
fmt.Fprintln(os.Stderr, "usage: resolver-tools [guard]")
os.Exit(2)
}
if err := guard(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
return
}
if err := stdio.Serve("", tools(ThisResolver(), History)); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
func guard() error {
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGTERM, syscall.SIGINT)
defer stop()
g := NewGuard()
if err := os.MkdirAll(g.Dir, 0o755); err != nil {
return err
}
g.writeHistory()
// Read what it applied last, so a restart takes the uplink's names off the link it names, never
// inferring which (design 08 sign-in-page §8).
g.applied = ReadUplinkApplied(g.Resolver.UplinkNamesAppliedFile)
go g.Run(ctx)
return g.Serve(ctx, Socket)
}
func str(description string) map[string]any {
return map[string]any{"type": "string", "description": description}
}
func arg(a map[string]any, k string) string {
v, _ := a[k].(string)
return v
}
// verb is one of the seat's verbs: listed as `<seat>.<verb>`, so the runtime serves it on the seat's
// subject, as <node>/node-resolver.<verb>.
func verb(name, description string, input map[string]any, run func(a map[string]any) (any, error)) stdio.Tool {
return stdio.Tool{Name: Seat + "." + name, Description: description, Input: input, Run: run}
}
func tools(r *Resolver, history string) []stdio.Tool {
ctx := context.Background()
return []stdio.Tool{
verb("routes", "What this machine's own resolver sends where: the mesh's resolvers, which answer every name "+
"not routed elsewhere, and each link given servers of its own with the domains routed to them. Also the "+
"resolver file's outside writes, newest first: when, who wrote it as far as the file says, whether a "+
"module took it, and when the module's own file stood again. (r)",
nil, func(map[string]any) (any, error) {
routes, err := r.Routes(ctx)
if err != nil {
return nil, err
}
return map[string]any{"mesh": routes.Mesh, "links": routes.Links, "outside_writes": ReadHistory(history),
"uplink_names": r.UplinkNamesAnswer(routes), "mesh_resolvers": r.MeshResolversAnswer()}, nil
}),
verb("route", "Send these domains, and every name under them, to these servers over this link, and only "+
"them: the link never answers other names, and the mesh's own domain is refused. Replaces whatever the "+
"link was given before; a link that goes away takes its route with it. (a)",
map[string]any{"link": str("the network link the servers are reached over, by name"),
"domains": str("the domains to route there, separated by spaces or commas"),
"servers": str("the servers' addresses, separated by spaces or commas")},
func(a map[string]any) (any, error) {
return r.Route(ctx, arg(a, "link"), Split(a["domains"]), Split(a["servers"]))
}),
verb("unroute", "Take one link's route away: its domains go to the mesh's resolvers again. Nothing changes "+
"when the link has none. (a)",
map[string]any{"link": str("the network link, by name")},
func(a map[string]any) (any, error) { return r.Unroute(ctx, arg(a, "link")) }),
verb("uplink-names", "Give this machine's own resolver the network manager's own check's host names, so a "+
"working network it tests resolves through the network it tests: at most 8, each a host name of "+
"letters, digits, hyphens and dots; an empty list takes them away. Refused, naming the name and why: "+
"not a host name, the root, the mesh's own domain or under it, or more than 8. (a)",
map[string]any{"names": str("the host names, separated by spaces or commas; empty clears them")},
func(a map[string]any) (any, error) { return r.SetUplinkNames(ctx, Split(a["names"])) }),
}
}