2016-12-13 22:22:05 +00:00
|
|
|
package test
|
2016-12-06 18:22:28 +00:00
|
|
|
|
|
|
|
import (
|
2021-09-14 12:11:07 +00:00
|
|
|
"context"
|
2016-12-06 18:22:28 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2021-08-20 15:27:41 +00:00
|
|
|
"github.com/fleetdm/fleet/v4/server"
|
|
|
|
|
2021-06-26 04:46:51 +00:00
|
|
|
"github.com/fleetdm/fleet/v4/server/fleet"
|
2021-11-17 22:03:30 +00:00
|
|
|
"github.com/fleetdm/fleet/v4/server/ptr"
|
2016-12-06 18:22:28 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func NewQuery(t *testing.T, ds fleet.Datastore, name, q string, authorID uint, saved bool) *fleet.Query {
|
2021-05-07 04:05:09 +00:00
|
|
|
authorPtr := &authorID
|
|
|
|
if authorID == 0 {
|
|
|
|
authorPtr = nil
|
|
|
|
}
|
2021-09-14 12:11:07 +00:00
|
|
|
query, err := ds.NewQuery(context.Background(), &fleet.Query{
|
2016-12-07 20:22:31 +00:00
|
|
|
Name: name,
|
|
|
|
Query: q,
|
2021-05-07 04:05:09 +00:00
|
|
|
AuthorID: authorPtr,
|
2016-12-09 17:12:45 +00:00
|
|
|
Saved: saved,
|
2016-12-06 18:22:28 +00:00
|
|
|
})
|
2021-09-07 16:48:04 +00:00
|
|
|
require.NoError(t, err)
|
2016-12-06 18:22:28 +00:00
|
|
|
|
|
|
|
// Loading gives us the timestamps
|
2021-09-14 12:11:07 +00:00
|
|
|
query, err = ds.Query(context.Background(), query.ID)
|
2021-09-07 16:48:04 +00:00
|
|
|
require.NoError(t, err)
|
2016-12-06 18:22:28 +00:00
|
|
|
|
|
|
|
return query
|
|
|
|
}
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func NewPack(t *testing.T, ds fleet.Datastore, name string) *fleet.Pack {
|
2021-09-14 12:11:07 +00:00
|
|
|
err := ds.ApplyPackSpecs(context.Background(), []*fleet.PackSpec{{Name: name}})
|
2016-12-06 18:22:28 +00:00
|
|
|
require.Nil(t, err)
|
|
|
|
|
|
|
|
// Loading gives us the timestamps
|
2021-09-14 12:11:07 +00:00
|
|
|
pack, ok, err := ds.PackByName(context.Background(), name)
|
2018-01-10 19:38:20 +00:00
|
|
|
require.True(t, ok)
|
2021-09-07 16:48:04 +00:00
|
|
|
require.NoError(t, err)
|
2016-12-06 18:22:28 +00:00
|
|
|
|
|
|
|
return pack
|
|
|
|
}
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func NewCampaign(t *testing.T, ds fleet.Datastore, queryID uint, status fleet.DistributedQueryStatus, now time.Time) *fleet.DistributedQueryCampaign {
|
2021-09-14 12:11:07 +00:00
|
|
|
campaign, err := ds.NewDistributedQueryCampaign(context.Background(), &fleet.DistributedQueryCampaign{
|
2021-06-06 22:07:29 +00:00
|
|
|
UpdateCreateTimestamps: fleet.UpdateCreateTimestamps{
|
|
|
|
CreateTimestamp: fleet.CreateTimestamp{
|
2016-12-06 18:22:28 +00:00
|
|
|
CreatedAt: now,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
QueryID: queryID,
|
|
|
|
Status: status,
|
|
|
|
})
|
2021-09-07 16:48:04 +00:00
|
|
|
require.NoError(t, err)
|
2016-12-06 18:22:28 +00:00
|
|
|
|
|
|
|
// Loading gives us the timestamps
|
2021-09-14 12:11:07 +00:00
|
|
|
campaign, err = ds.DistributedQueryCampaign(context.Background(), campaign.ID)
|
2021-09-07 16:48:04 +00:00
|
|
|
require.NoError(t, err)
|
2016-12-06 18:22:28 +00:00
|
|
|
|
|
|
|
return campaign
|
|
|
|
}
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func AddHostToCampaign(t *testing.T, ds fleet.Datastore, campaignID, hostID uint) {
|
2016-12-06 18:22:28 +00:00
|
|
|
_, err := ds.NewDistributedQueryCampaignTarget(
|
2021-09-14 12:11:07 +00:00
|
|
|
context.Background(),
|
2021-06-06 22:07:29 +00:00
|
|
|
&fleet.DistributedQueryCampaignTarget{
|
|
|
|
Type: fleet.TargetHost,
|
2016-12-06 18:22:28 +00:00
|
|
|
TargetID: hostID,
|
|
|
|
DistributedQueryCampaignID: campaignID,
|
|
|
|
})
|
2021-09-07 16:48:04 +00:00
|
|
|
require.NoError(t, err)
|
2016-12-06 18:22:28 +00:00
|
|
|
}
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func AddLabelToCampaign(t *testing.T, ds fleet.Datastore, campaignID, labelID uint) {
|
2016-12-06 18:22:28 +00:00
|
|
|
_, err := ds.NewDistributedQueryCampaignTarget(
|
2021-09-14 12:11:07 +00:00
|
|
|
context.Background(),
|
2021-06-06 22:07:29 +00:00
|
|
|
&fleet.DistributedQueryCampaignTarget{
|
|
|
|
Type: fleet.TargetLabel,
|
2016-12-06 18:22:28 +00:00
|
|
|
TargetID: labelID,
|
|
|
|
DistributedQueryCampaignID: campaignID,
|
|
|
|
})
|
2021-09-07 16:48:04 +00:00
|
|
|
require.NoError(t, err)
|
2016-12-06 18:22:28 +00:00
|
|
|
}
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func AddAllHostsLabel(t *testing.T, ds fleet.Datastore) {
|
2020-04-07 01:10:20 +00:00
|
|
|
_, err := ds.NewLabel(
|
2021-09-14 12:11:07 +00:00
|
|
|
context.Background(),
|
2021-06-06 22:07:29 +00:00
|
|
|
&fleet.Label{
|
2020-04-07 22:12:32 +00:00
|
|
|
Name: "All Hosts",
|
|
|
|
Query: "select 1",
|
2021-06-06 22:07:29 +00:00
|
|
|
LabelType: fleet.LabelTypeBuiltIn,
|
|
|
|
LabelMembershipType: fleet.LabelMembershipTypeManual,
|
2020-04-07 01:10:20 +00:00
|
|
|
},
|
|
|
|
)
|
2021-09-07 16:48:04 +00:00
|
|
|
require.NoError(t, err)
|
2020-04-07 01:10:20 +00:00
|
|
|
}
|
|
|
|
|
2022-10-08 12:57:46 +00:00
|
|
|
// NewHostOption is an Option for the NewHost function.
|
|
|
|
type NewHostOption func(*fleet.Host)
|
|
|
|
|
|
|
|
// WithComputerName sets the ComputerName in NewHost.
|
|
|
|
func WithComputerName(s string) NewHostOption {
|
|
|
|
return func(h *fleet.Host) {
|
|
|
|
h.ComputerName = s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-01 17:22:07 +00:00
|
|
|
func WithPlatform(s string) NewHostOption {
|
|
|
|
return func(h *fleet.Host) {
|
|
|
|
h.Platform = s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-08 12:57:46 +00:00
|
|
|
func NewHost(tb testing.TB, ds fleet.Datastore, name, ip, key, uuid string, now time.Time, options ...NewHostOption) *fleet.Host {
|
2021-07-19 18:08:41 +00:00
|
|
|
osqueryHostID, _ := server.GenerateRandomText(10)
|
2022-10-08 12:57:46 +00:00
|
|
|
h := &fleet.Host{
|
2021-06-24 00:32:19 +00:00
|
|
|
Hostname: name,
|
2022-12-26 21:32:39 +00:00
|
|
|
NodeKey: &key,
|
2021-06-24 00:32:19 +00:00
|
|
|
UUID: uuid,
|
|
|
|
DetailUpdatedAt: now,
|
|
|
|
LabelUpdatedAt: now,
|
2021-09-27 19:27:38 +00:00
|
|
|
PolicyUpdatedAt: now,
|
2021-06-24 00:32:19 +00:00
|
|
|
SeenTime: now,
|
2022-12-26 21:32:39 +00:00
|
|
|
OsqueryHostID: &osqueryHostID,
|
2021-11-17 22:03:30 +00:00
|
|
|
Platform: "darwin",
|
2022-10-08 12:57:46 +00:00
|
|
|
PublicIP: ip,
|
|
|
|
PrimaryIP: ip,
|
|
|
|
}
|
|
|
|
for _, o := range options {
|
|
|
|
o(h)
|
|
|
|
}
|
|
|
|
h, err := ds.NewHost(context.Background(), h)
|
2022-01-26 14:47:56 +00:00
|
|
|
require.NoError(tb, err)
|
|
|
|
require.NotZero(tb, h.ID)
|
|
|
|
require.NoError(tb, ds.MarkHostsSeen(context.Background(), []uint{h.ID}, now))
|
2016-12-06 18:22:28 +00:00
|
|
|
|
|
|
|
return h
|
|
|
|
}
|
|
|
|
|
2021-06-24 20:42:29 +00:00
|
|
|
func NewUser(t *testing.T, ds fleet.Datastore, name, email string, admin bool) *fleet.User {
|
2021-07-13 19:33:04 +00:00
|
|
|
role := fleet.RoleObserver
|
|
|
|
if admin {
|
|
|
|
role = fleet.RoleAdmin
|
|
|
|
}
|
2021-09-14 12:11:07 +00:00
|
|
|
u, err := ds.NewUser(context.Background(), &fleet.User{
|
2021-07-13 19:33:04 +00:00
|
|
|
Password: []byte("garbage"),
|
|
|
|
Salt: "garbage",
|
|
|
|
Name: name,
|
|
|
|
Email: email,
|
|
|
|
GlobalRole: &role,
|
2016-12-07 20:22:31 +00:00
|
|
|
})
|
|
|
|
|
2021-09-07 16:48:04 +00:00
|
|
|
require.NoError(t, err)
|
2016-12-07 20:22:31 +00:00
|
|
|
require.NotZero(t, u.ID)
|
|
|
|
|
|
|
|
return u
|
|
|
|
}
|
2018-06-15 14:13:11 +00:00
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func NewScheduledQuery(t *testing.T, ds fleet.Datastore, pid, qid, interval uint, snapshot, removed bool, name string) *fleet.ScheduledQuery {
|
2021-09-14 12:11:07 +00:00
|
|
|
sq, err := ds.NewScheduledQuery(context.Background(), &fleet.ScheduledQuery{
|
2021-05-07 04:05:09 +00:00
|
|
|
Name: name,
|
2018-06-15 14:13:11 +00:00
|
|
|
PackID: pid,
|
|
|
|
QueryID: qid,
|
|
|
|
Interval: interval,
|
|
|
|
Snapshot: &snapshot,
|
|
|
|
Removed: &removed,
|
2021-11-17 22:03:30 +00:00
|
|
|
Platform: ptr.String("darwin"),
|
2018-06-15 14:13:11 +00:00
|
|
|
})
|
2021-09-07 16:48:04 +00:00
|
|
|
require.NoError(t, err)
|
2018-06-15 14:13:11 +00:00
|
|
|
require.NotZero(t, sq.ID)
|
|
|
|
|
|
|
|
return sq
|
|
|
|
}
|