systemd-resolved: serve uplink-names, writing the host names file (issue 960)
The verb checks each host name (root, wildcard, trailing dot, a label over 63 bytes, the mesh's own domain or under it, more than 8), writes /var/lib/node-resolver/uplink-names whole through sudo -n install then sudo -n mv -f, and answers the names with the link carrying the default route (the lowest-metric kernel default route that is neither routed nor the private network's interface) and when they were set. Builds: design 08 sign-in-page §2, §5, §6
This commit is contained in:
@@ -2,6 +2,7 @@ package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@@ -10,13 +11,21 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// fakeResolved answers resolvectl as systemd-resolved would, and records what was asked of it.
|
||||
// fakeResolved answers resolvectl, and `ip route show default`, as the machine would, and records what was
|
||||
// asked of it.
|
||||
type fakeResolved struct {
|
||||
dns, domain, defaults string
|
||||
ip string
|
||||
changed [][]string
|
||||
}
|
||||
|
||||
func (f *fakeResolved) read(_ context.Context, name string, args ...string) (string, error) {
|
||||
if name == "ip" {
|
||||
if len(args) > 0 && args[0] == "-6" {
|
||||
return "", nil
|
||||
}
|
||||
return f.ip, nil
|
||||
}
|
||||
switch args[0] {
|
||||
case "dns":
|
||||
return f.dns, nil
|
||||
@@ -46,7 +55,14 @@ func aMachine(t *testing.T, f *fakeResolved, links ...string) *Resolver {
|
||||
if err := os.WriteFile(suffix, []byte("internal\n"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return &Resolver{Change: f.change, Read: f.read, NetDir: filepath.Join(dir, "net"), SuffixFile: suffix}
|
||||
return &Resolver{Change: f.change, Read: f.read, NetDir: filepath.Join(dir, "net"), SuffixFile: suffix,
|
||||
PrivateInterfaceFile: filepath.Join(dir, "private-interface"),
|
||||
UplinkNamesFile: filepath.Join(dir, "uplink-names"),
|
||||
UplinkNamesAppliedFile: filepath.Join(dir, "uplink-names.applied"),
|
||||
Now: func() time.Time { return time.Date(2026, 11, 24, 12, 0, 0, 0, time.UTC) },
|
||||
WriteUplinkNames: func(_ context.Context, content []byte) error {
|
||||
return os.WriteFile(filepath.Join(dir, "uplink-names"), content, 0o644)
|
||||
}}
|
||||
}
|
||||
|
||||
// A route sends the domains to the servers over the link, and only them: the link is first told it is
|
||||
@@ -173,6 +189,129 @@ func TestChangesAreEscalatedOnlyWhenNotRoot(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// Given names, uplink-names writes the file one a line and answers them (design 08 sign-in-page §2, §6;
|
||||
// R-1).
|
||||
func TestUplinkNamesWritesTheFileAndAnswersThem(t *testing.T) {
|
||||
f := &fakeResolved{}
|
||||
r := aMachine(t, f, "tun0")
|
||||
got, err := r.SetUplinkNames(context.Background(), []string{"Check.Example", "check2.example"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if strings.Join(got.Names, " ") != "check.example check2.example" {
|
||||
t.Errorf("the answer names %v", got.Names)
|
||||
}
|
||||
if got.SetAt.IsZero() {
|
||||
t.Error("set_at was not stamped")
|
||||
}
|
||||
raw, err := os.ReadFile(r.UplinkNamesFile)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if string(raw) != "check.example\ncheck2.example\n" {
|
||||
t.Errorf("the file holds %q", raw)
|
||||
}
|
||||
}
|
||||
|
||||
// An empty list empties the file (design 08 sign-in-page §2; R-2).
|
||||
func TestAnEmptyListEmptiesTheUplinkNamesFile(t *testing.T) {
|
||||
f := &fakeResolved{}
|
||||
r := aMachine(t, f, "tun0")
|
||||
if _, err := r.SetUplinkNames(context.Background(), []string{"check.example"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got, err := r.SetUplinkNames(context.Background(), nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(got.Names) != 0 {
|
||||
t.Errorf("the answer names %v, not empty", got.Names)
|
||||
}
|
||||
raw, err := os.ReadFile(r.UplinkNamesFile)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if string(raw) != "" {
|
||||
t.Errorf("the file holds %q, not emptied", raw)
|
||||
}
|
||||
}
|
||||
|
||||
// Each refusal of §2 is answered naming the name and the reason, and writes nothing (design 08
|
||||
// sign-in-page §2; acceptance test 4; R-3).
|
||||
func TestEachRefusalOfUplinkNamesIsNamedAndWritesNothing(t *testing.T) {
|
||||
for name, c := range map[string]struct{ given, says string }{
|
||||
"the root": {".", "root"},
|
||||
"empty string": {"", "root"},
|
||||
"a wildcard": {"*.example", "wildcard"},
|
||||
"a trailing dot": {"check.example.", "trailing dot"},
|
||||
"a label over 63": {strings.Repeat("a", 64) + ".example", "label over 63 bytes"},
|
||||
"the mesh's domain": {"internal", "mesh's own domain"},
|
||||
"under it": {"check.internal", "mesh's own domain"},
|
||||
"not a host name": {"a b/c", "not a host name"},
|
||||
} {
|
||||
f := &fakeResolved{}
|
||||
r := aMachine(t, f, "tun0")
|
||||
_, err := r.SetUplinkNames(context.Background(), []string{c.given})
|
||||
if err == nil || !strings.Contains(err.Error(), c.says) {
|
||||
t.Errorf("%s: %v", name, err)
|
||||
}
|
||||
if _, statErr := os.Stat(r.UplinkNamesFile); statErr == nil {
|
||||
t.Errorf("%s: the file was written: %s", name, contentOf(t, r.UplinkNamesFile))
|
||||
}
|
||||
}
|
||||
f := &fakeResolved{}
|
||||
r := aMachine(t, f, "tun0")
|
||||
nine := make([]string, 9)
|
||||
for i := range nine {
|
||||
nine[i] = fmt.Sprintf("check%d.example", i)
|
||||
}
|
||||
_, err := r.SetUplinkNames(context.Background(), nine)
|
||||
if err == nil || !strings.Contains(err.Error(), "at most 8") {
|
||||
t.Errorf("more than 8 names: %v", err)
|
||||
}
|
||||
if _, statErr := os.Stat(r.UplinkNamesFile); statErr == nil {
|
||||
t.Error("more than 8 names: the file was written")
|
||||
}
|
||||
}
|
||||
|
||||
// The link carrying the default route is the lowest-metric one that is neither routed (a VPN's) nor the
|
||||
// private network's; none when there is none (design 08 sign-in-page §7, §9; DQ-1, DQ-2).
|
||||
func TestDefaultRouteLinkIsTheLowestMetricOneThatIsNeitherRoutedNorPrivate(t *testing.T) {
|
||||
f := &fakeResolved{}
|
||||
r := aMachine(t, f, "tun0", "wlan0", "mesh0")
|
||||
if err := os.WriteFile(r.PrivateInterfaceFile, []byte("mesh0\n"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
f.ip = "default via 10.0.0.1 dev wlan0 metric 600\ndefault via 10.1.0.1 dev mesh0 metric 50\n"
|
||||
f.dns = "Link 7 (tun0): 10.9.0.1\n"
|
||||
f.domain = "Link 7 (tun0): ~corp.example\n"
|
||||
routes, err := r.Routes(context.Background())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
link, err := r.DefaultRouteLink(context.Background(), routes)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if link != "wlan0" {
|
||||
t.Errorf("the link is %q, not wlan0 (mesh0 is the private interface)", link)
|
||||
}
|
||||
// A link `route` routed is also never chosen.
|
||||
f.ip = "default via 10.0.0.1 dev tun0 metric 10\ndefault via 10.2.0.1 dev wlan0 metric 600\n"
|
||||
link, err = r.DefaultRouteLink(context.Background(), routes)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if link != "wlan0" {
|
||||
t.Errorf("the link is %q, not wlan0 (tun0 is routed)", link)
|
||||
}
|
||||
// None when there is none.
|
||||
f.ip = "default via 10.0.0.1 dev tun0 metric 10\ndefault via 10.1.0.1 dev mesh0 metric 50\n"
|
||||
if link, err = r.DefaultRouteLink(context.Background(), routes); err != nil || link != "" {
|
||||
t.Errorf("the link is %q, want none", link)
|
||||
}
|
||||
}
|
||||
|
||||
// One of the mesh's resolvers answers when it says anything, a name not found included; one that is silent
|
||||
// does not, and the guard does not wait on it past the config value answer-within, read from the file the
|
||||
// mesh renders, or a second before it has. (1981/AC3)
|
||||
|
||||
Reference in New Issue
Block a user