Agent Mesh
Go a2aa2a-protocolagent-meshagentic-aiinferencemcpmeshmesh-networknetwork-securitynetworking-aip2p-network
controlplane: answer the mesh protocol's endpoints for any origin
Antonio Ojea38dc24aefdab
diff --git a/internal/controlplane/server.go b/internal/controlplane/server.goindex bbda0e60..661c8921 100644--- a/internal/controlplane/server.go+++ b/internal/controlplane/server.go@@ -230,14 +230,14 @@ func (s *Server) RegisterRoutes(mux *http.ServeMux) {handle := func(pattern string, h http.HandlerFunc) {mux.Handle(pattern, observeRoute(pattern, h))}- handle("/info", s.HandleInfo)- handle("/register", noStore(s.HandleRegister))- handle("/keys", s.HandleKeys)+ handle("/info", meshSurface(s.HandleInfo))+ handle("/register", meshSurface(noStore(s.HandleRegister)))+ handle("/keys", meshSurface(s.HandleKeys))handle("/routers/lease", s.HandleRouterLease)- handle("/policies", s.HandlePolicies)- handle("/enroll", noStore(s.HandleEnroll))- handle("/enroll/status", noStore(s.HandleEnrollStatus))- handle("/refresh", noStore(s.HandleRefresh))+ handle("/policies", meshSurface(s.HandlePolicies))+ handle("/enroll", meshSurface(noStore(s.HandleEnroll)))+ handle("/enroll/status", meshSurface(noStore(s.HandleEnrollStatus)))+ handle("/refresh", meshSurface(noStore(s.HandleRefresh)))handle("/nodes/catalog", s.HandleNodeCatalog)handle("/admin/bootstrap-tokens", noStore(s.HandleAdminBootstrapTokens))handle("/admin/bootstrap-tokens/", noStore(s.HandleAdminBootstrapTokenAction))@@ -261,6 +261,26 @@ func noStore(h http.HandlerFunc) http.HandlerFunc {}}+// meshSurface lets a member running in a browser call an endpoint of the+// mesh protocol from any origin. These endpoints authenticate by what the+// request carries, a bootstrap token or an OIDC token in the body or a+// biscuit as a bearer, never by a cookie, so a page on another origin can+// send nothing a program could not send already. The operator plane+// (/admin, /user) is cookie-authenticated and gets no such header.+func meshSurface(h http.HandlerFunc) http.HandlerFunc {+ return func(w http.ResponseWriter, r *http.Request) {+ w.Header().Set("Access-Control-Allow-Origin", "*")+ if r.Method == http.MethodOptions {+ w.Header().Set("Access-Control-Allow-Methods", "GET, POST, OPTIONS")+ w.Header().Set("Access-Control-Allow-Headers", strings.Join([]string{"Authorization", "Content-Type", api.HeaderChallengeTimestamp, api.HeaderChallengeSignature}, ", "))+ w.Header().Set("Access-Control-Max-Age", "600")+ w.WriteHeader(http.StatusNoContent)+ return+ }+ h(w, r)+ }+}+func (s *Server) discoverProviders() error {s.providersMu.Lock()defer s.providersMu.Unlock()diff --git a/internal/controlplane/server_test.go b/internal/controlplane/server_test.goindex 8c4633ae..959ea277 100644--- a/internal/controlplane/server_test.go+++ b/internal/controlplane/server_test.go@@ -3018,6 +3018,79 @@ func TestInitRegisterRoutesEmbedded(t *testing.T) {}}+// TestMeshSurfaceCORS pins what a member in a browser needs from the control+// plane and what it must not get: the mesh protocol's endpoints answer a+// preflight and mark every response for any origin, the operator plane+// does neither.+func TestMeshSurfaceCORS(t *testing.T) {+ dbPath := filepath.Join(t.TempDir(), "cp-cors.db")+ store, err := storage.NewSQLStore("sqlite", dbPath)+ if err != nil {+ t.Fatalf("failed to create store: %v", err)+ }+ defer func() { _ = store.Close() }()+ srv, err := NewServer(Options{DriverName: "sqlite", DataSourceName: dbPath, AllowedAudiences: []string{"sam-mesh-audience"}, AdminToken: "admin-token"}, store)+ if err != nil {+ t.Fatalf("failed to create server: %v", err)+ }+ if err := srv.Init(); err != nil {+ t.Fatal(err)+ }+ defer func() { _ = srv.Close() }()+ mux := http.NewServeMux()+ srv.RegisterRoutes(mux)+ ts := httptest.NewServer(mux)+ defer ts.Close()+ client := &http.Client{Timeout: 5 * time.Second}++ for _, path := range []string{"/info", "/keys", "/enroll", "/enroll/status", "/register", "/refresh", "/policies"} {+ req, _ := http.NewRequest(http.MethodOptions, ts.URL+path, nil)+ req.Header.Set("Origin", "https://agent.example")+ req.Header.Set("Access-Control-Request-Method", "POST")+ req.Header.Set("Access-Control-Request-Headers", "content-type, "+api.HeaderChallengeTimestamp)+ resp, err := client.Do(req)+ if err != nil {+ t.Fatal(err)+ }+ _ = resp.Body.Close()+ if resp.StatusCode != http.StatusNoContent {+ t.Errorf("OPTIONS %s = %s, want 204", path, resp.Status)+ }+ if got := resp.Header.Get("Access-Control-Allow-Origin"); got != "*" {+ t.Errorf("OPTIONS %s Access-Control-Allow-Origin = %q, want *", path, got)+ }+ for _, h := range []string{"Authorization", "Content-Type", api.HeaderChallengeTimestamp, api.HeaderChallengeSignature} {+ if !strings.Contains(resp.Header.Get("Access-Control-Allow-Headers"), h) {+ t.Errorf("OPTIONS %s does not allow header %s: %q", path, h, resp.Header.Get("Access-Control-Allow-Headers"))+ }+ }+ }+ resp, err := client.Get(ts.URL + "/info")+ if err != nil {+ t.Fatal(err)+ }+ _ = resp.Body.Close()+ if got := resp.Header.Get("Access-Control-Allow-Origin"); got != "*" {+ t.Errorf("GET /info Access-Control-Allow-Origin = %q, want *", got)+ }++ for _, path := range []string{"/admin/status", "/user/status", "/routers/lease"} {+ req, _ := http.NewRequest(http.MethodOptions, ts.URL+path, nil)+ req.Header.Set("Origin", "https://agent.example")+ resp, err := client.Do(req)+ if err != nil {+ t.Fatal(err)+ }+ _ = resp.Body.Close()+ if got := resp.Header.Get("Access-Control-Allow-Origin"); got != "" {+ t.Errorf("OPTIONS %s Access-Control-Allow-Origin = %q, want none", path, got)+ }+ if resp.StatusCode == http.StatusNoContent {+ t.Errorf("OPTIONS %s answered a preflight", path)+ }+ }+}+// TestAdminBootstrapTokensList pins the admin listing surface used by// `sam-one token list`: created tokens show up, and the list is admin-gated.func TestAdminBootstrapTokensList(t *testing.T) {