carlos / platform Public

instancectl: every change to a host slice's allow reaches its running services by a unit-level write (#328)

Run 20261025T173953-8a3366: once carlos-instances.slice's allow is
rewritten while a unit runs (the probe's allowResolvers, every five
minutes on every box), later host-slice changes no longer reach that
unit's filter, though the slice reads back the new list. A revoke on
the live branch (parent allow, no restart) was reported done with the
address still open. A write on the unit's own IPAddressAllow makes
systemd recompile its filter from its own and its ancestors' lists.

After every change, root makes that write (set-property --runtime
<unit> IPAddressAllow=) on each service running in the slice, read from
cgroupfs. A revoke whose nudge fails is closed by restart; a grant
whose nudge fails reports failing and is retried; what a slice owes is
recorded before the write and retried by the next Assert. The restart
stays the backstop for a revoke to empty with no parent allow. Under
host claims every revoke restarts, since a claim unit's start refuses
drop-ins it did not write. A unit holding an allow of its own is never
nudged (the write would remove it).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev ef55bb3e0b4bd73e95b0e3c5c82f48619885971b parent df526b3
5 files changed, +404 −65
  • internal/instancectl/egress.go +184 −55
  • internal/instancectl/egress_restart_linux_test.go +196 −2
  • internal/instancectl/egress_startpaths_test.go +3 −0
  • internal/instancectl/egress_sweep_linux_test.go +7 −2
  • internal/instancectl/egress_test.go +14 −6
diff --git a/internal/instancectl/egress.go b/internal/instancectl/egress.go
index 6db9db4c2..4ccf6fda7 100644
--- a/internal/instancectl/egress.go
+++ b/internal/instancectl/egress.go
@@ -17,8 +17,26 @@
// unit able to reach the address until it was restarted. So a revoke that
// empties a slice whose parent holds no allow restarts the slice's running
// units (spec §4.3: "If systemd does not apply a revoke live, a revoke
-// restarts the host's units"; closeByRestart). Every other revoke, and
-// every grant, stays live. A per-host slice's allow
+// restarts the host's units"; restartRunning).
+//
+// Run 20261025T173953-8a3366 (plan 2 Task 14's isolation witness) found
+// the wider one: once carlos-instances.slice's allow is rewritten while a
+// unit runs (the probe's allowResolvers does it every five minutes on
+// every box), later changes to the host slice's allow, grants and revokes
+// alike, no longer reach that unit's filter, though the slice reads back
+// the new list. A further parent write does not help; a write on the
+// unit's own IPAddressAllow does, because systemd then recompiles that
+// unit's filter from its own list and every ancestor's. So after every
+// change to a host slice's allow root makes that write, `systemctl
+// set-property --runtime <unit> IPAddressAllow=`, on each service running
+// in the slice (nudgeRunning; proven by the kernel proof's run
+// 20261025T131702-62b868 and this run). A revoke whose nudge fails is
+// closed by restart; a grant whose nudge fails reports failing and is
+// retried. The restart above stays the backstop for a revoke to empty
+// with no parent allow, and under host claims every revoke restarts (a
+// claim unit's start refuses drop-ins it did not write).
+//
+// A per-host slice's allow
// adds to carlos-instances.slice's resolver allow rather than replacing
// it. And `systemctl show -p IPAddressAllow --value <slice>` lists only
// the slice's own entries, so the read-back is compared as it is, with
@@ -93,12 +111,23 @@ type EgressAsserter struct {
names map[string]string // instance id -> host, for the sweep; "" is two hosts
busy map[string]*sync.Mutex // slice -> held for one whole Assert or reset
failing map[string]int // host -> sweeps in a row its Assert failed
- // restartDue holds a slice whose revoke to empty still owes its
- // running units a restart (closeByRestart failed): the next Assert of
- // the slice tries again, though the slice already reads back empty.
- restartDue map[string]bool
+ // due holds, per slice, a change of its allow that has not yet been
+ // shown to reach the services running in it (reach): recorded before
+ // the write, cleared once every running service took it, and retried
+ // by the next Assert of the slice, though the slice already reads back
+ // what is wanted.
+ due map[string]egressDue
}
+// egressDue is what a slice still owes its running services.
+type egressDue int
+
+const (
+ dueNone egressDue = iota
+ dueGrant // a change that only added addresses: a nudge
+ dueRevoke // a change that removed one: a nudge, else a restart
+)
+
// egressRestarted is the report's Detail after a revoke closed its address
// by restarting the slice's running units.
const egressRestarted = "revoked (restarted)"
@@ -150,6 +179,9 @@ func (e *EgressAsserter) Assert(ctx context.Context, host string) error {
e.remember(host, pol)
}
+ // Under host claims every write is a reset, and a claim unit's start
+ // refuses drop-ins it did not write, so its revokes restart (reach).
+ claims := pol.Closed == EgressUnderClaims
var want []netip.Prefix
if pol.Closed != "" {
if rep.State == "" {
@@ -170,12 +202,11 @@ func (e *EgressAsserter) Assert(ctx context.Context, host string) error {
// slice was seen to hold.
rep.Allowed = held.addrs()
if held.equal(want) {
- if len(want) > 0 {
- // An allow in force again: no revoke is left to close.
- e.setRestartDue(slice, false)
- }
- if len(want) == 0 && e.isRestartDue(slice) {
- if err := e.closeByRestart(ctx, slice, &rep); err != nil {
+ // A change written earlier that has not reached the running
+ // services yet is retried here.
+ if d := e.dueOf(slice); d != dueNone {
+ revoke := d == dueRevoke
+ if err := e.reach(ctx, slice, revoke, revoke && len(want) == 0, claims, &rep); err != nil {
rep.State, rep.Detail = EgressFailing, err.Error()
e.record(host, rep)
e.logf("egress: %s: %v", slice, err)
@@ -187,8 +218,6 @@ func (e *EgressAsserter) Assert(ctx context.Context, host string) error {
}
}
readBefore := readErr == nil
- // The revoke the slice is about to get removes every address it held.
- emptied := readBefore && len(want) == 0 && !held.equal(nil)
fail := func(detail string) error {
rep.State, rep.Detail = EgressFailing, detail
e.record(host, rep)
@@ -207,12 +236,15 @@ func (e *EgressAsserter) Assert(ctx context.Context, host string) error {
e.logf("egress: %s: read back: %v; writing the allow", slice, readErr)
}
- if emptied {
- // The restart this revoke may owe is recorded before the reset is
- // written: an uncertain outcome (the write answers an error after
- // it applied, or the read after it fails) must not lose it, or the
- // next Assert would find the slice empty and stop there.
- e.setRestartDue(slice, true)
+ // What this change owes the running services is recorded before it is
+ // written: an uncertain outcome (the write answers an error after it
+ // applied, or the read after it fails) must not lose it, or the next
+ // Assert would find the slice as wanted and stop there. A change that
+ // removes an address, or whose before is unknown, is a revoke.
+ if !readBefore || held.removes(want) {
+ e.setDue(slice, dueRevoke)
+ } else if e.dueOf(slice) != dueRevoke {
+ e.setDue(slice, dueGrant)
}
args := []string{"set-property", "--runtime", slice, "IPAddressAllow="}
if len(want) > 0 {
@@ -241,24 +273,111 @@ func (e *EgressAsserter) Assert(ctx context.Context, host string) error {
return fail(fmt.Sprintf("after the write the slice holds %s, not %s", held.text(), prefixesText(want)))
}
e.logf("egress: %s: allow is now %s", slice, prefixesText(want))
- if len(want) > 0 {
- e.setRestartDue(slice, false)
- } else if emptied || e.isRestartDue(slice) {
- if err := e.closeByRestart(ctx, slice, &rep); err != nil {
- return fail(err.Error())
- }
+ revoke := e.dueOf(slice) == dueRevoke
+ if err := e.reach(ctx, slice, revoke, revoke && len(want) == 0, claims, &rep); err != nil {
+ return fail(err.Error())
}
e.record(host, rep)
return nil
}
-// closeByRestart finishes a revoke that emptied slice. With an allow on
-// carlos-instances.slice the revoke is live (Task 1's kernel proof) and
-// nothing more is done. With none, systemd may leave a running unit's
-// filter as it was (run 20261025T164418-7a14fe), so each service running
-// in the slice is restarted with try-restart, and the report's Detail says
-// so. A parent that cannot be read counts as holding nothing: a restart is
-// the safe side.
+// reach makes a change of slice's allow, already written and read back,
+// reach the services running in it (run 20261025T173953-8a3366, the top
+// of this file). It clears what the slice owes when every running service
+// took the change, and otherwise keeps it owed and returns an error:
+//
+// - a revoke to empty with no allow on carlos-instances.slice (or one
+// that cannot be read), and under host claims every revoke, restarts
+// the running services (restartRunning);
+// - every other change nudges them (nudgeRunning); a revoke whose nudge
+// fails is closed by restart, a grant whose nudge fails stays owed.
+func (e *EgressAsserter) reach(ctx context.Context, slice string, revoke, emptied, claims bool, rep *EgressReport) error {
+ if revoke && claims {
+ return e.restartRunning(ctx, slice, rep)
+ }
+ if emptied {
+ parent, err := e.readBack(ctx, InstancesSlice)
+ if err != nil {
+ e.logf("egress: %s: %s: read back: %v; restarting the slice's units to close its revoke", slice, InstancesSlice, err)
+ }
+ if err != nil || parent.equal(nil) {
+ return e.restartRunning(ctx, slice, rep)
+ }
+ }
+ err := e.nudgeRunning(ctx, slice)
+ if err == nil {
+ e.setDue(slice, dueNone)
+ return nil
+ }
+ if revoke {
+ e.logf("egress: %s: %v; restarting the slice's units to close its revoke", slice, err)
+ return e.restartRunning(ctx, slice, rep)
+ }
+ return fmt.Errorf("granted, but %v", err)
+}
+
+// nudgeRunning makes systemd recompile the filter of every service running
+// in slice, so it takes the slice's allow as written: a write on each
+// unit's own IPAddressAllow (`set-property --runtime <unit> IPAddressAllow=`,
+// runs 20261025T131702-62b868 and 20261025T173953-8a3366). The empty
+// assignment is the unit's own list as the templates leave it, so it is
+// made only on a unit whose own list reads back empty; a unit holding one
+// is an error (the write would remove it), as are a populated slice with
+// no service, an unreadable cgroup tree and any failed write. What runs is
+// read from cgroupfs (sliceRunningServices), so nothing from a request
+// reaches systemctl.
+func (e *EgressAsserter) nudgeRunning(ctx context.Context, slice string) error {
+ if e.CgroupRoot == "" {
+ return errors.New("with no cgroup root the slice's running units cannot be found to apply it to them")
+ }
+ populated, units, err := sliceRunningServices(e.CgroupRoot, slice)
+ if err != nil {
+ return fmt.Errorf("the slice's running units could not be read to apply it to them: %v", err)
+ }
+ if !populated {
+ return nil // nothing runs under an old filter
+ }
+ if len(units) == 0 {
+ return errors.New("the slice runs something that is not a service, so it cannot be applied to it")
+ }
+ var failed []string
+ for _, unit := range units {
+ if strings.HasPrefix(unit, "-") {
+ failed = append(failed, fmt.Sprintf("%q: not a unit name", unit))
+ continue
+ }
+ own, err := e.Systemctl(ctx, "show", "-p", "IPAddressAllow", "--value", unit)
+ if err != nil {
+ failed = append(failed, fmt.Sprintf("%s: read its own allow: %s: %v", unit, strings.TrimSpace(own), err))
+ continue
+ }
+ if strings.TrimSpace(own) != "" {
+ failed = append(failed, fmt.Sprintf("%s holds an allow of its own (%s)", unit, strings.TrimSpace(own)))
+ continue
+ }
+ if out, err := e.Systemctl(ctx, "set-property", "--runtime", unit, "IPAddressAllow="); err != nil {
+ failed = append(failed, fmt.Sprintf("%s: set-property: %s: %v", unit, strings.TrimSpace(out), err))
+ continue
+ }
+ e.logf("egress: %s: applied the slice's allow to %s", slice, unit)
+ }
+ if len(failed) > 0 {
+ return fmt.Errorf("applying it to the slice's running units failed: %s", strings.Join(failed, "; "))
+ }
+ return nil
+}
+
+// closeByRestart finishes a revoke that emptied slice, outside an Assert
+// (the sweep's reset of a slice it cannot name): reach, as a revoke to
+// empty, not under claims.
+func (e *EgressAsserter) closeByRestart(ctx context.Context, slice string, rep *EgressReport) error {
+ return e.reach(ctx, slice, true, true, false, rep)
+}
+
+// restartRunning closes a revoke by restarting each service running in
+// slice with try-restart, and the report's Detail says so: systemd may
+// leave a running unit's filter as it was (runs 20261025T164418-7a14fe,
+// 20261025T173953-8a3366).
//
// What runs in the slice is read from cgroupfs (sliceRunningServices):
// systemd's ConsistsOf does not list a slice's services (run
@@ -269,17 +388,9 @@ func (e *EgressAsserter) Assert(ctx context.Context, host string) error {
// running, is an error and keeps the restart due for the next Assert of
// the slice: a revoke is never reported done while something may still
// run under the old filter.
-func (e *EgressAsserter) closeByRestart(ctx context.Context, slice string, rep *EgressReport) error {
- parent, err := e.readBack(ctx, InstancesSlice)
- if err == nil && !parent.equal(nil) {
- e.setRestartDue(slice, false)
- return nil
- }
- if err != nil {
- e.logf("egress: %s: %s: read back: %v; restarting the slice's units to close its revoke", slice, InstancesSlice, err)
- }
+func (e *EgressAsserter) restartRunning(ctx context.Context, slice string, rep *EgressReport) error {
keepDue := func(err error) error {
- e.setRestartDue(slice, true)
+ e.setDue(slice, dueRevoke)
return err
}
if e.CgroupRoot == "" {
@@ -290,7 +401,7 @@ func (e *EgressAsserter) closeByRestart(ctx context.Context, slice string, rep *
return keepDue(fmt.Errorf("revoked, but the slice's running units could not be read to restart them: %v", err))
}
if !populated {
- e.setRestartDue(slice, false)
+ e.setDue(slice, dueNone)
return nil // nothing runs under the old filter
}
if len(units) == 0 {
@@ -321,12 +432,12 @@ func (e *EgressAsserter) closeByRestart(ctx context.Context, slice string, rep *
e.logf("egress: %s: restarted %s to close its revoke; its start failed: %s: %v", slice, unit, strings.TrimSpace(out), err)
continue
}
- e.logf("egress: %s: restarted %s to close its revoke (no allow on %s)", slice, unit, InstancesSlice)
+ e.logf("egress: %s: restarted %s to close its revoke", slice, unit)
}
if len(failed) > 0 {
return keepDue(fmt.Errorf("revoked, but restarting the slice's units failed: %s", strings.Join(failed, "; ")))
}
- e.setRestartDue(slice, false)
+ e.setDue(slice, dueNone)
if rep.Detail == "" {
rep.Detail = egressRestarted
} else {
@@ -335,23 +446,27 @@ func (e *EgressAsserter) closeByRestart(ctx context.Context, slice string, rep *
return nil
}
-func (e *EgressAsserter) isRestartDue(slice string) bool {
+// isRestartDue says whether slice still owes a revoke to its running
+// services.
+func (e *EgressAsserter) isRestartDue(slice string) bool { return e.dueOf(slice) == dueRevoke }
+
+func (e *EgressAsserter) dueOf(slice string) egressDue {
e.mu.Lock()
defer e.mu.Unlock()
- return e.restartDue[slice]
+ return e.due[slice]
}
-func (e *EgressAsserter) setRestartDue(slice string, due bool) {
+func (e *EgressAsserter) setDue(slice string, d egressDue) {
e.mu.Lock()
defer e.mu.Unlock()
- if !due {
- delete(e.restartDue, slice)
+ if d == dueNone {
+ delete(e.due, slice)
return
}
- if e.restartDue == nil {
- e.restartDue = map[string]bool{}
+ if e.due == nil {
+ e.due = map[string]egressDue{}
}
- e.restartDue[slice] = true
+ e.due[slice] = d
}
// heldUncertain says what the report's Allowed is worth when the slice
@@ -467,6 +582,20 @@ func parseHeld(tok string) (netip.Prefix, bool) {
return netip.PrefixFrom(a, a.BitLen()), true
}
+// removes is true when writing want would take away something the slice
+// holds: an entry not in want, or a token that did not parse.
+func (h heldSet) removes(want []netip.Prefix) bool {
+ if len(h.unknown) > 0 {
+ return true
+ }
+ for p := range h.set {
+ if !slices.Contains(want, p) {
+ return true
+ }
+ }
+ return false
+}
+
// equal is true when the slice holds exactly want; a token that did not
// parse makes it unequal.
func (h heldSet) equal(want []netip.Prefix) bool {
@@ -755,7 +884,7 @@ func (e *EgressAsserter) closeUnnamed(ctx context.Context, slice string) (string
}
return "", false
}
- e.setRestartDue(slice, true) // before the reset, as Assert records it
+ e.setDue(slice, dueRevoke) // before the reset, as Assert records it
if out, err := e.Systemctl(ctx, "set-property", "--runtime", slice, "IPAddressAllow="); err != nil {
e.logf("egress: %s: no host for this slice; closing its allow: set-property: %s: %v", slice, strings.TrimSpace(out), err)
return "", false
diff --git a/internal/instancectl/egress_restart_linux_test.go b/internal/instancectl/egress_restart_linux_test.go
index 3207bca06..0bd931944 100644
--- a/internal/instancectl/egress_restart_linux_test.go
+++ b/internal/instancectl/egress_restart_linux_test.go
@@ -88,7 +88,7 @@ func TestEgressRevokeToEmptyRestartsWithoutAParentAllow(t *testing.T) {
mustAssert(t, asserter(ctl, root, EgressPolicy{}), egressHost)
wantCalls(t, restarts(ctl), "show -p IPAddressAllow --value "+InstancesSlice, "try-restart "+seed)
})
- t.Run("parent holds an allow: the revoke is live, nothing restarts", func(t *testing.T) {
+ t.Run("parent holds an allow: the revoke is applied by a nudge, nothing restarts", func(t *testing.T) {
ctl, root := box(t, "10.0.5.20/32", "10.0.5.53/32")
rep := mustAssert(t, asserter(ctl, root, EgressPolicy{}), egressHost)
wantCalls(t, restarts(ctl), "show -p IPAddressAllow --value "+InstancesSlice)
@@ -121,7 +121,7 @@ func TestEgressRevokeToEmptyRestartsWithoutAParentAllow(t *testing.T) {
t.Run("under claims the reset of a stale allow restarts too, the detail kept", func(t *testing.T) {
ctl, root := box(t, "10.0.5.20/32", "")
rep := mustAssert(t, asserter(ctl, root, EgressPolicy{Closed: EgressUnderClaims, Detail: "not yet supported under claims"}), egressHost)
- wantCalls(t, restarts(ctl), "show -p IPAddressAllow --value "+InstancesSlice, "try-restart "+run)
+ wantCalls(t, restarts(ctl), "try-restart "+run) // under claims no parent read: every revoke restarts
if rep.State != EgressUnderClaims || rep.Detail != "not yet supported under claims; "+egressRestarted {
t.Fatalf("report = %+v", rep)
}
@@ -326,3 +326,197 @@ func TestEgressRestartNotProvenByAFailedRead(t *testing.T) {
t.Fatalf("report = %+v, due %v: want failing and the restart kept due", rep, e.isRestartDue(SliceName(egressHost)))
}
}
+
+// nudgeBox is a slice holding held, the parent holding parent, the run
+// unit running (and the seed unit's cgroup empty beside it), and an
+// asserter over p whose systemctl fails set-property on the unit names in
+// failNudge.
+func nudgeBox(t *testing.T, held, parent string, p EgressPolicy, failNudge map[string]bool) (*egressCtl, *EgressAsserter, string) {
+ t.Helper()
+ slice := SliceName(egressHost)
+ root := t.TempDir()
+ cgroupSlice(t, root, slice, true)
+ run := UnitName(RoleRun, egressHost)
+ cgroupService(t, root, slice, run, true)
+ cgroupService(t, root, slice, UnitName(RoleSeed, egressHost), false)
+ ctl := newEgressCtl()
+ ctl.setHeld(slice, held)
+ ctl.setHeld(InstancesSlice, parent)
+ ctl.active[run] = "active"
+ e := newEgressAsserter(ctl, staticSource(p, nil))
+ e.CgroupRoot = root
+ e.Systemctl = func(ctx context.Context, args ...string) (string, error) {
+ if len(args) >= 3 && args[0] == "set-property" && failNudge[args[2]] {
+ ctl.mu.Lock()
+ ctl.calls = append(ctl.calls, strings.Join(args, " "))
+ ctl.mu.Unlock()
+ return "Failed to set unit properties", errors.New("exit status 1")
+ }
+ return ctl.run(ctx, args...)
+ }
+ return ctl, e, run
+}
+
+// unitCalls keeps the calls that touch the run unit: its own allow read,
+// the nudge, and the restart's.
+func unitCalls(ctl *egressCtl, unit string) []string {
+ var out []string
+ for _, c := range ctl.takeCalls() {
+ if strings.HasSuffix(c, " "+unit) || strings.Contains(c, " "+unit+" ") {
+ out = append(out, c)
+ }
+ }
+ return out
+}
+
+// TestEgressChangeNudgesRunningServices pins the ruling on run
+// 20261025T173953-8a3366: after every change to a host slice's allow,
+// each service running in the slice gets the unit-level write, so systemd
+// recompiles its filter; a revoke whose nudge fails is closed by restart;
+// a grant whose nudge fails reports failing and is retried; under claims
+// every revoke restarts.
+func TestEgressChangeNudgesRunningServices(t *testing.T) {
+ slice := SliceName(egressHost)
+ grant := EgressPolicy{Allow: []string{"10.0.5.20"}}
+ nudged := func(run string) []string { return nudgeCalls(run) }
+ t.Run("a grant nudges each running service, nothing restarts", func(t *testing.T) {
+ ctl, e, run := nudgeBox(t, "", "10.0.5.53/32", grant, nil)
+ rep := mustAssert(t, e, egressHost)
+ wantCalls(t, unitCalls(ctl, run), nudged(run)...)
+ if rep.State != "" || rep.Detail != "" || e.dueOf(slice) != dueNone {
+ t.Fatalf("report = %+v, due %v", rep, e.dueOf(slice))
+ }
+ })
+ t.Run("a grant with no parent allow nudges too", func(t *testing.T) {
+ ctl, e, run := nudgeBox(t, "", "", grant, nil)
+ mustAssert(t, e, egressHost)
+ wantCalls(t, unitCalls(ctl, run), nudged(run)...)
+ })
+ t.Run("a narrowing nudges, nothing restarts", func(t *testing.T) {
+ ctl, e, run := nudgeBox(t, "10.0.5.20/32 10.0.5.21/32", "", EgressPolicy{Allow: []string{"10.0.5.21"}}, nil)
+ rep := mustAssert(t, e, egressHost)
+ wantCalls(t, unitCalls(ctl, run), nudged(run)...)
+ if rep.Detail != "" {
+ t.Fatalf("report = %+v", rep)
+ }
+ })
+ t.Run("a revoke to empty with a parent allow nudges, nothing restarts", func(t *testing.T) {
+ ctl, e, run := nudgeBox(t, "10.0.5.20/32", "10.0.5.53/32", EgressPolicy{}, nil)
+ rep := mustAssert(t, e, egressHost)
+ wantCalls(t, unitCalls(ctl, run), nudged(run)...)
+ if rep.Detail != "" {
+ t.Fatalf("report = %+v", rep)
+ }
+ })
+ t.Run("a revoke to empty with no parent allow restarts, no nudge (the backstop)", func(t *testing.T) {
+ ctl, e, run := nudgeBox(t, "10.0.5.20/32", "", EgressPolicy{}, nil)
+ rep := mustAssert(t, e, egressHost)
+ calls := unitCalls(ctl, run)
+ if !slices.Contains(calls, "try-restart "+run) || slices.Contains(calls, nudgeCalls(run)[1]) {
+ t.Fatalf("calls = %q, want a restart and no nudge", calls)
+ }
+ if rep.Detail != egressRestarted {
+ t.Fatalf("report = %+v", rep)
+ }
+ })
+ t.Run("under claims a revoke restarts, with a parent allow, no nudge", func(t *testing.T) {
+ ctl, e, run := nudgeBox(t, "10.0.5.20/32", "10.0.5.53/32", EgressPolicy{Closed: EgressUnderClaims, Detail: "not yet supported under claims"}, nil)
+ rep := mustAssert(t, e, egressHost)
+ calls := unitCalls(ctl, run)
+ if !slices.Contains(calls, "try-restart "+run) || slices.Contains(calls, nudgeCalls(run)[1]) {
+ t.Fatalf("calls = %q, want a restart and no nudge", calls)
+ }
+ if rep.State != EgressUnderClaims || rep.Detail != "not yet supported under claims; "+egressRestarted {
+ t.Fatalf("report = %+v", rep)
+ }
+ })
+ t.Run("a revoke whose nudge fails is closed by restart", func(t *testing.T) {
+ for _, tc := range []struct{ name, held, parent, want string }{
+ {"narrowed", "10.0.5.20/32 10.0.5.21/32", "", "10.0.5.21"},
+ {"to empty, parent allow", "10.0.5.20/32", "10.0.5.53/32", ""},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ p := EgressPolicy{}
+ if tc.want != "" {
+ p.Allow = []string{tc.want}
+ }
+ run := UnitName(RoleRun, egressHost)
+ ctl, e, _ := nudgeBox(t, tc.held, tc.parent, p, map[string]bool{run: true})
+ rep := mustAssert(t, e, egressHost)
+ calls := unitCalls(ctl, run)
+ if !slices.Contains(calls, nudgeCalls(run)[1]) || !slices.Contains(calls, "try-restart "+run) {
+ t.Fatalf("calls = %q, want the failed nudge, then a restart", calls)
+ }
+ if rep.State != "" || rep.Detail != egressRestarted || e.dueOf(SliceName(egressHost)) != dueNone {
+ t.Fatalf("report = %+v, due %v", rep, e.dueOf(SliceName(egressHost)))
+ }
+ })
+ }
+ })
+ t.Run("a revoke whose nudge and restart both fail stays owed and is retried", func(t *testing.T) {
+ run := UnitName(RoleRun, egressHost)
+ ctl, e, _ := nudgeBox(t, "10.0.5.20/32", "10.0.5.53/32", EgressPolicy{}, map[string]bool{run: true})
+ ctl.restartFail[run] = true
+ if err := e.Assert(context.Background(), egressHost); err == nil {
+ t.Fatal("Assert reported a revoke done with neither the nudge nor the restart taken")
+ }
+ if rep := e.Reports()[egressHost]; rep.State != EgressFailing || e.dueOf(slice) != dueRevoke {
+ t.Fatalf("report = %+v, due %v: want failing and the revoke owed", rep, e.dueOf(slice))
+ }
+ ctl.takeCalls()
+ ctl.restartFail[run] = false
+ rep := mustAssert(t, e, egressHost) // the slice already reads back empty
+ if calls := unitCalls(ctl, run); !slices.Contains(calls, "try-restart "+run) || rep.Detail != egressRestarted {
+ t.Fatalf("retry: calls %q, report %+v", calls, rep)
+ }
+ mustAssert(t, e, egressHost)
+ wantCalls(t, ctl.takeCalls(), showCall(egressHost))
+ })
+ t.Run("a grant whose nudge fails reports failing, restarts nothing, and is retried", func(t *testing.T) {
+ run := UnitName(RoleRun, egressHost)
+ fail := map[string]bool{run: true}
+ ctl, e, _ := nudgeBox(t, "", "10.0.5.53/32", grant, fail)
+ err := e.Assert(context.Background(), egressHost)
+ if err == nil {
+ t.Fatal("Assert reported a grant done that did not reach the running unit")
+ }
+ rep := e.Reports()[egressHost]
+ if rep.State != EgressFailing || !strings.Contains(rep.Detail, "granted, but") || !slices.Equal(rep.Allowed, []string{"10.0.5.20"}) || e.dueOf(slice) != dueGrant {
+ t.Fatalf("report = %+v, due %v: want failing, the slice's allow shown, the grant owed", rep, e.dueOf(slice))
+ }
+ if calls := unitCalls(ctl, run); slices.Contains(calls, "try-restart "+run) {
+ t.Fatalf("a failed grant nudge restarted the unit: %q", calls)
+ }
+ delete(fail, run)
+ rep = mustAssert(t, e, egressHost) // held == wanted: the nudge is retried
+ wantCalls(t, unitCalls(ctl, run), nudged(run)...)
+ if rep.State != "" || e.dueOf(slice) != dueNone {
+ t.Fatalf("retry: report = %+v, due %v", rep, e.dueOf(slice))
+ }
+ mustAssert(t, e, egressHost)
+ wantCalls(t, ctl.takeCalls(), showCall(egressHost))
+ })
+ t.Run("a unit holding an allow of its own is not nudged: the grant stays owed", func(t *testing.T) {
+ ctl, e, run := nudgeBox(t, "", "", grant, nil)
+ ctl.setHeld(run, "192.0.2.1/32")
+ if err := e.Assert(context.Background(), egressHost); err == nil || !strings.Contains(err.Error(), "allow of its own") {
+ t.Fatalf("Assert = %v, want the unit's own allow named", err)
+ }
+ if calls := unitCalls(ctl, run); slices.Contains(calls, nudgeCalls(run)[1]) {
+ t.Fatalf("the unit's own allow was overwritten: %q", calls)
+ }
+ if e.dueOf(slice) != dueGrant {
+ t.Fatalf("due %v, want the grant owed", e.dueOf(slice))
+ }
+ })
+ t.Run("nothing runs in the slice: nothing to nudge, nothing owed", func(t *testing.T) {
+ ctl := newEgressCtl()
+ e := newEgressAsserter(ctl, staticSource(grant, nil))
+ e.CgroupRoot = t.TempDir()
+ mustAssert(t, e, egressHost)
+ wantCalls(t, ctl.takeCalls(), showCall(egressHost), setCall(egressHost, "10.0.5.20/32"), showCall(egressHost))
+ if e.dueOf(slice) != dueNone {
+ t.Fatalf("due %v", e.dueOf(slice))
+ }
+ })
+}
diff --git a/internal/instancectl/egress_startpaths_test.go b/internal/instancectl/egress_startpaths_test.go
index 2706fcee6..2e96f61f9 100644
--- a/internal/instancectl/egress_startpaths_test.go
+++ b/internal/instancectl/egress_startpaths_test.go
@@ -57,6 +57,9 @@ func startPathAsserter(systemctl func(context.Context, ...string) (string, error
Systemctl: systemctl,
Source: src,
LocalAddrs: func() ([]netip.Prefix, error) { return []netip.Prefix{netip.MustParsePrefix("127.0.0.0/8")}, nil },
+ // As production, a cgroup root (one with no carlos.slice: nothing
+ // runs for a change to be applied to).
+ CgroupRoot: os.TempDir(),
}
}
diff --git a/internal/instancectl/egress_sweep_linux_test.go b/internal/instancectl/egress_sweep_linux_test.go
index f1a57a001..452559ee0 100644
--- a/internal/instancectl/egress_sweep_linux_test.go
+++ b/internal/instancectl/egress_sweep_linux_test.go
@@ -119,6 +119,8 @@ func TestEgressSweepResetsAnUnattributableSlice(t *testing.T) {
t.Run("named once an assert has seen the host", func(t *testing.T) {
root := t.TempDir()
cgroupSlice(t, root, slice, true)
+ unit := UnitName(RoleRun, longEgressHost)
+ cgroupService(t, root, slice, unit, true)
ctl := newEgressCtl()
e := newEgressAsserter(ctl, staticSource(EgressPolicy{Allow: []string{"10.0.5.20"}}, nil))
e.CgroupRoot = root
@@ -126,7 +128,7 @@ func TestEgressSweepResetsAnUnattributableSlice(t *testing.T) {
ctl.takeCalls()
ctl.setHeld(slice, "")
e.Sweep(context.Background())
- wantCalls(t, ctl.takeCalls(), showCall(longEgressHost), setCall(longEgressHost, "10.0.5.20/32"), showCall(longEgressHost))
+ wantCalls(t, ctl.takeCalls(), append([]string{showCall(longEgressHost), setCall(longEgressHost, "10.0.5.20/32"), showCall(longEgressHost)}, nudgeCalls(unit)...)...)
})
}
@@ -134,6 +136,8 @@ func TestEgressLoopSeedsNamesFromHosts(t *testing.T) {
root := t.TempDir()
slice := SliceName(longEgressHost)
cgroupSlice(t, root, slice, true)
+ unit := UnitName(RoleRun, longEgressHost)
+ cgroupService(t, root, slice, unit, true)
ctl := newEgressCtl()
e := newEgressAsserter(ctl, staticSource(EgressPolicy{Allow: []string{"10.0.5.20"}}, nil))
e.CgroupRoot = root
@@ -159,12 +163,13 @@ func TestEgressLoopSeedsNamesFromHosts(t *testing.T) {
}
cancel()
<-done
- wantCalls(t, ctl.takeCalls(), showCall(longEgressHost), setCall(longEgressHost, "10.0.5.20/32"), showCall(longEgressHost))
+ wantCalls(t, ctl.takeCalls(), append([]string{showCall(longEgressHost), setCall(longEgressHost, "10.0.5.20/32"), showCall(longEgressHost)}, nudgeCalls(unit)...)...)
}
func TestEgressSweepLogsAHostFailingThreeSweeps(t *testing.T) {
root := t.TempDir()
cgroupSlice(t, root, SliceName(egressHost), true)
+ cgroupService(t, root, SliceName(egressHost), UnitName(RoleRun, egressHost), true)
ctl := newEgressCtl()
e := newEgressAsserter(ctl, staticSource(EgressPolicy{Allow: []string{"10.0.5.20"}}, nil))
e.CgroupRoot = root
diff --git a/internal/instancectl/egress_test.go b/internal/instancectl/egress_test.go
index 447ff0e66..897bfad37 100644
--- a/internal/instancectl/egress_test.go
+++ b/internal/instancectl/egress_test.go
@@ -126,10 +126,11 @@ func newEgressAsserter(ctl *egressCtl, src EgressSource) *EgressAsserter {
func showCall(host string) string { return "show -p IPAddressAllow --value " + SliceName(host) }
-// emptiedCalls are closeByRestart's systemctl calls after a revoke
-// empties a slice while carlos-instances.slice holds nothing: the parent's
-// allow. Nothing runs in the slice here (its cgroup is absent under the
-// asserter's root), so nothing is restarted.
+// emptiedCalls are reach's systemctl calls after a revoke empties a slice
+// outside host claims while carlos-instances.slice holds nothing: the
+// parent's allow. Nothing runs in the slice here (its cgroup is absent
+// under the asserter's root), so nothing is restarted. Under claims a
+// revoke restarts without reading the parent, so there are none.
func emptiedCalls(string) []string {
return []string{"show -p IPAddressAllow --value " + InstancesSlice}
}
@@ -292,7 +293,7 @@ func TestEgressUnderClaimsResetsAHeldAllow(t *testing.T) {
e := newEgressAsserter(ctl, staticSource(EgressPolicy{Closed: EgressUnderClaims}, nil))
rep := mustAssert(t, e, held)
- wantCalls(t, ctl.takeCalls(), append([]string{showCall(held), setCall(held), showCall(held)}, emptiedCalls(SliceName(held))...)...)
+ wantCalls(t, ctl.takeCalls(), showCall(held), setCall(held), showCall(held))
if rep.State != EgressUnderClaims || rep.Allowed != nil {
t.Fatalf("held slice: report = %+v, want state %q and nothing allowed", rep, EgressUnderClaims)
}
@@ -440,7 +441,7 @@ func TestEgressReadErrorWithNothingWantedWritesNothing(t *testing.T) {
}
rep := mustAssert(t, e, egressHost)
- wantCalls(t, ctl.takeCalls(), append([]string{showCall(egressHost), setCall(egressHost), showCall(egressHost)}, emptiedCalls(SliceName(egressHost))...)...)
+ wantCalls(t, ctl.takeCalls(), showCall(egressHost), setCall(egressHost), showCall(egressHost))
if rep.State != EgressUnderClaims || rep.Allowed != nil {
t.Fatalf("report = %+v, want state %q and nothing allowed", rep, EgressUnderClaims)
}
@@ -660,3 +661,10 @@ func TestEgressRoutesServedWithClaimsOn(t *testing.T) {
t.Fatalf("the legacy start reached systemctl: %s", ctl.joined())
}
}
+
+// nudgeCalls are nudgeRunning's systemctl calls for one running unit: its
+// own allow read back (empty), then the write that makes systemd
+// recompile its filter.
+func nudgeCalls(unit string) []string {
+ return []string{"show -p IPAddressAllow --value " + unit, "set-property --runtime " + unit + " IPAddressAllow="}
+}