systemd-resolved: answer with the generated contract's own types (review r1 C-6, C-8)
C-6: import noderesolver (mesh-networking's seat definition) at the zero version; routes, route, unroute and uplink-names answer with its RoutesAnswer, RouteAnswer, UnrouteAnswer and UplinkNamesAnswer, never a map or a parallel struct, and uplink-names' input names is a JSON array (arrayArg), never a comma- or space-split string. C-8: the names file write's 15 s is the stated constant WriteUplinkNamesBound, not an inline literal. Builds: design 08 sign-in-page §2, §5, §6, §9, DQ-12, C-6, C-8
This commit is contained in:
@@ -552,8 +552,9 @@ func (g *Guard) Answer(ctx context.Context, req Request) Reply {
|
||||
case "routes":
|
||||
var routes *Routes
|
||||
if routes, err = g.Resolver.Routes(ctx); err == nil {
|
||||
result = map[string]any{"mesh": routes.Mesh, "links": routes.Links,
|
||||
"uplink_names": g.Resolver.UplinkNamesAnswer(routes), "mesh_resolvers": g.Resolver.MeshResolversAnswer()}
|
||||
// The machine's socket answers routes the same as the bus's (§5, DQ-5), with no outside_writes
|
||||
// of its own list: a module on the machine reads displaced over its own verb.
|
||||
result = g.Resolver.RoutesAnswer(routes, nil)
|
||||
}
|
||||
case "route":
|
||||
var routed *Routed
|
||||
|
||||
@@ -495,12 +495,12 @@ func TestRoutesAnswersUplinkNamesAndMeshResolvers(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
un := r.UplinkNamesAnswer(routes)
|
||||
if names, _ := un["names"].([]string); strings.Join(names, ",") != "check.example" || un["link"] != "wlan0" || un["refused"] != "refused once" {
|
||||
un := r.UplinkNamesField(routes)
|
||||
if strings.Join(un.Names, ",") != "check.example" || un.Link != "wlan0" || un.Refused != "refused once" {
|
||||
t.Errorf("uplink_names is %+v", un)
|
||||
}
|
||||
mr := r.MeshResolversAnswer()
|
||||
if mr["silent"] != true || !mr["since"].(time.Time).Equal(since) {
|
||||
mr := r.MeshResolversField()
|
||||
if !mr.Silent || mr.Since != since.UTC().Format(time.RFC3339) {
|
||||
t.Errorf("mesh_resolvers is %+v", mr)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -63,6 +63,25 @@ func arg(a map[string]any, k string) string {
|
||||
return v
|
||||
}
|
||||
|
||||
// stringArray declares an input as an array of strings, as the seat's definition says `names` is (design 08
|
||||
// sign-in-page C-6, DQ-12): never a string a caller must separate by hand.
|
||||
func stringArray(description string) map[string]any {
|
||||
return map[string]any{"type": "array", "items": map[string]any{"type": "string"}, "description": description}
|
||||
}
|
||||
|
||||
// arrayArg reads an argument declared with stringArray: a JSON array of strings, empty for anything else
|
||||
// (never comma- or space-split, unlike Split).
|
||||
func arrayArg(a map[string]any, k string) []string {
|
||||
list, _ := a[k].([]any)
|
||||
out := make([]string, 0, len(list))
|
||||
for _, v := range list {
|
||||
if s, ok := v.(string); ok {
|
||||
out = append(out, s)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// 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 {
|
||||
@@ -81,8 +100,8 @@ func tools(r *Resolver, history string) []stdio.Tool {
|
||||
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
|
||||
answer := r.RoutesAnswer(routes, ReadHistory(history))
|
||||
return &answer, 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 "+
|
||||
@@ -101,7 +120,7 @@ func tools(r *Resolver, history string) []stdio.Tool {
|
||||
"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"])) }),
|
||||
map[string]any{"names": stringArray("the host names, at most 8; an empty array clears them")},
|
||||
func(a map[string]any) (any, error) { return r.SetUplinkNames(ctx, arrayArg(a, "names")) }),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,8 +27,27 @@ import (
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
noderesolver "git.novox.be/novox/mesh-networking/modules/mesh-connectivity-contracts/go/seats/noderesolver"
|
||||
)
|
||||
|
||||
// rfc3339 is a time as the generated contract's answers hold one, RFC 3339, empty for the zero time
|
||||
// (design 08 sign-in-page C-6, DQ-12).
|
||||
func rfc3339(t time.Time) string {
|
||||
if t.IsZero() {
|
||||
return ""
|
||||
}
|
||||
return t.UTC().Format(time.RFC3339)
|
||||
}
|
||||
|
||||
// rfc3339Ptr is the same for a time that may not have happened yet.
|
||||
func rfc3339Ptr(t *time.Time) string {
|
||||
if t == nil {
|
||||
return ""
|
||||
}
|
||||
return rfc3339(*t)
|
||||
}
|
||||
|
||||
// Runner runs one command — as root when it changes something — and answers what it printed.
|
||||
type Runner func(ctx context.Context, name string, args ...string) (string, error)
|
||||
|
||||
@@ -288,10 +307,10 @@ func (r *Resolver) Routes(ctx context.Context) (*Routes, error) {
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// UplinkNamesAnswer is `routes`'s `uplink_names` field (design 08 sign-in-page §2, §9; DQ-3): the applied
|
||||
// list's names that resolved holds on that link now, never the file's, the link, and resolved's refusal or
|
||||
// empty.
|
||||
func (r *Resolver) UplinkNamesAnswer(routes *Routes) map[string]any {
|
||||
// UplinkNamesField is `routes`'s `uplink_names` field, the generated contract's own type (design 08
|
||||
// sign-in-page §2, §9, C-6; DQ-3, DQ-12): the applied list's names that resolved holds on that link now,
|
||||
// never the file's, the link, and resolved's refusal or empty.
|
||||
func (r *Resolver) UplinkNamesField(routes *Routes) noderesolver.UplinkNames {
|
||||
applied := ReadUplinkApplied(r.UplinkNamesAppliedFile)
|
||||
held := []string{}
|
||||
for _, l := range routes.Links {
|
||||
@@ -308,14 +327,58 @@ func (r *Resolver) UplinkNamesAnswer(routes *Routes) map[string]any {
|
||||
}
|
||||
}
|
||||
}
|
||||
return map[string]any{"names": held, "link": applied.Link, "refused": applied.Refused}
|
||||
return noderesolver.UplinkNames{Names: held, Link: applied.Link, Refused: applied.Refused}
|
||||
}
|
||||
|
||||
// MeshResolversAnswer is `routes`'s `mesh_resolvers` field (design 08 sign-in-page §9; S-N5): whether the
|
||||
// mesh's resolvers are silent now, by ADR 0688's guard, and since when.
|
||||
func (r *Resolver) MeshResolversAnswer() map[string]any {
|
||||
// MeshResolversField is `routes`'s `mesh_resolvers` field, the generated contract's own type (design 08
|
||||
// sign-in-page §9, C-6; S-N5, DQ-12): whether the mesh's resolvers are silent now, by ADR 0688's guard, and
|
||||
// since when.
|
||||
func (r *Resolver) MeshResolversField() noderesolver.MeshResolvers {
|
||||
s := ReadFallback(r.FallbackFile)
|
||||
return map[string]any{"silent": s.Silent, "since": s.Since}
|
||||
return noderesolver.MeshResolvers{Silent: s.Silent, Since: rfc3339(s.Since)}
|
||||
}
|
||||
|
||||
// ScopeField is one scope as the generated contract answers it (design 08 sign-in-page C-6, DQ-12).
|
||||
func ScopeField(s Scope) noderesolver.Scope {
|
||||
return noderesolver.Scope{Link: s.Link, Servers: orEmpty(s.Servers), Domains: orEmpty(s.Domains),
|
||||
DefaultRoute: s.DefaultRoute != nil && *s.DefaultRoute}
|
||||
}
|
||||
|
||||
// ScopeFields is every scope as the generated contract answers them.
|
||||
func ScopeFields(ss []Scope) []noderesolver.Scope {
|
||||
out := make([]noderesolver.Scope, len(ss))
|
||||
for i, s := range ss {
|
||||
out[i] = ScopeField(s)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// OutsideWriteField is one outside write as the generated contract answers it.
|
||||
func OutsideWriteField(d Displacement) noderesolver.OutsideWrite {
|
||||
return noderesolver.OutsideWrite{ID: d.ID, At: rfc3339(d.At), Writer: d.Writer, TakenBy: d.TakenBy,
|
||||
TakenAt: rfc3339Ptr(d.TakenAt), Ended: rfc3339Ptr(d.Ended), How: d.How}
|
||||
}
|
||||
|
||||
// OutsideWriteFields is every outside write as the generated contract answers them.
|
||||
func OutsideWriteFields(ds []Displacement) []noderesolver.OutsideWrite {
|
||||
out := make([]noderesolver.OutsideWrite, len(ds))
|
||||
for i, d := range ds {
|
||||
out[i] = OutsideWriteField(d)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// RoutesAnswer is `routes`'s whole answer, the generated contract's own type (design 08 sign-in-page §2,
|
||||
// §5, §9, C-6; DQ-12): what this machine's own resolver sends where, the resolver file's outside writes,
|
||||
// and the uplink's names and the mesh's fallback.
|
||||
func (r *Resolver) RoutesAnswer(routes *Routes, outside []Displacement) noderesolver.RoutesAnswer {
|
||||
return noderesolver.RoutesAnswer{
|
||||
Mesh: ScopeField(routes.Mesh),
|
||||
Links: ScopeFields(routes.Links),
|
||||
OutsideWrites: OutsideWriteFields(outside),
|
||||
UplinkNames: r.UplinkNamesField(routes),
|
||||
MeshResolvers: r.MeshResolversField(),
|
||||
}
|
||||
}
|
||||
|
||||
// withoutMarked is domains without those marked (design 08 sign-in-page DMC-2).
|
||||
@@ -337,12 +400,8 @@ func orEmpty(s []string) []string {
|
||||
}
|
||||
|
||||
// Routed is what a route did.
|
||||
type Routed struct {
|
||||
Link string `json:"link"`
|
||||
Servers []string `json:"servers"`
|
||||
Domains []string `json:"domains"`
|
||||
Said string `json:"said"`
|
||||
}
|
||||
// Routed is `route`'s answer, the generated contract's own type (design 08 sign-in-page C-6, DQ-12).
|
||||
type Routed = noderesolver.RouteAnswer
|
||||
|
||||
var (
|
||||
linkName = regexp.MustCompile(`^[A-Za-z0-9_.:@-]{1,15}$`)
|
||||
@@ -501,12 +560,17 @@ func (r *Resolver) HostNames(given []string) ([]string, error) {
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// WriteUplinkNamesBound is how long the names file's write (install, then mv) is given before it is
|
||||
// abandoned (design 08 sign-in-page §6, C-8): a stated constant, since nothing here says it is a setting
|
||||
// the machine differs on.
|
||||
const WriteUplinkNamesBound = 15 * time.Second
|
||||
|
||||
// writeUplinkNamesFile writes UplinkNamesFile whole (design 08 sign-in-page §6): `sudo -n install -m 0644
|
||||
// -o root -g root /dev/stdin <file>.new` with the lines on its input, then `sudo -n mv -f <file>.new
|
||||
// <file>`, so a reader never sees it half-written. As root (the guard never calls this; only the verb,
|
||||
// over the bus, does), both run unescalated.
|
||||
func (r *Resolver) writeUplinkNamesFile(ctx context.Context, content []byte) error {
|
||||
ctx, cancel := context.WithTimeout(ctx, 15*time.Second)
|
||||
ctx, cancel := context.WithTimeout(ctx, WriteUplinkNamesBound)
|
||||
defer cancel()
|
||||
newPath := r.UplinkNamesFile + ".new"
|
||||
program, args := escalated(os.Getuid(), "install", []string{"-m", "0644", "-o", "root", "-g", "root", "/dev/stdin", newPath})
|
||||
@@ -524,18 +588,10 @@ func (r *Resolver) writeUplinkNamesFile(ctx context.Context, content []byte) err
|
||||
return err
|
||||
}
|
||||
|
||||
// UplinkNames is `uplink-names`'s answer (design 08 sign-in-page §2): the names now given, the link they
|
||||
// are answered over now (empty when no link carries a default route), and when the list was set.
|
||||
type UplinkNames struct {
|
||||
Names []string `json:"names"`
|
||||
Link string `json:"link"`
|
||||
SetAt time.Time `json:"set_at"`
|
||||
}
|
||||
|
||||
// SetUplinkNames writes the host names the uplink's holder gives for its network's own check, through
|
||||
// UplinkNamesFile (§6), and answers them with the link carrying the default route now (§2, DQ-2): empty
|
||||
// when there is no such link.
|
||||
func (r *Resolver) SetUplinkNames(ctx context.Context, given []string) (*UplinkNames, error) {
|
||||
// UplinkNamesFile (§6), and answers them with the generated contract's own type (C-6, DQ-12): the link
|
||||
// carrying the default route now (§2, DQ-2), empty when there is no such link, and when the list was set.
|
||||
func (r *Resolver) SetUplinkNames(ctx context.Context, given []string) (*noderesolver.UplinkNamesAnswer, error) {
|
||||
names, err := r.HostNames(given)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -551,7 +607,7 @@ func (r *Resolver) SetUplinkNames(ctx context.Context, given []string) (*UplinkN
|
||||
if routes, rerr := r.Routes(ctx); rerr == nil {
|
||||
link, _ = r.DefaultRouteLink(ctx, routes)
|
||||
}
|
||||
return &UplinkNames{Names: names, Link: link, SetAt: r.Now()}, nil
|
||||
return &noderesolver.UplinkNamesAnswer{Names: names, Link: link, SetAt: rfc3339(r.Now())}, nil
|
||||
}
|
||||
|
||||
// ReadUplinkNames reads the host names `uplink-names` last wrote, one a line; empty before it has.
|
||||
@@ -694,11 +750,8 @@ func (r *Resolver) Route(ctx context.Context, link string, domains, servers []st
|
||||
Said: fmt.Sprintf("%d domains go to %d servers over %s; every other name to the mesh's resolvers", len(ds), len(ss), link)}, nil
|
||||
}
|
||||
|
||||
// Unrouted is what taking a route away did.
|
||||
type Unrouted struct {
|
||||
Link string `json:"link"`
|
||||
Said string `json:"said"`
|
||||
}
|
||||
// Unrouted is `unroute`'s answer, the generated contract's own type (design 08 sign-in-page C-6, DQ-12).
|
||||
type Unrouted = noderesolver.UnrouteAnswer
|
||||
|
||||
// Unroute takes one link's route away. A link that has gone has nothing to take away.
|
||||
func (r *Resolver) Unroute(ctx context.Context, link string) (*Unrouted, error) {
|
||||
|
||||
@@ -202,7 +202,7 @@ func TestUplinkNamesWritesTheFileAndAnswersThem(t *testing.T) {
|
||||
if strings.Join(got.Names, " ") != "check.example check2.example" {
|
||||
t.Errorf("the answer names %v", got.Names)
|
||||
}
|
||||
if got.SetAt.IsZero() {
|
||||
if got.SetAt == "" {
|
||||
t.Error("set_at was not stamped")
|
||||
}
|
||||
raw, err := os.ReadFile(r.UplinkNamesFile)
|
||||
|
||||
@@ -2,7 +2,10 @@ module systemd-resolved
|
||||
|
||||
go 1.26.0
|
||||
|
||||
require git.novox.be/novox/mesh-sdk/go v0.0.0-00010101000000-000000000000
|
||||
require (
|
||||
git.novox.be/novox/mesh-networking/modules/mesh-connectivity-contracts/go/seats/noderesolver v0.0.0-00010101000000-000000000000
|
||||
git.novox.be/novox/mesh-sdk/go v0.0.0-00010101000000-000000000000
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/klauspost/compress v1.20.1 // indirect
|
||||
|
||||
Reference in New Issue
Block a user