Agent Mesh
Go a2aa2a-protocolagent-meshagentic-aiinferencemcpmeshmesh-networknetwork-securitynetworking-aip2p-network
controlplane: split Start into Init and RegisterRoutes for embedders
Antonio Ojea3bdfc3ae7416
diff --git a/internal/controlplane/server.go b/internal/controlplane/server.goindex f9c6b2df..e3c5c7be 100644--- a/internal/controlplane/server.go+++ b/internal/controlplane/server.go@@ -116,8 +116,49 @@ func (s *Server) getMeshAdapter() MeshAdapter {return s.mesh}+// Start boots up HTTP services, sets up OIDC providers, loads initial keys and policies, and schedules rotations.// Start boots up HTTP services, sets up OIDC providers, loads initial keys and policies, and schedules rotations.func (s *Server) Start() error {+ if err := s.Init(); err != nil {+ return err+ }++ // Setup listener+ l, err := net.Listen("tcp", s.config.ListenAddr)+ if err != nil {+ return err+ }+ s.listener = l++ mux := http.NewServeMux()+ s.RegisterRoutes(mux)++ s.httpServer = &http.Server{+ Handler: mux,+ // Mitigate Slowloris-style resource exhaustion from slow/malicious clients.+ ReadHeaderTimeout: 10 * time.Second,+ ReadTimeout: 30 * time.Second,+ WriteTimeout: 30 * time.Second,+ IdleTimeout: 120 * time.Second,+ }++ s.wg.Add(1)+ go func() {+ defer s.wg.Done()+ logger.Infof("SAM Control Plane listening on http://%s", s.config.ListenAddr)+ if err := s.httpServer.Serve(l); err != nil && !errors.Is(err, http.ErrServerClosed) {+ logger.Errorf("HTTP Server error: %v", err)+ }+ }()++ return nil+}++// Init prepares the control plane without binding a listener: it bootstraps+// the signing keyring, discovers OIDC providers and starts the key-rotation+// loop. Embedders that own their own listener call Init + RegisterRoutes+// instead of Start.+func (s *Server) Init() error {// Initialize Keyringctx := context.Background()_, _, err := s.store.GetCurrentKey(ctx)@@ -141,14 +182,15 @@ func (s *Server) Start() error {return fmt.Errorf("failed OIDC discovery: %w", err)}- // Setup listener- l, err := net.Listen("tcp", s.config.ListenAddr)- if err != nil {- return err- }- s.listener = l+ // Start key rotation routine+ s.wg.Add(1)+ go s.runKeyRotationLoop()- mux := http.NewServeMux()+ return nil+}++// RegisterRoutes registers every control-plane HTTP handler on mux.+func (s *Server) RegisterRoutes(mux *http.ServeMux) {mux.HandleFunc("/healthz", s.HandleHealthz)mux.HandleFunc("/readyz", s.HandleReadyz)mux.Handle("/metrics", promhttp.Handler())@@ -168,30 +210,6 @@ func (s *Server) Start() error {mux.HandleFunc("/user/status", s.HandleUserStatus)mux.HandleFunc("/user/bootstrap-tokens", s.HandleUserBootstrapTokens)mux.HandleFunc("/user/revoke", s.HandleUserRevoke)-- s.httpServer = &http.Server{- Handler: mux,- // Mitigate Slowloris-style resource exhaustion from slow/malicious clients.- ReadHeaderTimeout: 10 * time.Second,- ReadTimeout: 30 * time.Second,- WriteTimeout: 30 * time.Second,- IdleTimeout: 120 * time.Second,- }-- s.wg.Add(1)- go func() {- defer s.wg.Done()- logger.Infof("SAM Control Plane listening on http://%s", s.config.ListenAddr)- if err := s.httpServer.Serve(l); err != nil && !errors.Is(err, http.ErrServerClosed) {- logger.Errorf("HTTP Server error: %v", err)- }- }()-- // Start key rotation routine- s.wg.Add(1)- go s.runKeyRotationLoop()-- return nil}func (s *Server) discoverProviders() error {diff --git a/internal/controlplane/server_test.go b/internal/controlplane/server_test.goindex e7e8d538..00bf21e4 100644--- a/internal/controlplane/server_test.go+++ b/internal/controlplane/server_test.go@@ -1864,3 +1864,72 @@ func TestBootstrapTokenOwnerPropagatesToNode(t *testing.T) {t.Errorf("enrolled node owner = %q, want %q", enrolled.OwnerID, ownerSub)}}++// TestInitRegisterRoutesEmbedded pins the embedder seam used by single-binary+// distributions: Init + RegisterRoutes on an externally owned mux/listener,+// with no OIDC issuer configured, and a Close that never saw Start.+func TestInitRegisterRoutesEmbedded(t *testing.T) {+ dbPath := filepath.Join(t.TempDir(), "cp-embedded.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"},+ }, store)+ if err != nil {+ t.Fatalf("failed to create server: %v", err)+ }++ if err := srv.Init(); err != nil {+ t.Fatalf("Init() with empty OIDC issuer failed: %v", err)+ }+ if _, _, err := store.GetCurrentKey(context.Background()); err != nil {+ t.Fatalf("Init() did not bootstrap the signing keyring: %v", err)+ }+ if got := srv.Addr(); got != "" {+ t.Errorf("Addr() without an owned listener = %q, want empty", got)+ }++ mux := http.NewServeMux()+ srv.RegisterRoutes(mux)+ ts := httptest.NewServer(mux)+ defer ts.Close()++ client := &http.Client{Timeout: 5 * time.Second}+ for _, path := range []string{"/healthz", "/readyz", "/keys", "/info"} {+ resp, err := client.Get(ts.URL + path)+ if err != nil {+ t.Fatalf("GET %s failed: %v", path, err)+ }+ if resp.StatusCode != http.StatusOK {+ t.Errorf("GET %s status = %s, want 200", path, resp.Status)+ }+ if path != "/info" {+ _ = resp.Body.Close()+ continue+ }+ body, err := io.ReadAll(resp.Body)+ _ = resp.Body.Close()+ if err != nil {+ t.Fatalf("failed to read /info body: %v", err)+ }+ var info api.ControlPlaneInfoResponse+ if err := proto.Unmarshal(body, &info); err != nil {+ t.Fatalf("failed to unmarshal ControlPlaneInfoResponse: %v", err)+ }+ if info.OidcIssuer != "" {+ t.Errorf("/info issuer = %q, want empty (no OIDC configured)", info.OidcIssuer)+ }+ }++ // Close must shut down the rotation loop cleanly even though the server+ // never owned an http.Server or listener.+ if err := srv.Close(); err != nil {+ t.Fatalf("Close() without Start failed: %v", err)+ }+}