carlos / platform Public

instancectl: a start with no policy is not refused for a failed slice read unless the slice may hold an allow (#328)

Coordinator ruling on Task 15's concern 1. Start paths (run, sidecar,
seed, gen, cmd, the claim unit) assert through AssertStart: a read of
the slice that fails with nothing wanted reports failing and lets the
start go ahead, as main would, unless root knows the slice may hold an
allow (a change it owes its running services, or a last read-back that
held one); then it refuses as before. The sweep and POST /egress keep
Assert's behaviour: nothing written, failing, retried.

The byte-identical proof gains the failed-read start on every path
(main's outcome and transcript, the report failing); new tests pin the
refusal on a slice that held an allow or owes a revoke, and the start
going ahead again once a revoke read back empty.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev 5285f69e2dc1613d53a3caf8533b1d4327306753 parent f509134
7 files changed, +299 −42
  • internal/instancectl/claimunit.go +1 −1
  • internal/instancectl/cmd.go +1 −1
  • internal/instancectl/egress.go +58 −7
  • internal/instancectl/egress_restart_linux_test.go +52 −0
  • internal/instancectl/egress_startpaths_test.go +184 −30
  • internal/instancectl/gen.go +1 −1
  • internal/instancectl/server.go +2 −2
diff --git a/internal/instancectl/claimunit.go b/internal/instancectl/claimunit.go
index d03d0cc4e..34fc81ee5 100644
--- a/internal/instancectl/claimunit.go
+++ b/internal/instancectl/claimunit.go
@@ -171,7 +171,7 @@ func (u ClaimUnits) start(ctx context.Context, in ClaimUnit, launch *Launch, che
if err := sliceTier(ctx, u.Systemctl, in.Host, tier); err != nil {
return ClaimUnitStarted{}, err
}
- if err := u.Egress.Assert(ctx, in.Host); err != nil {
+ if err := u.Egress.AssertStart(ctx, in.Host); err != nil {
return ClaimUnitStarted{}, fmt.Errorf("egress: %w", err)
}
var invocation string
diff --git a/internal/instancectl/cmd.go b/internal/instancectl/cmd.go
index c55b4a569..3de279d51 100644
--- a/internal/instancectl/cmd.go
+++ b/internal/instancectl/cmd.go
@@ -88,7 +88,7 @@ func (s *Server) handleCmd(w http.ResponseWriter, r *http.Request) {
return
}
// The egress allow is asserted on a populated slice too (#328).
- if err := s.Egress.Assert(ctx, req.host); err != nil {
+ if err := s.Egress.AssertStart(ctx, req.host); err != nil {
http.Error(w, "egress: "+err.Error(), http.StatusBadGateway)
return
}
diff --git a/internal/instancectl/egress.go b/internal/instancectl/egress.go
index 7938a1f92..73bc676d6 100644
--- a/internal/instancectl/egress.go
+++ b/internal/instancectl/egress.go
@@ -122,6 +122,10 @@ type EgressAsserter struct {
// by the next Assert of the slice, though the slice already reads back
// what is wanted.
due map[string]egressDue
+ // heldSome is, per slice, whether its last successful read-back held
+ // an allow. A start path whose read fails with nothing wanted refuses
+ // only on such a slice, or one owing a change (mayHold, AssertStart).
+ heldSome map[string]bool
}
// egressDue is what a slice still owes its running services.
@@ -151,13 +155,30 @@ const egressFailingSweeps = 3
// Assert brings host's slice to its policy and records the report. It
// returns an error only when the slice could not be read or written, or
-// a write did not hold: a start path refuses the start on it. A read
-// that fails with nothing wanted writes nothing (no difference is
-// established); with an allow wanted, the allow is written and the read
-// after it decides. A policy that cannot be read or vouched for is not
-// an error: the slice is closed (or kept at the last policy read) and
-// the report says why.
+// a write did not hold. A read that fails with nothing wanted writes
+// nothing (no difference is established) and is an error, so the caller
+// and the loop retry; with an allow wanted, the allow is written and the
+// read after it decides. A policy that cannot be read or vouched for is
+// not an error: the slice is closed (or kept at the last policy read) and
+// the report says why. The sweep and POST /egress use it; start paths use
+// AssertStart.
func (e *EgressAsserter) Assert(ctx context.Context, host string) error {
+ return e.assert(ctx, host, false)
+}
+
+// AssertStart is Assert for a start path, which refuses the start on its
+// error. It differs in one case: a read that fails with nothing wanted
+// is not an error unless the slice may hold an allow (mayHold: a change
+// it owes its running services, or a last read-back that held
+// something). Main would have started that instance, and a host with no
+// policy must start as on main; the report says failing and the loop
+// retries. A slice that may hold an allow refuses, as Assert does, since
+// the start could otherwise run under an allow that is being revoked.
+func (e *EgressAsserter) AssertStart(ctx context.Context, host string) error {
+ return e.assert(ctx, host, true)
+}
+
+func (e *EgressAsserter) assert(ctx context.Context, host string, start bool) error {
if e == nil {
return nil
}
@@ -235,7 +256,12 @@ func (e *EgressAsserter) Assert(ctx context.Context, host string) error {
// reset written blind would leave a runtime override on a
// slice that may hold nothing. The caller and the loop retry,
// and a revoke lands as soon as the slice can be read.
- return fail("read back: " + readErr.Error() + "; nothing written")
+ err := fail("read back: " + readErr.Error() + "; nothing written")
+ if start && !e.mayHold(slice) {
+ e.logf("egress: %s: starting anyway: nothing is wanted and the slice is not known to hold an allow", slice)
+ return nil
+ }
+ return err
}
// An allow is wanted: write it, and the read after it decides.
e.logf("egress: %s: read back: %v; writing the allow", slice, readErr)
@@ -478,6 +504,30 @@ func (e *EgressAsserter) setDue(slice string, d egressDue) {
e.due[slice] = d
}
+// mayHold says whether slice may hold an allow as far as this asserter
+// knows: it owes its running services a change (due: recorded before any
+// write, so a write whose outcome is unknown counts), or its last
+// successful read-back held something (heldSome). A fresh asserter knows
+// nothing of a slice.
+func (e *EgressAsserter) mayHold(slice string) bool {
+ e.mu.Lock()
+ defer e.mu.Unlock()
+ return e.due[slice] != dueNone || e.heldSome[slice]
+}
+
+func (e *EgressAsserter) setHeldSome(slice string, some bool) {
+ e.mu.Lock()
+ defer e.mu.Unlock()
+ if !some {
+ delete(e.heldSome, slice)
+ return
+ }
+ if e.heldSome == nil {
+ e.heldSome = map[string]bool{}
+ }
+ e.heldSome[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 {
@@ -573,6 +623,7 @@ func (e *EgressAsserter) readBack(ctx context.Context, slice string) (heldSet, e
h.unknown = append(h.unknown, tok)
}
}
+ e.setHeldSome(slice, !h.equal(nil))
return h, nil
}
diff --git a/internal/instancectl/egress_restart_linux_test.go b/internal/instancectl/egress_restart_linux_test.go
index 7be41cec9..9c3d37c05 100644
--- a/internal/instancectl/egress_restart_linux_test.go
+++ b/internal/instancectl/egress_restart_linux_test.go
@@ -557,3 +557,55 @@ func TestEgressSweepUnnamedSliceKeepsTheClaimsRule(t *testing.T) {
})
}
}
+
+// TestEgressStartRefusedWhileARevokeIsOwed (coordinator ruling on Task
+// 15's concern 1): a revoke to empty whose restart failed leaves the slice
+// owing it, though the slice reads back empty. A start whose read then
+// fails is refused, as Assert refuses; a start on a slice the asserter
+// knows nothing of goes ahead, reporting failing, and writes nothing.
+func TestEgressStartRefusedWhileARevokeIsOwed(t *testing.T) {
+ slice := SliceName(egressHost)
+ failReads := func(e *EgressAsserter) {
+ next := e.Systemctl
+ e.Systemctl = func(ctx context.Context, args ...string) (string, error) {
+ if len(args) == 5 && args[2] == "IPAddressAllow" && args[4] == slice {
+ return "Failed to get properties", errors.New("exit status 1")
+ }
+ return next(ctx, args...)
+ }
+ }
+ t.Run("a revoke owed", func(t *testing.T) {
+ ctl, e, run := restartBox(t, egressHost)
+ ctl.restartFail[run] = true
+ if err := e.Assert(context.Background(), egressHost); err == nil {
+ t.Fatal("the revoke reported done though its restart failed")
+ }
+ if ctl.held[slice] != "" || !e.isRestartDue(slice) {
+ t.Fatalf("fixture: slice holds %q, restart due %v; want empty and due", ctl.held[slice], e.isRestartDue(slice))
+ }
+ failReads(e)
+ ctl.takeCalls()
+ if err := e.AssertStart(context.Background(), egressHost); err == nil {
+ t.Fatal("AssertStart let a start go ahead while a revoke is owed and the slice cannot be read")
+ }
+ if sets := ctl.sets(); len(sets) != 0 {
+ t.Fatalf("wrote %q", sets)
+ }
+ })
+ t.Run("nothing known", func(t *testing.T) {
+ ctl, e, _ := restartBox(t, egressHost) // the slice holds 10.0.5.20/32, unread
+ failReads(e)
+ if err := e.AssertStart(context.Background(), egressHost); err != nil {
+ t.Fatalf("AssertStart = %v; a slice not known to hold an allow starts as on main", err)
+ }
+ if rep := e.Reports()[egressHost]; rep.State != EgressFailing {
+ t.Fatalf("report = %+v, want failing", rep)
+ }
+ if err := e.Assert(context.Background(), egressHost); err == nil {
+ t.Fatal("Assert (the sweep, POST /egress) no longer fails on a failed read")
+ }
+ if sets := ctl.sets(); len(sets) != 0 {
+ t.Fatalf("wrote %q", sets)
+ }
+ })
+}
diff --git a/internal/instancectl/egress_startpaths_test.go b/internal/instancectl/egress_startpaths_test.go
index b16f9a275..2e678185a 100644
--- a/internal/instancectl/egress_startpaths_test.go
+++ b/internal/instancectl/egress_startpaths_test.go
@@ -15,13 +15,15 @@ import (
// egressLayer puts a slice's IPAddressAllow over a recording fake
// systemctl (#328): next records every call, a set-property's
-// IPAddressAllow= assignments replace what show then reads back, and
-// failSet fails that set-property once it is recorded.
+// IPAddressAllow= assignments replace what show then reads back, failSet
+// fails that set-property once it is recorded, and failShow fails every
+// read of an allow once it is recorded.
type egressLayer struct {
- next func(context.Context, ...string) (string, error)
- mu sync.Mutex
- held map[string]string // slice -> show's answer
- failSet error
+ next func(context.Context, ...string) (string, error)
+ mu sync.Mutex
+ held map[string]string // slice -> show's answer
+ failSet error
+ failShow error
}
func (l *egressLayer) run(ctx context.Context, args ...string) (string, error) {
@@ -33,6 +35,9 @@ func (l *egressLayer) run(ctx context.Context, args ...string) (string, error) {
defer l.mu.Unlock()
switch {
case len(args) == 5 && args[0] == "show" && args[1] == "-p" && args[2] == "IPAddressAllow" && args[3] == "--value":
+ if l.failShow != nil {
+ return "Failed to get properties: Connection timed out", l.failShow
+ }
return l.held[args[4]] + "\n", nil
case len(args) >= 4 && args[0] == "set-property" && strings.HasPrefix(args[3], "IPAddressAllow="):
if l.failSet != nil {
@@ -283,6 +288,27 @@ func runningSliceRoot(t *testing.T, host string) string {
return root
}
+// failedReadDetail is the report's Detail after failFirstAllowRead's
+// failure, with nothing wanted.
+const failedReadDetail = "read back: Failed to get properties: Connection timed out: exit status 1; nothing written"
+
+// failFirstAllowRead fails the first read of a host slice's allow, after
+// next has recorded it: systemd (or D-Bus) not answering once.
+func failFirstAllowRead(next func(context.Context, ...string) (string, error)) func(context.Context, ...string) (string, error) {
+ var mu sync.Mutex
+ failed := false
+ return func(ctx context.Context, args ...string) (string, error) {
+ out, err := next(ctx, args...)
+ mu.Lock()
+ defer mu.Unlock()
+ if !failed && len(args) == 5 && args[0] == "show" && args[2] == "IPAddressAllow" && strings.HasPrefix(args[4], hostSlicePrefix) {
+ failed = true
+ return "Failed to get properties: Connection timed out", errors.New("exit status 1")
+ }
+ return out, err
+ }
+}
+
// checkNoEgressWrite: no set-property of an allow on anything (a slice or
// a unit: the nudge), and no restart.
func checkNoEgressWrite(t *testing.T, transcript string) {
@@ -300,8 +326,11 @@ func checkNoEgressWrite(t *testing.T, transcript string) {
// every start path, with Egress set and no policy, the transcript must be
// the Egress-nil transcript (main's) plus the asserter's one read of the
// slice's allow: no set-property (so no runtime drop-in), no nudge of a
-// running service, no restart. Before claims and under them. The sweep's
-// half is TestStartTranscriptsUnchangedWithNoPolicyLoop.
+// running service, no restart. Before claims and under them; and with
+// the read failing, where the start still goes ahead as on main
+// (AssertStart: the asserter does not know the slice to hold an allow)
+// and the report says failing. The sweep's half is
+// TestStartTranscriptsUnchangedWithNoPolicyLoop.
func TestStartTranscriptsUnchangedWithNoPolicy(t *testing.T) {
for _, tc := range startPathCases() {
t.Run(tc.name, func(t *testing.T) {
@@ -310,20 +339,31 @@ func TestStartTranscriptsUnchangedWithNoPolicy(t *testing.T) {
t.Fatalf("Egress nil: status %d, body %q", status, body)
}
for _, a := range noPolicyAsserters() {
- t.Run(a.name, func(t *testing.T) {
- on := tc.ctl()
- status, body, s := runStartPath(t, tc, on, func(s *Server) {
- s.Egress = a.make(on.run, runningSliceRoot(t, genHost))
- })
- if status != 200 {
- t.Fatalf("Egress set: status %d, body %q", status, body)
- }
- checkNoEgressWrite(t, on.joined())
- checkOneReadMore(t, off.joined(), on.joined(), genHost)
- if got := s.Egress.Reports(); !reflect.DeepEqual(got, map[string]EgressReport{genHost: a.report}) {
- t.Fatalf("reports = %+v, want %s: %+v", got, genHost, a.report)
+ for _, failRead := range []bool{false, true} {
+ name, want := a.name, a.report
+ if failRead {
+ name += ", the read fails"
+ want = EgressReport{State: EgressFailing, Detail: failedReadDetail}
}
- })
+ t.Run(name, func(t *testing.T) {
+ on := tc.ctl()
+ ctl := on.run
+ if failRead {
+ ctl = failFirstAllowRead(on.run)
+ }
+ status, body, s := runStartPath(t, tc, on, func(s *Server) {
+ s.Egress = a.make(ctl, runningSliceRoot(t, genHost))
+ })
+ if status != 200 {
+ t.Fatalf("Egress set: status %d, body %q; main starts it", status, body)
+ }
+ checkNoEgressWrite(t, on.joined())
+ checkOneReadMore(t, off.joined(), on.joined(), genHost)
+ if got := s.Egress.Reports(); !reflect.DeepEqual(got, map[string]EgressReport{genHost: want}) {
+ t.Fatalf("reports = %+v, want %s: %+v", got, genHost, want)
+ }
+ })
+ }
}
})
}
@@ -333,19 +373,133 @@ func TestStartTranscriptsUnchangedWithNoPolicy(t *testing.T) {
t.Fatal(err)
}
for _, a := range noPolicyAsserters() {
- t.Run(a.name, func(t *testing.T) {
- u2, onCtl, in2, _ := claimUnitFixture(t)
- u2.Egress = a.make(onCtl.run, runningSliceRoot(t, in2.Host))
- if _, err := u2.Start(context.Background(), in2); err != nil {
- t.Fatal(err)
+ for _, failRead := range []bool{false, true} {
+ name, want := a.name, a.report
+ if failRead {
+ name += ", the read fails"
+ want = EgressReport{State: EgressFailing, Detail: failedReadDetail}
+ }
+ t.Run(name, func(t *testing.T) {
+ u2, onCtl, in2, _ := claimUnitFixture(t)
+ ctl := onCtl.run
+ if failRead {
+ ctl = failFirstAllowRead(onCtl.run)
+ }
+ u2.Egress = a.make(ctl, runningSliceRoot(t, in2.Host))
+ if _, err := u2.Start(context.Background(), in2); err != nil {
+ t.Fatalf("Start: %v; main starts it", err)
+ }
+ checkNoEgressWrite(t, onCtl.joined())
+ checkOneReadMore(t, offCtl.joined(), onCtl.joined(), in.Host)
+ if got := u2.Egress.Reports(); !reflect.DeepEqual(got, map[string]EgressReport{in2.Host: want}) {
+ t.Fatalf("reports = %+v, want %s: %+v", got, in2.Host, want)
+ }
+ })
+ }
+ }
+ })
+}
+
+// TestEgressStartRefusedWhenTheSliceMayHoldAnAllow (coordinator ruling on
+// Task 15's concern 1): a start whose read of the slice fails, with
+// nothing wanted, is still refused when root knows the slice may hold an
+// allow, and nothing is written:
+// - "held an allow": POST /egress read 10.0.5.20/32 back (already as
+// wanted, so nothing was written); the policy is then revoked;
+// - "a revoke owed": the slice owes its running services a revoke (as
+// after a revoke whose restart failed; the linux
+// TestEgressStartRefusedWhileARevokeIsOwed makes one).
+//
+// And once a revoke has landed and the slice read back empty ("revoked
+// since"), a start whose read fails goes ahead again, as on main.
+func TestEgressStartRefusedWhenTheSliceMayHoldAnAllow(t *testing.T) {
+ for _, tc := range startPathCases() {
+ for _, known := range []string{"held an allow", "a revoke owed", "revoked since"} {
+ t.Run(tc.name+"/"+known, func(t *testing.T) {
+ ctl := tc.ctl()
+ layer := &egressLayer{next: ctl.run, held: map[string]string{SliceName(genHost): "10.0.5.20/32"}}
+ var mu sync.Mutex
+ policy := EgressPolicy{Allow: []string{"10.0.5.20"}}
+ if known == "a revoke owed" {
+ policy, layer.held = EgressPolicy{}, nil
+ }
+ src := func(context.Context, string) (EgressPolicy, error) {
+ mu.Lock()
+ defer mu.Unlock()
+ return policy, nil
+ }
+ s, c := newServer(t, ctl, func(s *Server) {
+ tc.setup(t, s)
+ s.Egress = startPathAsserter(layer.run, src)
+ })
+ egressPost := func(want string) {
+ t.Helper()
+ resp := post(t, c, "/egress?host="+genHost)
+ resp.Body.Close()
+ if resp.StatusCode != 204 || layer.held[SliceName(genHost)] != want {
+ t.Fatalf("POST /egress = %d, slice %q: want 204 and %q", resp.StatusCode, layer.held[SliceName(genHost)], want)
+ }
+ }
+ switch known {
+ case "held an allow", "revoked since":
+ egressPost("10.0.5.20/32")
+ if strings.Contains(ctl.joined(), "set-property") {
+ t.Fatalf("fixture: POST /egress wrote:\n%s", ctl.joined())
+ }
+ mu.Lock()
+ policy = EgressPolicy{}
+ mu.Unlock()
+ if known == "revoked since" {
+ egressPost("")
+ }
+ case "a revoke owed":
+ s.Egress.setDue(SliceName(genHost), dueRevoke)
+ }
+ before := len(strings.Split(ctl.joined(), "\n"))
+ layer.mu.Lock()
+ layer.failShow = errors.New("exit status 1")
+ layer.mu.Unlock()
+
+ resp := post(t, c, tc.path)
+ defer resp.Body.Close()
+ body, _ := io.ReadAll(resp.Body)
+ if known == "revoked since" {
+ if resp.StatusCode != 200 {
+ t.Fatalf("status %d, body %q; a slice read back empty starts as on main", resp.StatusCode, body)
+ }
+ return
}
- checkNoEgressWrite(t, onCtl.joined())
- checkOneReadMore(t, offCtl.joined(), onCtl.joined(), in.Host)
- if got := u2.Egress.Reports(); !reflect.DeepEqual(got, map[string]EgressReport{in2.Host: a.report}) {
- t.Fatalf("reports = %+v, want %s: %+v", got, in2.Host, a.report)
+ if resp.StatusCode != 502 || !strings.HasPrefix(string(body), "egress: ") {
+ t.Fatalf("status %d, body %q; want 502 and egress: <err>", resp.StatusCode, body)
+ }
+ calls := strings.Split(ctl.joined(), "\n")[before:]
+ for _, c := range calls {
+ if strings.HasPrefix(c, "start ") || (strings.HasPrefix(c, "set-property ") && strings.Contains(c, "IPAddressAllow=")) {
+ t.Fatalf("got %q after the failed read:\n%s", c, strings.Join(calls, "\n"))
+ }
+ }
+ if rep := s.Egress.Reports()[genHost]; rep.State != EgressFailing {
+ t.Fatalf("report = %+v, want failing", rep)
}
})
}
+ }
+ t.Run("claim/held an allow", func(t *testing.T) {
+ u, ctl, in, _ := claimUnitFixture(t)
+ layer := &egressLayer{next: ctl.run}
+ u.Egress = startPathAsserter(layer.run, allowOneSource)
+ if err := u.Egress.Assert(context.Background(), in.Host); err != nil {
+ t.Fatal(err)
+ }
+ u.Egress.Source = staticSource(EgressPolicy{}, nil)
+ layer.failShow = errors.New("exit status 1")
+ ctl.calls = nil
+ if _, err := u.Start(context.Background(), in); err == nil || !strings.HasPrefix(err.Error(), "egress: ") {
+ t.Fatalf("Start = %v, want egress: <err>", err)
+ }
+ if calls := ctl.joined(); strings.Contains(calls, "start ") || strings.Contains(calls, "IPAddressAllow=") {
+ t.Fatalf("started or wrote after the failed read:\n%s", calls)
+ }
})
}
diff --git a/internal/instancectl/gen.go b/internal/instancectl/gen.go
index 1b6d34ad8..4de779bda 100644
--- a/internal/instancectl/gen.go
+++ b/internal/instancectl/gen.go
@@ -200,7 +200,7 @@ func (s *Server) startGen(w http.ResponseWriter, r *http.Request, req request) {
}
}
// The egress allow is asserted on a populated slice too (#328).
- if err := s.Egress.Assert(ctx, req.host); err != nil {
+ if err := s.Egress.AssertStart(ctx, req.host); err != nil {
fail(http.StatusBadGateway, "egress: "+err.Error())
return
}
diff --git a/internal/instancectl/server.go b/internal/instancectl/server.go
index ccb9fabb3..b3cc63aa7 100644
--- a/internal/instancectl/server.go
+++ b/internal/instancectl/server.go
@@ -557,7 +557,7 @@ func (s *Server) handleStart(w http.ResponseWriter, r *http.Request) {
}
// The egress allow is asserted whether or not the tier was left to
// the generation owner (#328).
- if err := s.Egress.Assert(ctx, req.host); err != nil {
+ if err := s.Egress.AssertStart(ctx, req.host); err != nil {
http.Error(w, "egress: "+err.Error(), http.StatusBadGateway)
return
}
@@ -636,7 +636,7 @@ func (s *Server) handleSeed(w http.ResponseWriter, r *http.Request) {
http.Error(w, "set-property: "+err.Error(), http.StatusBadGateway)
return
}
- if err := s.Egress.Assert(ctx, req.host); err != nil {
+ if err := s.Egress.AssertStart(ctx, req.host); err != nil {
http.Error(w, "egress: "+err.Error(), http.StatusBadGateway)
return
}