host: egress witnesses name the revoke branch they exercise; the claims witness expects the restart (#328)
- TestRootEgressRevokeWhileRunning has three subtests, one per branch of the asserter's revoke: to empty with no parent allow (the run unit is restarted, "revoked (restarted)"), to empty with an allow on carlos-instances.slice (live, the invocation kept), and 10.0.5.20 + 10.0.5.21 narrowed to 10.0.5.21 with no parent allow (live, no restart). - TestRootEgressMixedBoxWritesNothing: the box has no parent allow, so the not_nested reset restarts the run unit; it serves again, then is closed. - TestRootEgressInstanceControlUnderClaims: the claims service's first sweep and the later POST /egress each restart the tenant, which is then unreachable; GET /egress says under_claims with "revoked (restarted)".
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 file changed,
+189
−41
internal/host/egress_startpaths_rootrunner_linux_test.go+189 −41
diff --git a/internal/host/egress_startpaths_rootrunner_linux_test.go b/internal/host/egress_startpaths_rootrunner_linux_test.go| index a88899056..45189f42c 100644 |
| --- a/internal/host/egress_startpaths_rootrunner_linux_test.go |
| +++ b/internal/host/egress_startpaths_rootrunner_linux_test.go |
| @@ -1065,15 +1065,105 @@ func TestRootEgressStartPathsPreClaim(t *testing.T) { |
| } |
| // TestRootEgressRevokeWhileRunning: with the run unit serving, a cleared |
| -// column and POST /egress close the address within 2 s; a regrant opens |
| -// it again; then a revoke whose write fails answers 502 and reports |
| -// failing with the address still held, and the next sweep closes it |
| -// (Astra plan R1-11: the failing write has something to write). |
| +// column and POST /egress close the address; a regrant opens it again; |
| +// then a revoke whose write fails answers 502 and reports failing with the |
| +// address still held, and the next sweep closes it (Astra plan R1-11: the |
| +// failing write has something to write). Each subtest names the branch of |
| +// the asserter's revoke it exercises (run 20261025T164418-7a14fe ruling): |
| +// |
| +// - "to empty, no parent allow": carlos-instances.slice holds nothing, so |
| +// each revoke to empty restarts the run unit (closeByRestart) and |
| +// reports "revoked (restarted)"; |
| +// - "to empty, parent allow": carlos-instances.slice holds an allow (the |
| +// resolver allow's stand-in, fd00:5::20), so each revoke is live: the |
| +// run unit keeps its invocation; |
| +// - "narrowed, no parent allow": 10.0.5.20 and 10.0.5.21 narrowed to |
| +// 10.0.5.21 is live with no parent allow (a non-empty result never |
| +// restarts). |
| func TestRootEgressRevokeWhileRunning(t *testing.T) { |
| + t.Run("to empty, no parent allow", func(t *testing.T) { egressRevokeToEmpty(t, false) }) |
| + t.Run("to empty, parent allow", func(t *testing.T) { egressRevokeToEmpty(t, true) }) |
| + t.Run("narrowed, no parent allow", egressRevokeNarrowed) |
| +} |
| + |
| +// egressParentAllow gives carlos-instances.slice an allow for the test, as |
| +// the probe's resolver allow does in production, and takes it off after. |
| +func egressParentAllow(t *testing.T, prefix string) { |
| + t.Helper() |
| + parent := instancectl.InstancesSlice |
| + for _, root := range []string{"/run/systemd/system.control", "/etc/systemd/system.control"} { |
| + if _, err := os.Lstat(filepath.Join(root, parent+".d")); !errors.Is(err, fs.ErrNotExist) { |
| + t.Fatal("parent slice already has set-property drop-ins", root, err) |
| + } |
| + } |
| + if got := egressShow(t, parent, "IPAddressAllow"); got != "" { |
| + t.Fatal("parent slice already carries an allow", got) |
| + } |
| + t.Cleanup(func() { |
| + if out, err := egressCtl("set-property", "--runtime", parent, "IPAddressAllow="); err != nil { |
| + t.Errorf("reset %s: %v: %s", parent, err, out) |
| + } |
| + if err := os.RemoveAll(filepath.Join("/run/systemd/system.control", parent+".d")); err != nil { |
| + t.Error(err) |
| + } |
| + if out, err := egressCtl("daemon-reload"); err != nil { |
| + t.Errorf("daemon-reload: %v: %s", err, out) |
| + } |
| + }) |
| + egressMustCtl(t, "set-property", "--runtime", parent, "IPAddressAllow=", "IPAddressAllow="+prefix) |
| +} |
| + |
| +// runInvocation is the run unit's InvocationID. |
| +func (b *egressBox) runInvocation(t *testing.T) string { |
| + t.Helper() |
| + return egressShow(t, instancectl.UnitName(instancectl.RoleRun, b.host), "InvocationID") |
| +} |
| + |
| +// closed checks a revoke to empty that has just returned: the slice holds |
| +// nothing; with restarted, the run unit has a new invocation (it serves |
| +// again before it is probed) and the report says "revoked (restarted)", |
| +// otherwise it kept its invocation and the report has no detail; either |
| +// way the tenant cannot reach target within 2 s. |
| +func (b *egressBox) closed(t *testing.T, step string, c *http.Client, target, before string, restarted bool) { |
| + t.Helper() |
| + unit := instancectl.UnitName(instancectl.RoleRun, b.host) |
| + if got := b.allow(t); got != "" { |
| + t.Fatalf("%s: slice holds %q", step, got) |
| + } |
| + after := b.runInvocation(t) |
| + if restarted { |
| + if after == before { |
| + t.Fatalf("%s: the run unit kept invocation %s: no restart with no parent allow", step, before) |
| + } |
| + egressServing(t, unit, c) |
| + } else if after != before { |
| + t.Fatalf("%s: the run unit was restarted (%s -> %s) though the revoke is live", step, before, after) |
| + } |
| + if !egressWithin(t, time.Now().Add(2*time.Second), c, false, target) { |
| + egressFirewallDiag(t, unit, b.slice, c, target) |
| + t.Fatalf("%s: 10.0.5.20 still reachable", step) |
| + } |
| + wantDetail := "" |
| + if restarted { |
| + wantDetail = "revoked (restarted)" |
| + } |
| + if rep := b.report(t); len(rep.Allowed) != 0 || rep.State != "" || rep.Detail != wantDetail { |
| + t.Fatalf("%s: GET /egress = %+v, want nothing allowed, no state, detail %q", step, rep, wantDetail) |
| + } |
| +} |
| + |
| +func egressRevokeToEmpty(t *testing.T, parentAllow bool) { |
| b := newEgressPreClaimBox(t) |
| ctx := t.Context() |
| target := b.targets["10.0.5.20"] |
| + restarted := !parentAllow |
| c := b.startRun(t) |
| + if parentAllow { |
| + egressParentAllow(t, "fd00:5::20/128") |
| + if !egressWithin(t, time.Now().Add(2*time.Second), c, true, b.targets["fd00:5::20"]) { |
| + t.Fatal("the parent's allow (fd00:5::20) is not in force for the run unit") |
| + } |
| + } |
| b.wantAllowed(t, "start") |
| if !egressReach(t, c, target) { |
| t.Fatal("start: the tenant cannot reach 10.0.5.20") |
| @@ -1084,33 +1174,29 @@ func TestRootEgressRevokeWhileRunning(t *testing.T) { |
| if err := b.reg.SetPolicy(b.host, "", "", ""); err != nil { |
| t.Fatal(err) |
| } |
| + before := b.runInvocation(t) |
| if err := b.client.Egress(ctx, b.host); err != nil { |
| t.Fatal("POST /egress (revoke):", err) |
| } |
| - if !egressWithin(t, time.Now().Add(2*time.Second), c, false, target) { |
| - egressFirewallDiag(t, instancectl.UnitName(instancectl.RoleRun, b.host), b.slice, c, target) |
| - t.Fatal("revoke: 10.0.5.20 still reachable 2 s after POST /egress") |
| - } |
| - if got := b.allow(t); got != "" { |
| - t.Fatalf("revoke: slice holds %q", got) |
| - } |
| - if rep := b.report(t); len(rep.Allowed) != 0 || rep.State != "" { |
| - t.Fatalf("revoke: GET /egress = %+v, want nothing allowed and no state", rep) |
| - } |
| + b.closed(t, "revoke", c, target, before, restarted) |
| // Regrant, so the failing revoke below has an allow to remove. |
| if err := b.reg.SetPolicy(b.host, "10.0.5.20", "", egressExpiry()); err != nil { |
| t.Fatal(err) |
| } |
| + before = b.runInvocation(t) |
| if err := b.client.Egress(ctx, b.host); err != nil { |
| t.Fatal("POST /egress (regrant):", err) |
| } |
| if !egressWithin(t, time.Now().Add(2*time.Second), c, true, target) { |
| t.Fatal("regrant: 10.0.5.20 not reachable 2 s after POST /egress") |
| } |
| + if after := b.runInvocation(t); after != before { |
| + t.Fatalf("regrant: the run unit was restarted (%s -> %s); a grant is live", before, after) |
| + } |
| b.wantAllowed(t, "regrant") |
| - // A revoke whose write fails. |
| + // A revoke whose write fails: nothing is written, nothing restarts. |
| if err := b.reg.SetPolicy(b.host, "", "", ""); err != nil { |
| t.Fatal(err) |
| } |
| @@ -1128,25 +1214,59 @@ func TestRootEgressRevokeWhileRunning(t *testing.T) { |
| if got := b.allow(t); got != "10.0.5.20/32" { |
| t.Fatalf("failing revoke: slice holds %q, want the allow still there", got) |
| } |
| + if after := b.runInvocation(t); after != before { |
| + t.Fatalf("failing revoke: the run unit was restarted (%s -> %s)", before, after) |
| + } |
| if !egressReach(t, c, target) { |
| t.Fatal("failing revoke: 10.0.5.20 closed though the write failed") |
| } |
| // The next sweep (the 30 s loop's pass) closes it. |
| b.egress.Sweep(ctx) |
| - deadline := time.Now().Add(2 * time.Second) |
| - if got := b.allow(t); got != "" { |
| - t.Fatalf("sweep: slice holds %q", got) |
| + b.closed(t, "sweep", c, target, before, restarted) |
| + if n := b.ctl.failed.Load(); n != 1 { |
| + t.Fatalf("%d failed set-property, want 1", n) |
| } |
| - if !egressWithin(t, deadline, c, false, target) { |
| - egressFirewallDiag(t, instancectl.UnitName(instancectl.RoleRun, b.host), b.slice, c, target) |
| - t.Fatal("sweep: 10.0.5.20 still reachable 2 s after the sweep") |
| +} |
| + |
| +func egressRevokeNarrowed(t *testing.T) { |
| + b := newEgressPreClaimBox(t) |
| + ctx := t.Context() |
| + c := b.startRun(t) |
| + b.wantAllowed(t, "start") |
| + if err := b.reg.SetPolicy(b.host, "10.0.5.20 10.0.5.21", "", egressExpiry()); err != nil { |
| + t.Fatal(err) |
| } |
| - if rep := b.report(t); rep.State != "" || len(rep.Allowed) != 0 { |
| - t.Fatalf("sweep: GET /egress = %+v, want no state and nothing allowed", rep) |
| + if err := b.client.Egress(ctx, b.host); err != nil { |
| + t.Fatal("POST /egress (widen):", err) |
| } |
| - if n := b.ctl.failed.Load(); n != 1 { |
| - t.Fatalf("%d failed set-property, want 1", n) |
| + for _, a := range []string{"10.0.5.20", "10.0.5.21"} { |
| + if !egressWithin(t, time.Now().Add(2*time.Second), c, true, b.targets[a]) { |
| + t.Fatalf("widen: %s not reachable 2 s after POST /egress", a) |
| + } |
| + } |
| + if err := b.reg.SetPolicy(b.host, "10.0.5.21", "", egressExpiry()); err != nil { |
| + t.Fatal(err) |
| + } |
| + before := b.runInvocation(t) |
| + if err := b.client.Egress(ctx, b.host); err != nil { |
| + t.Fatal("POST /egress (narrow):", err) |
| + } |
| + if !egressWithin(t, time.Now().Add(2*time.Second), c, false, b.targets["10.0.5.20"]) { |
| + egressFirewallDiag(t, instancectl.UnitName(instancectl.RoleRun, b.host), b.slice, c, b.targets["10.0.5.20"]) |
| + t.Fatal("narrow: 10.0.5.20 still reachable 2 s after POST /egress, with no parent allow") |
| + } |
| + if !egressReach(t, c, b.targets["10.0.5.21"]) { |
| + t.Fatal("narrow: 10.0.5.21 closed with 10.0.5.20") |
| + } |
| + if after := b.runInvocation(t); after != before { |
| + t.Fatalf("narrow: the run unit was restarted (%s -> %s); a non-empty revoke is live", before, after) |
| + } |
| + if got := b.allow(t); got != "10.0.5.21/32" { |
| + t.Fatalf("narrow: slice holds %q, want 10.0.5.21/32", got) |
| + } |
| + if rep := b.report(t); !slices.Equal(rep.Allowed, []string{"10.0.5.21"}) || rep.State != "" || rep.Detail != "" { |
| + t.Fatalf("narrow: GET /egress = %+v, want allowed [10.0.5.21], no state, no detail", rep) |
| } |
| } |
| @@ -1169,19 +1289,26 @@ func TestRootEgressMixedBoxWritesNothing(t *testing.T) { |
| DBPath: provision.DefaultDBPath("/data", flat), Socket: provision.DefaultSocket(flat)}); err != nil { |
| t.Fatal(err) |
| } |
| + before := b.runInvocation(t) |
| if err := b.client.Egress(ctx, b.host); err != nil { |
| t.Fatal("POST /egress:", err) |
| } |
| - if !egressWithin(t, time.Now().Add(2*time.Second), c, false, target) { |
| - egressFirewallDiag(t, instancectl.UnitName(instancectl.RoleRun, b.host), b.slice, c, target) |
| - t.Fatal("mixed box: 10.0.5.20 still reachable 2 s after POST /egress") |
| - } |
| if got := b.allow(t); got != "" { |
| t.Fatalf("mixed box: slice holds %q, want nothing", got) |
| } |
| + // No parent allow on this box: the reset to empty restarts the run |
| + // unit (closeByRestart), which serves again before it is probed. |
| + if after := b.runInvocation(t); after == before { |
| + t.Fatalf("mixed box: the run unit kept invocation %s", before) |
| + } |
| + egressServing(t, instancectl.UnitName(instancectl.RoleRun, b.host), c) |
| + if !egressWithin(t, time.Now().Add(2*time.Second), c, false, target) { |
| + egressFirewallDiag(t, instancectl.UnitName(instancectl.RoleRun, b.host), b.slice, c, target) |
| + t.Fatal("mixed box: 10.0.5.20 still reachable after POST /egress") |
| + } |
| rep := b.report(t) |
| - if rep.State != instancectl.EgressNotNested || len(rep.Allowed) != 0 || !strings.Contains(rep.Detail, flat) { |
| - t.Fatalf("mixed box: GET /egress = %+v, want not_nested naming %s and nothing allowed", rep, flat) |
| + if rep.State != instancectl.EgressNotNested || len(rep.Allowed) != 0 || !strings.Contains(rep.Detail, flat) || !strings.HasSuffix(rep.Detail, "; revoked (restarted)") { |
| + t.Fatalf("mixed box: GET /egress = %+v, want not_nested naming %s, revoked (restarted), and nothing allowed", rep, flat) |
| } |
| } |
| @@ -1306,7 +1433,9 @@ func plan14EgressClaimChecks(t *testing.T, host string, tenant *http.Client, tar |
| // constructs, a running tenant's host slice holds an allow granted |
| // pre-claim and the registry row carries one. The service's first sweep |
| // resets it; a hand regrant is closed again by POST /egress; GET /egress |
| -// reports under_claims. |
| +// reports under_claims. No ancestor slice holds an allow here, so each |
| +// revoke to empty also restarts the tenant (closeByRestart, the ruling on |
| +// run 20261025T164418-7a14fe) and the report says "revoked (restarted)". |
| func TestRootEgressInstanceControlUnderClaims(t *testing.T) { |
| runServingParkPeerNamespace(t, false, false, "seed-construction-egress") |
| } |
| @@ -1318,6 +1447,7 @@ type egressUnderClaimsWitness struct { |
| host, slice string |
| targets map[string]string |
| tenant *http.Client |
| + invocation string // the tenant's, before the control service starts |
| } |
| func (w *egressUnderClaimsWitness) prepare(t *testing.T) { |
| @@ -1361,6 +1491,7 @@ func (w *egressUnderClaimsWitness) prepare(t *testing.T) { |
| egressFirewallDiag(t, egressClaimsUnit+".service", w.slice, w.tenant, target) |
| t.Fatal("pre-claim: the tenant cannot reach 10.0.5.20 under the hand grant") |
| } |
| + w.invocation = egressShow(t, egressClaimsUnit+".service", "InvocationID") |
| } |
| // egressClaimsUnit is the claims witness's tenant unit. |
| @@ -1369,34 +1500,51 @@ const egressClaimsUnit = "carlos-egress-claims" |
| func (w *egressUnderClaimsWitness) check(t *testing.T, egress func(host string) servingPeerReply) { |
| t.Helper() |
| target := w.targets["10.0.5.20"] |
| + unit := egressClaimsUnit + ".service" |
| + // restartedFrom waits for the tenant's unit to leave invocation before |
| + // (no allow on carlos-instances.slice here, so a revoke to empty |
| + // restarts the slice's running units: closeByRestart) and to serve. |
| + restartedFrom := func(step, before string) { |
| + t.Helper() |
| + for deadline := time.Now().Add(10 * time.Second); egressShow(t, unit, "InvocationID") == before; time.Sleep(100 * time.Millisecond) { |
| + if time.Now().After(deadline) { |
| + t.Fatalf("%s: %s kept invocation %s: the revoke to empty did not restart it", step, unit, before) |
| + } |
| + } |
| + egressServing(t, unit, w.tenant) |
| + } |
| + |
| // The control service's first sweep (RunWithClaim's Loop) resets the |
| - // allow granted before claims were switched on. |
| + // allow granted before claims were switched on, and restarts the tenant. |
| for deadline := time.Now().Add(10 * time.Second); egressShow(t, w.slice, "IPAddressAllow") != ""; time.Sleep(100 * time.Millisecond) { |
| if time.Now().After(deadline) { |
| t.Fatalf("under claims: slice still holds %q 10 s after the control service started", egressShow(t, w.slice, "IPAddressAllow")) |
| } |
| } |
| + restartedFrom("sweep", w.invocation) |
| if !egressWithin(t, time.Now().Add(2*time.Second), w.tenant, false, target) { |
| - egressFirewallDiag(t, egressClaimsUnit+".service", w.slice, w.tenant, target) |
| + egressFirewallDiag(t, unit, w.slice, w.tenant, target) |
| t.Fatal("under claims: 10.0.5.20 reachable after the sweep reset the slice") |
| } |
| // A hand regrant, closed by POST /egress through the production peer |
| - // check; GET /egress reports under_claims. |
| + // check; GET /egress reports under_claims and the restart. |
| egressMustCtl(t, "set-property", "--runtime", w.slice, "IPAddressAllow=", "IPAddressAllow=10.0.5.20/32") |
| if !egressWithin(t, time.Now().Add(2*time.Second), w.tenant, true, target) { |
| t.Fatal("hand regrant: 10.0.5.20 not reachable") |
| } |
| + before := egressShow(t, unit, "InvocationID") |
| reply := egress(w.host) |
| - deadline := time.Now().Add(2 * time.Second) |
| - if rep := reply.Egress.Hosts[w.host]; rep.State != instancectl.EgressUnderClaims || rep.Detail != claimsEgressDetail || len(rep.Allowed) != 0 { |
| - t.Fatalf("under claims: GET /egress[%s] = %+v, want under_claims %q and nothing allowed", w.host, rep, claimsEgressDetail) |
| + wantDetail := claimsEgressDetail + "; revoked (restarted)" |
| + if rep := reply.Egress.Hosts[w.host]; rep.State != instancectl.EgressUnderClaims || rep.Detail != wantDetail || len(rep.Allowed) != 0 { |
| + t.Fatalf("under claims: GET /egress[%s] = %+v, want under_claims %q and nothing allowed", w.host, rep, wantDetail) |
| } |
| if got := egressShow(t, w.slice, "IPAddressAllow"); got != "" { |
| t.Fatalf("under claims: slice holds %q after POST /egress", got) |
| } |
| - if !egressWithin(t, deadline, w.tenant, false, target) { |
| - egressFirewallDiag(t, egressClaimsUnit+".service", w.slice, w.tenant, target) |
| - t.Fatal("under claims: 10.0.5.20 reachable 2 s after POST /egress") |
| + restartedFrom("POST /egress", before) |
| + if !egressWithin(t, time.Now().Add(2*time.Second), w.tenant, false, target) { |
| + egressFirewallDiag(t, unit, w.slice, w.tenant, target) |
| + t.Fatal("under claims: 10.0.5.20 reachable after POST /egress") |
| } |
| } |