Skip to content

Commit

Permalink
Bringing crd_handlers.go up to date with Maia's 34cf316 commit
Browse files Browse the repository at this point in the history
Signed-off-by: AmusedWaffle <[email protected]>
  • Loading branch information
AmusedWaffle committed Feb 7, 2025
1 parent 99c5d5e commit 2726912
Showing 1 changed file with 85 additions and 44 deletions.
129 changes: 85 additions & 44 deletions api/agent/crd_handlers.go
Original file line number Diff line number Diff line change
@@ -1,45 +1,86 @@
package api

import (
"encoding/json"
"net/http"
"strings"

crdmanager "github.com/spiffe/tornjak/pkg/agent/spirecrd"
)

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 crdmanager.ListFederationRelationshipsRequest
buf := new(strings.Builder)

n, err, data := copyIntoBuff(buf, w, r, "Error parsing data: ")
if n == 0 && data == "err" { return }

if n == 0 {
input = crdmanager.ListFederationRelationshipsRequest{}
} else {
err := json.Unmarshal([]byte(data), &input)
if isHttpError(err, w, "Error parsing data: ", 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 isHttpError(err, w, "Error: ", 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 isHttpError(err, w, "Error: ", http.StatusBadRequest) {
return
}
package api

import (
"encoding/json"
"net/http"
"strings"
"google.golang.org/protobuf/encoding/protojson"

crdmanager "github.com/spiffe/tornjak/pkg/agent/spirecrd"
trustdomain "github.com/spiffe/spire-api-sdk/proto/spire/api/server/trustdomain/v1"
)

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 crdmanager.ListFederationRelationshipsRequest
buf := new(strings.Builder)

n, err, data := copyIntoBuff(buf, w, r, "Error parsing data: ")
if n == 0 && data == "err" { return }

if n == 0 {
input = crdmanager.ListFederationRelationshipsRequest{}
} else {
err := json.Unmarshal([]byte(data), &input)
if isHttpError(err, w, "Error parsing data: ", 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 isHttpError(err, w, "Error: ", 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 isHttpError(err, w, "Error: ", http.StatusBadRequest) {
return
}
}

func (s *Server) CRDFederationCreate(w http.ResponseWriter, r *http.Request) {
if s.CRDManager == nil {
emsg := "Error: CRD Manager not configured on Tornjak."
retError(w, emsg, http.StatusBadRequest)
return
}
var input crdmanager.BatchCreateFederationRelationshipsRequest
var rawInput trustdomain.BatchCreateFederationRelationshipRequest
buf := new(strings.Builder)

n, err, data := copyIntoBuff(buf, w, r, "Error parsing data: ")
if n == 0 && data == "err" { return }

if n == 0 {
input = crdmanager.BatchCreateFederationRelationshipsRequest{}
} else {
// required to use protojson because of oneof field
err := protojson.Unmarshal([]byte(data), &rawInput)
if isHttpError(err, w, "Error parsing data: ", http.StatusBadRequest) {
return
}
input = crdmanager.BatchCreateFederationRelationshipsRequest(rawInput) //nolint:govet //Ignoring mutex (not being used) - sync.Mutex by value is unused for linter govet
}

ret, err := s.CRDManager.BatchCreateClusterFederatedTrustDomains(input) //nolint:govet //Ignoring mutex (not being used) - sync.Mutex by value is unused for linter govet
if isHttpError(err, w, "Error: ", 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 isHttpError(err, w, "Error: ", http.StatusBadRequest) {
return
}
}

0 comments on commit 2726912

Please sign in to comment.