2016-10-04 20:34:36 +00:00
|
|
|
package datastore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2016-11-22 21:56:05 +00:00
|
|
|
"github.com/WatchBeam/clock"
|
2021-06-06 22:07:29 +00:00
|
|
|
"github.com/fleetdm/fleet/server/fleet"
|
2021-05-25 22:46:46 +00:00
|
|
|
"github.com/fleetdm/fleet/server/ptr"
|
2020-11-11 17:59:12 +00:00
|
|
|
"github.com/fleetdm/fleet/server/test"
|
2016-10-04 20:34:36 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func testDeletePack(t *testing.T, ds fleet.Datastore) {
|
2018-01-10 19:38:20 +00:00
|
|
|
pack := test.NewPack(t, ds, "foo")
|
2016-10-17 19:30:47 +00:00
|
|
|
assert.NotEqual(t, uint(0), pack.ID)
|
2016-10-04 20:34:36 +00:00
|
|
|
|
2018-01-10 19:38:20 +00:00
|
|
|
pack, err := ds.Pack(pack.ID)
|
2016-10-04 20:34:36 +00:00
|
|
|
require.Nil(t, err)
|
|
|
|
|
2018-05-04 18:05:55 +00:00
|
|
|
err = ds.DeletePack(pack.Name)
|
2016-10-04 20:34:36 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
2016-10-17 19:30:47 +00:00
|
|
|
assert.NotEqual(t, uint(0), pack.ID)
|
2016-10-04 20:34:36 +00:00
|
|
|
pack, err = ds.Pack(pack.ID)
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
}
|
|
|
|
|
2021-06-18 16:43:16 +00:00
|
|
|
func testSavePack(t *testing.T, ds fleet.Datastore) {
|
|
|
|
expectedPack := &fleet.Pack{
|
|
|
|
Name: "foo",
|
|
|
|
HostIDs: []uint{1},
|
|
|
|
LabelIDs: []uint{1},
|
|
|
|
TeamIDs: []uint{1},
|
2020-11-05 05:18:02 +00:00
|
|
|
}
|
|
|
|
|
2021-06-18 16:43:16 +00:00
|
|
|
pack, err := ds.NewPack(expectedPack)
|
2020-11-05 05:18:02 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.NotEqual(t, uint(0), pack.ID)
|
2021-06-18 16:43:16 +00:00
|
|
|
test.EqualSkipTimestampsID(t, expectedPack, pack)
|
2020-11-05 05:18:02 +00:00
|
|
|
|
|
|
|
pack, err = ds.Pack(pack.ID)
|
|
|
|
require.NoError(t, err)
|
2021-06-18 16:43:16 +00:00
|
|
|
test.EqualSkipTimestampsID(t, expectedPack, pack)
|
|
|
|
|
|
|
|
expectedPack = &fleet.Pack{
|
|
|
|
ID: pack.ID,
|
|
|
|
Name: "bar",
|
|
|
|
HostIDs: []uint{3},
|
|
|
|
LabelIDs: []uint{4, 6},
|
|
|
|
TeamIDs: []uint{},
|
|
|
|
}
|
|
|
|
|
|
|
|
err = ds.SavePack(expectedPack)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
pack, err = ds.Pack(pack.ID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, "bar", pack.Name)
|
|
|
|
test.EqualSkipTimestampsID(t, expectedPack, pack)
|
2020-11-05 05:18:02 +00:00
|
|
|
}
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func testGetPackByName(t *testing.T, ds fleet.Datastore) {
|
2018-01-10 19:38:20 +00:00
|
|
|
pack := test.NewPack(t, ds, "foo")
|
2017-01-13 18:35:25 +00:00
|
|
|
assert.NotEqual(t, uint(0), pack.ID)
|
|
|
|
|
|
|
|
pack, ok, err := ds.PackByName(pack.Name)
|
|
|
|
require.Nil(t, err)
|
|
|
|
assert.True(t, ok)
|
|
|
|
assert.NotNil(t, pack)
|
|
|
|
assert.Equal(t, "foo", pack.Name)
|
|
|
|
|
|
|
|
pack, ok, err = ds.PackByName("bar")
|
|
|
|
require.Nil(t, err)
|
|
|
|
assert.False(t, ok)
|
|
|
|
assert.Nil(t, pack)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func testListPacks(t *testing.T, ds fleet.Datastore) {
|
|
|
|
p1 := &fleet.PackSpec{
|
2018-01-10 19:38:20 +00:00
|
|
|
ID: 1,
|
|
|
|
Name: "foo_pack",
|
|
|
|
}
|
2021-06-06 22:07:29 +00:00
|
|
|
p2 := &fleet.PackSpec{
|
2018-01-10 19:38:20 +00:00
|
|
|
ID: 2,
|
|
|
|
Name: "bar_pack",
|
|
|
|
}
|
2021-06-06 22:07:29 +00:00
|
|
|
err := ds.ApplyPackSpecs([]*fleet.PackSpec{p1})
|
2018-01-10 19:38:20 +00:00
|
|
|
require.Nil(t, err)
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
packs, err := ds.ListPacks(fleet.ListOptions{})
|
2018-01-10 19:38:20 +00:00
|
|
|
require.Nil(t, err)
|
|
|
|
assert.Len(t, packs, 1)
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
err = ds.ApplyPackSpecs([]*fleet.PackSpec{p1, p2})
|
2018-01-10 19:38:20 +00:00
|
|
|
require.Nil(t, err)
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
packs, err = ds.ListPacks(fleet.ListOptions{})
|
2018-01-10 19:38:20 +00:00
|
|
|
require.Nil(t, err)
|
|
|
|
assert.Len(t, packs, 2)
|
|
|
|
}
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func setupPackSpecsTest(t *testing.T, ds fleet.Datastore) []*fleet.PackSpec {
|
2021-06-18 16:43:16 +00:00
|
|
|
zwass := test.NewUser(t, ds, "Zach", "zwass", "zwass@example.com", true)
|
2021-06-06 22:07:29 +00:00
|
|
|
queries := []*fleet.Query{
|
2018-01-03 19:18:05 +00:00
|
|
|
{Name: "foo", Description: "get the foos", Query: "select * from foo"},
|
|
|
|
{Name: "bar", Description: "do some bars", Query: "select baz from bar"},
|
|
|
|
}
|
|
|
|
// Zach creates some queries
|
|
|
|
err := ds.ApplyQueries(zwass.ID, queries)
|
|
|
|
require.Nil(t, err)
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
labels := []*fleet.LabelSpec{
|
2021-06-18 16:43:16 +00:00
|
|
|
{
|
2018-01-10 19:38:20 +00:00
|
|
|
Name: "foo",
|
|
|
|
Query: "select * from foo",
|
|
|
|
},
|
2021-06-18 16:43:16 +00:00
|
|
|
{
|
2018-01-10 19:38:20 +00:00
|
|
|
Name: "bar",
|
|
|
|
Query: "select * from bar",
|
|
|
|
},
|
2021-06-18 16:43:16 +00:00
|
|
|
{
|
2018-01-10 19:38:20 +00:00
|
|
|
Name: "bing",
|
|
|
|
Query: "select * from bing",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err = ds.ApplyLabelSpecs(labels)
|
|
|
|
require.Nil(t, err)
|
2018-01-03 19:18:05 +00:00
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
expectedSpecs := []*fleet.PackSpec{
|
2021-06-18 16:43:16 +00:00
|
|
|
{
|
2018-01-03 19:18:05 +00:00
|
|
|
ID: 1,
|
|
|
|
Name: "test_pack",
|
2021-06-06 22:07:29 +00:00
|
|
|
Targets: fleet.PackSpecTargets{
|
2018-01-03 19:18:05 +00:00
|
|
|
Labels: []string{
|
|
|
|
"foo",
|
|
|
|
"bar",
|
|
|
|
"bing",
|
|
|
|
},
|
|
|
|
},
|
2021-06-06 22:07:29 +00:00
|
|
|
Queries: []fleet.PackSpecQuery{
|
2021-06-18 16:43:16 +00:00
|
|
|
{
|
2018-01-03 19:18:05 +00:00
|
|
|
QueryName: queries[0].Name,
|
2018-06-22 00:06:44 +00:00
|
|
|
Name: "q0",
|
2018-01-03 19:18:05 +00:00
|
|
|
Description: "test_foo",
|
|
|
|
Interval: 42,
|
|
|
|
},
|
2021-06-18 16:43:16 +00:00
|
|
|
{
|
2018-01-03 19:18:05 +00:00
|
|
|
QueryName: queries[0].Name,
|
2020-10-21 23:29:27 +00:00
|
|
|
Name: "foo_snapshot",
|
|
|
|
Interval: 600,
|
2021-05-25 22:46:46 +00:00
|
|
|
Snapshot: ptr.Bool(true),
|
|
|
|
Denylist: ptr.Bool(false),
|
2020-10-21 23:29:27 +00:00
|
|
|
},
|
2021-06-18 16:43:16 +00:00
|
|
|
{
|
2020-10-21 23:29:27 +00:00
|
|
|
Name: "q2",
|
|
|
|
QueryName: queries[1].Name,
|
|
|
|
Interval: 600,
|
2021-05-25 22:46:46 +00:00
|
|
|
Removed: ptr.Bool(false),
|
|
|
|
Shard: ptr.Uint(73),
|
|
|
|
Platform: ptr.String("foobar"),
|
|
|
|
Version: ptr.String("0.0.0.0.0.1"),
|
|
|
|
Denylist: ptr.Bool(true),
|
2020-10-21 23:29:27 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-06-18 16:43:16 +00:00
|
|
|
{
|
2020-10-21 23:29:27 +00:00
|
|
|
ID: 2,
|
|
|
|
Name: "test_pack_disabled",
|
|
|
|
Disabled: true,
|
2021-06-06 22:07:29 +00:00
|
|
|
Targets: fleet.PackSpecTargets{
|
2020-10-21 23:29:27 +00:00
|
|
|
Labels: []string{
|
|
|
|
"foo",
|
|
|
|
"bar",
|
|
|
|
"bing",
|
|
|
|
},
|
|
|
|
},
|
2021-06-06 22:07:29 +00:00
|
|
|
Queries: []fleet.PackSpecQuery{
|
2021-06-18 16:43:16 +00:00
|
|
|
{
|
2020-10-21 23:29:27 +00:00
|
|
|
QueryName: queries[0].Name,
|
|
|
|
Name: "q0",
|
|
|
|
Description: "test_foo",
|
|
|
|
Interval: 42,
|
|
|
|
},
|
2021-06-18 16:43:16 +00:00
|
|
|
{
|
2020-10-21 23:29:27 +00:00
|
|
|
QueryName: queries[0].Name,
|
2018-01-03 19:18:05 +00:00
|
|
|
Name: "foo_snapshot",
|
|
|
|
Interval: 600,
|
2021-05-25 22:46:46 +00:00
|
|
|
Snapshot: ptr.Bool(true),
|
2018-01-03 19:18:05 +00:00
|
|
|
},
|
2021-06-18 16:43:16 +00:00
|
|
|
{
|
2018-06-22 00:06:44 +00:00
|
|
|
Name: "q2",
|
2018-01-03 19:18:05 +00:00
|
|
|
QueryName: queries[1].Name,
|
|
|
|
Interval: 600,
|
2021-05-25 22:46:46 +00:00
|
|
|
Removed: ptr.Bool(false),
|
|
|
|
Shard: ptr.Uint(73),
|
|
|
|
Platform: ptr.String("foobar"),
|
|
|
|
Version: ptr.String("0.0.0.0.0.1"),
|
2018-01-03 19:18:05 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-05-08 01:54:29 +00:00
|
|
|
err = ds.ApplyPackSpecs(expectedSpecs)
|
2018-01-03 19:18:05 +00:00
|
|
|
require.Nil(t, err)
|
2018-05-08 01:54:29 +00:00
|
|
|
return expectedSpecs
|
|
|
|
}
|
2018-01-03 19:18:05 +00:00
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func testApplyPackSpecRoundtrip(t *testing.T, ds fleet.Datastore) {
|
2018-05-08 01:54:29 +00:00
|
|
|
expectedSpecs := setupPackSpecsTest(t, ds)
|
2018-01-03 19:18:05 +00:00
|
|
|
|
2018-05-08 01:54:29 +00:00
|
|
|
gotSpec, err := ds.GetPackSpecs()
|
2018-01-03 19:18:05 +00:00
|
|
|
require.Nil(t, err)
|
2018-05-08 01:54:29 +00:00
|
|
|
assert.Equal(t, expectedSpecs, gotSpec)
|
|
|
|
}
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func testGetPackSpec(t *testing.T, ds fleet.Datastore) {
|
2018-05-08 01:54:29 +00:00
|
|
|
expectedSpecs := setupPackSpecsTest(t, ds)
|
|
|
|
|
|
|
|
for _, s := range expectedSpecs {
|
|
|
|
spec, err := ds.GetPackSpec(s.Name)
|
|
|
|
require.Nil(t, err)
|
|
|
|
assert.Equal(t, s, spec)
|
|
|
|
}
|
2018-01-03 19:18:05 +00:00
|
|
|
}
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func testApplyPackSpecMissingQueries(t *testing.T, ds fleet.Datastore) {
|
2018-01-03 19:18:05 +00:00
|
|
|
// Do not define queries mentioned in spec
|
2021-06-06 22:07:29 +00:00
|
|
|
specs := []*fleet.PackSpec{
|
2021-06-18 16:43:16 +00:00
|
|
|
{
|
2018-01-03 19:18:05 +00:00
|
|
|
ID: 1,
|
|
|
|
Name: "test_pack",
|
2021-06-06 22:07:29 +00:00
|
|
|
Targets: fleet.PackSpecTargets{
|
2018-01-03 19:18:05 +00:00
|
|
|
Labels: []string{},
|
|
|
|
},
|
2021-06-06 22:07:29 +00:00
|
|
|
Queries: []fleet.PackSpecQuery{
|
2021-06-18 16:43:16 +00:00
|
|
|
{
|
2018-01-03 19:18:05 +00:00
|
|
|
QueryName: "bar",
|
|
|
|
Interval: 600,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should error due to unkown query
|
|
|
|
err := ds.ApplyPackSpecs(specs)
|
|
|
|
if assert.NotNil(t, err) {
|
|
|
|
assert.Contains(t, err.Error(), "unknown query 'bar'")
|
|
|
|
}
|
|
|
|
}
|
2018-01-10 19:38:20 +00:00
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func testApplyPackSpecMissingName(t *testing.T, ds fleet.Datastore) {
|
2019-01-17 23:59:42 +00:00
|
|
|
setupPackSpecsTest(t, ds)
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
specs := []*fleet.PackSpec{
|
2021-06-18 16:43:16 +00:00
|
|
|
{
|
2019-01-17 23:59:42 +00:00
|
|
|
Name: "test2",
|
2021-06-06 22:07:29 +00:00
|
|
|
Targets: fleet.PackSpecTargets{
|
2019-01-17 23:59:42 +00:00
|
|
|
Labels: []string{},
|
|
|
|
},
|
2021-06-06 22:07:29 +00:00
|
|
|
Queries: []fleet.PackSpecQuery{
|
2021-06-18 16:43:16 +00:00
|
|
|
{
|
2019-01-17 23:59:42 +00:00
|
|
|
QueryName: "foo",
|
|
|
|
Interval: 600,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err := ds.ApplyPackSpecs(specs)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Query name should have been copied into name field
|
|
|
|
spec, err := ds.GetPackSpec("test2")
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, "foo", spec.Queries[0].Name)
|
|
|
|
}
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func testListPacksForHost(t *testing.T, ds fleet.Datastore) {
|
2018-01-10 19:38:20 +00:00
|
|
|
if ds.Name() == "inmem" {
|
|
|
|
t.Skip("inmem is deprecated")
|
|
|
|
}
|
|
|
|
|
|
|
|
mockClock := clock.NewMockClock()
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
l1 := &fleet.LabelSpec{
|
2018-01-10 19:38:20 +00:00
|
|
|
ID: 1,
|
|
|
|
Name: "foo",
|
|
|
|
}
|
2021-06-06 22:07:29 +00:00
|
|
|
l2 := &fleet.LabelSpec{
|
2018-01-10 19:38:20 +00:00
|
|
|
ID: 2,
|
|
|
|
Name: "bar",
|
|
|
|
}
|
2021-06-06 22:07:29 +00:00
|
|
|
err := ds.ApplyLabelSpecs([]*fleet.LabelSpec{l1, l2})
|
2018-01-10 19:38:20 +00:00
|
|
|
require.Nil(t, err)
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
p1 := &fleet.PackSpec{
|
2018-01-10 19:38:20 +00:00
|
|
|
ID: 1,
|
|
|
|
Name: "foo_pack",
|
2021-06-06 22:07:29 +00:00
|
|
|
Targets: fleet.PackSpecTargets{
|
2018-01-10 19:38:20 +00:00
|
|
|
Labels: []string{
|
|
|
|
l1.Name,
|
|
|
|
l2.Name,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2021-06-06 22:07:29 +00:00
|
|
|
p2 := &fleet.PackSpec{
|
2018-01-10 19:38:20 +00:00
|
|
|
ID: 2,
|
|
|
|
Name: "shmoo_pack",
|
2021-06-06 22:07:29 +00:00
|
|
|
Targets: fleet.PackSpecTargets{
|
2018-01-10 19:38:20 +00:00
|
|
|
Labels: []string{
|
|
|
|
l2.Name,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2021-06-06 22:07:29 +00:00
|
|
|
err = ds.ApplyPackSpecs([]*fleet.PackSpec{p1, p2})
|
2018-01-10 19:38:20 +00:00
|
|
|
require.Nil(t, err)
|
|
|
|
|
|
|
|
h1 := test.NewHost(t, ds, "h1.local", "10.10.10.1", "1", "1", mockClock.Now())
|
|
|
|
|
|
|
|
packs, err := ds.ListPacksForHost(h1.ID)
|
|
|
|
require.Nil(t, err)
|
|
|
|
require.Len(t, packs, 0)
|
|
|
|
|
|
|
|
err = ds.RecordLabelQueryExecutions(
|
|
|
|
h1,
|
|
|
|
map[uint]bool{l1.ID: true},
|
|
|
|
mockClock.Now(),
|
|
|
|
)
|
|
|
|
require.Nil(t, err)
|
|
|
|
|
|
|
|
packs, err = ds.ListPacksForHost(h1.ID)
|
|
|
|
require.Nil(t, err)
|
|
|
|
if assert.Len(t, packs, 1) {
|
|
|
|
assert.Equal(t, "foo_pack", packs[0].Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = ds.RecordLabelQueryExecutions(
|
|
|
|
h1,
|
|
|
|
map[uint]bool{l1.ID: false, l2.ID: true},
|
|
|
|
mockClock.Now(),
|
|
|
|
)
|
|
|
|
require.Nil(t, err)
|
|
|
|
|
|
|
|
packs, err = ds.ListPacksForHost(h1.ID)
|
|
|
|
require.Nil(t, err)
|
|
|
|
assert.Len(t, packs, 2)
|
|
|
|
|
|
|
|
err = ds.RecordLabelQueryExecutions(
|
|
|
|
h1,
|
|
|
|
map[uint]bool{l1.ID: true, l2.ID: true},
|
|
|
|
mockClock.Now(),
|
|
|
|
)
|
|
|
|
require.Nil(t, err)
|
|
|
|
|
|
|
|
packs, err = ds.ListPacksForHost(h1.ID)
|
|
|
|
require.Nil(t, err)
|
|
|
|
assert.Len(t, packs, 2)
|
|
|
|
|
|
|
|
h2 := test.NewHost(t, ds, "h2.local", "10.10.10.2", "2", "2", mockClock.Now())
|
|
|
|
|
|
|
|
err = ds.RecordLabelQueryExecutions(
|
|
|
|
h2,
|
|
|
|
map[uint]bool{l2.ID: true},
|
|
|
|
mockClock.Now(),
|
|
|
|
)
|
|
|
|
require.Nil(t, err)
|
|
|
|
|
|
|
|
packs, err = ds.ListPacksForHost(h1.ID)
|
|
|
|
require.Nil(t, err)
|
|
|
|
assert.Len(t, packs, 2)
|
|
|
|
|
|
|
|
err = ds.RecordLabelQueryExecutions(
|
|
|
|
h1,
|
|
|
|
map[uint]bool{l2.ID: false},
|
|
|
|
mockClock.Now(),
|
|
|
|
)
|
|
|
|
require.Nil(t, err)
|
|
|
|
|
|
|
|
packs, err = ds.ListPacksForHost(h1.ID)
|
|
|
|
require.Nil(t, err)
|
|
|
|
if assert.Len(t, packs, 1) {
|
|
|
|
assert.Equal(t, "foo_pack", packs[0].Name)
|
|
|
|
}
|
|
|
|
}
|