2021-10-13 15:34:59 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/fleetdm/fleet/v4/server/contexts/viewer"
|
|
|
|
"github.com/fleetdm/fleet/v4/server/fleet"
|
|
|
|
"github.com/fleetdm/fleet/v4/server/mock"
|
|
|
|
"github.com/fleetdm/fleet/v4/server/ptr"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestTeamAuth(t *testing.T) {
|
|
|
|
ds := new(mock.Store)
|
|
|
|
license := &fleet.LicenseInfo{Tier: fleet.TierPremium, Expiration: time.Now().Add(24 * time.Hour)}
|
|
|
|
svc := newTestService(ds, nil, nil, TestServerOpts{License: license, SkipCreateTestUsers: true})
|
|
|
|
|
|
|
|
ds.AppConfigFunc = func(ctx context.Context) (*fleet.AppConfig, error) {
|
|
|
|
return &fleet.AppConfig{}, nil
|
|
|
|
}
|
|
|
|
ds.NewTeamFunc = func(ctx context.Context, team *fleet.Team) (*fleet.Team, error) {
|
|
|
|
return &fleet.Team{}, nil
|
|
|
|
}
|
|
|
|
ds.NewActivityFunc = func(ctx context.Context, user *fleet.User, activityType string, details *map[string]interface{}) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
ds.TeamFunc = func(ctx context.Context, tid uint) (*fleet.Team, error) {
|
|
|
|
return &fleet.Team{}, nil
|
|
|
|
}
|
|
|
|
ds.SaveTeamFunc = func(ctx context.Context, team *fleet.Team) (*fleet.Team, error) {
|
|
|
|
return &fleet.Team{}, nil
|
|
|
|
}
|
|
|
|
ds.ListUsersFunc = func(ctx context.Context, opt fleet.UserListOptions) ([]*fleet.User, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
ds.ListTeamsFunc = func(ctx context.Context, filter fleet.TeamFilter, opt fleet.ListOptions) ([]*fleet.Team, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
ds.DeleteTeamFunc = func(ctx context.Context, tid uint) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
ds.TeamEnrollSecretsFunc = func(ctx context.Context, teamID uint) ([]*fleet.EnrollSecret, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2021-11-11 16:45:39 +00:00
|
|
|
ds.ApplyEnrollSecretsFunc = func(ctx context.Context, teamID *uint, secrets []*fleet.EnrollSecret) error {
|
|
|
|
return nil
|
|
|
|
}
|
2022-02-03 19:24:03 +00:00
|
|
|
ds.TeamByNameFunc = func(ctx context.Context, name string) (*fleet.Team, error) {
|
|
|
|
switch name {
|
|
|
|
case "team1":
|
|
|
|
return &fleet.Team{ID: 1}, nil
|
|
|
|
default:
|
|
|
|
return &fleet.Team{ID: 2}, nil
|
|
|
|
}
|
|
|
|
}
|
2021-11-11 16:45:39 +00:00
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
user *fleet.User
|
|
|
|
shouldFailTeamWrite bool
|
|
|
|
shouldFailGlobalWrite bool
|
|
|
|
shouldFailRead bool
|
|
|
|
shouldFailTeamSecretsWrite bool
|
2021-10-13 15:34:59 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
"global admin",
|
|
|
|
&fleet.User{GlobalRole: ptr.String(fleet.RoleAdmin)},
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
false,
|
2021-11-11 16:45:39 +00:00
|
|
|
false,
|
2021-10-13 15:34:59 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"global maintainer",
|
|
|
|
&fleet.User{GlobalRole: ptr.String(fleet.RoleMaintainer)},
|
|
|
|
true,
|
|
|
|
true,
|
2021-11-11 16:45:39 +00:00
|
|
|
false,
|
|
|
|
false,
|
2021-10-13 15:34:59 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"global observer",
|
|
|
|
&fleet.User{GlobalRole: ptr.String(fleet.RoleObserver)},
|
|
|
|
true,
|
|
|
|
true,
|
|
|
|
true,
|
2021-11-11 16:45:39 +00:00
|
|
|
true,
|
2021-10-13 15:34:59 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"team admin, belongs to team",
|
|
|
|
&fleet.User{Teams: []fleet.UserTeam{{Team: fleet.Team{ID: 1}, Role: fleet.RoleAdmin}}},
|
|
|
|
false,
|
|
|
|
true,
|
|
|
|
false,
|
2021-11-11 16:45:39 +00:00
|
|
|
false,
|
2021-10-13 15:34:59 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"team maintainer, belongs to team",
|
|
|
|
&fleet.User{Teams: []fleet.UserTeam{{Team: fleet.Team{ID: 1}, Role: fleet.RoleMaintainer}}},
|
|
|
|
true,
|
|
|
|
true,
|
|
|
|
false,
|
2021-11-11 16:45:39 +00:00
|
|
|
false,
|
2021-10-13 15:34:59 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"team observer, belongs to team",
|
|
|
|
&fleet.User{Teams: []fleet.UserTeam{{Team: fleet.Team{ID: 1}, Role: fleet.RoleObserver}}},
|
|
|
|
true,
|
|
|
|
true,
|
|
|
|
true,
|
2021-11-11 16:45:39 +00:00
|
|
|
true,
|
2021-10-13 15:34:59 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"team admin, DOES NOT belong to team",
|
|
|
|
&fleet.User{Teams: []fleet.UserTeam{{Team: fleet.Team{ID: 2}, Role: fleet.RoleAdmin}}},
|
|
|
|
true,
|
|
|
|
true,
|
|
|
|
true,
|
2021-11-11 16:45:39 +00:00
|
|
|
true,
|
2021-10-13 15:34:59 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"team maintainer, DOES NOT belong to team",
|
|
|
|
&fleet.User{Teams: []fleet.UserTeam{{Team: fleet.Team{ID: 2}, Role: fleet.RoleMaintainer}}},
|
|
|
|
true,
|
|
|
|
true,
|
|
|
|
true,
|
2021-11-11 16:45:39 +00:00
|
|
|
true,
|
2021-10-13 15:34:59 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"team observer, DOES NOT belong to team",
|
|
|
|
&fleet.User{Teams: []fleet.UserTeam{{Team: fleet.Team{ID: 2}, Role: fleet.RoleObserver}}},
|
|
|
|
true,
|
|
|
|
true,
|
|
|
|
true,
|
2021-11-11 16:45:39 +00:00
|
|
|
true,
|
2021-10-13 15:34:59 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range testCases {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
ctx := viewer.NewContext(context.Background(), viewer.Viewer{User: tt.user})
|
|
|
|
|
|
|
|
_, err := svc.NewTeam(ctx, fleet.TeamPayload{Name: ptr.String("name")})
|
|
|
|
checkAuthErr(t, tt.shouldFailGlobalWrite, err)
|
|
|
|
|
|
|
|
_, err = svc.ModifyTeam(ctx, 1, fleet.TeamPayload{Name: ptr.String("othername")})
|
|
|
|
checkAuthErr(t, tt.shouldFailTeamWrite, err)
|
|
|
|
|
|
|
|
_, err = svc.ModifyTeamAgentOptions(ctx, 1, nil)
|
|
|
|
checkAuthErr(t, tt.shouldFailTeamWrite, err)
|
|
|
|
|
|
|
|
_, err = svc.AddTeamUsers(ctx, 1, []fleet.TeamUser{})
|
|
|
|
checkAuthErr(t, tt.shouldFailTeamWrite, err)
|
|
|
|
|
|
|
|
_, err = svc.DeleteTeamUsers(ctx, 1, []fleet.TeamUser{})
|
|
|
|
checkAuthErr(t, tt.shouldFailTeamWrite, err)
|
|
|
|
|
|
|
|
_, err = svc.ListTeamUsers(ctx, 1, fleet.ListOptions{})
|
|
|
|
checkAuthErr(t, tt.shouldFailRead, err)
|
|
|
|
|
|
|
|
_, err = svc.ListTeams(ctx, fleet.ListOptions{})
|
|
|
|
checkAuthErr(t, false, err) // everybody can do this
|
|
|
|
|
2022-02-04 17:33:22 +00:00
|
|
|
_, err = svc.GetTeam(ctx, 1)
|
|
|
|
checkAuthErr(t, tt.shouldFailRead, err)
|
|
|
|
|
2021-10-13 15:34:59 +00:00
|
|
|
err = svc.DeleteTeam(ctx, 1)
|
|
|
|
checkAuthErr(t, tt.shouldFailTeamWrite, err)
|
|
|
|
|
|
|
|
_, err = svc.TeamEnrollSecrets(ctx, 1)
|
|
|
|
checkAuthErr(t, tt.shouldFailRead, err)
|
2021-11-11 16:45:39 +00:00
|
|
|
|
|
|
|
_, err = svc.ModifyTeamEnrollSecrets(ctx, 1, []fleet.EnrollSecret{{Secret: "newteamsecret", CreatedAt: time.Now()}})
|
|
|
|
checkAuthErr(t, tt.shouldFailTeamSecretsWrite, err)
|
2022-02-03 19:24:03 +00:00
|
|
|
|
|
|
|
err = svc.ApplyTeamSpecs(ctx, []*fleet.TeamSpec{{Name: "team1"}})
|
|
|
|
checkAuthErr(t, tt.shouldFailTeamWrite, err)
|
2021-10-13 15:34:59 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|