2021-06-01 00:07:51 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-02-03 19:24:03 +00:00
|
|
|
"database/sql"
|
2021-06-01 00:07:51 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2021-07-23 20:00:26 +00:00
|
|
|
|
2021-07-19 18:08:41 +00:00
|
|
|
"github.com/fleetdm/fleet/v4/server"
|
2021-07-13 19:54:22 +00:00
|
|
|
"github.com/fleetdm/fleet/v4/server/authz"
|
2021-11-22 14:13:26 +00:00
|
|
|
"github.com/fleetdm/fleet/v4/server/contexts/ctxerr"
|
2021-08-02 22:06:27 +00:00
|
|
|
"github.com/fleetdm/fleet/v4/server/contexts/logging"
|
2021-06-26 04:46:51 +00:00
|
|
|
"github.com/fleetdm/fleet/v4/server/contexts/viewer"
|
|
|
|
"github.com/fleetdm/fleet/v4/server/fleet"
|
2021-11-11 16:45:39 +00:00
|
|
|
"github.com/fleetdm/fleet/v4/server/ptr"
|
2021-06-01 00:07:51 +00:00
|
|
|
)
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func (svc *Service) NewTeam(ctx context.Context, p fleet.TeamPayload) (*fleet.Team, error) {
|
|
|
|
if err := svc.authz.Authorize(ctx, &fleet.Team{}, fleet.ActionWrite); err != nil {
|
2021-06-03 23:24:15 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-06-24 00:32:31 +00:00
|
|
|
// Copy team options from global options
|
2021-09-14 12:11:07 +00:00
|
|
|
globalConfig, err := svc.ds.AppConfig(ctx)
|
2021-06-24 00:32:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
team := &fleet.Team{
|
|
|
|
AgentOptions: globalConfig.AgentOptions,
|
|
|
|
}
|
2021-06-01 00:07:51 +00:00
|
|
|
|
|
|
|
if p.Name == nil {
|
2021-06-06 22:07:29 +00:00
|
|
|
return nil, fleet.NewInvalidArgumentError("name", "missing required argument")
|
2021-06-01 00:07:51 +00:00
|
|
|
}
|
|
|
|
if *p.Name == "" {
|
2021-06-06 22:07:29 +00:00
|
|
|
return nil, fleet.NewInvalidArgumentError("name", "may not be empty")
|
2021-06-01 00:07:51 +00:00
|
|
|
}
|
|
|
|
team.Name = *p.Name
|
|
|
|
|
|
|
|
if p.Description != nil {
|
|
|
|
team.Description = *p.Description
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.Secrets != nil {
|
|
|
|
team.Secrets = p.Secrets
|
|
|
|
} else {
|
|
|
|
// Set up a default enroll secret
|
2021-07-19 18:08:41 +00:00
|
|
|
secret, err := server.GenerateRandomText(fleet.EnrollSecretDefaultLength)
|
2021-06-01 00:07:51 +00:00
|
|
|
if err != nil {
|
2021-11-22 14:13:26 +00:00
|
|
|
return nil, ctxerr.Wrap(ctx, err, "generate enroll secret string")
|
2021-06-01 00:07:51 +00:00
|
|
|
}
|
2021-06-06 22:07:29 +00:00
|
|
|
team.Secrets = []*fleet.EnrollSecret{{Secret: secret}}
|
2021-06-01 00:07:51 +00:00
|
|
|
}
|
2021-06-24 00:32:31 +00:00
|
|
|
|
2021-09-14 12:11:07 +00:00
|
|
|
team, err = svc.ds.NewTeam(ctx, team)
|
2021-06-01 00:07:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-07-13 19:54:22 +00:00
|
|
|
|
|
|
|
if err := svc.ds.NewActivity(
|
2021-09-14 12:11:07 +00:00
|
|
|
ctx,
|
2021-07-13 19:54:22 +00:00
|
|
|
authz.UserFromContext(ctx),
|
|
|
|
fleet.ActivityTypeCreatedTeam,
|
|
|
|
&map[string]interface{}{"team_id": team.ID, "team_name": team.Name},
|
|
|
|
); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-06-01 00:07:51 +00:00
|
|
|
return team, nil
|
|
|
|
}
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func (svc *Service) ModifyTeam(ctx context.Context, teamID uint, payload fleet.TeamPayload) (*fleet.Team, error) {
|
|
|
|
if err := svc.authz.Authorize(ctx, &fleet.Team{ID: teamID}, fleet.ActionWrite); err != nil {
|
2021-06-03 23:24:15 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-09-14 12:11:07 +00:00
|
|
|
team, err := svc.ds.Team(ctx, teamID)
|
2021-06-01 00:07:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if payload.Name != nil {
|
|
|
|
if *payload.Name == "" {
|
2021-06-06 22:07:29 +00:00
|
|
|
return nil, fleet.NewInvalidArgumentError("name", "may not be empty")
|
2021-06-01 00:07:51 +00:00
|
|
|
}
|
|
|
|
team.Name = *payload.Name
|
|
|
|
}
|
|
|
|
if payload.Description != nil {
|
|
|
|
team.Description = *payload.Description
|
|
|
|
}
|
|
|
|
|
2021-09-14 12:11:07 +00:00
|
|
|
return svc.ds.SaveTeam(ctx, team)
|
2021-06-01 00:07:51 +00:00
|
|
|
}
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func (svc *Service) ModifyTeamAgentOptions(ctx context.Context, teamID uint, options json.RawMessage) (*fleet.Team, error) {
|
|
|
|
if err := svc.authz.Authorize(ctx, &fleet.Team{ID: teamID}, fleet.ActionWrite); err != nil {
|
2021-06-03 23:24:15 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-09-14 12:11:07 +00:00
|
|
|
team, err := svc.ds.Team(ctx, teamID)
|
2021-06-01 00:07:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if options != nil {
|
|
|
|
team.AgentOptions = &options
|
|
|
|
} else {
|
|
|
|
team.AgentOptions = nil
|
|
|
|
}
|
|
|
|
|
2021-09-14 12:11:07 +00:00
|
|
|
return svc.ds.SaveTeam(ctx, team)
|
2021-06-01 00:07:51 +00:00
|
|
|
}
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func (svc *Service) AddTeamUsers(ctx context.Context, teamID uint, users []fleet.TeamUser) (*fleet.Team, error) {
|
|
|
|
if err := svc.authz.Authorize(ctx, &fleet.Team{ID: teamID}, fleet.ActionWrite); err != nil {
|
2021-06-03 23:24:15 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-10-13 15:34:59 +00:00
|
|
|
currentUser := authz.UserFromContext(ctx)
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
idMap := make(map[uint]fleet.TeamUser)
|
2021-06-01 00:07:51 +00:00
|
|
|
for _, user := range users {
|
2021-06-06 22:07:29 +00:00
|
|
|
if !fleet.ValidTeamRole(user.Role) {
|
|
|
|
return nil, fleet.NewInvalidArgumentError("users", fmt.Sprintf("%s is not a valid role for a team user", user.Role))
|
2021-06-01 00:07:51 +00:00
|
|
|
}
|
|
|
|
idMap[user.ID] = user
|
2021-10-13 15:34:59 +00:00
|
|
|
fullUser, err := svc.ds.UserByID(ctx, user.ID)
|
|
|
|
if err != nil {
|
2021-11-22 14:13:26 +00:00
|
|
|
return nil, ctxerr.Wrapf(ctx, err, "getting full user with id %d", user.ID)
|
2021-10-13 15:34:59 +00:00
|
|
|
}
|
|
|
|
if fullUser.GlobalRole != nil && currentUser.GlobalRole == nil {
|
2021-11-22 14:13:26 +00:00
|
|
|
return nil, ctxerr.New(ctx, "A user with a global role cannot be added to a team by a non global user.")
|
2021-10-13 15:34:59 +00:00
|
|
|
}
|
2021-06-01 00:07:51 +00:00
|
|
|
}
|
|
|
|
|
2021-09-14 12:11:07 +00:00
|
|
|
team, err := svc.ds.Team(ctx, teamID)
|
2021-06-01 00:07:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Replace existing
|
|
|
|
for i, existingUser := range team.Users {
|
|
|
|
if user, ok := idMap[existingUser.ID]; ok {
|
|
|
|
team.Users[i] = user
|
|
|
|
delete(idMap, user.ID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add new (that have not already been replaced)
|
|
|
|
for _, user := range idMap {
|
|
|
|
team.Users = append(team.Users, user)
|
|
|
|
}
|
|
|
|
|
2021-08-02 22:06:27 +00:00
|
|
|
logging.WithExtras(ctx, "users", team.Users)
|
|
|
|
|
2021-09-14 12:11:07 +00:00
|
|
|
return svc.ds.SaveTeam(ctx, team)
|
2021-06-01 00:07:51 +00:00
|
|
|
}
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func (svc *Service) DeleteTeamUsers(ctx context.Context, teamID uint, users []fleet.TeamUser) (*fleet.Team, error) {
|
|
|
|
if err := svc.authz.Authorize(ctx, &fleet.Team{ID: teamID}, fleet.ActionWrite); err != nil {
|
2021-06-03 23:24:15 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-06-01 00:07:51 +00:00
|
|
|
idMap := make(map[uint]bool)
|
|
|
|
for _, user := range users {
|
|
|
|
idMap[user.ID] = true
|
|
|
|
}
|
|
|
|
|
2021-09-14 12:11:07 +00:00
|
|
|
team, err := svc.ds.Team(ctx, teamID)
|
2021-06-01 00:07:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
newUsers := []fleet.TeamUser{}
|
2021-06-01 00:07:51 +00:00
|
|
|
// Delete existing
|
|
|
|
for _, existingUser := range team.Users {
|
|
|
|
if _, ok := idMap[existingUser.ID]; !ok {
|
|
|
|
// Only add non-deleted users
|
|
|
|
newUsers = append(newUsers, existingUser)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
team.Users = newUsers
|
|
|
|
|
2021-08-02 22:06:27 +00:00
|
|
|
logging.WithExtras(ctx, "users", team.Users)
|
|
|
|
|
2021-09-14 12:11:07 +00:00
|
|
|
return svc.ds.SaveTeam(ctx, team)
|
2021-06-01 00:07:51 +00:00
|
|
|
}
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func (svc *Service) ListTeamUsers(ctx context.Context, teamID uint, opt fleet.ListOptions) ([]*fleet.User, error) {
|
|
|
|
if err := svc.authz.Authorize(ctx, &fleet.Team{ID: teamID}, fleet.ActionRead); err != nil {
|
2021-06-03 23:24:15 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-09-14 12:11:07 +00:00
|
|
|
team, err := svc.ds.Team(ctx, teamID)
|
2021-06-01 00:07:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-09-14 12:11:07 +00:00
|
|
|
return svc.ds.ListUsers(ctx, fleet.UserListOptions{ListOptions: opt, TeamID: team.ID})
|
2021-06-01 00:07:51 +00:00
|
|
|
}
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func (svc *Service) ListTeams(ctx context.Context, opt fleet.ListOptions) ([]*fleet.Team, error) {
|
|
|
|
if err := svc.authz.Authorize(ctx, &fleet.Team{}, fleet.ActionRead); err != nil {
|
2021-06-03 23:24:15 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-06-01 00:07:51 +00:00
|
|
|
vc, ok := viewer.FromContext(ctx)
|
|
|
|
if !ok {
|
2021-06-06 22:07:29 +00:00
|
|
|
return nil, fleet.ErrNoContext
|
2021-06-01 00:07:51 +00:00
|
|
|
}
|
2021-06-06 22:07:29 +00:00
|
|
|
filter := fleet.TeamFilter{User: vc.User, IncludeObserver: true}
|
2021-06-01 00:07:51 +00:00
|
|
|
|
2021-09-14 12:11:07 +00:00
|
|
|
return svc.ds.ListTeams(ctx, filter, opt)
|
2021-06-01 00:07:51 +00:00
|
|
|
}
|
|
|
|
|
2022-01-13 19:57:44 +00:00
|
|
|
func (svc *Service) ListAvailableTeamsForUser(ctx context.Context, user *fleet.User) ([]*fleet.TeamSummary, error) {
|
|
|
|
if err := svc.authz.Authorize(ctx, &fleet.Team{}, fleet.ActionRead); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
availableTeams := []*fleet.TeamSummary{}
|
|
|
|
if user.GlobalRole != nil {
|
|
|
|
ts, err := svc.ds.TeamsSummary(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
availableTeams = append(availableTeams, ts...)
|
|
|
|
} else {
|
|
|
|
for _, t := range user.Teams {
|
|
|
|
// Convert from UserTeam to TeamSummary (i.e. omit the role, counts, agent options)
|
|
|
|
availableTeams = append(availableTeams, &fleet.TeamSummary{ID: t.ID, Name: t.Name, Description: t.Description})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return availableTeams, nil
|
|
|
|
}
|
|
|
|
|
2021-06-03 23:24:15 +00:00
|
|
|
func (svc *Service) DeleteTeam(ctx context.Context, teamID uint) error {
|
2021-06-06 22:07:29 +00:00
|
|
|
if err := svc.authz.Authorize(ctx, &fleet.Team{ID: teamID}, fleet.ActionWrite); err != nil {
|
2021-06-03 23:24:15 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-09-14 12:11:07 +00:00
|
|
|
team, err := svc.ds.Team(ctx, teamID)
|
2021-07-23 20:00:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
name := team.Name
|
|
|
|
|
2021-09-14 12:11:07 +00:00
|
|
|
if err := svc.ds.DeleteTeam(ctx, teamID); err != nil {
|
2021-07-13 19:54:22 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-08-02 22:06:27 +00:00
|
|
|
logging.WithExtras(ctx, "id", teamID)
|
|
|
|
|
2021-07-13 19:54:22 +00:00
|
|
|
return svc.ds.NewActivity(
|
2021-09-14 12:11:07 +00:00
|
|
|
ctx,
|
2021-07-13 19:54:22 +00:00
|
|
|
authz.UserFromContext(ctx),
|
|
|
|
fleet.ActivityTypeDeletedTeam,
|
2021-07-23 20:00:26 +00:00
|
|
|
&map[string]interface{}{"team_id": teamID, "team_name": name},
|
2021-07-13 19:54:22 +00:00
|
|
|
)
|
2021-06-01 00:07:51 +00:00
|
|
|
}
|
|
|
|
|
2022-02-04 17:33:22 +00:00
|
|
|
func (svc *Service) GetTeam(ctx context.Context, teamID uint) (*fleet.Team, error) {
|
|
|
|
if err := svc.authz.Authorize(ctx, &fleet.Team{ID: teamID}, fleet.ActionRead); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
logging.WithExtras(ctx, "id", teamID)
|
|
|
|
|
|
|
|
return svc.ds.Team(ctx, teamID)
|
|
|
|
}
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func (svc *Service) TeamEnrollSecrets(ctx context.Context, teamID uint) ([]*fleet.EnrollSecret, error) {
|
|
|
|
if err := svc.authz.Authorize(ctx, &fleet.Team{ID: teamID}, fleet.ActionRead); err != nil {
|
2021-06-03 23:24:15 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-09-14 12:11:07 +00:00
|
|
|
return svc.ds.TeamEnrollSecrets(ctx, teamID)
|
2021-06-01 00:07:51 +00:00
|
|
|
}
|
2021-11-11 16:45:39 +00:00
|
|
|
|
|
|
|
func (svc *Service) ModifyTeamEnrollSecrets(ctx context.Context, teamID uint, secrets []fleet.EnrollSecret) ([]*fleet.EnrollSecret, error) {
|
|
|
|
if err := svc.authz.Authorize(ctx, &fleet.EnrollSecret{TeamID: ptr.Uint(teamID)}, fleet.ActionWrite); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-11-17 16:26:24 +00:00
|
|
|
if secrets == nil {
|
|
|
|
return nil, fleet.NewInvalidArgumentError("secrets", "missing required argument")
|
2021-11-11 16:45:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var newSecrets []*fleet.EnrollSecret
|
|
|
|
for _, secret := range secrets {
|
|
|
|
newSecrets = append(newSecrets, &fleet.EnrollSecret{
|
|
|
|
Secret: secret.Secret,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if err := svc.ds.ApplyEnrollSecrets(ctx, ptr.Uint(teamID), newSecrets); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return newSecrets, nil
|
|
|
|
}
|
2022-02-03 19:24:03 +00:00
|
|
|
|
|
|
|
func (svc Service) ApplyTeamSpecs(ctx context.Context, specs []*fleet.TeamSpec) error {
|
|
|
|
if err := svc.authz.Authorize(ctx, &fleet.Team{}, fleet.ActionRead); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// check auth for all teams specified first
|
|
|
|
for _, spec := range specs {
|
|
|
|
team, err := svc.ds.TeamByName(ctx, spec.Name)
|
|
|
|
if err != nil {
|
|
|
|
if err := ctxerr.Cause(err); err == sql.ErrNoRows {
|
|
|
|
// can the user create a new team?
|
|
|
|
if err := svc.authz.Authorize(ctx, &fleet.Team{}, fleet.ActionWrite); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// can the user modify each team it's trying to modify
|
|
|
|
if err := svc.authz.Authorize(ctx, team, fleet.ActionWrite); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
config, err := svc.AppConfig(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, spec := range specs {
|
|
|
|
var secrets []*fleet.EnrollSecret
|
|
|
|
for _, secret := range spec.Secrets {
|
|
|
|
secrets = append(secrets, &fleet.EnrollSecret{
|
|
|
|
Secret: secret.Secret,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
team, err := svc.ds.TeamByName(ctx, spec.Name)
|
|
|
|
if err != nil {
|
|
|
|
if err := ctxerr.Cause(err); err == sql.ErrNoRows {
|
|
|
|
agentOptions := spec.AgentOptions
|
|
|
|
if agentOptions == nil {
|
|
|
|
agentOptions = config.AgentOptions
|
|
|
|
}
|
|
|
|
_, err = svc.ds.NewTeam(ctx, &fleet.Team{
|
|
|
|
Name: spec.Name,
|
|
|
|
AgentOptions: agentOptions,
|
|
|
|
Secrets: secrets,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
team.Name = spec.Name
|
|
|
|
team.AgentOptions = spec.AgentOptions
|
|
|
|
team.Secrets = secrets
|
|
|
|
|
|
|
|
_, err = svc.ds.SaveTeam(ctx, team)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = svc.ds.ApplyEnrollSecrets(ctx, ptr.Uint(team.ID), secrets)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|