carlos / platform Public

region HA P3b-2: readback and lookup failures do not revoke a live process

claude agent, operated by paul@keymail.dev 265a34b268df972ba69f7a3a49e4ce29bdfe55b0 parent 70de4f3
3 files changed, +48 −28
  • internal/instancectl/hairpin.go +9 −7
  • internal/instancectl/hairpin_invariant_test.go +30 −15
  • internal/instancectl/hairpin_retry_linux_test.go +9 −6
diff --git a/internal/instancectl/hairpin.go b/internal/instancectl/hairpin.go
index 280b08b20..fe37e1737 100644
--- a/internal/instancectl/hairpin.go
+++ b/internal/instancectl/hairpin.go
@@ -27,13 +27,13 @@ var (
hairpinInstall = (*hairpin.Lease).Install
hairpinVerify = (*hairpin.Lease).Verify
hairpinCheck = (*hairpin.Lease).CheckAddresses
- hairpinCurrent = hairpin.Current
hairpinMainProcess = hairpin.MainProcess
hairpinPrepare = (*hairpin.Lease).Prepare
hairpinResetServer = newHairpinResetServer
)
type hairpinLeaseKey struct{}
+type hairpinEnablingKey struct{}
type hairpinRequest struct {
Enable *bool `json:"enable"`
Process hairpin.Process `json:"process"`
@@ -100,7 +100,7 @@ func (s *Server) hairpinDeny(ctx context.Context, local []netip.Prefix) ([]netip
}
return errors.Join(readErr, s.applyLocalDeny(cleanup, local, true))
}
- if hairpinContextError(ctx, err) {
+ if enabling, _ := ctx.Value(hairpinEnablingKey{}).(bool); enabling || hairpinContextError(ctx, err) {
return local, errors.Join(err, op.Rollback(ctx))
}
return local, errors.Join(err, op.Revoke(ctx))
@@ -121,13 +121,14 @@ func (s *Server) hairpinOperations(ctx context.Context, l *hairpin.Lease, p hair
Exempt: func(ctx context.Context, on bool) error {
if on {
l.State = hairpin.State{Process: p, Active: true}
+ ctx = context.WithValue(ctx, hairpinEnablingKey{}, true)
} else {
l.State.Active = false
}
return s.denyLocalAddrs(ctx)
},
Save: func(on, closed bool) error {
- // Rollback cannot undo a Revoke performed by exemption readback.
+ // Rollback cannot undo an existing revocation for this process.
closed = closed || (l.State.Process == p && l.State.Closed)
state := hairpin.State{Process: p, Active: on && !closed, Closed: closed}
l.State = state
@@ -371,10 +372,11 @@ func (s *Server) resumeGroupHairpin(ctx context.Context) error {
}
p := l.State.Process
op := s.hairpinOperations(ctx, l, p)
- if err := hairpinCurrent(ctx, p); err != nil {
- if hairpinContextError(ctx, err) {
- return op.Rollback(ctx)
- }
+ current, lookupErr := hairpinMainProcess(ctx)
+ if lookupErr != nil {
+ return op.Rollback(ctx)
+ }
+ if p.PID <= 0 || current != p {
return op.Revoke(ctx)
}
if !l.State.Active {
diff --git a/internal/instancectl/hairpin_invariant_test.go b/internal/instancectl/hairpin_invariant_test.go
index 82f096286..1df667cc2 100644
--- a/internal/instancectl/hairpin_invariant_test.go
+++ b/internal/instancectl/hairpin_invariant_test.go
@@ -35,6 +35,7 @@ type hairpinMachine struct {
trace []string
removed, deactivated bool
readbackFailure bool
+ lookupFailed bool
}
func newHairpinMachine(t *testing.T) *hairpinMachine {
@@ -43,12 +44,12 @@ func newHairpinMachine(t *testing.T) *hairpinMachine {
m.l = &hairpin.Lease{Config: hairpin.Config{PublicIPs: []string{"8.8.8.8", "9.9.9.9"}}}
resetServer := hairpinResetServer
auth, watch, save, addresses, euid := hairpinAuthenticate, hairpinWatchExit, hairpinSave, hairpinAddresses, hairpinEUID
- install, verify, check, current, main, prepare := hairpinInstall, hairpinVerify, hairpinCheck, hairpinCurrent, hairpinMainProcess, hairpinPrepare
+ install, verify, check, main, prepare := hairpinInstall, hairpinVerify, hairpinCheck, hairpinMainProcess, hairpinPrepare
t.Cleanup(func() {
m.s.stopHairpinWatch()
hairpinResetServer = resetServer
hairpinAuthenticate, hairpinWatchExit, hairpinSave, hairpinAddresses, hairpinEUID = auth, watch, save, addresses, euid
- hairpinInstall, hairpinVerify, hairpinCheck, hairpinCurrent, hairpinMainProcess, hairpinPrepare = install, verify, check, current, main, prepare
+ hairpinInstall, hairpinVerify, hairpinCheck, hairpinMainProcess, hairpinPrepare = install, verify, check, main, prepare
})
hairpinResetServer = func() *Server { return m.s }
hairpinAuthenticate = func(ctx context.Context, _ net.Conn, p hairpin.Process) error {
@@ -57,16 +58,16 @@ func newHairpinMachine(t *testing.T) *hairpinMachine {
}
return ctx.Err()
}
- hairpinCurrent = func(ctx context.Context, p hairpin.Process) error {
+ hairpinMainProcess = func(ctx context.Context) (hairpin.Process, error) {
if err := m.step(ctx, "current"); err != nil {
- return err
+ m.lookupFailed = true
+ return hairpin.Process{}, err
}
- if !m.live || p != m.p {
- return errors.New("dead")
+ if !m.live {
+ return hairpin.Process{}, nil
}
- return nil
+ return m.p, nil
}
- hairpinMainProcess = func(ctx context.Context) (hairpin.Process, error) { return m.p, ctx.Err() }
hairpinPrepare = func(*hairpin.Lease, context.Context) error { return nil }
hairpinEUID = func() int { return 0 }
hairpinWatchExit = func(p hairpin.Process, _ func()) (func(), error) {
@@ -96,6 +97,9 @@ func newHairpinMachine(t *testing.T) *hairpinMachine {
}
hairpinCheck = func(_ *hairpin.Lease, ctx context.Context, on bool) error {
if err := m.step(ctx, "check"); err != nil {
+ if !hairpinContextError(ctx, err) {
+ m.readbackFailure = true
+ }
return err
}
if m.addresses != on {
@@ -116,6 +120,9 @@ func newHairpinMachine(t *testing.T) *hairpinMachine {
return err
}
m.addresses = on
+ if on {
+ return hairpinCheck(m.l, ctx, true)
+ }
if !on {
m.removed = true
}
@@ -198,6 +205,7 @@ func (m *hairpinMachine) context() (context.Context, context.CancelFunc) {
m.removed = false
m.deactivated = false
m.readbackFailure = false
+ m.lookupFailed = false
m.readbackCanceled = false
return ctx, cancel
}
@@ -254,7 +262,7 @@ func TestGroupHairpinRevocationGuard(t *testing.T) {
active, closed, successor bool
fail, cancel string
wantActive, wantClosed bool
- next int
+ next, nth int
}{
{name: "N1/predecessor-removal", path: "enable", active: true, successor: true, fail: "remove", next: 204},
{name: "N1/predecessor-watch", path: "enable", active: true, successor: true, fail: "watch", next: 204},
@@ -276,6 +284,9 @@ func TestGroupHairpinRevocationGuard(t *testing.T) {
{name: "exit", path: "exit", active: true, wantClosed: true, next: 403},
{name: "install", path: "enable", fail: "install", next: 204},
{name: "verify", path: "enable", fail: "verify", next: 204},
+ {name: "enable-readback-verify", path: "enable", fail: "verify", nth: 2, next: 204},
+ {name: "enable-readback-check", path: "enable", fail: "check", nth: 2, next: 204},
+ {name: "resume-current", path: "resume", active: true, fail: "current", next: 204},
{name: "addresses", path: "enable", fail: "addresses", next: 204},
{name: "exempt", path: "enable", fail: "exempt", next: 204},
{name: "save", path: "enable", fail: "save", next: 204},
@@ -288,7 +299,7 @@ func TestGroupHairpinRevocationGuard(t *testing.T) {
if tc.successor {
m.p.Start = "2"
}
- m.fail, m.cancelAt = tc.fail, tc.cancel
+ m.fail, m.cancelAt, m.nth = tc.fail, tc.cancel, tc.nth
ctx, cancel := m.context()
defer cancel()
m.act(ctx, tc.path)
@@ -340,7 +351,7 @@ func TestGroupHairpinSingleExitWatch(t *testing.T) {
func TestGroupHairpinSeededInvariants(t *testing.T) {
paths := []string{"enable", "enable", "disable", "clear", "resume", "exit", "successor", "deny", "stale"}
- failures := []string{"", "install", "verify", "addresses", "remove", "exempt", "deny", "save", "save-off", "watch"}
+ failures := []string{"", "current", "install", "verify", "check", "addresses", "remove", "exempt", "deny", "save", "save-off", "watch"}
cancellations := []string{"", "current", "watch", "install", "verify", "check", "addresses", "remove", "exempt", "deny", "save", "save-off"}
for seed := int64(0); seed < 500; seed++ {
t.Run(fmt.Sprintf("seed=%d", seed), func(t *testing.T) {
@@ -371,7 +382,14 @@ func TestGroupHairpinSeededInvariants(t *testing.T) {
}
default:
m.act(ctx, path)
- if path == "clear" || path == "disable" && m.live || path == "exit" && prior.Process == m.p {
+ if path == "clear" {
+ if m.lookupFailed || !m.live {
+ revoked[prior.Process] = true
+ } else {
+ revoked[m.p] = true
+ }
+ }
+ if path == "disable" && m.live || path == "exit" && prior.Process == m.p {
revoked[m.p] = true
}
if path == "resume" && prior.Process != m.p || path == "resume" && !m.live {
@@ -382,9 +400,6 @@ func TestGroupHairpinSeededInvariants(t *testing.T) {
if (path == "deny" || path == "resume") && prior.Active && m.readbackFailure && !m.readbackCanceled {
revoked[prior.Process] = true
}
- if path == "enable" && !prior.Active && m.readbackFailure && m.seen == 2 && !m.readbackCanceled {
- revoked[m.p] = true
- }
}
cancel()
if m.l.State.Active && revoked[m.l.State.Process] {
diff --git a/internal/instancectl/hairpin_retry_linux_test.go b/internal/instancectl/hairpin_retry_linux_test.go
index 77b43fef6..5454d5d2c 100644
--- a/internal/instancectl/hairpin_retry_linux_test.go
+++ b/internal/instancectl/hairpin_retry_linux_test.go
@@ -237,10 +237,9 @@ func TestGroupHairpinHandlerRollbackFailedRemovalRestoresDeny(t *testing.T) {
f.state(t, true, false)
}
-func TestGroupHairpinHandlerEnableRevocationKeepsTombstone(t *testing.T) {
+func TestGroupHairpinHandlerEnableReadbackFailureRetries(t *testing.T) {
f := newHairpinHandlerFixture(t)
- // Exemption refresh verifies again after address assignment. This refusal is
- // a security revocation even when it happens during the first enable.
+ // Exemption refresh verifies again before the first exemption is applied.
local := f.s.LocalAddrs
f.s.LocalAddrs = func() ([]netip.Prefix, error) {
if f.addresses {
@@ -251,11 +250,15 @@ func TestGroupHairpinHandlerEnableRevocationKeepsTombstone(t *testing.T) {
if got := f.request(t, true, f.p); got != 503 {
t.Fatal(got)
}
- f.state(t, false, true)
+ f.state(t, false, false)
f.fullDeny(t)
- if got := f.request(t, true, f.p); got != 409 {
- t.Fatalf("revocation retry=%d", got)
+ f.s.LocalAddrs = local
+ rules, _ := f.l.Config.Rules()
+ f.write(t, "rules", rules)
+ if got := f.request(t, true, f.p); got != 204 {
+ t.Fatalf("readback retry=%d", got)
}
+ f.state(t, true, false)
}
func TestGroupHairpinHandlerSuccessorFailedEnableRetry(t *testing.T) {