carlos / platform Public

edge: the hub routes /api/fleet/seeds/ to the console

The hub hands a fleet box's seeds door reports (placed, failed) to the
local console, as it already does for the replica door. One shared,
POST-only, clean-path check keeps (*Edge).ServeHTTP from growing.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by paul@keymail.dev a0b756f4a08e764ef6d76bc0f748eccf135ae545 parent 82e237a
3 files changed, +66 −1
  • internal/edge/edge.go +1 −1
  • internal/edge/replicaconsole.go +12 −0
  • internal/edge/replicaconsole_test.go +53 −0
diff --git a/internal/edge/edge.go b/internal/edge/edge.go
index afca1bd0e..734f42043 100644
--- a/internal/edge/edge.go
+++ b/internal/edge/edge.go
@@ -2409,7 +2409,7 @@ func (e *Edge) ServeHTTP(w http.ResponseWriter, r *http.Request) {
e.door.ServeHTTP(w, r)
return
}
- if p := r.URL.Path; r.Method == http.MethodPost && r.URL.RawPath == "" && path.Clean(p) == p && strings.HasPrefix(p, "/api/fleet/replicas/") {
+ if boxConsolePath(r) {
e.serveReplicaConsole(w, r)
return
}
diff --git a/internal/edge/replicaconsole.go b/internal/edge/replicaconsole.go
index 508310010..30d3700ec 100644
--- a/internal/edge/replicaconsole.go
+++ b/internal/edge/replicaconsole.go
@@ -6,6 +6,7 @@ import (
"net/http"
"net/http/httputil"
"net/netip"
+ "path"
"strconv"
"strings"
"time"
@@ -129,3 +130,14 @@ func (e *Edge) serveReplicaConsole(w http.ResponseWriter, r *http.Request) {
}
console.ServeHTTP(w, e.withForwardKey(r))
}
+
+// boxConsolePath: the box doors the hub hands to the local console, POST
+// only, with a clean path and no raw path: /api/fleet/replicas/ and
+// /api/fleet/seeds/ (the seeds door, a box's placed and failed reports).
+func boxConsolePath(r *http.Request) bool {
+ p := r.URL.Path
+ if r.Method != http.MethodPost || r.URL.RawPath != "" || path.Clean(p) != p {
+ return false
+ }
+ return strings.HasPrefix(p, "/api/fleet/replicas/") || strings.HasPrefix(p, "/api/fleet/seeds/")
+}
diff --git a/internal/edge/replicaconsole_test.go b/internal/edge/replicaconsole_test.go
index f665415a6..6f8b2c3bb 100644
--- a/internal/edge/replicaconsole_test.go
+++ b/internal/edge/replicaconsole_test.go
@@ -271,3 +271,56 @@ func TestReplicaConsoleAddrFromHostEnv(t *testing.T) {
t.Fatalf("good value: %v, configured %v", err, e.replicaConsole != nil)
}
}
+
+// TestHubRoutesFleetSeedsToConsole: the seeds door rides the same hub-to-
+// console hand-off as the replica door, POST only, clean paths only.
+func TestHubRoutesFleetSeedsToConsole(t *testing.T) {
+ var hits atomic.Int32
+ console := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ hits.Add(1)
+ w.Header().Set("X-Upstream-Path", r.URL.RequestURI())
+ _, _ = io.WriteString(w, `{}`)
+ }))
+ defer console.Close()
+ reg := openReg(t)
+ rt := registry.Route{Host: "console.test", App: "console", Unit: "console.service", Kind: "service", Addr: console.Listener.Addr().String(), Status: "ready", Version: "v-test"}
+ if err := reg.Upsert(rt); err != nil {
+ t.Fatal(err)
+ }
+ e, err := New(reg, nil)
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer e.probers.Stop()
+ e.EnableTunnels(NewTunnelHub("tunnel.test", nil))
+ const prefix = "/api/fleet/seeds/a/app/h/"
+ for _, tc := range []struct {
+ method, path string
+ hit bool
+ }{
+ {"POST", prefix + "placed", true},
+ {"POST", prefix + "failed", true},
+ {"GET", prefix + "placed", false},
+ {"POST", "/api/fleet/seeds", false},
+ {"POST", "/api/fleet/seeds-other/a", false},
+ {"POST", "/api/fleet/seeds/../../cli/apps/x", false},
+ {"POST", "//api/fleet/seeds/x", false},
+ {"POST", "/api/fleet/seeds/%2e%2e/x", false},
+ {"POST", "/api/fleet/seeds/a%2Fb/app/h/placed", false},
+ } {
+ t.Run(tc.method+tc.path, func(t *testing.T) {
+ ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
+ defer cancel()
+ req := httptest.NewRequest(tc.method, "https://tunnel.test"+tc.path, strings.NewReader(`{}`)).WithContext(ctx)
+ before := hits.Load()
+ w := httptest.NewRecorder()
+ e.ServeHTTP(w, req)
+ if got := hits.Load() > before; got != tc.hit {
+ t.Fatalf("console hit = %v, want %v (status %d)", got, tc.hit, w.Code)
+ }
+ if tc.hit && w.Header().Get("X-Upstream-Path") != tc.path {
+ t.Fatalf("upstream path: %q", w.Header().Get("X-Upstream-Path"))
+ }
+ })
+ }
+}