instancectl: the sweep's reset of an unnamed slice keeps the claims rule (#328)
Task 14 review finding 2: closeByRestart hard-coded claims off, so under host claims an unattributable slice's reset nudged claim units instead of restarting them. EgressAsserter.UnderClaims, set by claimsEgressAsserter, carries the mode to that path.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
4 files changed,
+62
−6
internal/host/egress_source.go+6 −2internal/host/egress_source_test.go+6 −0internal/instancectl/egress.go+13 −4internal/instancectl/egress_restart_linux_test.go+37 −0
diff --git a/internal/host/egress_source.go b/internal/host/egress_source.go| index b67f02171..8e76cd7b4 100644 |
| --- a/internal/host/egress_source.go |
| +++ b/internal/host/egress_source.go |
| @@ -385,9 +385,13 @@ func claimsEgressSource() instancectl.EgressSource { |
| } |
| // claimsEgressAsserter is root's production asserter under host claims: |
| -// newEgressAsserter over claimsEgressSource. hosts only names slices. |
| +// newEgressAsserter over claimsEgressSource, marked UnderClaims so every |
| +// revoke restarts, the sweep's reset of an unnamed slice included. hosts |
| +// only names slices. |
| func claimsEgressAsserter(hosts func() ([]string, error)) *instancectl.EgressAsserter { |
| - return newEgressAsserter(claimsEgressSource(), hosts) |
| + e := newEgressAsserter(claimsEgressSource(), hosts) |
| + e.UnderClaims = true |
| + return e |
| } |
| // newEgressAsserter is root's production asserter over src: the real |
diff --git a/internal/host/egress_source_test.go b/internal/host/egress_source_test.go| index 32c8e7f7a..6e59aa339 100644 |
| --- a/internal/host/egress_source_test.go |
| +++ b/internal/host/egress_source_test.go |
| @@ -728,6 +728,12 @@ func TestClaimsEgressSourceClosesEveryHost(t *testing.T) { |
| before := treeState(t, dd) |
| src := claimsEgressSource() |
| + if e := claimsEgressAsserter(nil); !e.UnderClaims { |
| + t.Fatal("the claims asserter is not marked UnderClaims: an unnamed slice's reset would nudge claim units") |
| + } |
| + if e := newEgressAsserter(src, nil); e.UnderClaims { |
| + t.Fatal("the pre-claim asserter is marked UnderClaims") |
| + } |
| want := instancectl.EgressPolicy{Closed: instancectl.EgressUnderClaims, Detail: claimsEgressDetail} |
| hosts := []string{host, "shop-b.example.com", "nowhere.example.com"} |
| for i := range 100 { |
diff --git a/internal/instancectl/egress.go b/internal/instancectl/egress.go| index 4ccf6fda7..7938a1f92 100644 |
| --- a/internal/instancectl/egress.go |
| +++ b/internal/instancectl/egress.go |
| @@ -104,6 +104,11 @@ type EgressAsserter struct { |
| CgroupRoot string // /sys/fs/cgroup; Sweep lists the populated host slices under it |
| Hosts func() ([]string, error) // names for slices, read once when Loop starts |
| Logf func(string, ...any) |
| + // UnderClaims is set when host claims are on: every revoke then |
| + // restarts the slice's running services and nothing is nudged (reach), |
| + // the sweep's reset of a slice it cannot name included, where there is |
| + // no host whose policy could say so. |
| + UnderClaims bool |
| mu sync.Mutex |
| last map[string]EgressPolicy // host -> the last policy Source read |
| @@ -181,7 +186,7 @@ func (e *EgressAsserter) Assert(ctx context.Context, host string) error { |
| // 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 |
| + claims := e.UnderClaims || pol.Closed == EgressUnderClaims |
| var want []netip.Prefix |
| if pol.Closed != "" { |
| if rep.State == "" { |
| @@ -325,7 +330,11 @@ func (e *EgressAsserter) reach(ctx context.Context, slice string, revoke, emptie |
| // 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. |
| +// reaches systemctl. The write leaves a runtime drop-in |
| +// (/run/systemd/system.control/<unit>.d/50-IPAddressAllow.conf) until |
| +// reboot; the claim checks accept exactly that one (host's |
| +// claimDropinsBesidesNudge), so a box that switches claims on without a |
| +// reboot can still launch the unit. |
| 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") |
| @@ -369,9 +378,9 @@ func (e *EgressAsserter) nudgeRunning(ctx context.Context, slice string) error { |
| // 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. |
| +// empty, under claims when the asserter is (UnderClaims). |
| func (e *EgressAsserter) closeByRestart(ctx context.Context, slice string, rep *EgressReport) error { |
| - return e.reach(ctx, slice, true, true, false, rep) |
| + return e.reach(ctx, slice, true, true, e.UnderClaims, rep) |
| } |
| // restartRunning closes a revoke by restarting each service running in |
diff --git a/internal/instancectl/egress_restart_linux_test.go b/internal/instancectl/egress_restart_linux_test.go| index 0bd931944..7be41cec9 100644 |
| --- a/internal/instancectl/egress_restart_linux_test.go |
| +++ b/internal/instancectl/egress_restart_linux_test.go |
| @@ -520,3 +520,40 @@ func TestEgressChangeNudgesRunningServices(t *testing.T) { |
| } |
| }) |
| } |
| + |
| +// Task 14 review finding 2: the sweep's reset of a slice it cannot name |
| +// keeps the claims rule. Under claims (UnderClaims) it restarts the |
| +// slice's running services though the parent holds an allow, and nudges |
| +// nothing (a claim unit's start refuses drop-ins it did not write); |
| +// pre-claim the same reset is applied by the nudge. |
| +func TestEgressSweepUnnamedSliceKeepsTheClaimsRule(t *testing.T) { |
| + for _, tc := range []struct { |
| + name string |
| + underClaims bool |
| + }{{"under claims: restart, no nudge", true}, {"pre-claim: nudge, no restart", false}} { |
| + t.Run(tc.name, func(t *testing.T) { |
| + slice := SliceName(longEgressHost) // a hashed id: no name for it here |
| + root := t.TempDir() |
| + cgroupSlice(t, root, slice, true) |
| + unit := UnitName(RoleRun, longEgressHost) |
| + cgroupService(t, root, slice, unit, true) |
| + ctl := newEgressCtl() |
| + ctl.setHeld(slice, "10.0.5.20/32") |
| + ctl.setHeld(InstancesSlice, "10.0.5.53/32") // the resolver allow |
| + ctl.active[unit] = "active" |
| + e := newEgressAsserter(ctl, staticSource(EgressPolicy{}, nil)) |
| + e.CgroupRoot = root |
| + e.UnderClaims = tc.underClaims |
| + e.Sweep(context.Background()) |
| + if got := ctl.held[slice]; got != "" { |
| + t.Fatalf("the sweep did not reset the unnamed slice: %q", got) |
| + } |
| + calls := ctl.takeCalls() |
| + restarted := slices.Contains(calls, "try-restart "+unit) |
| + nudged := slices.Contains(calls, nudgeCalls(unit)[1]) |
| + if restarted != tc.underClaims || nudged == tc.underClaims { |
| + t.Fatalf("restarted %v, nudged %v, under claims %v: calls %q", restarted, nudged, tc.underClaims, calls) |
| + } |
| + }) |
| + } |
| +} |