2021-09-29 17:07:10 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"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 TestGlobalScheduleAuth(t *testing.T) {
|
|
|
|
ds := new(mock.Store)
|
2022-11-15 14:08:05 +00:00
|
|
|
svc, ctx := newTestService(t, ds, nil, nil)
|
2021-09-29 17:07:10 +00:00
|
|
|
|
2023-07-25 00:17:20 +00:00
|
|
|
//
|
|
|
|
// All global schedule query methods use queries datastore methods.
|
|
|
|
//
|
|
|
|
|
|
|
|
ds.QueryFunc = func(ctx context.Context, id uint) (*fleet.Query, error) {
|
|
|
|
return &fleet.Query{
|
|
|
|
Name: "foobar",
|
|
|
|
Query: "SELECT 1;",
|
|
|
|
}, nil
|
2021-09-29 17:07:10 +00:00
|
|
|
}
|
2023-12-18 14:45:19 +00:00
|
|
|
ds.SaveQueryFunc = func(ctx context.Context, query *fleet.Query, shouldDiscardResults bool, shouldDeleteStats bool) error {
|
2023-07-25 00:17:20 +00:00
|
|
|
return nil
|
2021-09-29 17:07:10 +00:00
|
|
|
}
|
2023-07-25 00:17:20 +00:00
|
|
|
ds.NewActivityFunc = func(ctx context.Context, user *fleet.User, activity fleet.ActivityDetails) error {
|
|
|
|
return nil
|
2021-12-15 14:06:34 +00:00
|
|
|
}
|
2023-07-25 00:17:20 +00:00
|
|
|
ds.ListQueriesFunc = func(ctx context.Context, opt fleet.ListQueryOptions) ([]*fleet.Query, error) {
|
|
|
|
return nil, nil
|
2021-12-15 14:06:34 +00:00
|
|
|
}
|
2023-07-25 00:17:20 +00:00
|
|
|
ds.NewQueryFunc = func(ctx context.Context, query *fleet.Query, opts ...fleet.OptionalArg) (*fleet.Query, error) {
|
|
|
|
return &fleet.Query{}, nil
|
2021-12-15 14:06:34 +00:00
|
|
|
}
|
2023-07-25 00:17:20 +00:00
|
|
|
ds.DeleteQueryFunc = func(ctx context.Context, teamID *uint, name string) error {
|
2021-12-15 14:06:34 +00:00
|
|
|
return nil
|
|
|
|
}
|
2021-09-29 17:07:10 +00:00
|
|
|
|
2022-01-18 01:52:09 +00:00
|
|
|
testCases := []struct {
|
2021-09-29 17:07:10 +00:00
|
|
|
name string
|
|
|
|
user *fleet.User
|
|
|
|
shouldFailWrite bool
|
|
|
|
shouldFailRead bool
|
|
|
|
}{
|
|
|
|
{
|
2022-04-18 17:27:30 +00:00
|
|
|
name: "global admin",
|
|
|
|
user: &fleet.User{GlobalRole: ptr.String(fleet.RoleAdmin)},
|
|
|
|
shouldFailWrite: false,
|
|
|
|
shouldFailRead: false,
|
2021-09-29 17:07:10 +00:00
|
|
|
},
|
|
|
|
{
|
2022-04-18 17:27:30 +00:00
|
|
|
name: "global maintainer",
|
|
|
|
user: &fleet.User{GlobalRole: ptr.String(fleet.RoleMaintainer)},
|
|
|
|
shouldFailWrite: false,
|
|
|
|
shouldFailRead: false,
|
2021-09-29 17:07:10 +00:00
|
|
|
},
|
|
|
|
{
|
2022-04-18 17:27:30 +00:00
|
|
|
name: "global observer",
|
|
|
|
user: &fleet.User{GlobalRole: ptr.String(fleet.RoleObserver)},
|
|
|
|
shouldFailWrite: true,
|
|
|
|
shouldFailRead: false,
|
2021-09-29 17:07:10 +00:00
|
|
|
},
|
2021-10-13 15:34:59 +00:00
|
|
|
{
|
2022-04-18 17:27:30 +00:00
|
|
|
name: "team admin",
|
|
|
|
user: &fleet.User{Teams: []fleet.UserTeam{{Team: fleet.Team{ID: 1}, Role: fleet.RoleAdmin}}},
|
|
|
|
shouldFailWrite: true,
|
|
|
|
shouldFailRead: false,
|
2021-10-13 15:34:59 +00:00
|
|
|
},
|
2021-09-29 17:07:10 +00:00
|
|
|
{
|
2022-04-18 17:27:30 +00:00
|
|
|
name: "team maintainer",
|
|
|
|
user: &fleet.User{Teams: []fleet.UserTeam{{Team: fleet.Team{ID: 1}, Role: fleet.RoleMaintainer}}},
|
|
|
|
shouldFailWrite: true,
|
|
|
|
shouldFailRead: false,
|
2021-09-29 17:07:10 +00:00
|
|
|
},
|
|
|
|
{
|
2022-04-18 17:27:30 +00:00
|
|
|
name: "team observer",
|
|
|
|
user: &fleet.User{Teams: []fleet.UserTeam{{Team: fleet.Team{ID: 1}, Role: fleet.RoleObserver}}},
|
|
|
|
shouldFailWrite: true,
|
|
|
|
shouldFailRead: false,
|
2021-09-29 17:07:10 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range testCases {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2022-11-15 14:08:05 +00:00
|
|
|
ctx := viewer.NewContext(ctx, viewer.Viewer{User: tt.user})
|
2021-09-29 17:07:10 +00:00
|
|
|
|
|
|
|
_, err := svc.GetGlobalScheduledQueries(ctx, fleet.ListOptions{})
|
|
|
|
checkAuthErr(t, tt.shouldFailRead, err)
|
2021-12-15 14:06:34 +00:00
|
|
|
|
2023-07-25 00:17:20 +00:00
|
|
|
_, err = svc.GlobalScheduleQuery(ctx, &fleet.ScheduledQuery{
|
|
|
|
Name: "query",
|
|
|
|
QueryName: "query",
|
|
|
|
Interval: 10,
|
|
|
|
})
|
2021-12-15 14:06:34 +00:00
|
|
|
checkAuthErr(t, tt.shouldFailWrite, err)
|
|
|
|
|
|
|
|
_, err = svc.ModifyGlobalScheduledQueries(ctx, 1, fleet.ScheduledQueryPayload{})
|
|
|
|
checkAuthErr(t, tt.shouldFailWrite, err)
|
|
|
|
|
|
|
|
err = svc.DeleteGlobalScheduledQueries(ctx, 1)
|
|
|
|
checkAuthErr(t, tt.shouldFailWrite, err)
|
2021-09-29 17:07:10 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|