carlos / platform Public

edge: a backhaul scene box can restart its edge process (asset cache plan 3 Task 9 fix round 1)

newBackhaulEdgeWith keeps the box's listeners, registry, bucket handle and
raw certificate cache, and boots the edge process behind them: a new Edge,
cert mirror, autocert manager and unread home map. The 443 handler, the
TLS GetCertificate and the :80 chain reach whichever boot is live, so
restart is a fresh boot on the same addresses. The first boot is composed
exactly as before.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev 5df842d218850385585369806d4b60e00eeac5db parent 12e74c2
1 file changed, +82 −38
  • internal/edge/backhaul_e2e_test.go +82 −38
diff --git a/internal/edge/backhaul_e2e_test.go b/internal/edge/backhaul_e2e_test.go
index 3008e6f61..e2e3ec50d 100644
--- a/internal/edge/backhaul_e2e_test.go
+++ b/internal/edge/backhaul_e2e_test.go
@@ -261,9 +261,27 @@ type backhaulEdge struct {
// between the two edges from it; nil, the door is exactly as before.
tap atomic.Pointer[e2eTap]
+ // What a restart (boot) needs again: the box's registry database, its
+ // activator, its label and the bucket handle it opened.
+ reg *registry.Registry
+ act Activator
+ box string
+ // live is the edge process the listeners hand requests and handshakes
+ // to. The listeners are the box's addresses and outlive a restart; the
+ // process behind them does not.
+ live atomic.Pointer[backhaulLive]
+
client *http.Client
}
+// backhaulLive is one boot of a box's edge process: the Edge, the handshake
+// chain (mirror over autocert) and the composed :80 chain.
+type backhaulLive struct {
+ edge *Edge
+ getCert func(*tls.ClientHelloInfo) (*tls.Certificate, error)
+ plain http.Handler
+}
+
// e2eTap records one box's 443 door: each request's headers as they arrived
// and the final response headers written for it, in arrival order.
type e2eTap struct {
@@ -332,42 +350,16 @@ func newBackhaulEdge(t *testing.T, bucketDir, box string, ca *e2eCA, from *net.T
// for none, as for `carlos edge`).
func newBackhaulEdgeWith(t *testing.T, bucketDir, box string, ca *e2eCA, from *net.TCPAddr, act Activator, routes []registry.Route) *backhaulEdge {
t.Helper()
- be := &backhaulEdge{store: release.NewDirStore(bucketDir)}
- reg := openReg(t)
+ be := &backhaulEdge{store: release.NewDirStore(bucketDir), reg: openReg(t), act: act, box: box}
for _, rt := range routes {
- if err := reg.Upsert(rt); err != nil {
+ if err := be.reg.Upsert(rt); err != nil {
t.Fatalf("upsert %s: %v", rt.Host, err)
}
}
- e, err := New(reg, act)
- if err != nil {
- t.Fatalf("New: %v", err)
- }
- t.Cleanup(e.probers.Stop)
- be.edge = e
- e.staticStore = be.store
- WithBoxLabel(box)(e)
- // Not the issuer, and never able to become one: the gate is shut and the
- // manager below points at a dead directory. Everything this box serves on
- // 443 therefore came out of the bucket.
- WithIssuerGate(func() (context.Context, bool) { return nil, false })(e)
-
- // The RAW cache, kept before the publishing wrapper goes on (edge.go).
+ // The RAW cache — this box's own disk, which starts empty, is only ever
+ // written by the mirror, and is kept across a restart.
be.local = autocert.DirCache(t.TempDir())
- cache := e.armCertIssuance(be.local)
- be.mirror = e.newCertMirror(be.local)
- if be.mirror == nil {
- t.Fatal("a store-backed edge must have a mirror to serve from")
- }
- be.mgr = offlineManager(t, cache, e.issuerHostPolicy())
- tlsCfg := be.mgr.TLSConfig()
- tlsCfg.GetCertificate = be.mirror.wrapGetCertificate(tlsCfg.GetCertificate)
-
- be.homes = e.newHomeMap()
- if be.homes == nil {
- t.Fatal("a store-backed edge must have a home map")
- }
- e.homes.Store(be.homes)
+ tlsCfg := be.boot(t)
// 443: the edge itself, counted.
https := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
@@ -382,21 +374,17 @@ func newBackhaulEdgeWith(t *testing.T, bucketDir, box string, ca *e2eCA, from *n
tap.mu.Unlock()
w = &e2eTapWriter{ResponseWriter: w, tap: tap}
}
- e.ServeHTTP(w, r)
+ be.live.Load().edge.ServeHTTP(w, r)
}))
https.TLS = tlsCfg
https.StartTLS()
t.Cleanup(https.Close)
be.tlsAddr = https.Listener.Addr().String()
- // :80, outermost first, exactly as RunWithActivatorReady composes it: the
- // mirror's challenge handler, then autocert's, then the redirect fallback.
- // Composed ONCE, before anything listens, for the reason edge.go composes
- // it there — HTTPHandler is what arms autocert's HTTP-01 path.
- acmeAndFallback := be.mirror.challengeHandler(be.mgr.HTTPHandler(e.httpFallbackHandler()))
+ // :80, the live boot's composed chain (boot).
plain := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
be.plainCalls.Add(1)
- acmeAndFallback.ServeHTTP(w, r)
+ be.live.Load().plain.ServeHTTP(w, r)
}))
t.Cleanup(plain.Close)
be.plainAddr = plain.Listener.Addr().String()
@@ -405,6 +393,62 @@ func newBackhaulEdgeWith(t *testing.T, bucketDir, box string, ca *e2eCA, from *n
return be
}
+// boot starts the box's edge process over its registry, bucket and raw
+// certificate cache: a new Edge, its cert mirror and autocert manager, its
+// home map, none of which has read anything yet. It returns the TLS config
+// the 443 listener is given on the first boot; that config's GetCertificate
+// reaches whichever boot is live, so a restart keeps the listener.
+func (be *backhaulEdge) boot(t *testing.T) *tls.Config {
+ t.Helper()
+ e, err := New(be.reg, be.act)
+ if err != nil {
+ t.Fatalf("New: %v", err)
+ }
+ t.Cleanup(e.probers.Stop)
+ e.staticStore = be.store
+ WithBoxLabel(be.box)(e)
+ // Not the issuer, and never able to become one: the gate is shut and the
+ // manager below points at a dead directory. Everything this box serves on
+ // 443 therefore came out of the bucket.
+ WithIssuerGate(func() (context.Context, bool) { return nil, false })(e)
+
+ // The RAW cache was kept aside before the publishing wrapper goes on
+ // (edge.go).
+ cache := e.armCertIssuance(be.local)
+ mirror := e.newCertMirror(be.local)
+ if mirror == nil {
+ t.Fatal("a store-backed edge must have a mirror to serve from")
+ }
+ mgr := offlineManager(t, cache, e.issuerHostPolicy())
+ tlsCfg := mgr.TLSConfig()
+ live := &backhaulLive{edge: e, getCert: mirror.wrapGetCertificate(tlsCfg.GetCertificate)}
+ tlsCfg.GetCertificate = func(hello *tls.ClientHelloInfo) (*tls.Certificate, error) {
+ return be.live.Load().getCert(hello)
+ }
+ // :80, outermost first, exactly as RunWithActivatorReady composes it: the
+ // mirror's challenge handler, then autocert's, then the redirect fallback.
+ // Composed ONCE per boot, before it is live, for the reason edge.go
+ // composes it there — HTTPHandler is what arms autocert's HTTP-01 path.
+ live.plain = mirror.challengeHandler(mgr.HTTPHandler(e.httpFallbackHandler()))
+
+ homes := e.newHomeMap()
+ if homes == nil {
+ t.Fatal("a store-backed edge must have a home map")
+ }
+ e.homes.Store(homes)
+ be.edge, be.mirror, be.mgr, be.homes = e, mirror, mgr, homes
+ be.live.Store(live)
+ return tlsCfg
+}
+
+// restart is the box's edge process restarting: a fresh boot on the same
+// registry, bucket, raw certificate cache and listeners. The caller re-arms
+// whatever the process arms after New (cache, meters, asset cache).
+func (be *backhaulEdge) restart(t *testing.T) {
+ t.Helper()
+ be.boot(t)
+}
+
// e2eClientFor is a client that resolves every hostname to ONE box's two
// listeners — the deployment's DNS, in other words, pointed wherever the test
// wants this request to land. https connections are handshaken here rather than