fleet/server/service/scheduled_queries_test.go
Lucas Manuel Rodriguez da171d3b8d
Merge pull request from GHSA-pr2g-j78h-84cr
* Fix access control issues with users

* Fix access control issues with packs

* Fix access control issues with software

* Changes suggested by Martin

* All users can access the global schedule

* Restrict access to activities

* Add explicit test for team admin escalation vuln

* All global users should be able to read all software

* Handbook editor pass - Security - GitHub Security (#5108)

* Update security.md

All edits are recorded by line:

395 replaced “open-source” with “open source”
411 replaced “open-source” with “open source”
439 added “the” before “comment”; replaced “repositories,” with “repositories”
445 deleted “being” before “located”
458 added “and” after “PR”
489 replaced “on” with “in”
493 replaced “open-source” with “open source”; Replaced “privileges,” with “privileges”

* Update security.md

line 479

* Update security.md

added (static analysis tools used to identify problems in code) to line 479

* Fix UI

* Fix UI

* revert api v1 to latest in documentation (#5149)

* revert api v1 to latest in documentation

* Update fleetctl doc page

Co-authored-by: Noah Talerman <noahtal@umich.edu>

* Add team admin team policy automation; fix e2e

* Update to company page of the handbook (#5164)

Updated "Why do we use a wireframe-first approach?" section of company.md

* removed extra data on smaller screens (#5154)

* Update for team automations; e2e

* Jira Integration: Cypress e2e tests only (#5055)

* Update company.md (#5170)

This is to update the formatting under "empathy" and to fix the spelling of "help text."
This was done as per @mikermcneil .
This is related to #https://github.com/fleetdm/fleet/pull/4941 and https://github.com/fleetdm/fleet/issues/4902

* fix update updated_at for aggregated_stats (#5112)

Update the updated_at column when using ON DUPLICATE UPDATE so that
the counts_updated_at is up to date

* basic sql formatting in code ie whitespace around operators

* Fix e2e test

* Fix tests in server/authz

Co-authored-by: gillespi314 <73313222+gillespi314@users.noreply.github.com>
Co-authored-by: Desmi-Dizney <99777687+Desmi-Dizney@users.noreply.github.com>
Co-authored-by: Michal Nicpon <39177923+michalnicp@users.noreply.github.com>
Co-authored-by: Noah Talerman <noahtal@umich.edu>
Co-authored-by: Mike Thomas <78363703+mike-j-thomas@users.noreply.github.com>
Co-authored-by: Martavis Parker <47053705+martavis@users.noreply.github.com>
Co-authored-by: RachelElysia <71795832+RachelElysia@users.noreply.github.com>
2022-04-18 10:27:30 -07:00

237 lines
6.7 KiB
Go

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"
"github.com/fleetdm/fleet/v4/server/test"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestScheduledQueriesAuth(t *testing.T) {
ds := new(mock.Store)
svc := newTestService(t, ds, nil, nil)
ds.ListScheduledQueriesInPackWithStatsFunc = func(ctx context.Context, id uint, opts fleet.ListOptions) ([]*fleet.ScheduledQuery, error) {
return nil, nil
}
ds.NewScheduledQueryFunc = func(ctx context.Context, sq *fleet.ScheduledQuery, opts ...fleet.OptionalArg) (*fleet.ScheduledQuery, error) {
return sq, nil
}
ds.QueryFunc = func(ctx context.Context, id uint) (*fleet.Query, error) {
return &fleet.Query{}, nil
}
ds.ScheduledQueryFunc = func(ctx context.Context, id uint) (*fleet.ScheduledQuery, error) {
return &fleet.ScheduledQuery{}, nil
}
ds.SaveScheduledQueryFunc = func(ctx context.Context, sq *fleet.ScheduledQuery) (*fleet.ScheduledQuery, error) {
return sq, nil
}
ds.DeleteScheduledQueryFunc = func(ctx context.Context, id uint) error {
return nil
}
testCases := []struct {
name string
user *fleet.User
shouldFailWrite bool
shouldFailRead bool
}{
{
name: "global admin",
user: &fleet.User{GlobalRole: ptr.String(fleet.RoleAdmin)},
shouldFailWrite: false,
shouldFailRead: false,
},
{
name: "global maintainer",
user: &fleet.User{GlobalRole: ptr.String(fleet.RoleMaintainer)},
shouldFailWrite: false,
shouldFailRead: false,
},
{
name: "global observer",
user: &fleet.User{GlobalRole: ptr.String(fleet.RoleObserver)},
shouldFailWrite: true,
shouldFailRead: true,
},
// Team users cannot read or write scheduled queries using the below service APIs.
// Team users must use the "Team" endpoints (GetTeamScheduledQueries, TeamScheduleQuery,
// ModifyTeamScheduledQueries and DeleteTeamScheduledQueries).
{
name: "team admin",
user: &fleet.User{Teams: []fleet.UserTeam{{Team: fleet.Team{ID: 1}, Role: fleet.RoleAdmin}}},
shouldFailWrite: true,
shouldFailRead: true,
},
{
name: "team maintainer",
user: &fleet.User{Teams: []fleet.UserTeam{{Team: fleet.Team{ID: 1}, Role: fleet.RoleMaintainer}}},
shouldFailWrite: true,
shouldFailRead: true,
},
{
name: "team observer",
user: &fleet.User{Teams: []fleet.UserTeam{{Team: fleet.Team{ID: 1}, Role: fleet.RoleObserver}}},
shouldFailWrite: true,
shouldFailRead: true,
},
}
for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) {
ctx := viewer.NewContext(context.Background(), viewer.Viewer{User: tt.user})
_, err := svc.GetScheduledQueriesInPack(ctx, 1, fleet.ListOptions{})
checkAuthErr(t, tt.shouldFailRead, err)
_, err = svc.ScheduleQuery(ctx, &fleet.ScheduledQuery{})
checkAuthErr(t, tt.shouldFailWrite, err)
_, err = svc.GetScheduledQuery(ctx, 1)
checkAuthErr(t, tt.shouldFailRead, err)
_, err = svc.ModifyScheduledQuery(ctx, 1, fleet.ScheduledQueryPayload{})
checkAuthErr(t, tt.shouldFailWrite, err)
err = svc.DeleteScheduledQuery(ctx, 1)
checkAuthErr(t, tt.shouldFailWrite, err)
})
}
}
func TestScheduleQuery(t *testing.T) {
ds := new(mock.Store)
svc := newTestService(t, ds, nil, nil)
expectedQuery := &fleet.ScheduledQuery{
Name: "foobar",
QueryName: "foobar",
QueryID: 3,
}
ds.NewScheduledQueryFunc = func(ctx context.Context, q *fleet.ScheduledQuery, opts ...fleet.OptionalArg) (*fleet.ScheduledQuery, error) {
assert.Equal(t, expectedQuery, q)
return expectedQuery, nil
}
_, err := svc.ScheduleQuery(test.UserContext(test.UserAdmin), expectedQuery)
assert.NoError(t, err)
assert.True(t, ds.NewScheduledQueryFuncInvoked)
}
func TestScheduleQueryNoName(t *testing.T) {
ds := new(mock.Store)
svc := newTestService(t, ds, nil, nil)
expectedQuery := &fleet.ScheduledQuery{
Name: "foobar",
QueryName: "foobar",
QueryID: 3,
}
ds.QueryFunc = func(ctx context.Context, qid uint) (*fleet.Query, error) {
require.Equal(t, expectedQuery.QueryID, qid)
return &fleet.Query{Name: expectedQuery.QueryName}, nil
}
ds.ListScheduledQueriesInPackWithStatsFunc = func(ctx context.Context, id uint, opts fleet.ListOptions) ([]*fleet.ScheduledQuery, error) {
// No matching query
return []*fleet.ScheduledQuery{
{
Name: "froobling",
},
}, nil
}
ds.NewScheduledQueryFunc = func(ctx context.Context, q *fleet.ScheduledQuery, opts ...fleet.OptionalArg) (*fleet.ScheduledQuery, error) {
assert.Equal(t, expectedQuery, q)
return expectedQuery, nil
}
_, err := svc.ScheduleQuery(
test.UserContext(test.UserAdmin),
&fleet.ScheduledQuery{QueryID: expectedQuery.QueryID},
)
assert.NoError(t, err)
assert.True(t, ds.NewScheduledQueryFuncInvoked)
}
func TestScheduleQueryNoNameMultiple(t *testing.T) {
ds := new(mock.Store)
svc := newTestService(t, ds, nil, nil)
expectedQuery := &fleet.ScheduledQuery{
Name: "foobar-1",
QueryName: "foobar",
QueryID: 3,
}
ds.QueryFunc = func(ctx context.Context, qid uint) (*fleet.Query, error) {
require.Equal(t, expectedQuery.QueryID, qid)
return &fleet.Query{Name: expectedQuery.QueryName}, nil
}
ds.ListScheduledQueriesInPackWithStatsFunc = func(ctx context.Context, id uint, opts fleet.ListOptions) ([]*fleet.ScheduledQuery, error) {
// No matching query
return []*fleet.ScheduledQuery{
{
Name: "foobar",
},
}, nil
}
ds.NewScheduledQueryFunc = func(ctx context.Context, q *fleet.ScheduledQuery, opts ...fleet.OptionalArg) (*fleet.ScheduledQuery, error) {
assert.Equal(t, expectedQuery, q)
return expectedQuery, nil
}
_, err := svc.ScheduleQuery(
test.UserContext(test.UserAdmin),
&fleet.ScheduledQuery{QueryID: expectedQuery.QueryID},
)
assert.NoError(t, err)
assert.True(t, ds.NewScheduledQueryFuncInvoked)
}
func TestFindNextNameForQuery(t *testing.T) {
testCases := []struct {
name string
scheduled []*fleet.ScheduledQuery
expected string
}{
{
name: "foobar",
scheduled: []*fleet.ScheduledQuery{},
expected: "foobar",
},
{
name: "foobar",
scheduled: []*fleet.ScheduledQuery{
{
Name: "foobar",
},
},
expected: "foobar-1",
},
{
name: "foobar",
scheduled: []*fleet.ScheduledQuery{
{
Name: "foobar",
},
{
Name: "foobar-1",
},
},
expected: "foobar-1-1",
},
}
for _, tt := range testCases {
t.Run("", func(t *testing.T) {
assert.Equal(t, tt.expected, findNextNameForQuery(tt.name, tt.scheduled))
})
}
}