carlos / platform Public

egress: the drop-in tests that touch the filesystem run on linux only; the portable ones read through a seam (#328)

Task 15 re-review: off linux the trusted drop-in reader refuses every
read, so the untagged claim-nudge tests and the proof's failed-read
start cases (a missing control directory read as absent) failed there.

- EgressAsserter's controlDir/dropinOwner become one seam, readDropin
  (nil is TrustedDropinReader{}.Read); the test asserters answer
  "not there" through it without touching the filesystem (noDropins),
  so TestStartTranscriptsUnchangedWithNoPolicy and
  TestEgressStartRefusedWhenTheSliceMayHoldAnAllow are portable.
- TestClaimNudgeDropinReadsAnEscapedUnit and
  TestClaimNudgeDropinHookTakesEscapedNames move to
  internal/host/claimunit_nudge_linux_test.go (//go:build linux).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev 5eb941a253ab3a672c674da6fb55d03e21a1cb7a parent 818a139
7 files changed, +102 −79
  • internal/host/claimunit_effective_test.go +0 −65
  • internal/host/claimunit_nudge_linux_test.go +80 −0
  • internal/instancectl/egress.go +5 −5
  • internal/instancectl/egress_dropin.go +5 −1
  • internal/instancectl/egress_restart_linux_test.go +4 −4
  • internal/instancectl/egress_startpaths_test.go +1 −1
  • internal/instancectl/egress_test.go +7 −3

CI failed — run details

diff --git a/internal/host/claimunit_effective_test.go b/internal/host/claimunit_effective_test.go
index e0d55d92a..b3de2ed16 100644
--- a/internal/host/claimunit_effective_test.go
+++ b/internal/host/claimunit_effective_test.go
@@ -2,10 +2,7 @@ package host
import (
"context"
- "errors"
- "io/fs"
"os"
- "path/filepath"
"slices"
"strings"
"testing"
@@ -184,65 +181,3 @@ func TestClaimPredecessorAcceptsOnlyTheEgressNudgeDropin(t *testing.T) {
})
}
}
-
-// TestClaimNudgeDropinHookTakesEscapedNames: the production hook, with
-// its root-owner check, pointed at a root-owned directory every box has
-// (/usr), reaches a dashed unit's drop-in directory and finds it absent;
-// rootstate's trusted open refused the escaped name before looking.
-//
-// Not parallel: it swaps claimNudgeReader.
-func TestClaimNudgeDropinHookTakesEscapedNames(t *testing.T) {
- old := claimNudgeReader
- t.Cleanup(func() { claimNudgeReader = old })
- claimNudgeReader = instancectl.TrustedDropinReader{ControlDir: "/usr"}
- for _, host := range []string{"shop.example", "shop-a.example"} {
- unit := instancectl.UnitName(instancectl.RoleRun, host)
- path := filepath.Join("/usr", unit+".d", "50-IPAddressAllow.conf")
- if _, err := readClaimNudgeDropin(path); !errors.Is(err, fs.ErrNotExist) {
- t.Fatalf("readClaimNudgeDropin(%s) = %v, want not found", path, err)
- }
- }
-}
-
-// TestClaimNudgeDropinReadsAnEscapedUnit (Task 15 review): the claim
-// checks read root's nudge drop-in through instancectl's trusted reader,
-// which takes a unit name as systemd escapes it. rootstate's trusted open
-// refused the `\` in every unit of a host with a dash, so a claim start
-// of such a unit failed once root had nudged it. The trust checks
-// themselves are instancectl's TestTrustedDropinReaderEscapedNames.
-func TestClaimNudgeDropinReadsAnEscapedUnit(t *testing.T) {
- t.Parallel()
- unit := instancectl.UnitName(instancectl.RoleRun, "shop-a.example.com")
- if !strings.Contains(unit, `\x2d`) {
- t.Fatalf("fixture: %s is not an escaped unit name", unit)
- }
- path := claimNudgeDropinPath(unit)
- if want := filepath.Join(instancectl.RuntimeControlDir, unit+".d", "50-IPAddressAllow.conf"); path != want {
- t.Fatalf("claimNudgeDropinPath = %s, want %s", path, want)
- }
-
- // The reader readClaimNudgeDropin uses, over a control directory the
- // test owns, reads the nudge systemd writes for the escaped unit.
- root, err := filepath.EvalSymlinks(t.TempDir())
- if err != nil {
- t.Fatal(err)
- }
- dir := filepath.Join(root, "system.control")
- if err := os.MkdirAll(filepath.Join(dir, unit+".d"), 0o755); err != nil {
- t.Fatal(err)
- }
- nudge := "# This is a drop-in unit file extension, created via \"systemctl set-property\"\n# or an equivalent operation. Do not edit.\n[Service]\nIPAddressAllow=\n"
- local := filepath.Join(dir, unit+".d", "50-IPAddressAllow.conf")
- if err := os.WriteFile(local, []byte(nudge), 0o644); err != nil {
- t.Fatal(err)
- }
- body, err := instancectl.TrustedDropinReader{ControlDir: dir, Owner: uint32(os.Getuid())}.ReadPath(local)
- if err != nil || !claimNudgeDropinBody(body) {
- t.Fatalf("the nudge for %s: %q, %v; want it read and accepted", unit, body, err)
- }
-
- // The production hook reads only under systemd's control directory.
- if _, err := readClaimNudgeDropin(local); !errors.Is(err, os.ErrPermission) {
- t.Fatalf("readClaimNudgeDropin(%s) = %v, want a refusal outside %s", local, err, instancectl.RuntimeControlDir)
- }
-}
diff --git a/internal/host/claimunit_nudge_linux_test.go b/internal/host/claimunit_nudge_linux_test.go
new file mode 100644
index 000000000..2cc0d5f5d
--- /dev/null
+++ b/internal/host/claimunit_nudge_linux_test.go
@@ -0,0 +1,80 @@
+//go:build linux
+
+package host
+
+// The claim checks' nudge read on a real filesystem (Task 15 review): the
+// trusted reader is linux's (instancectl.TrustedDropinReader); elsewhere it
+// refuses every read.
+
+import (
+ "errors"
+ "io/fs"
+ "os"
+ "path/filepath"
+ "strings"
+ "testing"
+
+ "github.com/carlosframework/platform/internal/instancectl"
+)
+
+// TestClaimNudgeDropinHookTakesEscapedNames: the production hook, with
+// its root-owner check, pointed at a root-owned directory every box has
+// (/usr), reaches a dashed unit's drop-in directory and finds it absent;
+// rootstate's trusted open refused the escaped name before looking.
+//
+// Not parallel: it swaps claimNudgeReader.
+func TestClaimNudgeDropinHookTakesEscapedNames(t *testing.T) {
+ old := claimNudgeReader
+ t.Cleanup(func() { claimNudgeReader = old })
+ claimNudgeReader = instancectl.TrustedDropinReader{ControlDir: "/usr"}
+ for _, host := range []string{"shop.example", "shop-a.example"} {
+ unit := instancectl.UnitName(instancectl.RoleRun, host)
+ path := filepath.Join("/usr", unit+".d", "50-IPAddressAllow.conf")
+ if _, err := readClaimNudgeDropin(path); !errors.Is(err, fs.ErrNotExist) {
+ t.Fatalf("readClaimNudgeDropin(%s) = %v, want not found", path, err)
+ }
+ }
+}
+
+// TestClaimNudgeDropinReadsAnEscapedUnit (Task 15 review): the claim
+// checks read root's nudge drop-in through instancectl's trusted reader,
+// which takes a unit name as systemd escapes it. rootstate's trusted open
+// refused the `\` in every unit of a host with a dash, so a claim start
+// of such a unit failed once root had nudged it. The trust checks
+// themselves are instancectl's TestTrustedDropinReaderEscapedNames.
+func TestClaimNudgeDropinReadsAnEscapedUnit(t *testing.T) {
+ t.Parallel()
+ unit := instancectl.UnitName(instancectl.RoleRun, "shop-a.example.com")
+ if !strings.Contains(unit, `\x2d`) {
+ t.Fatalf("fixture: %s is not an escaped unit name", unit)
+ }
+ path := claimNudgeDropinPath(unit)
+ if want := filepath.Join(instancectl.RuntimeControlDir, unit+".d", "50-IPAddressAllow.conf"); path != want {
+ t.Fatalf("claimNudgeDropinPath = %s, want %s", path, want)
+ }
+
+ // The reader readClaimNudgeDropin uses, over a control directory the
+ // test owns, reads the nudge systemd writes for the escaped unit.
+ root, err := filepath.EvalSymlinks(t.TempDir())
+ if err != nil {
+ t.Fatal(err)
+ }
+ dir := filepath.Join(root, "system.control")
+ if err := os.MkdirAll(filepath.Join(dir, unit+".d"), 0o755); err != nil {
+ t.Fatal(err)
+ }
+ nudge := "# This is a drop-in unit file extension, created via \"systemctl set-property\"\n# or an equivalent operation. Do not edit.\n[Service]\nIPAddressAllow=\n"
+ local := filepath.Join(dir, unit+".d", "50-IPAddressAllow.conf")
+ if err := os.WriteFile(local, []byte(nudge), 0o644); err != nil {
+ t.Fatal(err)
+ }
+ body, err := instancectl.TrustedDropinReader{ControlDir: dir, Owner: uint32(os.Getuid())}.ReadPath(local)
+ if err != nil || !claimNudgeDropinBody(body) {
+ t.Fatalf("the nudge for %s: %q, %v; want it read and accepted", unit, body, err)
+ }
+
+ // The production hook reads only under systemd's control directory.
+ if _, err := readClaimNudgeDropin(local); !errors.Is(err, os.ErrPermission) {
+ t.Fatalf("readClaimNudgeDropin(%s) = %v, want a refusal outside %s", local, err, instancectl.RuntimeControlDir)
+ }
+}
diff --git a/internal/instancectl/egress.go b/internal/instancectl/egress.go
index b381b5e28..d869b8935 100644
--- a/internal/instancectl/egress.go
+++ b/internal/instancectl/egress.go
@@ -126,11 +126,11 @@ type EgressAsserter struct {
// an allow. A start path whose read fails with nothing wanted refuses
// only on such a slice, or one owing a change (mayHold, AssertStart).
heldSome map[string]bool
- // controlDir is systemd's runtime control directory ("" is
- // RuntimeControlDir) and dropinOwner the owner its drop-ins must have
- // (root); tests point both at a tree of their own.
- controlDir string
- dropinOwner uint32
+ // readDropin reads a drop-in under systemd's runtime control
+ // directory; nil is TrustedDropinReader{}.Read (root's files under
+ // RuntimeControlDir). Tests answer for it, or point a reader at a tree
+ // of their own.
+ readDropin func(sub, name string) ([]byte, error)
}
// egressDue is what a slice still owes its running services.
diff --git a/internal/instancectl/egress_dropin.go b/internal/instancectl/egress_dropin.go
index ee1cccd32..c904813fa 100644
--- a/internal/instancectl/egress_dropin.go
+++ b/internal/instancectl/egress_dropin.go
@@ -71,7 +71,11 @@ func (r TrustedDropinReader) ReadPath(path string) ([]byte, error) {
// file), or whose content is not a [Slice] section of IPAddressAllow=
// lines, is an error: the caller refuses on it.
func (e *EgressAsserter) sliceAllowOnDisk(slice string) (bool, error) {
- body, err := TrustedDropinReader{ControlDir: e.controlDir, Owner: e.dropinOwner}.Read(slice+".d", allowDropinName)
+ read := e.readDropin
+ if read == nil {
+ read = TrustedDropinReader{}.Read
+ }
+ body, err := read(slice+".d", allowDropinName)
if errors.Is(err, fs.ErrNotExist) {
return false, nil
}
diff --git a/internal/instancectl/egress_restart_linux_test.go b/internal/instancectl/egress_restart_linux_test.go
index 438ccc95d..51cf297d2 100644
--- a/internal/instancectl/egress_restart_linux_test.go
+++ b/internal/instancectl/egress_restart_linux_test.go
@@ -656,7 +656,7 @@ func dropinBox(t *testing.T, e *EgressAsserter, slice, body string) string {
t.Fatal(err)
}
}
- e.controlDir, e.dropinOwner = dir, uint32(os.Getuid())
+ e.readDropin = TrustedDropinReader{ControlDir: dir, Owner: uint32(os.Getuid())}.Read
return dir
}
@@ -680,7 +680,7 @@ func TestSliceAllowOnDisk(t *testing.T) {
}
}},
{name: "no control directory", setup: func(t *testing.T, e *EgressAsserter, dir string) {
- e.controlDir = filepath.Join(dir, "absent")
+ e.readDropin = TrustedDropinReader{ControlDir: filepath.Join(dir, "absent"), Owner: uint32(os.Getuid())}.Read
}},
{name: "an allow, as systemd writes it", body: systemdAllow, held: true},
{name: "the empty reset", body: "[Slice]\nIPAddressAllow=\n"},
@@ -726,8 +726,8 @@ func TestSliceAllowOnDisk(t *testing.T) {
t.Fatal(err)
}
}, err: true},
- {name: "another owner", body: "[Slice]\nIPAddressAllow=\n", setup: func(t *testing.T, e *EgressAsserter, _ string) {
- e.dropinOwner = uint32(os.Getuid()) + 1 // root's files must be root's
+ {name: "another owner", body: "[Slice]\nIPAddressAllow=\n", setup: func(t *testing.T, e *EgressAsserter, dir string) {
+ e.readDropin = TrustedDropinReader{ControlDir: dir, Owner: uint32(os.Getuid()) + 1}.Read // root's files must be root's
}, err: true},
} {
t.Run(tc.name, func(t *testing.T) {
diff --git a/internal/instancectl/egress_startpaths_test.go b/internal/instancectl/egress_startpaths_test.go
index 5a88ba6aa..8ef4861c2 100644
--- a/internal/instancectl/egress_startpaths_test.go
+++ b/internal/instancectl/egress_startpaths_test.go
@@ -67,7 +67,7 @@ func startPathAsserter(systemctl func(context.Context, ...string) (string, error
// runs for a change to be applied to).
CgroupRoot: os.TempDir(),
// No runtime drop-in for any slice.
- controlDir: noControlDir,
+ readDropin: noDropins,
}
}
diff --git a/internal/instancectl/egress_test.go b/internal/instancectl/egress_test.go
index a134ebbc8..c070230c8 100644
--- a/internal/instancectl/egress_test.go
+++ b/internal/instancectl/egress_test.go
@@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"io"
+ "io/fs"
"net/http"
"net/netip"
"os"
@@ -123,12 +124,15 @@ func newEgressAsserter(ctl *egressCtl, src EgressSource) *EgressAsserter {
Logf: func(string, ...any) {},
// No runtime drop-ins: a directory that does not exist. Tests of
// what one says make their own (dropinBox).
- controlDir: noControlDir,
+ readDropin: noDropins,
}
}
-// noControlDir is a systemd runtime control directory that does not exist.
-const noControlDir = "/nonexistent-carlos-test/system.control"
+// noDropins answers every runtime drop-in read with "not there", without
+// touching the filesystem, on any platform.
+func noDropins(sub, name string) ([]byte, error) {
+ return nil, fmt.Errorf("%s/%s: %w", sub, name, fs.ErrNotExist)
+}
func showCall(host string) string { return "show -p IPAddressAllow --value " + SliceName(host) }