carlos / platform Public

region-ha p1b-2: the edge's view of a box

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev 436b33c9932e20f2f5ca425294d0c89664832837 parent c01c4d4
3 files changed, +216 −0
  • internal/edge/holderview.go +69 −0
  • internal/edge/holderview_test.go +134 −0
  • internal/edge/homemap.go +13 −0

Intent

Attributed to this commit: 19.7 g CO₂e (2,570,133 tokens). This is an attribution, not a measurement — usage counted from this commit's own marker forward to the next one on region-ha-p1b2-forwarding, never backward from it. A turn that produced more than one commit books all of its tokens to the last one, and a concurrent session on this branch can add to the same figure. Some of it was priced using the shared default rate, because no model-specific factor row exists yet for at least one model involved. The Carbon view has the repo total, which is never derived from this attribution.

diff --git a/internal/edge/holderview.go b/internal/edge/holderview.go
new file mode 100644
index 000000000..4260475e1
--- /dev/null
+++ b/internal/edge/holderview.go
@@ -0,0 +1,69 @@
+package edge
+
+import (
+ "time"
+
+ "github.com/carlosframework/platform/internal/boxchoice"
+ "github.com/carlosframework/platform/internal/edgesfile"
+)
+
+// holderMaxSkew is how far in the future a file's generation may be before it
+// is not believed: box clocks are synchronised, not equal. boxchoice's own
+// bound, which that package keeps unexported.
+const holderMaxSkew = time.Minute
+
+// boxInView reports whether the box labelled ref is a valid forward target for
+// this box at now, and the address to dial it at.
+//
+// "In view" is the edge's half of region HA forwarding: ref is another box in
+// this box's own region and placement, writing to the same data store with the
+// same secrets key, whose edges file is current. The test is made on the fold's
+// own files, so it is as fresh as the last poll and needs no request to the
+// bucket. Every rule fails closed: a file this build cannot read a field from
+// is out of view, and with no file of this box's own there is nothing to
+// compare against, so nothing is in view.
+//
+// - ref is not this box (a box never forwards to itself);
+// - its generation is no older than boxchoice.MaxEdgesAge and no more than
+// holderMaxSkew in the future;
+// - its region and placement equal this box's own file's;
+// - its data bucket is non-empty and equals this box's;
+// - its secrets key fingerprint equals this box's. Both empty is equal: a
+// deployment with no key. One empty is a mismatch.
+//
+// dial is edgesfile.Reader.Dial for the file, built as foldHomes builds its
+// reader: this box's own file as Self and this edge's region HA switch.
+func (m *homeMap) boxInView(ref string, now time.Time) (dial string, ok bool) {
+ if ref == "" || m.self == "" || ref == m.self {
+ return "", false
+ }
+ var own, peer edgesFileView
+ ownKnown, peerKnown := false, false
+ for _, f := range m.snapshot().files {
+ switch f.Box {
+ case m.self:
+ own, ownKnown = f, true
+ case ref:
+ peer, peerKnown = f, true
+ }
+ }
+ if !ownKnown || !peerKnown {
+ return "", false
+ }
+ at, err := time.Parse(time.RFC3339, peer.Generation)
+ if err != nil {
+ return "", false
+ }
+ if age := now.Sub(at); age > boxchoice.MaxEdgesAge || age < -holderMaxSkew {
+ return "", false
+ }
+ if peer.Region != own.Region || peer.Placement != own.Placement {
+ return "", false
+ }
+ if peer.DataBucket == "" || peer.DataBucket != own.DataBucket || peer.SecretsKeyFP != own.SecretsKeyFP {
+ return "", false
+ }
+ reader := edgesfile.Reader{Box: m.self, Self: own.dialPoint(), SelfKnown: true, PrivateDial: m.privateDial}
+ dial = reader.Dial(peer.dialPoint())
+ return dial, dial != ""
+}
diff --git a/internal/edge/holderview_test.go b/internal/edge/holderview_test.go
new file mode 100644
index 000000000..ccbb32716
--- /dev/null
+++ b/internal/edge/holderview_test.go
@@ -0,0 +1,134 @@
+package edge
+
+import (
+ "encoding/json"
+ "fmt"
+ "net/http/httptest"
+ "testing"
+ "time"
+)
+
+// viewFile is one edges file body for the holder view tests. gen is an RFC 3339
+// generation; the dial fields are the same EIP and private address for every
+// box so the dial is the only thing a case reads off the address.
+func viewFile(box, region, placement string, gen time.Time, bucket, fp string) string {
+ return fmt.Sprintf(`{"v":1,"box":%q,"eip":"9.9.9.%d","private_ip":"10.0.1.%d","private_cidr":"10.0.0.0/16","region":%q,"placement":%q,"generation":%q,"data_bucket":%q,"secrets_key_fp":%q,"hosts":{}}`,
+ box, len(box), len(box), region, placement, gen.UTC().Format(time.RFC3339), bucket, fp)
+}
+
+// boxOf is the box label a test file body carries.
+func boxOf(t *testing.T, body string) string {
+ t.Helper()
+ var f edgesFileView
+ if err := json.Unmarshal([]byte(body), &f); err != nil {
+ t.Fatal(err)
+ }
+ return f.Box
+}
+
+func TestHolderViewRules(t *testing.T) {
+ now := time.Date(2026, 10, 30, 12, 0, 0, 0, time.UTC)
+ type peer struct {
+ name string
+ body string
+ in bool
+ }
+ own := viewFile("a", "ie", "p", now, "bkt", "fp1")
+ peers := []peer{
+ {"fresh", viewFile("b", "ie", "p", now.Add(-time.Minute), "bkt", "fp1"), true},
+ {"old", viewFile("c", "ie", "p", now.Add(-16*time.Minute), "bkt", "fp1"), false},
+ {"future", viewFile("d", "ie", "p", now.Add(2*time.Minute), "bkt", "fp1"), false},
+ {"region", viewFile("e", "uk", "p", now, "bkt", "fp1"), false},
+ {"placement", viewFile("f", "ie", "q", now, "bkt", "fp1"), false},
+ {"nobucket", viewFile("g", "ie", "p", now, "", "fp1"), false},
+ {"bucket", viewFile("h", "ie", "p", now, "other", "fp1"), false},
+ {"fp", viewFile("i", "ie", "p", now, "bkt", "fp2"), false},
+ {"emptyfp", viewFile("j", "ie", "p", now, "bkt", ""), false},
+ {"nogen", `{"v":1,"box":"k","eip":"1.2.3.4","region":"ie","placement":"p","data_bucket":"bkt","secrets_key_fp":"fp1","hosts":{}}`, false},
+ {"badgen", `{"v":1,"box":"l","eip":"1.2.3.4","region":"ie","placement":"p","generation":"yesterday","data_bucket":"bkt","secrets_key_fp":"fp1","hosts":{}}`, false},
+ }
+ s := meshTestStore(t)
+ putJSON(t, s, "control/edges/a.json", own)
+ for _, p := range peers {
+ putJSON(t, s, "control/edges/"+p.name+".json", p.body)
+ }
+ m := newHomeMap(s, "a")
+ refreshHomeMap(t, m)
+ for _, p := range peers {
+ _, ok := m.boxInView(boxOf(t, p.body), now)
+ if ok != p.in {
+ t.Errorf("%s: in view = %v, want %v", p.name, ok, p.in)
+ }
+ }
+ if _, ok := m.boxInView("a", now); ok {
+ t.Error("this box is in its own view")
+ }
+ if _, ok := m.boxInView("nobody", now); ok {
+ t.Error("an unknown box is in view")
+ }
+ if _, ok := m.boxInView("", now); ok {
+ t.Error("the empty label is in view")
+ }
+
+ // Both empty secrets fingerprints are equal.
+ s2 := meshTestStore(t)
+ putJSON(t, s2, "control/edges/a.json", viewFile("a", "ie", "p", now, "bkt", ""))
+ putJSON(t, s2, "control/edges/b.json", viewFile("b", "ie", "p", now, "bkt", ""))
+ putJSON(t, s2, "control/edges/c.json", viewFile("c", "ie", "p", now, "bkt", "fp1"))
+ m2 := newHomeMap(s2, "a")
+ refreshHomeMap(t, m2)
+ if _, ok := m2.boxInView("b", now); !ok {
+ t.Error("both fingerprints empty is a mismatch")
+ }
+ if _, ok := m2.boxInView("c", now); ok {
+ t.Error("own fingerprint empty and the peer's set is in view")
+ }
+
+ // This box's own file absent: nothing is in view.
+ s3 := meshTestStore(t)
+ putJSON(t, s3, "control/edges/b.json", viewFile("b", "ie", "p", now, "bkt", "fp1"))
+ m3 := newHomeMap(s3, "a")
+ refreshHomeMap(t, m3)
+ if _, ok := m3.boxInView("b", now); ok {
+ t.Error("a box is in view with no file of our own")
+ }
+ // Own bucket empty: nothing is in view either.
+ s4 := meshTestStore(t)
+ putJSON(t, s4, "control/edges/a.json", viewFile("a", "ie", "p", now, "", "fp1"))
+ putJSON(t, s4, "control/edges/b.json", viewFile("b", "ie", "p", now, "", "fp1"))
+ m4 := newHomeMap(s4, "a")
+ refreshHomeMap(t, m4)
+ if _, ok := m4.boxInView("b", now); ok {
+ t.Error("a box is in view with no data bucket on either side")
+ }
+}
+
+func TestHolderViewDialsPrivateWithRegionHA(t *testing.T) {
+ now := time.Date(2026, 10, 30, 12, 0, 0, 0, time.UTC)
+ s := meshTestStore(t)
+ putJSON(t, s, "control/edges/a.json", viewFile("a", "ie", "p", now, "bkt", "fp"))
+ putJSON(t, s, "control/edges/bb.json", viewFile("bb", "ie", "p", now, "bkt", "fp"))
+ for _, ha := range []bool{false, true} {
+ m := newHomeMap(s, "a")
+ m.privateDial = ha
+ refreshHomeMap(t, m)
+ dial, ok := m.boxInView("bb", now)
+ want := "9.9.9.2"
+ if ha {
+ want = "10.0.1.2"
+ }
+ if !ok || dial != want {
+ t.Errorf("region HA %v: dial = %q (ok=%v), want %q", ha, dial, ok, want)
+ }
+ }
+}
+
+func TestHopVerifiedExported(t *testing.T) {
+ r := httptest.NewRequest("GET", "http://x.test/", nil)
+ if HopVerified(r) {
+ t.Error("a plain request is a verified hop")
+ }
+ if !HopVerified(r.WithContext(withHopVerified(r.Context()))) {
+ t.Error("a request marked as a verified hop is not")
+ }
+}
diff --git a/internal/edge/homemap.go b/internal/edge/homemap.go
index b5e32d997..e9e0bc001 100644
--- a/internal/edge/homemap.go
+++ b/internal/edge/homemap.go
@@ -143,6 +143,14 @@ type edgesFileView struct {
PrivateCIDR string `json:"private_cidr"`
Region string `json:"region"`
Placement string `json:"placement"`
+ // Generation, DataBucket and SecretsKeyFP are what the holder view reads
+ // (holderview.go): when the box last wrote the file (RFC 3339), its data
+ // bucket, and the fingerprint of its secrets key. The writer's names
+ // (internal/edgesfile snapshot.go); absent from an older writer, and a
+ // file without them is then never in view.
+ Generation string `json:"generation"`
+ DataBucket string `json:"data_bucket"`
+ SecretsKeyFP string `json:"secrets_key_fp"`
}
// dialPoint is f's dial fields, as edgesfile reads them.
@@ -1220,6 +1228,11 @@ func hopVerified(ctx context.Context) bool {
return v
}
+// HopVerified is hopVerified for the activator, which holds the request and
+// not the edge's context plumbing: a request that arrived as a proven hop
+// from another edge in this deployment's mesh.
+func HopVerified(r *http.Request) bool { return hopVerified(r.Context()) }
+
// hopDoorKey marks a verified hop whose sender vouched, with
// X-Carlos-Hop-Door, that the request came through its door. It never stands
// alone: acceptHop sets it only beside hopVerifiedKey. It carries no caller,