@@ -23,7 +23,6 @@ import (
2323 "encoding/base64"
2424 "encoding/json"
2525 "fmt"
26- "io"
2726 "net"
2827 "net/http"
2928 "net/http/httputil"
@@ -38,7 +37,6 @@ import (
3837 "github.com/libp2p/go-libp2p/core/network"
3938 "github.com/libp2p/go-libp2p/core/peer"
4039 "github.com/prometheus/client_golang/prometheus/promhttp"
41- "google.golang.org/protobuf/encoding/protojson"
4240)
4341
4442// StartSidecarServer serves the node's local API on a TCP address, on a Unix
@@ -57,12 +55,10 @@ func StartSidecarServer(node *SamNode, addr, socketPath, token, certFile, keyFil
5755
5856 // Protected endpoints. allowAuthorizationFallback=true is safe here: none of
5957 // these ever forward the inbound Authorization header to another service.
60- mux .Handle ("/sam/service/register" , withAuth (token , true , withMeshConnection (node , http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
61- handleRegisterService (node , w , r )
62- }))))
63- mux .Handle ("/sam/service/unregister" , withAuth (token , true , withMeshConnection (node , http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
64- handleUnregisterService (node , w , r )
65- }))))
58+ // Services are declared in the node's configuration and registered at
59+ // startup; there is deliberately no runtime registration surface, so no
60+ // credential held by an agent can point the mesh at a new backend or
61+ // withdraw a sibling service.
6662 mux .Handle ("/sam/service/discover" , withAuth (token , true , withMeshConnection (node , http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
6763 handleDiscoverService (node , w , r )
6864 }))))
@@ -448,90 +444,10 @@ func constantTimeEqual(got, want string) bool {
448444 return subtle .ConstantTimeCompare (gotHash [:], wantHash [:]) == 1
449445}
450446
451- type ServiceRequest struct {
452- ServiceName string `json:"service_name"`
453- }
454-
455447// maxRequestBodyBytes caps request bodies read into memory to guard
456448// against memory-exhaustion from oversized payloads.
457449const maxRequestBodyBytes = 1 << 20 // 1 MiB
458450
459- func handleRegisterService (node * SamNode , w http.ResponseWriter , r * http.Request ) {
460- if r .Method != http .MethodPost {
461- http .Error (w , "Method not allowed" , http .StatusMethodNotAllowed )
462- return
463- }
464-
465- r .Body = http .MaxBytesReader (w , r .Body , maxRequestBodyBytes )
466- body , err := io .ReadAll (r .Body )
467- if err != nil {
468- http .Error (w , "Failed to read request body" , http .StatusInternalServerError )
469- return
470- }
471- _ = r .Body .Close ()
472-
473- var req api.RegisterServiceRequest
474- if err := protojson .Unmarshal (body , & req ); err != nil {
475- http .Error (w , fmt .Sprintf ("Invalid request body: %v" , err ), http .StatusBadRequest )
476- return
477- }
478-
479- if req .Service == nil {
480- http .Error (w , "service field is required" , http .StatusBadRequest )
481- return
482- }
483-
484- if req .Service .Name == "" || req .Service .Type == api .ServiceType_SERVICE_TYPE_UNSPECIFIED {
485- http .Error (w , "name and type are required" , http .StatusBadRequest )
486- return
487- }
488-
489- if req .Backend == nil {
490- http .Error (w , "backend is required" , http .StatusBadRequest )
491- return
492- }
493-
494- if err := node .RegisterService (r .Context (), & req ); err != nil {
495- logger .Errorf ("Failed to register service: %v" , err )
496- http .Error (w , fmt .Sprintf ("Failed to register service: %v" , err ), http .StatusInternalServerError )
497- return
498- }
499-
500- w .WriteHeader (http .StatusOK )
501- if _ , err := w .Write ([]byte ("Service registered" )); err != nil {
502- logger .Errorf ("Failed to write response: %v" , err )
503- }
504- }
505-
506- func handleUnregisterService (node * SamNode , w http.ResponseWriter , r * http.Request ) {
507- if r .Method != http .MethodPost {
508- http .Error (w , "Method not allowed" , http .StatusMethodNotAllowed )
509- return
510- }
511-
512- var req api.ServiceInfo
513- r .Body = http .MaxBytesReader (w , r .Body , maxRequestBodyBytes )
514- if err := json .NewDecoder (r .Body ).Decode (& req ); err != nil {
515- http .Error (w , "Invalid request body" , http .StatusBadRequest )
516- return
517- }
518-
519- if req .Name == "" {
520- http .Error (w , "name is required" , http .StatusBadRequest )
521- return
522- }
523-
524- if err := node .UnregisterService (r .Context (), req .Name ); err != nil {
525- http .Error (w , fmt .Sprintf ("Failed to unregister service: %v" , err ), http .StatusInternalServerError )
526- return
527- }
528-
529- w .WriteHeader (http .StatusOK )
530- if _ , err := w .Write ([]byte ("Service unregistered" )); err != nil {
531- logger .Errorf ("Failed to write response: %v" , err )
532- }
533- }
534-
535451func handleDiscoverService (node * SamNode , w http.ResponseWriter , r * http.Request ) {
536452 if r .Method != http .MethodGet {
537453 http .Error (w , "Method not allowed" , http .StatusMethodNotAllowed )
0 commit comments