hostbox.Actor carries the box binding (roster binding task 2)
hostbox.Actor gains Binding, hostactors.BoxBinding of its row in the roster read that produced it, plus Bound() (the actor with the roster write counter cleared) and SameBinding. IdentityProvider.Verify's actorMatches also requires a binding. productionClaimRoster.Actor and verifyClaimCleanupEnrollment compute it from the read they already make. Reconciles 7b805720: the actor's QuarantineRevision field is removed. The binding digests the row's quarantine revision and state, so a box quarantined and readmitted since startup still never matches its startup actor, without a second field doing the same job. Comparisons still read Revision here; task 3 moves them to Binding. Tests: TestIdentityVerifyRequiresBinding, TestActorBoundIgnoresOnlyRevision; the handover-recovery fixture's opaque binding defaults to its numeral.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
7 files changed,
+157
−24
internal/host/claim_regional_revision_test.go+15 −15internal/host/claimcleanup_identity.go+4 −1internal/host/claimhandover_recovery_test.go+7 −0internal/host/claimproduction.go+7 −1internal/hostbox/identity.go+25 −5internal/hostbox/identity_binding_test.go+97 −0internal/hostbox/identity_region_test.go+2 −2
diff --git a/internal/host/claim_regional_revision_test.go b/internal/host/claim_regional_revision_test.go| index 9d63705d3..d823d7da5 100644 |
| --- a/internal/host/claim_regional_revision_test.go |
| +++ b/internal/host/claim_regional_revision_test.go |
| @@ -19,7 +19,7 @@ import ( |
| // Every actor field but the roster-wide Revision is part of the enrollment |
| // a built assembly is bound to. The reflection walk covers fields added later. |
| func TestSameVerifiedActorIgnoresOnlyRevision(t *testing.T) { |
| - base := hostbox.Actor{FleetHomeRegion: "ie", HomeRegion: "ie", ControlBucket: "deployment", Box: "box", Enrollment: strings.Repeat("a", 32), Revision: 7, Region: "ie", Bucket: "data", Namespace: "platform", Cloud: hostbox.CloudIdentity{Provider: "aws", Account: "111122223333", Region: "eu-west-1", InstanceID: "i-0abc"}, Eligible: true, QuarantineRevision: 2} |
| + base := hostbox.Actor{FleetHomeRegion: "ie", HomeRegion: "ie", ControlBucket: "deployment", Box: "box", Enrollment: strings.Repeat("a", 32), Revision: 7, Region: "ie", Bucket: "data", Namespace: "platform", Cloud: hostbox.CloudIdentity{Provider: "aws", Account: "111122223333", Region: "eu-west-1", InstanceID: "i-0abc"}, Eligible: true, Binding: "binding"} |
| var walk func(v reflect.Value, path string) |
| walk = func(v reflect.Value, path string) { |
| for i := 0; i < v.NumField(); i++ { |
| @@ -130,20 +130,20 @@ func TestClaimRegionalEnrollmentFieldChanges(t *testing.T) { |
| } |
| e := claimRegionalEnrollment{identity: identity, actor: actor} |
| for name, edit := range map[string]func(*hostbox.Actor){ |
| - "revision": func(a *hostbox.Actor) { a.Revision++ }, |
| - "eligible": func(a *hostbox.Actor) { a.Eligible = false }, |
| - "quarantined": func(a *hostbox.Actor) { a.Quarantined = true }, |
| - "quarantine revision": func(a *hostbox.Actor) { a.QuarantineRevision++ }, |
| - "box": func(a *hostbox.Actor) { a.Box = "other" }, |
| - "enrollment": func(a *hostbox.Actor) { a.Enrollment = "re-enrolled" }, |
| - "region": func(a *hostbox.Actor) { a.Region = "other" }, |
| - "bucket": func(a *hostbox.Actor) { a.Bucket = "other" }, |
| - "namespace": func(a *hostbox.Actor) { a.Namespace = "other" }, |
| - "control bucket": func(a *hostbox.Actor) { a.ControlBucket = "other" }, |
| - "fleet home region": func(a *hostbox.Actor) { a.FleetHomeRegion = "other" }, |
| - "cloud account": func(a *hostbox.Actor) { a.Cloud.Account = "444455556666" }, |
| - "cloud region": func(a *hostbox.Actor) { a.Cloud.Region = "eu-west-2" }, |
| - "cloud instance": func(a *hostbox.Actor) { a.Cloud.InstanceID = "i-0def" }, |
| + "revision": func(a *hostbox.Actor) { a.Revision++ }, |
| + "eligible": func(a *hostbox.Actor) { a.Eligible = false }, |
| + "quarantined": func(a *hostbox.Actor) { a.Quarantined = true }, |
| + "binding": func(a *hostbox.Actor) { a.Binding += "-changed" }, |
| + "box": func(a *hostbox.Actor) { a.Box = "other" }, |
| + "enrollment": func(a *hostbox.Actor) { a.Enrollment = "re-enrolled" }, |
| + "region": func(a *hostbox.Actor) { a.Region = "other" }, |
| + "bucket": func(a *hostbox.Actor) { a.Bucket = "other" }, |
| + "namespace": func(a *hostbox.Actor) { a.Namespace = "other" }, |
| + "control bucket": func(a *hostbox.Actor) { a.ControlBucket = "other" }, |
| + "fleet home region": func(a *hostbox.Actor) { a.FleetHomeRegion = "other" }, |
| + "cloud account": func(a *hostbox.Actor) { a.Cloud.Account = "444455556666" }, |
| + "cloud region": func(a *hostbox.Actor) { a.Cloud.Region = "eu-west-2" }, |
| + "cloud instance": func(a *hostbox.Actor) { a.Cloud.InstanceID = "i-0def" }, |
| } { |
| t.Run(name, func(t *testing.T) { |
| changed := identity |
diff --git a/internal/host/claimcleanup_identity.go b/internal/host/claimcleanup_identity.go| index 5b2d8cca3..9c8adb32f 100644 |
| --- a/internal/host/claimcleanup_identity.go |
| +++ b/internal/host/claimcleanup_identity.go |
| @@ -35,7 +35,10 @@ func verifyClaimCleanupEnrollment(ctx context.Context, identity hostbox.Identity |
| if !ok || roster.Roster.RequestLimitNS != int64(hostbox.RequestLimit) { |
| return nil, ErrClaimStopIdentity |
| } |
| - actor := hostbox.Actor{FleetHomeRegion: roster.Roster.HomeRegion, ControlBucket: directory.Bucket, Box: row.BoxID, Enrollment: row.Enrollment, Revision: roster.Roster.Revision, Region: row.Region, Bucket: identity.Bucket, Namespace: row.Namespace, Cloud: hostbox.CloudIdentity{Provider: row.Cloud.Provider, Account: row.Cloud.Account, Region: row.Cloud.Region, InstanceID: row.Cloud.InstanceID}} |
| + // Binding is the row's binding in this startup read. It stamps only what the |
| + // cleanup identity itself constructs; fresh authority is never this actor. |
| + binding, _ := hostactors.BoxBinding(roster.Roster, identity.Box) |
| + actor := hostbox.Actor{FleetHomeRegion: roster.Roster.HomeRegion, ControlBucket: directory.Bucket, Box: row.BoxID, Enrollment: row.Enrollment, Revision: roster.Roster.Revision, Binding: binding, Region: row.Region, Bucket: identity.Bucket, Namespace: row.Namespace, Cloud: hostbox.CloudIdentity{Provider: row.Cloud.Provider, Account: row.Cloud.Account, Region: row.Cloud.Region, InstanceID: row.Cloud.InstanceID}} |
| identity.Roster = cleanupCloudRoster{actor} |
| if _, err := identity.Verify(ctx); err != nil { |
| return nil, err |
diff --git a/internal/host/claimhandover_recovery_test.go b/internal/host/claimhandover_recovery_test.go| index 2559e339d..809ca67e6 100644 |
| --- a/internal/host/claimhandover_recovery_test.go |
| +++ b/internal/host/claimhandover_recovery_test.go |
| @@ -47,6 +47,13 @@ func handoverRecoveryIdentity(t *testing.T, actor hostbox.Actor) hostbox.Identit |
| t.Fatal(err) |
| } |
| actor.Bucket, actor.Namespace, actor.Eligible = "data", "platform", true |
| + if actor.Binding == "" { |
| + // Fixture shorthand: these fixtures stamp installed state with the |
| + // actor's numeral, so the opaque binding defaults to that string. The |
| + // production roster computes hostactors.BoxBinding; the binding |
| + // witnesses (claim_binding_*_test.go) run on it. |
| + actor.Binding = fmt.Sprint(actor.Revision) |
| + } |
| actor.Cloud = hostbox.CloudIdentity{Provider: "aws", Account: "111122223333", Region: "eu-west-1", InstanceID: "i-0abc"} |
| doc, err := json.Marshal(map[string]string{"accountId": actor.Cloud.Account, "region": actor.Cloud.Region, "instanceId": actor.Cloud.InstanceID}) |
| if err != nil { |
diff --git a/internal/host/claimproduction.go b/internal/host/claimproduction.go| index dad097aa3..1569d66b7 100644 |
| --- a/internal/host/claimproduction.go |
| +++ b/internal/host/claimproduction.go |
| @@ -393,7 +393,13 @@ func (p productionClaimRoster) Actor(ctx context.Context, box string) (hostbox.A |
| if local.Digest != remote.Digest || o.Roster.Publication != "published" || o.Roster.FloorSHA256 != remote.Digest { |
| return a, hostfloor.ErrFloor |
| } |
| - return hostbox.Actor{FleetHomeRegion: o.Roster.HomeRegion, ControlBucket: p.Directory.Bucket, Box: box, Enrollment: b.Enrollment, Revision: o.Roster.Revision, Region: b.Region, Bucket: p.DataBucket, Namespace: b.Namespace, Eligible: true, QuarantineRevision: b.QuarantineRevision, Cloud: hostbox.CloudIdentity{Provider: b.Cloud.Provider, Account: b.Cloud.Account, Region: b.Cloud.Region, InstanceID: b.Cloud.InstanceID}}, nil |
| + // The binding comes from this same verified read, so it names exactly the |
| + // row and deployment facts checked above. |
| + binding, ok := hostactors.BoxBinding(o.Roster, box) |
| + if !ok { |
| + return a, hostbox.ErrChanged |
| + } |
| + return hostbox.Actor{FleetHomeRegion: o.Roster.HomeRegion, ControlBucket: p.Directory.Bucket, Box: box, Enrollment: b.Enrollment, Revision: o.Roster.Revision, Region: b.Region, Bucket: p.DataBucket, Namespace: b.Namespace, Eligible: true, Binding: binding, Cloud: hostbox.CloudIdentity{Provider: b.Cloud.Provider, Account: b.Cloud.Account, Region: b.Cloud.Region, InstanceID: b.Cloud.InstanceID}}, nil |
| } |
| type productionClaimRegional struct { |
diff --git a/internal/hostbox/identity.go b/internal/hostbox/identity.go| index 8e60153ba..36d831b15 100644 |
| --- a/internal/hostbox/identity.go |
| +++ b/internal/hostbox/identity.go |
| @@ -22,11 +22,31 @@ type Actor struct { |
| Cloud CloudIdentity |
| Eligible bool |
| Quarantined bool |
| - // QuarantineRevision is the roster row's quarantine count. It only grows, |
| - // so an actor verified after a quarantine and readmission never equals |
| - // one verified before it, whatever else the roster revision does. |
| - QuarantineRevision uint64 |
| + // Binding is hostactors.BoxBinding of this box's row in the roster read |
| + // that produced the actor: the digest installed state is stamped with and |
| + // compared against. Unlike Revision, the roster's write counter, it moves |
| + // only when this box's identity or state or the deployment's binding facts |
| + // change (a quarantine, a readmission, a re-enrolment, a floor publish), |
| + // never on holds, activations or other boxes' rows. |
| + Binding string |
| } |
| + |
| +// Bound returns the actor with the roster write counter cleared: the part of |
| +// the actor that names one enrollment under one binding. Persist this, not the |
| +// actor, wherever a later comparison must survive unrelated roster writes. |
| +func (a Actor) Bound() Actor { |
| + a.Revision = 0 |
| + return a |
| +} |
| + |
| +// SameBinding reports whether two actors are the same enrollment under the |
| +// same binding. Every field but Revision must match; Binding carries the |
| +// quarantine revision and every other bound fact, so a box quarantined and |
| +// readmitted since a is never SameBinding with it. |
| +func SameBinding(a, b Actor) bool { |
| + return a.Bound() == b.Bound() |
| +} |
| + |
| type CloudIdentity struct{ Provider, Account, Region, InstanceID string } |
| type Roster interface { |
| Actor(context.Context, string) (Actor, error) |
| @@ -83,7 +103,7 @@ func (p IdentityProvider) Verify(ctx context.Context) (Actor, error) { |
| } |
| func (p IdentityProvider) actorMatches(a Actor) bool { |
| - return p.AWSRegion != "" && a.Box == p.Box && a.Enrollment != "" && a.Revision > 0 && a.Eligible && !a.Quarantined && a.Region == p.Region && a.Bucket == p.Bucket && a.Namespace == p.Namespace && a.Cloud.Provider == "aws" && a.Cloud.Account == p.Account && a.Cloud.Region == p.AWSRegion && a.Cloud.InstanceID != "" |
| + return p.AWSRegion != "" && a.Box == p.Box && a.Enrollment != "" && a.Revision > 0 && a.Binding != "" && a.Eligible && !a.Quarantined && a.Region == p.Region && a.Bucket == p.Bucket && a.Namespace == p.Namespace && a.Cloud.Provider == "aws" && a.Cloud.Account == p.Account && a.Cloud.Region == p.AWSRegion && a.Cloud.InstanceID != "" |
| } |
| // LocalAuthority is Plan 8's nonblocking local-state reader. It checks its own |
diff --git a/internal/hostbox/identity_binding_test.go b/internal/hostbox/identity_binding_test.go| new file mode 100644 |
| index 000000000..46ae14033 |
| --- /dev/null |
| +++ b/internal/hostbox/identity_binding_test.go |
| @@ -0,0 +1,97 @@ |
| +package hostbox |
| + |
| +import ( |
| + "crypto" |
| + "crypto/rand" |
| + "crypto/rsa" |
| + "crypto/sha256" |
| + "crypto/x509" |
| + "encoding/base64" |
| + "encoding/json" |
| + "errors" |
| + "reflect" |
| + "testing" |
| + |
| + "github.com/carlosframework/platform/internal/boxid" |
| +) |
| + |
| +// L1: a roster row read without a binding is not a verified actor. Every |
| +// installed-state comparison stamps and compares the binding, so an actor |
| +// without one must never leave Verify. |
| +func TestIdentityVerifyRequiresBinding(t *testing.T) { |
| + key, err := rsa.GenerateKey(rand.Reader, 2048) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + actor := Actor{Box: "box", Enrollment: "en", Revision: 1, Binding: "binding", Region: "ie", Bucket: "data", Namespace: "platform", Eligible: true, Cloud: CloudIdentity{Provider: "aws", Account: "111122223333", Region: "eu-west-1", InstanceID: "i-0abc"}} |
| + doc, err := json.Marshal(map[string]string{"accountId": actor.Cloud.Account, "region": actor.Cloud.Region, "instanceId": actor.Cloud.InstanceID}) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + sum := sha256.Sum256(doc) |
| + sig, err := rsa.SignPKCS1v15(rand.Reader, key, crypto.SHA256, sum[:]) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + raw, err := json.Marshal(boxid.Record{V: 1, Box: actor.Box, IdentityDocument: base64.StdEncoding.EncodeToString(doc), IdentitySignature: base64.StdEncoding.EncodeToString(sig)}) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + p := IdentityProvider{Account: actor.Cloud.Account, Box: actor.Box, Region: actor.Region, AWSRegion: actor.Cloud.Region, Bucket: actor.Bucket, Namespace: actor.Namespace, Roster: enrollmentRoster{actor}, Evidence: enrollmentEvidence(raw), Certs: map[string]*x509.Certificate{actor.Cloud.Region: {PublicKey: &key.PublicKey, PublicKeyAlgorithm: x509.RSA}}} |
| + if got, err := p.Verify(t.Context()); err != nil || got != actor { |
| + t.Fatalf("bound actor: got=%+v err=%v", got, err) |
| + } |
| + unbound := actor |
| + unbound.Binding = "" |
| + if p.actorMatches(unbound) { |
| + t.Fatal("actorMatches admitted an actor without a binding") |
| + } |
| + p.Roster = enrollmentRoster{unbound} |
| + if _, err := p.Verify(t.Context()); !errors.Is(err, ErrChanged) { |
| + t.Fatalf("Verify without a binding: %v, want ErrChanged", err) |
| + } |
| +} |
| + |
| +// Bound clears only Revision, and SameBinding ignores only Revision: every |
| +// other actor field, Binding included, still separates two actors. The |
| +// reflection walk covers fields added later. |
| +func TestActorBoundIgnoresOnlyRevision(t *testing.T) { |
| + base := Actor{FleetHomeRegion: "ie", HomeRegion: "ie", ControlBucket: "deployment", Box: "box", Enrollment: "enrolled", Revision: 7, Binding: "binding", Region: "ie", Bucket: "data", Namespace: "platform", Cloud: CloudIdentity{Provider: "aws", Account: "111122223333", Region: "eu-west-1", InstanceID: "i-0abc"}, Eligible: true} |
| + if b := base.Bound(); b.Revision != 0 || func() Actor { c := base; c.Revision = 0; return c }() != b { |
| + t.Fatalf("Bound = %+v", b) |
| + } |
| + if !SameBinding(base, base.Bound()) { |
| + t.Fatal("an actor is not SameBinding with its bound copy") |
| + } |
| + var walk func(v reflect.Value, index []int, path string) |
| + walk = func(v reflect.Value, index []int, path string) { |
| + for i := 0; i < v.NumField(); i++ { |
| + f := v.Field(i) |
| + at := append(append([]int{}, index...), i) |
| + name := path + v.Type().Field(i).Name |
| + if f.Kind() == reflect.Struct { |
| + walk(f, at, name+".") |
| + continue |
| + } |
| + changed := base |
| + field := reflect.ValueOf(&changed).Elem().FieldByIndex(at) |
| + switch field.Kind() { |
| + case reflect.String: |
| + field.SetString(field.String() + "-changed") |
| + case reflect.Uint64: |
| + field.SetUint(field.Uint() + 1) |
| + case reflect.Bool: |
| + field.SetBool(!field.Bool()) |
| + default: |
| + t.Fatalf("%s: unhandled kind %s", name, field.Kind()) |
| + } |
| + if got, want := SameBinding(changed, base), name == "Revision"; got != want { |
| + t.Errorf("%s changed: SameBinding = %v, want %v", name, got, want) |
| + } |
| + if got, want := changed.Bound() == base.Bound(), name == "Revision"; got != want { |
| + t.Errorf("%s changed: Bound equal = %v, want %v", name, got, want) |
| + } |
| + } |
| + } |
| + walk(reflect.ValueOf(base), nil, "") |
| +} |
diff --git a/internal/hostbox/identity_region_test.go b/internal/hostbox/identity_region_test.go| index 712d95656..40cdbc35e 100644 |
| --- a/internal/hostbox/identity_region_test.go |
| +++ b/internal/hostbox/identity_region_test.go |
| @@ -16,7 +16,7 @@ import ( |
| func TestIdentitySeparatesPlacementAndCloudRegion(t *testing.T) { |
| p := IdentityProvider{Account: "123", Box: "box", Region: "ie", AWSRegion: "eu-west-1", Bucket: "data", Namespace: "platform"} |
| - a := Actor{Box: "box", Enrollment: "en", Revision: 1, Region: "ie", Bucket: "data", Namespace: "platform", Eligible: true, Cloud: CloudIdentity{Provider: "aws", Account: "123", Region: "eu-west-1", InstanceID: "i-box"}} |
| + a := Actor{Box: "box", Enrollment: "en", Revision: 1, Binding: "binding", Region: "ie", Bucket: "data", Namespace: "platform", Eligible: true, Cloud: CloudIdentity{Provider: "aws", Account: "123", Region: "eu-west-1", InstanceID: "i-box"}} |
| if !p.actorMatches(a) { |
| t.Fatal("placement and AWS regions were conflated") |
| } |
| @@ -40,7 +40,7 @@ func TestIdentityEnrollmentBindsSignedCloudAndPlacement(t *testing.T) { |
| t.Fatal(err) |
| } |
| cert := &x509.Certificate{PublicKey: &key.PublicKey, PublicKeyAlgorithm: x509.RSA} |
| - actor := Actor{Box: "annie-welding", Enrollment: "en", Revision: 1, Region: "ie", Bucket: "data", Namespace: "platform", Eligible: true, Cloud: CloudIdentity{Provider: "aws", Account: "111122223333", Region: "eu-west-1", InstanceID: "i-0abc"}} |
| + actor := Actor{Box: "annie-welding", Enrollment: "en", Revision: 1, Binding: "binding", Region: "ie", Bucket: "data", Namespace: "platform", Eligible: true, Cloud: CloudIdentity{Provider: "aws", Account: "111122223333", Region: "eu-west-1", InstanceID: "i-0abc"}} |
| p := IdentityProvider{Account: actor.Cloud.Account, Box: actor.Box, Region: actor.Region, AWSRegion: actor.Cloud.Region, Bucket: actor.Bucket, Namespace: actor.Namespace, Roster: enrollmentRoster{actor}, Certs: map[string]*x509.Certificate{"eu-west-1": cert}} |
| record := func(box, account, region, instance string) []byte { |
| t.Helper() |