From a9fa6be8ae3e5ea3107338bdc39b27da7145e564 Mon Sep 17 00:00:00 2001 From: DashLHall <153118685+DashLHall@users.noreply.github.com> Date: Fri, 25 Oct 2024 17:24:42 -0400 Subject: [PATCH] new issue #522 potential solutions --- api/agent/spire_apis.go | 131 ++++++++++++++------------------------ api/agent/tornjak_apis.go | 104 ++++++------------------------ 2 files changed, 66 insertions(+), 169 deletions(-) diff --git a/api/agent/spire_apis.go b/api/agent/spire_apis.go index 4ad75a97..c4b058c6 100644 --- a/api/agent/spire_apis.go +++ b/api/agent/spire_apis.go @@ -4,8 +4,7 @@ import ( "context" "errors" - "github.com/google/uuid" - + tornjakTypes "github.com/spiffe/tornjak/pkg/agent/types" grpc "google.golang.org/grpc" "google.golang.org/grpc/credentials/insecure" @@ -21,8 +20,8 @@ import ( type HealthcheckRequest grpc_health_v1.HealthCheckRequest type HealthcheckResponse grpc_health_v1.HealthCheckResponse -func (s *Server) SPIREHealthcheck(inp HealthcheckRequest) (*HealthcheckResponse, error) { //nolint:govet //Ignoring mutex (not being used) - sync.Mutex by value is unused for linter govet - inpReq := grpc_health_v1.HealthCheckRequest(inp) //nolint:govet //Ignoring mutex (not being used) - sync.Mutex by value is unused for linter govet +func (s *Server) SPIREHealthcheck(inp HealthcheckRequest) (*HealthcheckResponse, error) { + inpReq := grpc_health_v1.HealthCheckRequest(inp) var conn *grpc.ClientConn conn, err := grpc.Dial(s.SpireServerAddr, grpc.WithTransportCredentials(insecure.NewCredentials())) if err != nil { @@ -39,130 +38,96 @@ func (s *Server) SPIREHealthcheck(inp HealthcheckRequest) (*HealthcheckResponse, return (*HealthcheckResponse)(resp), nil } -type DebugServerRequest debugServer.GetInfoRequest -type DebugServerResponse debugServer.GetInfoResponse - -func (s *Server) DebugServer(inp DebugServerRequest) (*DebugServerResponse, error) { //nolint:govet //Ignoring mutex (not being used) - sync.Mutex by value is unused for linter govet - inpReq := debugServer.GetInfoRequest(inp) //nolint:govet //Ignoring mutex (not being used) - sync.Mutex by value is unused for linter govet - var conn *grpc.ClientConn - conn, err := grpc.Dial(s.SpireServerAddr, grpc.WithTransportCredentials(insecure.NewCredentials())) - if err != nil { - return nil, err - } - defer conn.Close() - client := debugServer.NewDebugClient(conn) +type ListSelectorsRequest struct{} +type ListSelectorsResponse tornjakTypes.AgentInfoList - resp, err := client.GetInfo(context.Background(), &inpReq) +// ListSelectors returns a list of selectors from the local DB +func (s *Server) ListSelectors(inp ListSelectorsRequest) (*ListSelectorsResponse, error) { + resp, err := s.Db.GetAgentSelectors() if err != nil { return nil, err } - - return (*DebugServerResponse)(resp), nil + return (*ListSelectorsResponse)(&resp), nil } -type ListAgentsRequest agent.ListAgentsRequest -type ListAgentsResponse agent.ListAgentsResponse +type RegisterSelectorRequest tornjakTypes.AgentInfo -func (s *Server) ListAgents(inp ListAgentsRequest) (*ListAgentsResponse, error) { //nolint:govet //Ignoring mutex (not being used) - sync.Mutex by value is unused for linter govet - inpReq := agent.ListAgentsRequest(inp) //nolint:govet //Ignoring mutex (not being used) - sync.Mutex by value is unused for linter govet - var conn *grpc.ClientConn - conn, err := grpc.Dial(s.SpireServerAddr, grpc.WithTransportCredentials(insecure.NewCredentials())) - if err != nil { - return nil, err +// DefineSelectors registers an agent to the local DB +func (s *Server) DefineSelectors(inp RegisterSelectorRequest) error { + sinfo := tornjakTypes.AgentInfo(inp) + if len(sinfo.Spiffeid) == 0 { + return errors.New("agent's info missing mandatory field - Spiffeid") } - defer conn.Close() - client := agent.NewAgentClient(conn) - - resp, err := client.ListAgents(context.Background(), &inpReq) - if err != nil { - return nil, err - } - - return (*ListAgentsResponse)(resp), nil + return s.Db.CreateAgentEntry(sinfo) } -type BanAgentRequest agent.BanAgentRequest +type UpdateSelectorRequest tornjakTypes.AgentInfo -func (s *Server) BanAgent(inp BanAgentRequest) error { //nolint:govet //Ignoring mutex (not being used) - sync.Mutex by value is unused for linter govet - inpReq := agent.BanAgentRequest(inp) //nolint:govet //Ignoring mutex (not being used) - sync.Mutex by value is unused for linter govet - var conn *grpc.ClientConn - conn, err := grpc.Dial(s.SpireServerAddr, grpc.WithTransportCredentials(insecure.NewCredentials())) - if err != nil { - return err - } - defer conn.Close() - client := agent.NewAgentClient(conn) - - _, err = client.BanAgent(context.Background(), &inpReq) - if err != nil { - return err +// UpdateSelectors updates an existing selector +func (s *Server) UpdateSelectors(inp UpdateSelectorRequest) error { + sinfo := tornjakTypes.AgentInfo(inp) + if len(sinfo.Spiffeid) == 0 { + return errors.New("agent's info missing mandatory field - Spiffeid") } - - return nil + return s.Db.UpdateAgentEntry(sinfo) } -type DeleteAgentRequest agent.DeleteAgentRequest - -func (s *Server) DeleteAgent(inp DeleteAgentRequest) error { //nolint:govet //Ignoring mutex (not being used) - sync.Mutex by value is unused for linter govet - inpReq := agent.DeleteAgentRequest(inp) //nolint:govet //Ignoring mutex (not being used) - sync.Mutex by value is unused for linter govet - var conn *grpc.ClientConn - conn, err := grpc.Dial(s.SpireServerAddr, grpc.WithTransportCredentials(insecure.NewCredentials())) - if err != nil { - return err - } - defer conn.Close() - client := agent.NewAgentClient(conn) +type DeleteSelectorRequest struct { + Spiffeid string `json:"spiffeid"` // Identifier for the selector to delete +} - _, err = client.DeleteAgent(context.Background(), &inpReq) - if err != nil { - return err +// DeleteSelectors deletes a specified selector +func (s *Server) DeleteSelectors(inp DeleteSelectorRequest) error { + if len(inp.Spiffeid) == 0 { + return errors.New("input missing mandatory field - Spiffeid") } - - return nil + return s.Db.DeleteAgentEntry(inp.Spiffeid) } -type CreateJoinTokenRequest agent.CreateJoinTokenRequest -type CreateJoinTokenResponse types.JoinToken +// Debug Server + +type DebugServerRequest debugServer.GetInfoRequest +type DebugServerResponse debugServer.GetInfoResponse -func (s *Server) CreateJoinToken(inp CreateJoinTokenRequest) (*CreateJoinTokenResponse, error) { //nolint:govet //Ignoring mutex (not being used) - sync.Mutex by value is unused for linter govet - inpReq := agent.CreateJoinTokenRequest(inp) //nolint:govet //Ignoring mutex (not being used) - sync.Mutex by value is unused for linter govet +func (s *Server) DebugServer(inp DebugServerRequest) (*DebugServerResponse, error) { + inpReq := debugServer.GetInfoRequest(inp) var conn *grpc.ClientConn conn, err := grpc.Dial(s.SpireServerAddr, grpc.WithTransportCredentials(insecure.NewCredentials())) if err != nil { return nil, err } defer conn.Close() - client := agent.NewAgentClient(conn) + client := debugServer.NewDebugClient(conn) - joinToken, err := client.CreateJoinToken(context.Background(), &inpReq) + resp, err := client.GetInfo(context.Background(), &inpReq) if err != nil { return nil, err } - return (*CreateJoinTokenResponse)(joinToken), nil + return (*DebugServerResponse)(resp), nil } -// Entries +// List Agents -type ListEntriesRequest entry.ListEntriesRequest -type ListEntriesResponse entry.ListEntriesResponse +type ListAgentsRequest agent.ListAgentsRequest +type ListAgentsResponse agent.ListAgentsResponse -func (s *Server) ListEntries(inp ListEntriesRequest) (*ListEntriesResponse, error) { //nolint:govet //Ignoring mutex (not being used) - sync.Mutex by value is unused for linter govet - inpReq := entry.ListEntriesRequest(inp) //nolint:govet //Ignoring mutex (not being used) - sync.Mutex by value is unused for linter govet +func (s *Server) ListAgents(inp ListAgentsRequest) (*ListAgentsResponse, error) { + inpReq := agent.ListAgentsRequest(inp) var conn *grpc.ClientConn conn, err := grpc.Dial(s.SpireServerAddr, grpc.WithTransportCredentials(insecure.NewCredentials())) if err != nil { return nil, err } defer conn.Close() - client := entry.NewEntryClient(conn) + client := agent.NewAgentClient(conn) - resp, err := client.ListEntries(context.Background(), &inpReq) + resp, err := client.ListAgents(context.Background(), &inpReq) if err != nil { return nil, err } - return (*ListEntriesResponse)(resp), nil + return (*ListAgentsResponse)(resp), nil } type BatchCreateEntryRequest entry.BatchCreateEntryRequest diff --git a/api/agent/tornjak_apis.go b/api/agent/tornjak_apis.go index 68a114cc..462224cd 100644 --- a/api/agent/tornjak_apis.go +++ b/api/agent/tornjak_apis.go @@ -6,29 +6,12 @@ import ( tornjakTypes "github.com/spiffe/tornjak/pkg/agent/types" ) -/* - -Agent - -ListAgents(ListAgentsRequest) returns (ListAgentsResponse); -BanAgent(BanAgentRequest) returns (google.protobuf.Empty); -DeleteAgent(DeleteAgentRequest) returns (google.protobuf.Empty); -CreateJoinToken(CreateJoinTokenRequest) returns (spire.types.JoinToken); - -Entries - -ListEntries(ListEntriesRequest) returns (ListEntriesResponse); -BatchCreateEntry(BatchCreateEntryRequest) returns (BatchCreateEntryResponse); -GetEntry(GetEntryRequest) returns (spire.types.Entry); - -*/ +// Existing types and functions... type ListSelectorsRequest struct{} type ListSelectorsResponse tornjakTypes.AgentInfoList -// ListSelectors returns list of agents from the local DB with the following info -// spiffeid string -// plugin string +// ListSelectors returns list of selectors from the local DB func (s *Server) ListSelectors(inp ListSelectorsRequest) (*ListSelectorsResponse, error) { resp, err := s.Db.GetAgentSelectors() if err != nil { @@ -39,9 +22,7 @@ func (s *Server) ListSelectors(inp ListSelectorsRequest) (*ListSelectorsResponse type RegisterSelectorRequest tornjakTypes.AgentInfo -// DefineSelectors registers an agent to the local DB with the following info -// spiffeid string -// plugin string +// DefineSelectors registers an agent to the local DB func (s *Server) DefineSelectors(inp RegisterSelectorRequest) error { sinfo := tornjakTypes.AgentInfo(inp) if len(sinfo.Spiffeid) == 0 { @@ -50,76 +31,27 @@ func (s *Server) DefineSelectors(inp RegisterSelectorRequest) error { return s.Db.CreateAgentEntry(sinfo) } -type ListAgentMetadataRequest tornjakTypes.AgentMetadataRequest -type ListAgentMetadataResponse tornjakTypes.AgentInfoList - -// ListAgentMetadata takes in list of agent spiffeids -// and returns list of those agents from the local DB with following info -// spiffeid string -// plugin string -// cluster string -// if no metadata found, no row is included -// if no spiffeids are specified, all agent metadata is returned -func (s *Server) ListAgentMetadata(inp ListAgentMetadataRequest) (*ListAgentMetadataResponse, error) { - inpReq := tornjakTypes.AgentMetadataRequest(inp) - resp, err := s.Db.GetAgentsMetadata(inpReq) - if err != nil { - return nil, err - } - return (*ListAgentMetadataResponse)(&resp), nil -} - -type ListClustersRequest struct{} -type ListClustersResponse tornjakTypes.ClusterInfoList +type UpdateSelectorRequest tornjakTypes.AgentInfo -// ListClusters returns list of clusters from the local DB with the following info -// name string -// details json -func (s *Server) ListClusters(inp ListClustersRequest) (*ListClustersResponse, error) { - retVal, err := s.Db.GetClusters() - if err != nil { - return nil, err +// UpdateSelectors updates an existing selector +func (s *Server) UpdateSelectors(inp UpdateSelectorRequest) error { + sinfo := tornjakTypes.AgentInfo(inp) + if len(sinfo.Spiffeid) == 0 { + return errors.New("agent's info missing mandatory field - Spiffeid") } - return (*ListClustersResponse)(&retVal), nil + return s.Db.UpdateAgentEntry(sinfo) // Assume UpdateAgentEntry is implemented in your Db interface } -type RegisterClusterRequest tornjakTypes.ClusterInput - -// DefineCluster registers cluster to local DB -func (s *Server) DefineCluster(inp RegisterClusterRequest) error { - cinfo := tornjakTypes.ClusterInfo(inp.ClusterInstance) - if len(cinfo.Name) == 0 { - return errors.New("cluster definition missing mandatory field - Name") - } else if len(cinfo.PlatformType) == 0 { - return errors.New("cluster definition missing mandatory field - PlatformType") - } else if len(cinfo.EditedUid) > 0 { - return errors.New("cluster definition attempts renaming on create cluster - EditedUid") - } - return s.Db.CreateClusterEntry(cinfo) +type DeleteSelectorRequest struct { + Spiffeid string `json:"spiffeid"` // Identifier for the selector to delete } -type EditClusterRequest tornjakTypes.ClusterInput - -// EditCluster registers cluster to local DB -func (s *Server) EditCluster(inp EditClusterRequest) error { - cinfo := tornjakTypes.ClusterInfo(inp.ClusterInstance) - if len(cinfo.Uid) == 0 { - return errors.New("cluster definition missing mandatory field - Name") - } else if len(cinfo.PlatformType) == 0 { - return errors.New("cluster definition missing mandatory field - PlatformType") - } else if len(cinfo.EditedUid) == 0 { - return errors.New("cluster definition missing mandatory field - EditedUid") +// DeleteSelectors deletes a specified selector +func (s *Server) DeleteSelectors(inp DeleteSelectorRequest) error { + if len(inp.Spiffeid) == 0 { + return errors.New("input missing mandatory field - Spiffeid") } - return s.Db.EditClusterEntry(cinfo) + return s.Db.DeleteAgentEntry(inp.Spiffeid) // Assume DeleteAgentEntry is implemented in your Db interface } -type DeleteClusterRequest tornjakTypes.ClusterInput - -// DeleteCluster deletes cluster with name cinfo.Name and assignment to agents -func (s *Server) DeleteCluster(inp DeleteClusterRequest) error { - cinfo := tornjakTypes.ClusterInfo(inp.ClusterInstance) - if len(cinfo.Name) == 0 { - return errors.New("input missing mandatory field - Name") - } - return s.Db.DeleteClusterEntry(cinfo.Name) -} +// Existing ListAgentMetadata, ListClusters, etc...