carlos / platform Public

host: egress witnesses hold each revoke to one deadline from the operation's return (#328)

Review fix round 1, finding 4: `closed` waited up to 10 s for the
restarted tenant and then started a fresh 2 s window. Now one deadline is
taken when the revoking operation (POST /egress, a sweep) returns and is
carried through the invocation check, the tenant serving again and the
connectivity check: egressLiveBound (2 s) for a live revoke or grant,
egressRestartBound (10 s) for one closed by restarting the slice's units.
The claims witness takes the sweep's deadline when the slice is first
seen empty (the sweep runs in the control service; the reset is written
before the restart), and the POST's when the peer's reply returns. The
widen check holds both addresses to one deadline.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev 9be4add61fc77fd8d45d89aedf2d0186fe28c7ce parent 06bb1fd
1 file changed, +72 −39
  • internal/host/egress_startpaths_rootrunner_linux_test.go +72 −39
diff --git a/internal/host/egress_startpaths_rootrunner_linux_test.go b/internal/host/egress_startpaths_rootrunner_linux_test.go
index 45189f42c..142194883 100644
--- a/internal/host/egress_startpaths_rootrunner_linux_test.go
+++ b/internal/host/egress_startpaths_rootrunner_linux_test.go
@@ -361,7 +361,23 @@ func egressTenantClient(socket string) *http.Client {
// egressServing waits up to 10 s for a tenant to answer on its socket.
func egressServing(t *testing.T, unit string, c *http.Client) {
t.Helper()
- deadline := time.Now().Add(10 * time.Second)
+ egressServingBy(t, unit, c, time.Now().Add(10*time.Second))
+}
+
+// The bounds a revoke is held to, each taken once, when the operation
+// that revokes (POST /egress, a sweep) returns, and carried through the
+// tenant's readiness and the connectivity check (review fix round 1):
+// a live revoke closes within egressLiveBound; one closed by restarting
+// the slice's units (closeByRestart) has egressRestartBound to restart
+// the unit, see it serve again and find the address closed.
+const (
+ egressLiveBound = 2 * time.Second
+ egressRestartBound = 10 * time.Second
+)
+
+// egressServingBy waits until deadline for a tenant to answer on its socket.
+func egressServingBy(t *testing.T, unit string, c *http.Client, deadline time.Time) {
+ t.Helper()
for {
resp, err := c.Get("http://tenant/")
if err == nil {
@@ -1119,12 +1135,14 @@ func (b *egressBox) runInvocation(t *testing.T) string {
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) {
+// closed checks a revoke to empty that has just returned, against the one
+// deadline taken when it returned (egressRevokeDeadline): the slice holds
+// nothing; with restarted, the run unit has a new invocation, serves again
+// and is closed to target, all by the deadline (egressRestartBound), and
+// the report says "revoked (restarted)"; otherwise it kept its invocation,
+// is closed by the deadline (egressLiveBound), and the report has no
+// detail.
+func (b *egressBox) closed(t *testing.T, step string, c *http.Client, target, before string, restarted bool, deadline time.Time) {
t.Helper()
unit := instancectl.UnitName(instancectl.RoleRun, b.host)
if got := b.allow(t); got != "" {
@@ -1135,13 +1153,13 @@ func (b *egressBox) closed(t *testing.T, step string, c *http.Client, target, be
if after == before {
t.Fatalf("%s: the run unit kept invocation %s: no restart with no parent allow", step, before)
}
- egressServing(t, unit, c)
+ egressServingBy(t, unit, c, deadline)
} 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) {
+ if !egressWithin(t, deadline, c, false, target) {
egressFirewallDiag(t, unit, b.slice, c, target)
- t.Fatalf("%s: 10.0.5.20 still reachable", step)
+ t.Fatalf("%s: 10.0.5.20 still reachable at the revoke's deadline", step)
}
wantDetail := ""
if restarted {
@@ -1152,6 +1170,16 @@ func (b *egressBox) closed(t *testing.T, step string, c *http.Client, target, be
}
}
+// egressRevokeDeadline is the deadline for a revoke whose operation has
+// just returned: egressRestartBound when it restarts the slice's units,
+// egressLiveBound when it is live.
+func egressRevokeDeadline(restarted bool) time.Time {
+ if restarted {
+ return time.Now().Add(egressRestartBound)
+ }
+ return time.Now().Add(egressLiveBound)
+}
+
func egressRevokeToEmpty(t *testing.T, parentAllow bool) {
b := newEgressPreClaimBox(t)
ctx := t.Context()
@@ -1160,7 +1188,7 @@ func egressRevokeToEmpty(t *testing.T, parentAllow bool) {
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"]) {
+ if !egressWithin(t, time.Now().Add(egressLiveBound), c, true, b.targets["fd00:5::20"]) {
t.Fatal("the parent's allow (fd00:5::20) is not in force for the run unit")
}
}
@@ -1178,7 +1206,7 @@ func egressRevokeToEmpty(t *testing.T, parentAllow bool) {
if err := b.client.Egress(ctx, b.host); err != nil {
t.Fatal("POST /egress (revoke):", err)
}
- b.closed(t, "revoke", c, target, before, restarted)
+ b.closed(t, "revoke", c, target, before, restarted, egressRevokeDeadline(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 {
@@ -1188,7 +1216,7 @@ func egressRevokeToEmpty(t *testing.T, parentAllow bool) {
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) {
+ if !egressWithin(t, time.Now().Add(egressLiveBound), c, true, target) {
t.Fatal("regrant: 10.0.5.20 not reachable 2 s after POST /egress")
}
if after := b.runInvocation(t); after != before {
@@ -1223,7 +1251,7 @@ func egressRevokeToEmpty(t *testing.T, parentAllow bool) {
// The next sweep (the 30 s loop's pass) closes it.
b.egress.Sweep(ctx)
- b.closed(t, "sweep", c, target, before, restarted)
+ b.closed(t, "sweep", c, target, before, restarted, egressRevokeDeadline(restarted))
if n := b.ctl.failed.Load(); n != 1 {
t.Fatalf("%d failed set-property, want 1", n)
}
@@ -1240,8 +1268,9 @@ func egressRevokeNarrowed(t *testing.T) {
if err := b.client.Egress(ctx, b.host); err != nil {
t.Fatal("POST /egress (widen):", err)
}
+ widened := time.Now().Add(egressLiveBound) // one deadline for both addresses
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]) {
+ if !egressWithin(t, widened, c, true, b.targets[a]) {
t.Fatalf("widen: %s not reachable 2 s after POST /egress", a)
}
}
@@ -1252,7 +1281,7 @@ func egressRevokeNarrowed(t *testing.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"]) {
+ if !egressWithin(t, time.Now().Add(egressLiveBound), 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")
}
@@ -1293,18 +1322,20 @@ func TestRootEgressMixedBoxWritesNothing(t *testing.T) {
if err := b.client.Egress(ctx, b.host); err != nil {
t.Fatal("POST /egress:", err)
}
+ // No parent allow on this box: the reset to empty restarts the run
+ // unit (closeByRestart). One deadline from the POST's return covers the
+ // restart, the tenant serving again and the address closed.
+ deadline := egressRevokeDeadline(true)
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) {
+ egressServingBy(t, instancectl.UnitName(instancectl.RoleRun, b.host), c, deadline)
+ if !egressWithin(t, deadline, 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")
+ t.Fatal("mixed box: 10.0.5.20 still reachable at the revoke's deadline")
}
rep := b.report(t)
if rep.State != instancectl.EgressNotNested || len(rep.Allowed) != 0 || !strings.Contains(rep.Detail, flat) || !strings.HasSuffix(rep.Detail, "; revoked (restarted)") {
@@ -1487,7 +1518,7 @@ func (w *egressUnderClaimsWitness) prepare(t *testing.T) {
t.Fatal("baseline: the tenant reaches 10.0.5.20 with no allow: the template's deny is not in force")
}
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) {
+ if !egressWithin(t, time.Now().Add(egressLiveBound), w.tenant, true, target) {
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")
}
@@ -1501,40 +1532,46 @@ func (w *egressUnderClaimsWitness) check(t *testing.T, egress func(host string)
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) {
+ // closedBy checks, against one deadline, that the tenant's unit left
+ // invocation before (no allow on carlos-instances.slice here, so a
+ // revoke to empty restarts the slice's running units: closeByRestart),
+ // serves again, and is closed to target.
+ closedBy := func(step, before string, deadline time.Time) {
t.Helper()
- for deadline := time.Now().Add(10 * time.Second); egressShow(t, unit, "InvocationID") == before; time.Sleep(100 * time.Millisecond) {
+ for egressShow(t, unit, "InvocationID") == before {
if time.Now().After(deadline) {
t.Fatalf("%s: %s kept invocation %s: the revoke to empty did not restart it", step, unit, before)
}
+ time.Sleep(100 * time.Millisecond)
+ }
+ egressServingBy(t, unit, w.tenant, deadline)
+ if !egressWithin(t, deadline, w.tenant, false, target) {
+ egressFirewallDiag(t, unit, w.slice, w.tenant, target)
+ t.Fatalf("%s: 10.0.5.20 reachable at the revoke's deadline", step)
}
- egressServing(t, unit, w.tenant)
}
// The control service's first sweep (RunWithClaim's Loop) resets the
// allow granted before claims were switched on, and restarts the tenant.
+ // The sweep runs in the control service, so its return cannot be seen
+ // here: the deadline is taken when the slice is first seen empty (the
+ // reset is written before the restart), with egressRestartBound.
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, unit, w.slice, w.tenant, target)
- t.Fatal("under claims: 10.0.5.20 reachable after the sweep reset the slice")
- }
+ closedBy("sweep", w.invocation, time.Now().Add(egressRestartBound))
// A hand regrant, closed by POST /egress through the production peer
// 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) {
+ if !egressWithin(t, time.Now().Add(egressLiveBound), 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(egressRestartBound) // from the POST's return
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)
@@ -1542,9 +1579,5 @@ func (w *egressUnderClaimsWitness) check(t *testing.T, egress func(host string)
if got := egressShow(t, w.slice, "IPAddressAllow"); got != "" {
t.Fatalf("under claims: slice holds %q after POST /egress", got)
}
- 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")
- }
+ closedBy("POST /egress", before, deadline)
}