carlos / platform Public

region HA: restrict live takeover to unbound identities

Enforce the invariant that live EC2 fallback is allowed only when the owner has no binding. Identity read errors, invalid records and identifier mismatches fail closed. Keep finalizer refusal observations and the fallback lifetime recheck.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by claude agent, operated by paul@keymail.dev 93f01b57b504b5776a276f3700c04fbe72422748 parent 6be0846
7 files changed, +153 −7
  • internal/activator/haadopt.go +10 −2
  • internal/activator/termination_proof_test.go +2 −2
  • internal/host/hadeparture_identity.go +20 −1
  • internal/host/hadepartures_proof_followups_test.go +116 −0
  • internal/lease/termination_proof.go +1 −1
  • internal/lease/termination_proof_test.go +1 −1
  • internal/termfence/termination_proof.go +3 −0
diff --git a/internal/activator/haadopt.go b/internal/activator/haadopt.go
index 272895d8e..0afc8e2cc 100644
--- a/internal/activator/haadopt.go
+++ b/internal/activator/haadopt.go
@@ -296,9 +296,17 @@ func (a *Service) AdoptHost(ctx context.Context, rt registry.Route, req Adoption
if err = phase("acquiring", expected, nil); err != nil {
return Deferred, err
}
- ctx = lease.WithTerminationProof(ctx, b.TerminationProofs)
ctx = context.WithValue(ctx, adoptionCauseKey{}, true)
- held, err := lease.AcquireHAForDuty(ctx, b.Cfg.Bucket, b.Cfg.hibernatePrefix(rt.Host), b.Cfg.Region, hafresh.Marker{Box: self.Ref, IID: self.IID, Lifetime: req.Lifetime}, hafresh.Proofs{SameBoxEmpty: b.sameBoxEmpty(ctx, rt), OwnerTerminated: b.ownerTerminated(ctx, rt)}, func(v hafresh.View) error {
+ proofs := hafresh.Proofs{SameBoxEmpty: b.sameBoxEmpty(ctx, rt), OwnerTerminated: b.ownerTerminated(ctx, rt)}
+ if b.TerminationProofs != nil {
+ adapter := *b.TerminationProofs
+ adapter.Observe = func(iid string, gone bool, err error) { b.reportTakeover(ctx, rt, iid, gone, err) }
+ if b.OwnerTerminated != nil {
+ proofs.OwnerTerminated = func(iid string) (bool, error) { return b.OwnerTerminated(ctx, iid) }
+ }
+ ctx = lease.WithTerminationProof(ctx, &adapter)
+ }
+ held, err := lease.AcquireHAForDuty(ctx, b.Cfg.Bucket, b.Cfg.hibernatePrefix(rt.Host), b.Cfg.Region, hafresh.Marker{Box: self.Ref, IID: self.IID, Lifetime: req.Lifetime}, proofs, func(v hafresh.View) error {
if v.ETag != view.ETag || v.Lock.Generation != view.Lock.Generation {
return ErrDutyDeferred
}
diff --git a/internal/activator/termination_proof_test.go b/internal/activator/termination_proof_test.go
index 21ad20353..dffb553a5 100644
--- a/internal/activator/termination_proof_test.go
+++ b/internal/activator/termination_proof_test.go
@@ -51,7 +51,7 @@ func TestTerminationProofWiring(t *testing.T) {
t.Fatal("unverified identity granted acquire")
}
}
- if fallbacks != 1 || resolves != 1 {
+ if fallbacks != 0 || resolves != 1 {
t.Fatalf("fallbacks=%d resolves=%d", fallbacks, resolves)
}
cause := "visitor"
@@ -82,7 +82,7 @@ func TestTerminationProofUnboundTakeover(t *testing.T) {
b.Cfg.Region = "eu-west-1"
logs := captureLog(t)
b.TerminationProofs = &lease.TerminationProofAdapter{Cache: &termfence.ProofCache{}, Resolve: func(context.Context, string, string) (termfence.ProofIdentity, error) {
- return termfence.ProofIdentity{}, errors.New("unverified")
+ return termfence.ProofIdentity{}, termfence.ErrProofUnbound
}, Lifetime: func(context.Context, string, string, string) (string, error) {
if mode == "lifetime" {
return "wrong", nil
diff --git a/internal/host/hadeparture_identity.go b/internal/host/hadeparture_identity.go
index aaf05d746..1ba579756 100644
--- a/internal/host/hadeparture_identity.go
+++ b/internal/host/hadeparture_identity.go
@@ -187,9 +187,28 @@ func (r *departureIdentities) Observe(ctx context.Context, o hadeparture.BoxObse
b.Observation = o
return o, r.Registry.SaveHAIdentity(ctx, b)
}
+
+// Proof permits live confirmation only after clean not-found reads for the
+// pair, the ref and the iid. Any read/context/decode/verification error, a
+// binding under either identifier, unknown/placement-null/other-region
+// observation, or a missing second read fails closed.
func (r *departureIdentities) Proof(ctx context.Context, ref, iid string) (termfence.ProofIdentity, error) {
+ if ref == "" || iid == "" {
+ return termfence.ProofIdentity{}, errDepartureIdentity
+ }
o, ok, err := r.Resolve(ctx, ref, iid)
- if err != nil || !ok || !o.Known || o.PlacementNull || o.Box.Scope.CloudRegion != r.Region {
+ if err != nil {
+ return termfence.ProofIdentity{}, err
+ }
+ if !ok {
+ for _, key := range [][2]string{{ref, ""}, {"", iid}} {
+ if _, found, err := r.Registry.HAIdentity(ctx, key[0], key[1]); err != nil || found {
+ return termfence.ProofIdentity{}, errors.Join(errDepartureIdentity, err)
+ }
+ }
+ return termfence.ProofIdentity{}, termfence.ErrProofUnbound
+ }
+ if !o.Known || o.PlacementNull || o.Box.Scope.CloudRegion != r.Region {
return termfence.ProofIdentity{}, errDepartureIdentity
}
b, ok, err := r.Registry.HAIdentity(ctx, ref, iid)
diff --git a/internal/host/hadepartures_proof_followups_test.go b/internal/host/hadepartures_proof_followups_test.go
new file mode 100644
index 000000000..ac34431aa
--- /dev/null
+++ b/internal/host/hadepartures_proof_followups_test.go
@@ -0,0 +1,116 @@
+package host
+
+import (
+ "context"
+ "database/sql"
+ "encoding/json"
+ "strings"
+ "testing"
+ "time"
+
+ "github.com/carlosframework/platform/internal/activator"
+ "github.com/carlosframework/platform/internal/hadeparture"
+ "github.com/carlosframework/platform/internal/hafresh"
+ "github.com/carlosframework/platform/internal/lease"
+ "github.com/carlosframework/platform/internal/lease/locktest"
+ "github.com/carlosframework/platform/internal/provision"
+ "github.com/carlosframework/platform/internal/registry"
+)
+
+func TestTerminationProofIdentityRefusals(t *testing.T) {
+ for _, mode := range []string{"unbound", "read", "decode", "verification", "digest", "proof-bucket", "unknown", "placement-null", "other-region", "wrong-ref", "wrong-iid"} {
+ t.Run(mode, func(t *testing.T) {
+ t.Cleanup(lease.ProcessStateForTest())
+ fx := locktest.NewLockFixture(t)
+ a, _, describes := departureArmFixtureBucket(t, fx.Bucket)
+ a.Backend.Registry = a.Registry
+ a.Backend.Cfg.Record = func(context.Context, registry.Route) (activator.RecordFacts, error) {
+ return activator.RecordFacts{LifetimeEntry: provision.LifetimeEntry{ID: "life", HA: true}}, nil
+ }
+ lease.SetSelf(hafresh.Self{Ref: a.Self.Ref, IID: a.Self.InstanceID, Account: a.Self.Account, Region: a.Self.Scope.CloudRegion})
+ if err := lease.SetObligationDir(t.TempDir()); err != nil {
+ t.Fatal(err)
+ }
+ if err := a.Registry.Upsert(registry.Route{Host: "shop.example.com", Kind: "instance", Provisioned: true, HA: true}); err != nil {
+ t.Fatal(err)
+ }
+ c, l, err := armHADepartures(t.Context(), a)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if err = l.Stop(context.Background()); err != nil {
+ t.Fatal(err)
+ }
+ ref, iid := "dead", "i-1123456789abcdef0"
+ if mode != "unbound" {
+ _, err = c.cfg.Identities.Observe(t.Context(), hadeparture.BoxObservation{Known: true, Box: hadeparture.Box{Ref: ref, Scope: a.Self.Scope}, EdgesKey: "control/edges/dead.json", EdgesETag: "exact"})
+ if err != nil {
+ t.Fatal(err)
+ }
+ db, err := sql.Open("sqlite", a.Registry.Path())
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer db.Close()
+ var query string
+ switch mode {
+ case "read":
+ query = `ALTER TABLE ha_box_identities RENAME TO unavailable_identities`
+ case "decode":
+ query = `UPDATE ha_box_identities SET body='{'`
+ case "verification":
+ query = `UPDATE ha_box_identities SET body=json_set(body,'$.Raw','e30=')`
+ case "digest":
+ query = `UPDATE ha_box_identities SET body=json_set(body,'$.Digest','bad')`
+ case "proof-bucket":
+ query = `UPDATE ha_box_identities SET observation=json_set(observation,'$.Box.Scope.DataBucket','')`
+ case "unknown":
+ query = `UPDATE ha_box_identities SET observation=json_set(observation,'$.Known',json('false'))`
+ case "placement-null":
+ query = `UPDATE ha_box_identities SET observation=json_set(observation,'$.PlacementNull',json('true'))`
+ case "other-region":
+ query = `UPDATE ha_box_identities SET observation=json_set(observation,'$.Box.Scope.CloudRegion','us-east-1')`
+ case "wrong-ref":
+ ref = "other"
+ case "wrong-iid":
+ iid = "i-2123456789abcdef0"
+ }
+ if query != "" {
+ if _, err = db.Exec(query); err != nil {
+ t.Fatal(err)
+ }
+ }
+ }
+ body, _ := json.Marshal(hafresh.Lock{Generation: 1, Owner: hafresh.OwnerFor(hafresh.Self{Ref: ref, IID: iid}, 1, true), ExpiresAt: time.Now().Add(-time.Hour)})
+ fx.PutLock("instances/shop.example.com", body)
+ observed := 0
+ a.Backend.TerminationProofs.Observe = func(_ string, gone bool, err error) {
+ observed++
+ if mode != "unbound" && (gone || err == nil) {
+ t.Errorf("refusal observation gone=%t err=%v", gone, err)
+ }
+ }
+ held, err := lease.AcquireHA(lease.WithTerminationProof(t.Context(), a.Backend.TerminationProofs), fx.Bucket, "instances/shop.example.com", a.Self.Scope.CloudRegion, hafresh.Marker{Lifetime: "life"}, hafresh.Proofs{OwnerTerminated: func(iid string) (bool, error) { return a.Confirmer.Terminated(t.Context(), iid) }})
+ if held != nil {
+ defer held.Release(context.Background())
+ }
+ if mode == "unbound" {
+ if err != nil || *describes != 1 {
+ t.Fatalf("unbound: err=%v describes=%d", err, *describes)
+ }
+ } else {
+ if err == nil || *describes != 0 {
+ t.Errorf("identity %s: err=%v describes=%d", mode, err, *describes)
+ }
+ for _, request := range fx.Requests() {
+ if strings.HasPrefix(request, "PUT ") {
+ t.Errorf("refused identity wrote %s", request)
+ }
+ }
+ }
+ if observed != 1 {
+ t.Errorf("observations=%d", observed)
+ }
+ })
+ }
+}
diff --git a/internal/lease/termination_proof.go b/internal/lease/termination_proof.go
index ce04d220a..b90426cc0 100644
--- a/internal/lease/termination_proof.go
+++ b/internal/lease/termination_proof.go
@@ -63,7 +63,7 @@ func bindTerminationProof(ctx context.Context, client *s3.Client, bucket, prefix
}
binding, err := adapter.Resolve(ctx, owner.Ref, iid)
if err != nil {
- if fallback != nil {
+ if errors.Is(err, termfence.ErrProofUnbound) && fallback != nil {
gone, err := fallback(iid)
if !gone || err != nil {
return false, err
diff --git a/internal/lease/termination_proof_test.go b/internal/lease/termination_proof_test.go
index a86b1e3a2..deb3b9a94 100644
--- a/internal/lease/termination_proof_test.go
+++ b/internal/lease/termination_proof_test.go
@@ -65,7 +65,7 @@ func TestTerminationProofAcquireOnly(t *testing.T) {
lifetimeReads := 0
adapter := &TerminationProofAdapter{Cache: &termfence.ProofCache{}, Confirmer: confirm, Resolve: func(ctx context.Context, ref, iid string) (termfence.ProofIdentity, error) {
if strings.HasPrefix(mode, "unbound") || ref != "departed" || iid != "i-1123456789abcdef0" {
- return termfence.ProofIdentity{}, errors.New("unbound")
+ return termfence.ProofIdentity{}, termfence.ErrProofUnbound
}
return binding, nil
}, Lifetime: func(context.Context, string, string, string) (string, error) {
diff --git a/internal/termfence/termination_proof.go b/internal/termfence/termination_proof.go
index 7f5588a3b..c0e3a5f82 100644
--- a/internal/termfence/termination_proof.go
+++ b/internal/termfence/termination_proof.go
@@ -19,6 +19,9 @@ import (
const MaxTerminationProofBytes = 2048
+// ErrProofUnbound means the identity registry has no binding for either owner identifier.
+var ErrProofUnbound = errors.New("ha_termination_proof_unbound")
+
var ErrTerminationProof = errors.New("ha_termination_proof_invalid")
var proofIID = regexp.MustCompile(`^i-([0-9a-f]{8}|[0-9a-f]{17})$`)
var proofAccount = regexp.MustCompile(`^[0-9]{12}$`)