diff --git a/api/agent/crd_handlers.go b/api/agent/crd_handlers.go new file mode 100644 index 00000000..6a0fcb77 --- /dev/null +++ b/api/agent/crd_handlers.go @@ -0,0 +1,60 @@ +package api + +import ( + "encoding/json" + "fmt" + "io" + "net/http" + "strings" + + trustdomain "github.com/spiffe/spire-api-sdk/proto/spire/api/server/trustdomain/v1" + // "google.golang.org/protobuf/encoding/protojson" +) + +func (s *Server) CRDFederationList(w http.ResponseWriter, r *http.Request) { + // if CRD management not configured + if s.CRDManager == nil { + emsg := "Error: CRD Manager not configured on Tornjak." + retError(w, emsg, http.StatusBadRequest) + return + } + // if CRD management is configured + var input trustdomain.ListFederationRelationshipsRequest + buf := new(strings.Builder) + + n, err := io.Copy(buf, r.Body) + if err != nil { + emsg := fmt.Sprintf("Error parsing data: %v", err.Error()) + retError(w, emsg, http.StatusBadRequest) + return + } + data := buf.String() + + if n == 0 { + input = trustdomain.ListFederationRelationshipsRequest{} + } else { + err := json.Unmarshal([]byte(data), &input) + if err != nil { + emsg := fmt.Sprintf("Error parsing data: %v", err.Error()) + retError(w, emsg, http.StatusBadRequest) + return + } + } + + ret, err := s.CRDManager.ListClusterFederatedTrustDomains(input) //nolint:govet //Ignoring mutex (not being used) - sync.Mutex by value is unused for linter govet + if err != nil { + emsg := fmt.Sprintf("Error: %v", err.Error()) + retError(w, emsg, http.StatusInternalServerError) + return + } + + cors(w, r) + je := json.NewEncoder(w) + err = je.Encode(ret) //nolint:govet //Ignoring mutex (not being used) - sync.Mutex by value is unused for linter govet + if err != nil { + emsg := fmt.Sprintf("Error: %v", err.Error()) + retError(w, emsg, http.StatusBadRequest) + return + } + +} diff --git a/api/agent/server.go b/api/agent/server.go index 2405d65f..27de0e54 100644 --- a/api/agent/server.go +++ b/api/agent/server.go @@ -212,6 +212,9 @@ func (s *Server) GetRouter() http.Handler { apiRtr.HandleFunc("/api/v1/spire/federations", s.federationUpdate).Methods(http.MethodPatch) apiRtr.HandleFunc("/api/v1/spire/federations", s.federationDelete).Methods(http.MethodDelete) + // SPIRE CRD Federations + apiRtr.HandleFunc("/api/v1/spire-controller-manager/clusterfederatedtrustdomains", s.CRDFederationList).Methods(http.MethodGet, http.MethodOptions) + // Tornjak specific apiRtr.HandleFunc("/api/v1/tornjak/serverinfo", s.tornjakGetServerInfo).Methods(http.MethodGet, http.MethodOptions) // Agents Selectors diff --git a/pkg/agent/spirecrd/crdmanager.go b/pkg/agent/spirecrd/crdmanager.go index cd856805..7513df4f 100644 --- a/pkg/agent/spirecrd/crdmanager.go +++ b/pkg/agent/spirecrd/crdmanager.go @@ -2,13 +2,14 @@ package spirecrd import ( trustdomain "github.com/spiffe/spire-api-sdk/proto/spire/api/server/trustdomain/v1" + "fmt" ) // CRDManager defines the interface for managing CRDs type CRDManager interface { // TODO add List/Create/Update/Delete functions for Federation CRD // ListClusterFederatedTrustDomain has the same signature as spire api - ListClusterFederatedTrustDomain(trustdomain.ListFederationRelationshipsRequest) (trustdomain.ListFederationRelationshipsResponse, error) + ListClusterFederatedTrustDomains(trustdomain.ListFederationRelationshipsRequest) (trustdomain.ListFederationRelationshipsResponse, error) } type SPIRECRDManager struct { @@ -22,6 +23,7 @@ func NewSPIRECRDManager(className string) (*SPIRECRDManager, error) { }, nil } -func (s *SPIRECRDManager) ListClusterFederatedTrustDomain(inp trustdomain.ListFederationRelationshipsRequest) (trustdomain.ListFederationRelationshipsResponse, error) { //nolint:govet //Ignoring mutex (not being used) - sync.Mutex by value is unused for linter govet - return trustdomain.ListFederationRelationshipsResponse{}, nil +func (s *SPIRECRDManager) ListClusterFederatedTrustDomains(inp trustdomain.ListFederationRelationshipsRequest) (trustdomain.ListFederationRelationshipsResponse, error) { //nolint:govet //Ignoring mutex (not being used) - sync.Mutex by value is unused for linter govet + fmt.Printf("list crd federation endpoint hit") + return trustdomain.ListFederationRelationshipsResponse{}, nil }