instancectl: a revoke that empties a slice with no parent allow restarts its running units (#328)
Root-runner run 20261025T164418-7a14fe: with no allow on any ancestor, a slice whose allow was reset to empty left a running unit able to reach the address until the unit was restarted (unit and slice read back empty, the template deny on the unit; reachable before a restart, not after). Ruling (spec §4.3, "If systemd does not apply a revoke live, a revoke restarts the host's units"), applied narrowly: when an assert removes every address a slice held and carlos-instances.slice holds no allow (or cannot be read), the slice's running members (ConsistsOf, ActiveState active/activating/reloading/refreshing/deactivating) get a try-restart after the reset, and the report's Detail says "revoked (restarted)" (appended to a closed policy's own detail). A restart that does not end the old invocation keeps the restart due: the report is failing and the next Assert of the slice retries, though the slice already reads empty. A try-restart whose start fails but whose old invocation is gone (a claim unit without a reservation) counts as closed. With a parent allow (the resolver allow, the normal case) the revoke stays live, as Task 1 proved; a revoke to a non-empty list stays live. The sweep's reset of a slice it cannot name takes the same path. Under claims too.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
4 files changed,
+282
−6
internal/host/egress_claim_wiring_test.go+9 −0internal/instancectl/egress.go+127 −2internal/instancectl/egress_sweep_linux_test.go+1 −1internal/instancectl/egress_test.go+145 −3
diff --git a/internal/host/egress_claim_wiring_test.go b/internal/host/egress_claim_wiring_test.go| index 86b061fc2..4696172af 100644 |
| --- a/internal/host/egress_claim_wiring_test.go |
| +++ b/internal/host/egress_claim_wiring_test.go |
| @@ -70,6 +70,15 @@ func (c *claimEgressCtl) run(_ context.Context, args ...string) (string, error) |
| switch { |
| case len(args) == 5 && args[0] == "show" && args[1] == "-p" && args[2] == "IPAddressAllow" && args[3] == "--value": |
| return c.allow[args[4]], nil |
| + case len(args) == 5 && args[0] == "show" && args[1] == "-p" && args[2] == "ConsistsOf" && args[3] == "--value": |
| + // The slice's one member: the ordinary run unit (closeByRestart |
| + // lists it after a revoke empties the slice with no parent allow). |
| + return instancectl.UnitName(instancectl.RoleRun, claimEgressHost) + "\n", nil |
| + case len(args) == 5 && args[0] == "show" && args[1] == "-p" && args[2] == "ActiveState" && args[3] == "--value": |
| + if c.active { |
| + return "active\n", nil |
| + } |
| + return "inactive\n", nil |
| case len(args) == 5 && args[0] == "show" && args[1] == "-p" && args[4] == "--all": |
| var out strings.Builder |
| for _, key := range strings.Split(args[2], ",") { |
diff --git a/internal/instancectl/egress.go b/internal/instancectl/egress.go| index 19a219772..9edecbba9 100644 |
| --- a/internal/instancectl/egress.go |
| +++ b/internal/instancectl/egress.go |
| @@ -9,8 +9,16 @@ |
| // slice show is own entries only: yes |
| // |
| // So a slice's IPAddressAllow written with set-property --runtime takes |
| -// effect on the instances already running in it, both ways: Assert never |
| -// restarts a unit to close or open an address. A per-host slice's allow |
| +// effect on the instances already running in it, both ways, with an allow |
| +// on carlos-instances.slice (the resolver allow) in place. |
| +// |
| +// Root-runner run 20261025T164418-7a14fe found the exception: a slice's |
| +// allow reset to EMPTY while no ancestor holds an allow left a running |
| +// 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 |
| // 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 |
| @@ -85,8 +93,16 @@ 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 |
| } |
| +// egressRestarted is the report's Detail after a revoke closed its address |
| +// by restarting the slice's running units. |
| +const egressRestarted = "revoked (restarted)" |
| + |
| // EgressStatus is GET /egress's answer: every host's last report. |
| type EgressStatus struct { |
| Hosts map[string]EgressReport `json:"hosts"` |
| @@ -154,11 +170,21 @@ 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 && e.isRestartDue(slice) { |
| + if err := e.closeByRestart(ctx, slice, &rep); err != nil { |
| + rep.State, rep.Detail = EgressFailing, err.Error() |
| + e.record(host, rep) |
| + e.logf("egress: %s: %v", slice, err) |
| + return fmt.Errorf("%s: %v", slice, err) |
| + } |
| + } |
| e.record(host, rep) |
| return nil |
| } |
| } |
| 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) |
| @@ -204,10 +230,106 @@ 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 emptied { |
| + e.setRestartDue(slice, true) |
| + if err := e.closeByRestart(ctx, slice, &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 unit of the slice |
| +// that is running 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. The unit names are the slice's members as |
| +// systemd lists them; nothing from a request reaches systemctl. A failure |
| +// keeps the restart due for the next Assert of the slice. |
| +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) |
| + } |
| + out, err := e.Systemctl(ctx, "show", "-p", "ConsistsOf", "--value", slice) |
| + if err != nil { |
| + return fmt.Errorf("revoked, but the slice's units could not be listed to restart them: %s: %v", strings.TrimSpace(out), err) |
| + } |
| + var failed []string |
| + restarted := 0 |
| + for _, unit := range strings.Fields(out) { |
| + if !strings.HasSuffix(unit, ".service") || strings.HasPrefix(unit, "-") { |
| + continue |
| + } |
| + state, err := e.Systemctl(ctx, "show", "-p", "ActiveState", "--value", unit) |
| + if err != nil { |
| + failed = append(failed, fmt.Sprintf("%s: state: %v", unit, err)) |
| + continue |
| + } |
| + switch strings.TrimSpace(state) { |
| + case "active", "activating", "reloading", "refreshing", "deactivating": |
| + default: |
| + continue // nothing of it runs under the old filter |
| + } |
| + inv, _ := e.Systemctl(ctx, "show", "-p", "InvocationID", "--value", unit) |
| + if out, err := e.Systemctl(ctx, "try-restart", unit); err != nil { |
| + // A restart whose start fails (a claim unit's start without a |
| + // reservation, say) still ended the old invocation, and that |
| + // is what closes the address. |
| + after, _ := e.Systemctl(ctx, "show", "-p", "InvocationID", "--value", unit) |
| + now, _ := e.Systemctl(ctx, "show", "-p", "ActiveState", "--value", unit) |
| + gone := strings.TrimSpace(after) != strings.TrimSpace(inv) || strings.TrimSpace(now) == "inactive" || strings.TrimSpace(now) == "failed" |
| + if !gone { |
| + failed = append(failed, fmt.Sprintf("%s: try-restart: %s: %v", unit, strings.TrimSpace(out), err)) |
| + continue |
| + } |
| + e.logf("egress: %s: restarted %s to close its revoke; its start failed: %s: %v", slice, unit, strings.TrimSpace(out), err) |
| + } else { |
| + e.logf("egress: %s: restarted %s to close its revoke (no allow on %s)", slice, unit, InstancesSlice) |
| + } |
| + restarted++ |
| + } |
| + if len(failed) > 0 { |
| + e.setRestartDue(slice, true) |
| + return fmt.Errorf("revoked, but restarting the slice's units failed: %s", strings.Join(failed, "; ")) |
| + } |
| + e.setRestartDue(slice, false) |
| + if restarted > 0 { |
| + if rep.Detail == "" { |
| + rep.Detail = egressRestarted |
| + } else { |
| + rep.Detail += "; " + egressRestarted |
| + } |
| + } |
| + return nil |
| +} |
| + |
| +func (e *EgressAsserter) isRestartDue(slice string) bool { |
| + e.mu.Lock() |
| + defer e.mu.Unlock() |
| + return e.restartDue[slice] |
| +} |
| + |
| +func (e *EgressAsserter) setRestartDue(slice string, due bool) { |
| + e.mu.Lock() |
| + defer e.mu.Unlock() |
| + if !due { |
| + delete(e.restartDue, slice) |
| + return |
| + } |
| + if e.restartDue == nil { |
| + e.restartDue = map[string]bool{} |
| + } |
| + e.restartDue[slice] = true |
| +} |
| + |
| // heldUncertain says what the report's Allowed is worth when the slice |
| // could not be read after a write. |
| func heldUncertain(readBefore bool) string { |
| @@ -614,6 +736,9 @@ func (e *EgressAsserter) closeUnnamed(ctx context.Context, slice string) (string |
| e.logf("egress: %s: no host for this slice; after closing its allow the slice holds %s", slice, held.text()) |
| default: |
| e.logf("egress: %s: no host for this slice; its allow is closed", slice) |
| + if err := e.closeByRestart(ctx, slice, &EgressReport{}); err != nil { |
| + e.logf("egress: %s: no host for this slice; %v", slice, err) |
| + } |
| } |
| return "", false |
| } |
diff --git a/internal/instancectl/egress_sweep_linux_test.go b/internal/instancectl/egress_sweep_linux_test.go| index 27e9cb7cd..f1a57a001 100644 |
| --- a/internal/instancectl/egress_sweep_linux_test.go |
| +++ b/internal/instancectl/egress_sweep_linux_test.go |
| @@ -98,7 +98,7 @@ func TestEgressSweepResetsAnUnattributableSlice(t *testing.T) { |
| e.Logf = func(f string, a ...any) { logs = append(logs, fmt.Sprintf(f, a...)) } |
| e.Sweep(context.Background()) |
| show := "show -p IPAddressAllow --value " + slice |
| - wantCalls(t, ctl.takeCalls(), show, "set-property --runtime "+slice+" IPAddressAllow=", show) |
| + wantCalls(t, ctl.takeCalls(), append([]string{show, "set-property --runtime " + slice + " IPAddressAllow=", show}, emptiedCalls(slice)...)...) |
| want := "egress: " + slice + ": no host for this slice; its allow is closed" |
| if !slices.Contains(logs, want) { |
| t.Fatalf("logs = %q, want %q", logs, want) |
diff --git a/internal/instancectl/egress_test.go b/internal/instancectl/egress_test.go| index 68f6dccbc..752293d82 100644 |
| --- a/internal/instancectl/egress_test.go |
| +++ b/internal/instancectl/egress_test.go |
| @@ -28,9 +28,18 @@ type egressCtl struct { |
| calls []string |
| held map[string]string // slice -> show's answer |
| ignore bool // accept set-property but change nothing |
| + // For a revoke closed by restart (closeByRestart): a slice's members, |
| + // each unit's ActiveState and InvocationID (a try-restart moves it on), |
| + // and units whose try-restart fails. |
| + members map[string]string |
| + active map[string]string |
| + invocations map[string]int |
| + restartFail map[string]bool |
| } |
| -func newEgressCtl() *egressCtl { return &egressCtl{held: map[string]string{}} } |
| +func newEgressCtl() *egressCtl { |
| + return &egressCtl{held: map[string]string{}, members: map[string]string{}, active: map[string]string{}, invocations: map[string]int{}, restartFail: map[string]bool{}} |
| +} |
| func (f *egressCtl) run(_ context.Context, args ...string) (string, error) { |
| f.mu.Lock() |
| @@ -39,6 +48,21 @@ func (f *egressCtl) run(_ context.Context, args ...string) (string, error) { |
| switch { |
| case len(args) == 5 && args[0] == "show" && args[1] == "-p" && args[2] == "IPAddressAllow" && args[3] == "--value": |
| return f.held[args[4]] + "\n", nil |
| + case len(args) == 5 && args[0] == "show" && args[1] == "-p" && args[2] == "ConsistsOf" && args[3] == "--value": |
| + return f.members[args[4]] + "\n", nil |
| + case len(args) == 5 && args[0] == "show" && args[1] == "-p" && args[2] == "ActiveState" && args[3] == "--value": |
| + if st, ok := f.active[args[4]]; ok { |
| + return st + "\n", nil |
| + } |
| + return "inactive\n", nil |
| + case len(args) == 5 && args[0] == "show" && args[1] == "-p" && args[2] == "InvocationID" && args[3] == "--value": |
| + return strconv.Itoa(f.invocations[args[4]]) + "\n", nil |
| + case len(args) == 2 && args[0] == "try-restart": |
| + if f.restartFail[args[1]] { |
| + return "Job failed", errors.New("exit status 1") |
| + } |
| + f.invocations[args[1]]++ |
| + return "", nil |
| case len(args) >= 4 && args[0] == "set-property" && args[1] == "--runtime": |
| if f.ignore { |
| return "", nil |
| @@ -99,6 +123,13 @@ func newEgressAsserter(ctl *egressCtl, src EgressSource) *EgressAsserter { |
| func showCall(host string) string { return "show -p IPAddressAllow --value " + SliceName(host) } |
| +// emptiedCalls are closeByRestart's reads after a revoke empties slice |
| +// while carlos-instances.slice holds nothing: the parent's allow, then the |
| +// slice's members (none running here, so nothing is restarted). |
| +func emptiedCalls(slice string) []string { |
| + return []string{"show -p IPAddressAllow --value " + InstancesSlice, "show -p ConsistsOf --value " + slice} |
| +} |
| + |
| func setCall(host string, list ...string) string { |
| c := "set-property --runtime " + SliceName(host) + " IPAddressAllow=" |
| if len(list) > 0 { |
| @@ -257,7 +288,7 @@ func TestEgressUnderClaimsResetsAHeldAllow(t *testing.T) { |
| e := newEgressAsserter(ctl, staticSource(EgressPolicy{Closed: EgressUnderClaims}, nil)) |
| rep := mustAssert(t, e, held) |
| - wantCalls(t, ctl.takeCalls(), showCall(held), setCall(held), showCall(held)) |
| + wantCalls(t, ctl.takeCalls(), append([]string{showCall(held), setCall(held), showCall(held)}, emptiedCalls(SliceName(held))...)...) |
| if rep.State != EgressUnderClaims || rep.Allowed != nil { |
| t.Fatalf("held slice: report = %+v, want state %q and nothing allowed", rep, EgressUnderClaims) |
| } |
| @@ -405,7 +436,7 @@ func TestEgressReadErrorWithNothingWantedWritesNothing(t *testing.T) { |
| } |
| rep := mustAssert(t, e, egressHost) |
| - wantCalls(t, ctl.takeCalls(), showCall(egressHost), setCall(egressHost), showCall(egressHost)) |
| + wantCalls(t, ctl.takeCalls(), append([]string{showCall(egressHost), setCall(egressHost), showCall(egressHost)}, emptiedCalls(SliceName(egressHost))...)...) |
| if rep.State != EgressUnderClaims || rep.Allowed != nil { |
| t.Fatalf("report = %+v, want state %q and nothing allowed", rep, EgressUnderClaims) |
| } |
| @@ -625,3 +656,114 @@ func TestEgressRoutesServedWithClaimsOn(t *testing.T) { |
| t.Fatalf("the legacy start reached systemctl: %s", ctl.joined()) |
| } |
| } |
| + |
| +// TestEgressRevokeToEmptyRestartsWithoutAParentAllow pins the ruling on |
| +// run 20261025T164418-7a14fe: a revoke that empties a slice while |
| +// carlos-instances.slice holds no allow restarts the slice's running |
| +// units; every other revoke stays live. |
| +func TestEgressRevokeToEmptyRestartsWithoutAParentAllow(t *testing.T) { |
| + slice := SliceName(egressHost) |
| + run, seed := UnitName(RoleRun, egressHost), UnitName(RoleSeed, egressHost) |
| + restarts := func(ctl *egressCtl) []string { |
| + var out []string |
| + for _, c := range ctl.takeCalls() { |
| + if strings.HasPrefix(c, "try-restart ") || strings.HasPrefix(c, "show -p ConsistsOf ") || c == "show -p IPAddressAllow --value "+InstancesSlice { |
| + out = append(out, c) |
| + } |
| + } |
| + return out |
| + } |
| + box := func(held, parent string) *egressCtl { |
| + ctl := newEgressCtl() |
| + ctl.setHeld(slice, held) |
| + ctl.setHeld(InstancesSlice, parent) |
| + ctl.members[slice] = run + " " + seed + " carlos-instances-x.slice" |
| + ctl.active[run] = "active" |
| + ctl.active[seed] = "inactive" |
| + return ctl |
| + } |
| + |
| + t.Run("parent empty and target empty: running units restart", func(t *testing.T) { |
| + ctl := box("10.0.5.20/32", "") |
| + rep := mustAssert(t, newEgressAsserter(ctl, staticSource(EgressPolicy{}, nil)), egressHost) |
| + wantCalls(t, restarts(ctl), "show -p IPAddressAllow --value "+InstancesSlice, "show -p ConsistsOf --value "+slice, "try-restart "+run) |
| + if rep.State != "" || rep.Detail != egressRestarted || len(rep.Allowed) != 0 { |
| + t.Fatalf("report = %+v, want no state and detail %q", rep, egressRestarted) |
| + } |
| + }) |
| + t.Run("parent holds an allow: the revoke is live, nothing restarts", func(t *testing.T) { |
| + ctl := box("10.0.5.20/32", "10.0.5.53/32") |
| + rep := mustAssert(t, newEgressAsserter(ctl, staticSource(EgressPolicy{}, nil)), egressHost) |
| + wantCalls(t, restarts(ctl), "show -p IPAddressAllow --value "+InstancesSlice) |
| + if rep.Detail != "" { |
| + t.Fatalf("report = %+v, want no detail", rep) |
| + } |
| + }) |
| + t.Run("target not empty: the narrowing is live, nothing restarts", func(t *testing.T) { |
| + ctl := box("10.0.5.20/32 10.0.5.21/32", "") |
| + rep := mustAssert(t, newEgressAsserter(ctl, staticSource(EgressPolicy{Allow: []string{"10.0.5.21"}}, nil)), egressHost) |
| + wantCalls(t, restarts(ctl)) |
| + if rep.Detail != "" || !slices.Equal(rep.Allowed, []string{"10.0.5.21"}) { |
| + t.Fatalf("report = %+v", rep) |
| + } |
| + }) |
| + t.Run("nothing held, nothing wanted: no write, nothing restarts", func(t *testing.T) { |
| + ctl := box("", "") |
| + mustAssert(t, newEgressAsserter(ctl, staticSource(EgressPolicy{}, nil)), egressHost) |
| + wantCalls(t, ctl.takeCalls(), showCall(egressHost)) |
| + }) |
| + t.Run("under claims the reset of a stale allow restarts too, the detail kept", func(t *testing.T) { |
| + ctl := box("10.0.5.20/32", "") |
| + rep := mustAssert(t, newEgressAsserter(ctl, staticSource(EgressPolicy{Closed: EgressUnderClaims, Detail: "not yet supported under claims"}, nil)), egressHost) |
| + wantCalls(t, restarts(ctl), "show -p IPAddressAllow --value "+InstancesSlice, "show -p ConsistsOf --value "+slice, "try-restart "+run) |
| + if rep.State != EgressUnderClaims || rep.Detail != "not yet supported under claims; "+egressRestarted { |
| + t.Fatalf("report = %+v", rep) |
| + } |
| + }) |
| + t.Run("a restart whose start fails still ended the old invocation", func(t *testing.T) { |
| + ctl := box("10.0.5.20/32", "") |
| + ctl.restartFail[run] = true |
| + e := newEgressAsserter(ctl, staticSource(EgressPolicy{}, nil)) |
| + // The fake's failing try-restart leaves the invocation as it was; |
| + // the unit reading failed after it (its first read, the listing, |
| + // sees it active) is what counts as closed. |
| + orig := e.Systemctl |
| + calls := 0 |
| + e.Systemctl = func(ctx context.Context, args ...string) (string, error) { |
| + if len(args) == 5 && args[2] == "ActiveState" && args[4] == run { |
| + calls++ |
| + if calls > 1 { |
| + return "failed\n", nil |
| + } |
| + } |
| + return orig(ctx, args...) |
| + } |
| + rep := mustAssert(t, e, egressHost) |
| + if rep.Detail != egressRestarted { |
| + t.Fatalf("report = %+v, want detail %q", rep, egressRestarted) |
| + } |
| + }) |
| + t.Run("a restart that does not happen stays due and is retried", func(t *testing.T) { |
| + ctl := box("10.0.5.20/32", "") |
| + ctl.restartFail[run] = true |
| + e := newEgressAsserter(ctl, staticSource(EgressPolicy{}, nil)) |
| + if err := e.Assert(context.Background(), egressHost); err == nil { |
| + t.Fatal("Assert succeeded though the running unit kept its invocation") |
| + } |
| + if rep := e.Reports()[egressHost]; rep.State != EgressFailing || !strings.Contains(rep.Detail, "try-restart") { |
| + t.Fatalf("report = %+v, want failing naming the try-restart", rep) |
| + } |
| + ctl.takeCalls() |
| + ctl.restartFail[run] = false |
| + rep := mustAssert(t, e, egressHost) // the slice already reads back empty |
| + if rep.State != "" || rep.Detail != egressRestarted { |
| + t.Fatalf("retry: report = %+v, want detail %q", rep, egressRestarted) |
| + } |
| + if !slices.Contains(ctl.takeCalls(), "try-restart "+run) { |
| + t.Fatal("retry did not restart the unit") |
| + } |
| + ctl.takeCalls() |
| + mustAssert(t, e, egressHost) |
| + wantCalls(t, ctl.takeCalls(), showCall(egressHost)) |
| + }) |
| +} |