Skip to content

Commit

Permalink
initial function handlers added
Browse files Browse the repository at this point in the history
Signed-off-by: Maia Iyer <[email protected]>
  • Loading branch information
maia-iyer committed Oct 18, 2024
1 parent 8b183e3 commit 4e69216
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 3 deletions.
60 changes: 60 additions & 0 deletions api/agent/crd_handlers.go
Original file line number Diff line number Diff line change
@@ -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
}

}
3 changes: 3 additions & 0 deletions api/agent/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 5 additions & 3 deletions pkg/agent/spirecrd/crdmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}

0 comments on commit 4e69216

Please sign in to comment.