2021-03-18 04:59:00 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-05-12 17:38:00 +00:00
|
|
|
"encoding/json"
|
2021-03-18 04:59:00 +00:00
|
|
|
|
2021-06-26 04:46:51 +00:00
|
|
|
"github.com/fleetdm/fleet/v4/server/fleet"
|
2021-03-18 04:59:00 +00:00
|
|
|
"github.com/go-kit/kit/endpoint"
|
|
|
|
)
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Create Team
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
type createTeamRequest struct {
|
2021-06-06 22:07:29 +00:00
|
|
|
payload fleet.TeamPayload
|
2021-03-18 04:59:00 +00:00
|
|
|
}
|
|
|
|
|
2021-05-12 17:38:00 +00:00
|
|
|
type teamResponse struct {
|
2021-06-06 22:07:29 +00:00
|
|
|
Team *fleet.Team `json:"team,omitempty"`
|
2021-08-24 17:35:03 +00:00
|
|
|
Err error `json:"error,omitempty"`
|
2021-03-18 04:59:00 +00:00
|
|
|
}
|
|
|
|
|
2021-05-12 17:38:00 +00:00
|
|
|
func (r teamResponse) error() error { return r.Err }
|
2021-03-18 04:59:00 +00:00
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func makeCreateTeamEndpoint(svc fleet.Service) endpoint.Endpoint {
|
2021-03-18 04:59:00 +00:00
|
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
|
|
|
req := request.(createTeamRequest)
|
|
|
|
|
|
|
|
team, err := svc.NewTeam(ctx, req.payload)
|
|
|
|
if err != nil {
|
2021-05-12 17:38:00 +00:00
|
|
|
return teamResponse{Err: err}, nil
|
2021-03-18 04:59:00 +00:00
|
|
|
}
|
|
|
|
|
2021-05-12 17:38:00 +00:00
|
|
|
return teamResponse{Team: team}, nil
|
2021-03-18 04:59:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Modify Team
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
type modifyTeamRequest struct {
|
|
|
|
ID uint
|
2021-06-06 22:07:29 +00:00
|
|
|
payload fleet.TeamPayload
|
2021-03-18 04:59:00 +00:00
|
|
|
}
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func makeModifyTeamEndpoint(svc fleet.Service) endpoint.Endpoint {
|
2021-03-18 04:59:00 +00:00
|
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
|
|
|
req := request.(modifyTeamRequest)
|
|
|
|
team, err := svc.ModifyTeam(ctx, req.ID, req.payload)
|
|
|
|
if err != nil {
|
2021-05-12 17:38:00 +00:00
|
|
|
return teamResponse{Err: err}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return teamResponse{Team: team}, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Modify Team Agent Options
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
type modifyTeamAgentOptionsRequest struct {
|
|
|
|
ID uint
|
|
|
|
options json.RawMessage
|
|
|
|
}
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func makeModifyTeamAgentOptionsEndpoint(svc fleet.Service) endpoint.Endpoint {
|
2021-05-12 17:38:00 +00:00
|
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
|
|
|
req := request.(modifyTeamAgentOptionsRequest)
|
|
|
|
team, err := svc.ModifyTeamAgentOptions(ctx, req.ID, req.options)
|
|
|
|
if err != nil {
|
|
|
|
return teamResponse{Err: err}, nil
|
2021-03-18 04:59:00 +00:00
|
|
|
}
|
|
|
|
|
2021-05-12 17:38:00 +00:00
|
|
|
return teamResponse{Team: team}, err
|
2021-03-18 04:59:00 +00:00
|
|
|
}
|
|
|
|
}
|
2021-04-06 18:40:14 +00:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// List Teams
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
type listTeamsRequest struct {
|
2021-06-06 22:07:29 +00:00
|
|
|
ListOptions fleet.ListOptions
|
2021-04-06 18:40:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type listTeamsResponse struct {
|
2021-06-06 22:07:29 +00:00
|
|
|
Teams []fleet.Team `json:"teams"`
|
2021-08-24 17:35:03 +00:00
|
|
|
Err error `json:"error,omitempty"`
|
2021-04-06 18:40:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r listTeamsResponse) error() error { return r.Err }
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func makeListTeamsEndpoint(svc fleet.Service) endpoint.Endpoint {
|
2021-04-06 18:40:14 +00:00
|
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
|
|
|
req := request.(listTeamsRequest)
|
|
|
|
teams, err := svc.ListTeams(ctx, req.ListOptions)
|
|
|
|
if err != nil {
|
|
|
|
return listTeamsResponse{Err: err}, nil
|
|
|
|
}
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
resp := listTeamsResponse{Teams: []fleet.Team{}}
|
2021-04-06 18:40:14 +00:00
|
|
|
for _, team := range teams {
|
|
|
|
resp.Teams = append(resp.Teams, *team)
|
|
|
|
}
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
}
|
2021-04-20 17:20:52 +00:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Delete Team
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
type deleteTeamRequest struct {
|
|
|
|
ID uint `json:"id"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type deleteTeamResponse struct {
|
|
|
|
Err error `json:"error,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r deleteTeamResponse) error() error { return r.Err }
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func makeDeleteTeamEndpoint(svc fleet.Service) endpoint.Endpoint {
|
2021-04-20 17:20:52 +00:00
|
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
|
|
|
req := request.(deleteTeamRequest)
|
|
|
|
err := svc.DeleteTeam(ctx, req.ID)
|
|
|
|
if err != nil {
|
|
|
|
return deleteTeamResponse{Err: err}, nil
|
|
|
|
}
|
|
|
|
return deleteTeamResponse{}, nil
|
|
|
|
}
|
|
|
|
}
|
2021-04-22 03:54:09 +00:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// List Team Users
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
type listTeamUsersRequest struct {
|
|
|
|
TeamID uint
|
2021-06-06 22:07:29 +00:00
|
|
|
ListOptions fleet.ListOptions
|
2021-04-22 03:54:09 +00:00
|
|
|
}
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func makeListTeamUsersEndpoint(svc fleet.Service) endpoint.Endpoint {
|
2021-04-22 03:54:09 +00:00
|
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
|
|
|
req := request.(listTeamUsersRequest)
|
|
|
|
users, err := svc.ListTeamUsers(ctx, req.TeamID, req.ListOptions)
|
|
|
|
if err != nil {
|
|
|
|
return listUsersResponse{Err: err}, nil
|
|
|
|
}
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
resp := listUsersResponse{Users: []fleet.User{}}
|
2021-04-22 03:54:09 +00:00
|
|
|
for _, user := range users {
|
|
|
|
resp.Users = append(resp.Users, *user)
|
|
|
|
}
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Add / Delete Team Users
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
type modifyTeamUsersRequest struct {
|
|
|
|
TeamID uint // From request path
|
|
|
|
// User ID and role must be specified for add users, user ID must be
|
|
|
|
// specified for delete users.
|
2021-06-06 22:07:29 +00:00
|
|
|
Users []fleet.TeamUser `json:"users"`
|
2021-04-22 03:54:09 +00:00
|
|
|
}
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func makeAddTeamUsersEndpoint(svc fleet.Service) endpoint.Endpoint {
|
2021-04-22 03:54:09 +00:00
|
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
|
|
|
req := request.(modifyTeamUsersRequest)
|
|
|
|
team, err := svc.AddTeamUsers(ctx, req.TeamID, req.Users)
|
|
|
|
if err != nil {
|
2021-05-12 17:38:00 +00:00
|
|
|
return teamResponse{Err: err}, nil
|
2021-04-22 03:54:09 +00:00
|
|
|
}
|
|
|
|
|
2021-05-12 17:38:00 +00:00
|
|
|
return teamResponse{Team: team}, err
|
2021-04-22 03:54:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func makeDeleteTeamUsersEndpoint(svc fleet.Service) endpoint.Endpoint {
|
2021-04-22 03:54:09 +00:00
|
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
|
|
|
req := request.(modifyTeamUsersRequest)
|
|
|
|
team, err := svc.DeleteTeamUsers(ctx, req.TeamID, req.Users)
|
|
|
|
if err != nil {
|
2021-05-12 17:38:00 +00:00
|
|
|
return teamResponse{Err: err}, nil
|
2021-04-22 03:54:09 +00:00
|
|
|
}
|
|
|
|
|
2021-05-12 17:38:00 +00:00
|
|
|
return teamResponse{Team: team}, err
|
2021-04-22 03:54:09 +00:00
|
|
|
}
|
|
|
|
}
|
2021-05-31 16:02:05 +00:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Get enroll secrets for team
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
type teamEnrollSecretsRequest struct {
|
|
|
|
TeamID uint
|
|
|
|
}
|
|
|
|
|
|
|
|
type teamEnrollSecretsResponse struct {
|
2021-06-06 22:07:29 +00:00
|
|
|
Secrets []*fleet.EnrollSecret `json:"secrets"`
|
2021-08-24 17:35:03 +00:00
|
|
|
Err error `json:"error,omitempty"`
|
2021-05-31 16:02:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r teamEnrollSecretsResponse) error() error { return r.Err }
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func makeTeamEnrollSecretsEndpoint(svc fleet.Service) endpoint.Endpoint {
|
2021-05-31 16:02:05 +00:00
|
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
|
|
|
req := request.(teamEnrollSecretsRequest)
|
|
|
|
secrets, err := svc.TeamEnrollSecrets(ctx, req.TeamID)
|
|
|
|
if err != nil {
|
|
|
|
return teamEnrollSecretsResponse{Err: err}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return teamEnrollSecretsResponse{Secrets: secrets}, err
|
|
|
|
}
|
|
|
|
}
|