systemd-resolved: the guard applies the uplink's names every round (issue 960)
Each round it finds the link carrying the default route (the lowest-metric kernel default route that is neither one route routed nor the private network's interface), gives it the names from uplink-names's file plus its own search and routing domains, and takes them off the link they were on before when that differs. What it applied is kept in uplink-names.applied, read at its own start so a restart never infers which link to take them off, and a resolved refusal is journalled once per change, not every round. The guard's own names are left out of whether a link is judged `route` routed, so the fallback still gives a link holding only them its default route back while the mesh's resolvers are silent (ADR 0688 stands). Builds: design 08 sign-in-page §7, §8
This commit is contained in:
@@ -4,6 +4,8 @@ import (
|
||||
"bufio"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@@ -20,6 +22,13 @@ const vpnFile = "# Dynamic resolv.conf(5) file for glibc resolver(3) generated b
|
||||
|
||||
// aGuardedMachine is a guard over a temporary /etc and /run, with a clock the test moves.
|
||||
func aGuardedMachine(t *testing.T) (*Guard, *time.Time, *fakeResolved) {
|
||||
t.Helper()
|
||||
g, now, f, _ := aGuardedMachineWithLinks(t, "tun0")
|
||||
return g, now, f
|
||||
}
|
||||
|
||||
// aGuardedMachineWithLinks is the same, over the links named, and answers the resolver too.
|
||||
func aGuardedMachineWithLinks(t *testing.T, links ...string) (*Guard, *time.Time, *fakeResolved, *Resolver) {
|
||||
t.Helper()
|
||||
dir := t.TempDir()
|
||||
for _, d := range []string{"etc/node-resolver", "run"} {
|
||||
@@ -31,10 +40,11 @@ func aGuardedMachine(t *testing.T) (*Guard, *time.Time, *fakeResolved) {
|
||||
write(t, filepath.Join(dir, "etc/resolv.conf"), meshFile)
|
||||
now := time.Date(2026, 10, 7, 12, 0, 0, 0, time.UTC)
|
||||
f := &fakeResolved{}
|
||||
r := aMachine(t, f, links...)
|
||||
g := &Guard{Path: filepath.Join(dir, "etc/resolv.conf"), KeptPath: filepath.Join(dir, "etc/node-resolver/resolv.conf"),
|
||||
Dir: filepath.Join(dir, "run"), Hold: Hold, Now: func() time.Time { return now },
|
||||
Resolver: aMachine(t, f, "tun0"), Log: t.Logf, wake: make(chan struct{}, 1)}
|
||||
return g, &now, f
|
||||
Resolver: r, Log: t.Logf, wake: make(chan struct{}, 1)}
|
||||
return g, &now, f, r
|
||||
}
|
||||
|
||||
func write(t *testing.T, path, content string) {
|
||||
@@ -286,6 +296,119 @@ func TestTheNetworksServersAnswerWhileTheMeshsResolversAreSilent(t *testing.T) {
|
||||
step(g, routed, "Link 2 (wlan0): yes\nLink 7 (tun0): no\n", "resolvectl default-route wlan0 false")
|
||||
}
|
||||
|
||||
// The uplink's names are answered over the link carrying the default route, and move with the route: the
|
||||
// old link loses them and keeps its own search domain (design 08 sign-in-page §7, §9; R-4, R-5).
|
||||
func TestUplinkNamesMoveWithTheDefaultRoute(t *testing.T) {
|
||||
g, _, f, r := aGuardedMachineWithLinks(t, "wlan0", "wlan1")
|
||||
f.dns = "Link 2 (wlan0): 192.168.1.1\n"
|
||||
f.domain = "Link 2 (wlan0): lan\n"
|
||||
f.ip = "default via 10.0.0.1 dev wlan0 metric 100\n"
|
||||
if err := os.WriteFile(r.UplinkNamesFile, []byte("check.example\n"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
g.KeepUplinkNames(context.Background())
|
||||
if len(f.changed) != 1 || strings.Join(f.changed[0], " ") != "resolvectl domain wlan0 lan ~check.example" {
|
||||
t.Fatalf("wlan0 was given %v", f.changed)
|
||||
}
|
||||
if g.applied.Link != "wlan0" || strings.Join(g.applied.Names, ",") != "check.example" {
|
||||
t.Fatalf("applied is %+v", g.applied)
|
||||
}
|
||||
applied := contentOf(t, r.UplinkNamesAppliedFile)
|
||||
if !strings.Contains(applied, "wlan0") || !strings.Contains(applied, "check.example") {
|
||||
t.Errorf("uplink-names.applied does not say so: %s", applied)
|
||||
}
|
||||
// The route moves to wlan1: wlan0, as resolved now holds it, keeps "lan" and the uplink's name.
|
||||
f.domain = "Link 2 (wlan0): lan ~check.example\nLink 3 (wlan1):\n"
|
||||
f.ip = "default via 10.1.0.1 dev wlan1 metric 50\n"
|
||||
f.changed = nil
|
||||
g.KeepUplinkNames(context.Background())
|
||||
if len(f.changed) != 2 {
|
||||
t.Fatalf("moving the route changed %v", f.changed)
|
||||
}
|
||||
if strings.Join(f.changed[0], " ") != "resolvectl domain wlan0 lan" {
|
||||
t.Errorf("wlan0 lost the uplink's name but not its own: %v", f.changed[0])
|
||||
}
|
||||
if strings.Join(f.changed[1], " ") != "resolvectl domain wlan1 ~check.example" {
|
||||
t.Errorf("wlan1 was not given the uplink's name: %v", f.changed[1])
|
||||
}
|
||||
if g.applied.Link != "wlan1" {
|
||||
t.Errorf("applied is now %+v", g.applied)
|
||||
}
|
||||
}
|
||||
|
||||
// A guard restarted with an applied file takes the uplink's names off the link it names, never inferring
|
||||
// which (design 08 sign-in-page §8, §9; R-6).
|
||||
func TestARestartedGuardTakesTheUplinkNamesOffTheLinkItNames(t *testing.T) {
|
||||
g, _, f, r := aGuardedMachineWithLinks(t, "wlan0", "wlan1")
|
||||
f.dns = "Link 2 (wlan0): 192.168.1.1\n"
|
||||
f.domain = "Link 2 (wlan0): lan ~check.example\nLink 3 (wlan1):\n"
|
||||
f.ip = "default via 10.1.0.1 dev wlan1 metric 50\n" // the route has already moved to wlan1
|
||||
applied := UplinkApplied{Names: []string{"check.example"}, Link: "wlan0", At: time.Now()}
|
||||
raw, _ := json.Marshal(applied)
|
||||
if err := os.WriteFile(r.UplinkNamesAppliedFile, raw, 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(r.UplinkNamesFile, []byte("check.example\n"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
g.applied = ReadUplinkApplied(r.UplinkNamesAppliedFile) // what the guard does at its start
|
||||
g.KeepUplinkNames(context.Background())
|
||||
if len(f.changed) != 2 || strings.Join(f.changed[0], " ") != "resolvectl domain wlan0 lan" ||
|
||||
strings.Join(f.changed[1], " ") != "resolvectl domain wlan1 ~check.example" {
|
||||
t.Errorf("the restarted guard did %v", f.changed)
|
||||
}
|
||||
}
|
||||
|
||||
// A link holding only the uplink's names is still given its default route back while the mesh's resolvers
|
||||
// are silent: the guard's own names are left out of whether it is judged `route` routed (design 08
|
||||
// sign-in-page §7, §9; DMC-2; issue 1981's AC1; R-7).
|
||||
func TestALinkOfOnlyUplinkNamesStillGetsItsDefaultRouteBack(t *testing.T) {
|
||||
g, _, f, r := aGuardedMachineWithLinks(t, "tun0")
|
||||
g.Resolver.Answers = func(context.Context, string) bool { return false } // the mesh is silent
|
||||
f.dns = "Global: 10.42.0.1\nLink 7 (tun0): 10.9.0.1\n"
|
||||
f.domain = "Global: ~.\nLink 7 (tun0): ~check.example\n" // only the uplink's name, no search domain
|
||||
f.defaults = "Link 7 (tun0): no\n"
|
||||
applied := UplinkApplied{Names: []string{"check.example"}, Link: "tun0", At: time.Now()}
|
||||
raw, _ := json.Marshal(applied)
|
||||
if err := os.WriteFile(r.UplinkNamesAppliedFile, raw, 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
g.KeepRoutes(context.Background())
|
||||
if len(f.changed) != 1 || strings.Join(f.changed[0], " ") != "resolvectl default-route tun0 true" {
|
||||
t.Errorf("the fallback did %v, want tun0 given its default route back", f.changed)
|
||||
}
|
||||
}
|
||||
|
||||
// A refusal by resolved is journalled once per change, never every round (design 08 sign-in-page §8; R-8).
|
||||
func TestARefusalByResolvedIsJournalledOncePerChange(t *testing.T) {
|
||||
g, _, f, r := aGuardedMachineWithLinks(t, "wlan0")
|
||||
f.dns = "Link 2 (wlan0): 192.168.1.1\n"
|
||||
f.ip = "default via 10.0.0.1 dev wlan0 metric 100\n"
|
||||
if err := os.WriteFile(r.UplinkNamesFile, []byte("check.example\n"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
refuse := true
|
||||
g.Resolver.Change = func(ctx context.Context, name string, args ...string) (string, error) {
|
||||
if refuse && len(args) > 0 && args[0] == "domain" {
|
||||
return "", errors.New("refused: not a domain mesh owns")
|
||||
}
|
||||
return f.change(ctx, name, args...)
|
||||
}
|
||||
var said []string
|
||||
g.Log = func(format string, a ...any) { said = append(said, fmt.Sprintf(format, a...)) }
|
||||
g.KeepUplinkNames(context.Background())
|
||||
g.KeepUplinkNames(context.Background()) // the file and the link are unchanged: no new refusal to say
|
||||
count := 0
|
||||
for _, s := range said {
|
||||
if strings.Contains(s, "refuses") {
|
||||
count++
|
||||
}
|
||||
}
|
||||
if count != 1 {
|
||||
t.Errorf("the refusal was journalled %d times, want once: %v", count, said)
|
||||
}
|
||||
}
|
||||
|
||||
// A network joined while the mesh's resolvers are silent keeps its default route, and loses it once one answers.
|
||||
// (1981/AC2)
|
||||
func TestANetworkJoinedWhileTheMeshIsSilentKeepsItsDefaultRoute(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user