carlos / platform Public

instancectl: egress keeps the held allow in a failed report; no blind reset on a failed read (#328)

Review fix round 1. A failed write or post-write read keeps the last
successful read-back in Allowed, re-reads once after a failed write to
catch a partial change, and says in Detail when the held list may be
stale. A first read that fails with nothing wanted writes nothing and
reports failing (the caller and the loop retry); with an allow wanted
the allow is still written.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev fa9c586e826fa529723d079613cf8178de5c4bc2 parent 3b54d94
2 files changed, +162 −42
  • internal/instancectl/egress.go +50 −20
  • internal/instancectl/egress_test.go +112 −22
diff --git a/internal/instancectl/egress.go b/internal/instancectl/egress.go
index 16c070def..d0eacb674 100644
--- a/internal/instancectl/egress.go
+++ b/internal/instancectl/egress.go
@@ -86,9 +86,12 @@ type EgressAsserter struct {
// 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 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 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.
func (e *EgressAsserter) Assert(ctx context.Context, host string) error {
if e == nil {
return nil
@@ -130,15 +133,33 @@ func (e *EgressAsserter) Assert(ctx context.Context, host string) error {
slice := SliceName(host)
held, readErr := e.readBack(ctx, slice)
- if readErr == nil && held.equal(want) {
+ if readErr == nil {
+ // The last successful read-back stands in the report until a
+ // later read replaces it, so a failed write never hides what the
+ // slice was seen to hold.
rep.Allowed = held.addrs()
- e.record(host, rep)
- return nil
+ if held.equal(want) {
+ e.record(host, rep)
+ return nil
+ }
}
- if readErr != nil {
- // A read-back that fails is a difference: the write below is
- // what is wanted either way, and the read after it decides.
- e.logf("egress: %s: read back: %v", slice, readErr)
+ readBefore := readErr == nil
+ fail := func(detail string) error {
+ rep.State, rep.Detail = EgressFailing, detail
+ e.record(host, rep)
+ e.logf("egress: %s: %s", slice, detail)
+ return fmt.Errorf("%s: %s", slice, detail)
+ }
+ if !readBefore {
+ if len(want) == 0 {
+ // No difference is established and nothing is wanted: a
+ // 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")
+ }
+ // An allow is wanted: write it, and the read after it decides.
+ e.logf("egress: %s: read back: %v; writing the allow", slice, readErr)
}
args := []string{"set-property", "--runtime", slice, "IPAddressAllow="}
@@ -150,28 +171,37 @@ func (e *EgressAsserter) Assert(ctx context.Context, host string) error {
args = append(args, "IPAddressAllow="+strings.Join(list, " "))
}
if out, err := e.Systemctl(ctx, args...); err != nil {
- rep.State, rep.Detail = EgressFailing, fmt.Sprintf("set-property: %s: %v", strings.TrimSpace(out), err)
- e.record(host, rep)
- return fmt.Errorf("%s: %s", slice, rep.Detail)
+ detail := fmt.Sprintf("set-property: %s: %v", strings.TrimSpace(out), err)
+ // The write may have changed the slice in part: read it once more.
+ after, rerr := e.readBack(ctx, slice)
+ if rerr != nil {
+ return fail(detail + "; read back: " + rerr.Error() + "; " + heldUncertain(readBefore))
+ }
+ rep.Allowed = after.addrs()
+ return fail(detail + "; the slice now holds " + after.text())
}
held, readErr = e.readBack(ctx, slice)
if readErr != nil {
- rep.State, rep.Detail = EgressFailing, "read back after the write: "+readErr.Error()
- e.record(host, rep)
- return fmt.Errorf("%s: %s", slice, rep.Detail)
+ return fail("read back after the write: " + readErr.Error() + "; " + heldUncertain(readBefore))
}
rep.Allowed = held.addrs()
if !held.equal(want) {
- rep.State, rep.Detail = EgressFailing, fmt.Sprintf("after the write the slice holds %s, not %s", held.text(), prefixesText(want))
- e.record(host, rep)
- e.logf("egress: %s: %s", slice, rep.Detail)
- return fmt.Errorf("%s: %s", slice, rep.Detail)
+ 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))
e.record(host, rep)
return nil
}
+// heldUncertain says what the report's Allowed is worth when the slice
+// could not be read after a write.
+func heldUncertain(readBefore bool) string {
+ if readBefore {
+ return "what the slice holds is as read before the write and may have changed"
+ }
+ return "what the slice holds is not known"
+}
+
// Reports is a copy of every host's last report.
func (e *EgressAsserter) Reports() map[string]EgressReport {
if e == nil {
diff --git a/internal/instancectl/egress_test.go b/internal/instancectl/egress_test.go
index d1284bb7f..b21d1e0f0 100644
--- a/internal/instancectl/egress_test.go
+++ b/internal/instancectl/egress_test.go
@@ -369,40 +369,130 @@ func TestEgressReportsIsACopy(t *testing.T) {
}
}
-func TestEgressReadBackErrorStillWrites(t *testing.T) {
- ctl := newEgressCtl()
- ctl.setHeld(SliceName(egressHost), "10.0.5.21/32")
- e := newEgressAsserter(ctl, staticSource(EgressPolicy{}, nil))
- first := true
- e.Systemctl = func(ctx context.Context, args ...string) (string, error) {
+// failingShow wraps ctl so that the first n `show` calls fail (each still
+// recorded); everything else is ctl's.
+func failingShow(ctl *egressCtl, n int) func(context.Context, ...string) (string, error) {
+ return func(ctx context.Context, args ...string) (string, error) {
out, err := ctl.run(ctx, args...)
- if args[0] == "show" && first {
- first = false
+ if args[0] == "show" && n > 0 {
+ n--
return "Failed to get properties", errors.New("exit status 1")
}
return out, err
}
+}
+
+// Fix round 1, item 2: a read that fails with nothing wanted establishes
+// no difference, so nothing is written; the next assert whose read works
+// lands the revoke.
+func TestEgressReadErrorWithNothingWantedWritesNothing(t *testing.T) {
+ ctl := newEgressCtl()
+ ctl.setHeld(SliceName(egressHost), "10.0.5.20/32")
+ e := newEgressAsserter(ctl, staticSource(EgressPolicy{Closed: EgressUnderClaims}, nil))
+ e.Systemctl = failingShow(ctl, 1)
+ if err := e.Assert(context.Background(), egressHost); err == nil {
+ t.Fatal("Assert = nil, want the read error")
+ }
+ wantCalls(t, ctl.takeCalls(), showCall(egressHost))
+ if rep := e.Reports()[egressHost]; rep.State != EgressFailing || !strings.Contains(rep.Detail, "Failed to get properties") || rep.Allowed != nil {
+ t.Fatalf("report = %+v, want state %q naming the read error", rep, EgressFailing)
+ }
+
rep := mustAssert(t, e, egressHost)
- wantCalls(t, ctl.sets(), setCall(egressHost))
- if !reflect.DeepEqual(rep, EgressReport{}) {
- t.Fatalf("report = %+v, want {}", rep)
+ wantCalls(t, ctl.takeCalls(), showCall(egressHost), setCall(egressHost), showCall(egressHost))
+ if rep.State != EgressUnderClaims || rep.Allowed != nil {
+ t.Fatalf("report = %+v, want state %q and nothing allowed", rep, EgressUnderClaims)
+ }
+
+ // No policy at all: the same, never a blind reset.
+ ctl2 := newEgressCtl()
+ e2 := newEgressAsserter(ctl2, staticSource(EgressPolicy{}, nil))
+ e2.Systemctl = failingShow(ctl2, 1)
+ if err := e2.Assert(context.Background(), egressHost); err == nil {
+ t.Fatal("Assert with no policy = nil, want the read error")
}
+ wantCalls(t, ctl2.takeCalls(), showCall(egressHost))
}
-func TestEgressWriteErrorIsFailing(t *testing.T) {
+// Fix round 1, item 2: with an allow wanted, a failed first read still
+// writes it, and the read after the write decides.
+func TestEgressReadErrorWithAnAllowWantedWrites(t *testing.T) {
ctl := newEgressCtl()
e := newEgressAsserter(ctl, staticSource(EgressPolicy{Allow: []string{"10.0.5.20"}}, nil))
- e.Systemctl = func(ctx context.Context, args ...string) (string, error) {
- if args[0] == "set-property" {
- return "Failed to set unit properties", errors.New("exit status 1")
- }
- return ctl.run(ctx, args...)
- }
- if err := e.Assert(context.Background(), egressHost); err == nil {
- t.Fatal("Assert = nil, want the set-property error")
+ e.Systemctl = failingShow(ctl, 1)
+ rep := mustAssert(t, e, egressHost)
+ wantCalls(t, ctl.takeCalls(), showCall(egressHost), setCall(egressHost, "10.0.5.20/32"), showCall(egressHost))
+ if want := (EgressReport{Allowed: []string{"10.0.5.20"}}); !reflect.DeepEqual(rep, want) {
+ t.Fatalf("report = %+v, want %+v", rep, want)
}
- if rep := e.Reports()[egressHost]; rep.State != EgressFailing || !strings.Contains(rep.Detail, "Failed to set unit properties") {
- t.Fatalf("report = %+v, want state %q naming systemctl's output", rep, EgressFailing)
+}
+
+// Fix round 1, item 1: a failed write never hides an allow the slice was
+// seen to hold. Each case revokes a held 10.0.5.20/32.
+func TestEgressWriteErrorIsFailing(t *testing.T) {
+ for _, tc := range []struct {
+ name string
+ setFails bool // set-property returns an error
+ setApplies bool // ... after changing the slice anyway (a partial change)
+ failShowsAt int // 1-based show call that fails (0: none)
+ wantAllowed []string
+ wantDetail []string
+ }{
+ {name: "reset fails, slice unchanged", setFails: true,
+ wantAllowed: []string{"10.0.5.20"}, wantDetail: []string{"Failed to set unit properties", "the slice now holds 10.0.5.20"}},
+ {name: "reset fails, slice changed anyway", setFails: true, setApplies: true,
+ wantAllowed: nil, wantDetail: []string{"Failed to set unit properties", "the slice now holds nothing"}},
+ {name: "reset fails, re-read fails", setFails: true, failShowsAt: 2,
+ wantAllowed: []string{"10.0.5.20"}, wantDetail: []string{"Failed to set unit properties", "as read before the write and may have changed"}},
+ {name: "reset written, read after it fails", setApplies: true, failShowsAt: 2,
+ wantAllowed: []string{"10.0.5.20"}, wantDetail: []string{"read back after the write", "as read before the write and may have changed"}},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ ctl := newEgressCtl()
+ ctl.setHeld(SliceName(egressHost), "10.0.5.20/32")
+ e := newEgressAsserter(ctl, staticSource(EgressPolicy{Closed: EgressExpired}, nil))
+ shows := 0
+ e.Systemctl = func(ctx context.Context, args ...string) (string, error) {
+ switch args[0] {
+ case "set-property":
+ if !tc.setApplies {
+ ctl.mu.Lock()
+ ctl.calls = append(ctl.calls, strings.Join(args, " "))
+ ctl.mu.Unlock()
+ } else if _, err := ctl.run(ctx, args...); err != nil {
+ return "", err
+ }
+ if tc.setFails {
+ return "Failed to set unit properties", errors.New("exit status 1")
+ }
+ return "", nil
+ case "show":
+ shows++
+ out, err := ctl.run(ctx, args...)
+ if shows == tc.failShowsAt {
+ return "Failed to get properties", errors.New("exit status 1")
+ }
+ return out, err
+ }
+ return ctl.run(ctx, args...)
+ }
+ if err := e.Assert(context.Background(), egressHost); err == nil {
+ t.Fatal("Assert = nil, want an error")
+ }
+ wantCalls(t, ctl.sets(), setCall(egressHost))
+ rep := e.Reports()[egressHost]
+ if rep.State != EgressFailing {
+ t.Fatalf("State = %q, want %q", rep.State, EgressFailing)
+ }
+ if !reflect.DeepEqual(rep.Allowed, tc.wantAllowed) {
+ t.Fatalf("Allowed = %q, want %q", rep.Allowed, tc.wantAllowed)
+ }
+ for _, d := range tc.wantDetail {
+ if !strings.Contains(rep.Detail, d) {
+ t.Fatalf("Detail = %q, want it to contain %q", rep.Detail, d)
+ }
+ }
+ })
}
}