2016-10-04 20:34:36 +00:00
|
|
|
package datastore
|
|
|
|
|
|
|
|
import (
|
2016-11-16 13:47:49 +00:00
|
|
|
"fmt"
|
2018-01-03 19:18:05 +00:00
|
|
|
"sort"
|
2016-10-04 20:34:36 +00:00
|
|
|
"testing"
|
|
|
|
|
2020-11-11 17:59:12 +00:00
|
|
|
"github.com/fleetdm/fleet/server/kolide"
|
|
|
|
"github.com/fleetdm/fleet/server/test"
|
2016-10-04 20:34:36 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2016-12-06 18:16:04 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2016-10-04 20:34:36 +00:00
|
|
|
)
|
|
|
|
|
2018-01-03 19:18:05 +00:00
|
|
|
func testApplyQueries(t *testing.T, ds kolide.Datastore) {
|
2020-04-07 01:10:20 +00:00
|
|
|
test.AddAllHostsLabel(t, ds)
|
|
|
|
|
2018-01-03 19:18:05 +00:00
|
|
|
zwass := test.NewUser(t, ds, "Zach", "zwass", "zwass@kolide.co", true)
|
|
|
|
groob := test.NewUser(t, ds, "Victor", "groob", "victor@kolide.co", true)
|
|
|
|
expectedQueries := []*kolide.Query{
|
|
|
|
{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, expectedQueries)
|
|
|
|
require.Nil(t, err)
|
|
|
|
|
|
|
|
queries, err := ds.ListQueries(kolide.ListOptions{})
|
|
|
|
require.Nil(t, err)
|
|
|
|
require.Len(t, queries, len(expectedQueries))
|
|
|
|
for i, q := range queries {
|
|
|
|
comp := expectedQueries[i]
|
|
|
|
assert.Equal(t, comp.Name, q.Name)
|
|
|
|
assert.Equal(t, comp.Description, q.Description)
|
|
|
|
assert.Equal(t, comp.Query, q.Query)
|
2018-10-11 19:37:18 +00:00
|
|
|
assert.Equal(t, &zwass.ID, q.AuthorID)
|
2018-01-03 19:18:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Victor modifies a query (but also pushes the same version of the
|
|
|
|
// first query)
|
|
|
|
expectedQueries[1].Query = "not really a valid query ;)"
|
|
|
|
err = ds.ApplyQueries(groob.ID, expectedQueries)
|
|
|
|
require.Nil(t, err)
|
|
|
|
|
|
|
|
queries, err = ds.ListQueries(kolide.ListOptions{})
|
|
|
|
require.Nil(t, err)
|
|
|
|
require.Len(t, queries, len(expectedQueries))
|
|
|
|
for i, q := range queries {
|
|
|
|
comp := expectedQueries[i]
|
|
|
|
assert.Equal(t, comp.Name, q.Name)
|
|
|
|
assert.Equal(t, comp.Description, q.Description)
|
|
|
|
assert.Equal(t, comp.Query, q.Query)
|
2018-10-11 19:37:18 +00:00
|
|
|
assert.Equal(t, &groob.ID, q.AuthorID)
|
2018-01-03 19:18:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Zach adds a third query (but does not re-apply the others)
|
|
|
|
expectedQueries = append(expectedQueries,
|
|
|
|
&kolide.Query{Name: "trouble", Description: "Look out!", Query: "select * from time"},
|
|
|
|
)
|
|
|
|
err = ds.ApplyQueries(zwass.ID, []*kolide.Query{expectedQueries[2]})
|
|
|
|
require.Nil(t, err)
|
|
|
|
|
|
|
|
queries, err = ds.ListQueries(kolide.ListOptions{})
|
|
|
|
require.Nil(t, err)
|
|
|
|
require.Len(t, queries, len(expectedQueries))
|
|
|
|
for i, q := range queries {
|
|
|
|
comp := expectedQueries[i]
|
|
|
|
assert.Equal(t, comp.Name, q.Name)
|
|
|
|
assert.Equal(t, comp.Description, q.Description)
|
|
|
|
assert.Equal(t, comp.Query, q.Query)
|
|
|
|
}
|
2018-10-11 19:37:18 +00:00
|
|
|
assert.Equal(t, &groob.ID, queries[0].AuthorID)
|
|
|
|
assert.Equal(t, &groob.ID, queries[1].AuthorID)
|
|
|
|
assert.Equal(t, &zwass.ID, queries[2].AuthorID)
|
2018-01-03 19:18:05 +00:00
|
|
|
}
|
|
|
|
|
2016-10-04 20:34:36 +00:00
|
|
|
func testDeleteQuery(t *testing.T, ds kolide.Datastore) {
|
2016-12-13 22:22:05 +00:00
|
|
|
user := test.NewUser(t, ds, "Zach", "zwass", "zwass@kolide.co", true)
|
2016-12-07 20:22:31 +00:00
|
|
|
|
2016-10-04 20:34:36 +00:00
|
|
|
query := &kolide.Query{
|
2016-11-16 13:47:49 +00:00
|
|
|
Name: "foo",
|
|
|
|
Query: "bar",
|
2018-10-11 19:37:18 +00:00
|
|
|
AuthorID: &user.ID,
|
2016-10-04 20:34:36 +00:00
|
|
|
}
|
|
|
|
query, err := ds.NewQuery(query)
|
2016-12-07 20:22:31 +00:00
|
|
|
require.Nil(t, err)
|
2016-12-13 22:22:05 +00:00
|
|
|
require.NotNil(t, query)
|
2016-10-04 20:34:36 +00:00
|
|
|
assert.NotEqual(t, query.ID, 0)
|
|
|
|
|
2018-05-04 18:05:55 +00:00
|
|
|
err = ds.DeleteQuery(query.Name)
|
2016-12-07 20:22:31 +00:00
|
|
|
require.Nil(t, err)
|
2016-10-04 20:34:36 +00:00
|
|
|
|
|
|
|
assert.NotEqual(t, query.ID, 0)
|
|
|
|
_, err = ds.Query(query.ID)
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
}
|
|
|
|
|
2017-01-13 18:35:25 +00:00
|
|
|
func testGetQueryByName(t *testing.T, ds kolide.Datastore) {
|
|
|
|
user := test.NewUser(t, ds, "Zach", "zwass", "zwass@kolide.co", true)
|
|
|
|
test.NewQuery(t, ds, "q1", "select * from time", user.ID, true)
|
2018-05-09 23:54:42 +00:00
|
|
|
actual, err := ds.QueryByName("q1")
|
2017-01-13 18:35:25 +00:00
|
|
|
require.Nil(t, err)
|
|
|
|
assert.Equal(t, "q1", actual.Name)
|
2017-04-18 19:41:51 +00:00
|
|
|
assert.Equal(t, "select * from time", actual.Query)
|
2017-01-13 18:35:25 +00:00
|
|
|
|
2018-05-09 23:54:42 +00:00
|
|
|
actual, err = ds.QueryByName("xxx")
|
2018-06-15 14:13:11 +00:00
|
|
|
assert.Error(t, err)
|
2018-05-09 23:54:42 +00:00
|
|
|
assert.True(t, kolide.IsNotFound(err))
|
2017-01-13 18:35:25 +00:00
|
|
|
}
|
|
|
|
|
2016-12-09 17:12:45 +00:00
|
|
|
func testDeleteQueries(t *testing.T, ds kolide.Datastore) {
|
2016-12-13 22:22:05 +00:00
|
|
|
user := test.NewUser(t, ds, "Zach", "zwass", "zwass@kolide.co", true)
|
2016-12-09 17:12:45 +00:00
|
|
|
|
2016-12-13 22:22:05 +00:00
|
|
|
q1 := test.NewQuery(t, ds, "q1", "select * from time", user.ID, true)
|
|
|
|
q2 := test.NewQuery(t, ds, "q2", "select * from processes", user.ID, true)
|
|
|
|
q3 := test.NewQuery(t, ds, "q3", "select 1", user.ID, true)
|
|
|
|
q4 := test.NewQuery(t, ds, "q4", "select * from osquery_info", user.ID, true)
|
2016-12-09 17:12:45 +00:00
|
|
|
|
|
|
|
queries, err := ds.ListQueries(kolide.ListOptions{})
|
|
|
|
require.Nil(t, err)
|
|
|
|
assert.Len(t, queries, 4)
|
|
|
|
|
|
|
|
deleted, err := ds.DeleteQueries([]uint{q1.ID, q3.ID})
|
|
|
|
require.Nil(t, err)
|
|
|
|
assert.Equal(t, uint(2), deleted)
|
|
|
|
|
|
|
|
queries, err = ds.ListQueries(kolide.ListOptions{})
|
|
|
|
require.Nil(t, err)
|
|
|
|
assert.Len(t, queries, 2)
|
|
|
|
|
|
|
|
deleted, err = ds.DeleteQueries([]uint{q2.ID})
|
|
|
|
require.Nil(t, err)
|
|
|
|
assert.Equal(t, uint(1), deleted)
|
|
|
|
|
|
|
|
queries, err = ds.ListQueries(kolide.ListOptions{})
|
|
|
|
require.Nil(t, err)
|
|
|
|
assert.Len(t, queries, 1)
|
|
|
|
|
|
|
|
deleted, err = ds.DeleteQueries([]uint{q2.ID, q4.ID})
|
|
|
|
require.Nil(t, err)
|
|
|
|
assert.Equal(t, uint(1), deleted)
|
|
|
|
|
|
|
|
queries, err = ds.ListQueries(kolide.ListOptions{})
|
|
|
|
require.Nil(t, err)
|
|
|
|
assert.Len(t, queries, 0)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-10-04 20:34:36 +00:00
|
|
|
func testSaveQuery(t *testing.T, ds kolide.Datastore) {
|
2016-12-13 22:22:05 +00:00
|
|
|
user := test.NewUser(t, ds, "Zach", "zwass", "zwass@kolide.co", true)
|
2016-12-07 20:22:31 +00:00
|
|
|
|
2016-10-04 20:34:36 +00:00
|
|
|
query := &kolide.Query{
|
2016-12-07 20:22:31 +00:00
|
|
|
Name: "foo",
|
|
|
|
Query: "bar",
|
2018-10-11 19:37:18 +00:00
|
|
|
AuthorID: &user.ID,
|
2016-10-04 20:34:36 +00:00
|
|
|
}
|
|
|
|
query, err := ds.NewQuery(query)
|
2016-12-07 20:22:31 +00:00
|
|
|
require.Nil(t, err)
|
2016-12-13 22:22:05 +00:00
|
|
|
require.NotNil(t, query)
|
2016-10-04 20:34:36 +00:00
|
|
|
assert.NotEqual(t, 0, query.ID)
|
|
|
|
|
|
|
|
query.Query = "baz"
|
|
|
|
err = ds.SaveQuery(query)
|
|
|
|
|
2016-12-07 20:22:31 +00:00
|
|
|
require.Nil(t, err)
|
2016-10-04 20:34:36 +00:00
|
|
|
|
|
|
|
queryVerify, err := ds.Query(query.ID)
|
2016-12-07 20:22:31 +00:00
|
|
|
require.Nil(t, err)
|
2016-12-13 22:22:05 +00:00
|
|
|
require.NotNil(t, queryVerify)
|
2016-10-04 20:34:36 +00:00
|
|
|
assert.Equal(t, "baz", queryVerify.Query)
|
2016-12-07 20:22:31 +00:00
|
|
|
assert.Equal(t, "Zach", queryVerify.AuthorName)
|
2016-10-04 20:34:36 +00:00
|
|
|
}
|
2016-11-16 13:47:49 +00:00
|
|
|
|
|
|
|
func testListQuery(t *testing.T, ds kolide.Datastore) {
|
2016-12-13 22:22:05 +00:00
|
|
|
user := test.NewUser(t, ds, "Zach", "zwass", "zwass@kolide.co", true)
|
2016-12-07 20:22:31 +00:00
|
|
|
|
2016-11-16 13:47:49 +00:00
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
_, err := ds.NewQuery(&kolide.Query{
|
2016-12-07 20:22:31 +00:00
|
|
|
Name: fmt.Sprintf("name%02d", i),
|
|
|
|
Query: fmt.Sprintf("query%02d", i),
|
|
|
|
Saved: true,
|
2018-10-11 19:37:18 +00:00
|
|
|
AuthorID: &user.ID,
|
2016-11-16 13:47:49 +00:00
|
|
|
})
|
2016-12-06 18:16:04 +00:00
|
|
|
require.Nil(t, err)
|
2016-11-16 13:47:49 +00:00
|
|
|
}
|
|
|
|
|
2016-12-06 18:16:04 +00:00
|
|
|
// One unsaved query should not be returned
|
|
|
|
_, err := ds.NewQuery(&kolide.Query{
|
2016-12-07 20:22:31 +00:00
|
|
|
Name: "unsaved",
|
|
|
|
Query: "select * from time",
|
|
|
|
Saved: false,
|
2018-10-11 19:37:18 +00:00
|
|
|
AuthorID: &user.ID,
|
2016-12-06 18:16:04 +00:00
|
|
|
})
|
|
|
|
require.Nil(t, err)
|
|
|
|
|
2016-11-16 13:47:49 +00:00
|
|
|
opts := kolide.ListOptions{}
|
|
|
|
results, err := ds.ListQueries(opts)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, 10, len(results))
|
|
|
|
}
|
2016-12-06 18:22:28 +00:00
|
|
|
|
|
|
|
func testLoadPacksForQueries(t *testing.T, ds kolide.Datastore) {
|
2018-01-03 19:18:05 +00:00
|
|
|
zwass := test.NewUser(t, ds, "Zach", "zwass", "zwass@kolide.co", true)
|
|
|
|
queries := []*kolide.Query{
|
|
|
|
{Name: "q1", Query: "select * from time"},
|
|
|
|
{Name: "q2", Query: "select * from osquery_info"},
|
|
|
|
}
|
|
|
|
err := ds.ApplyQueries(zwass.ID, queries)
|
|
|
|
require.Nil(t, err)
|
2016-12-07 20:22:31 +00:00
|
|
|
|
2018-01-03 19:18:05 +00:00
|
|
|
specs := []*kolide.PackSpec{
|
|
|
|
&kolide.PackSpec{Name: "p1"},
|
|
|
|
&kolide.PackSpec{Name: "p2"},
|
|
|
|
&kolide.PackSpec{Name: "p3"},
|
|
|
|
}
|
|
|
|
err = ds.ApplyPackSpecs(specs)
|
|
|
|
require.Nil(t, err)
|
2016-12-06 18:22:28 +00:00
|
|
|
|
2018-05-09 23:54:42 +00:00
|
|
|
q0, err := ds.QueryByName(queries[0].Name)
|
2018-01-03 19:18:05 +00:00
|
|
|
require.Nil(t, err)
|
|
|
|
assert.Empty(t, q0.Packs)
|
2016-12-06 18:22:28 +00:00
|
|
|
|
2018-05-09 23:54:42 +00:00
|
|
|
q1, err := ds.QueryByName(queries[1].Name)
|
2018-01-03 19:18:05 +00:00
|
|
|
require.Nil(t, err)
|
|
|
|
assert.Empty(t, q1.Packs)
|
|
|
|
|
|
|
|
specs = []*kolide.PackSpec{
|
|
|
|
&kolide.PackSpec{
|
|
|
|
Name: "p2",
|
|
|
|
Queries: []kolide.PackSpecQuery{
|
|
|
|
kolide.PackSpecQuery{
|
2018-06-22 00:06:44 +00:00
|
|
|
Name: "q0",
|
2018-01-03 19:18:05 +00:00
|
|
|
QueryName: queries[0].Name,
|
|
|
|
Interval: 60,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err = ds.ApplyPackSpecs(specs)
|
|
|
|
require.Nil(t, err)
|
2016-12-06 18:22:28 +00:00
|
|
|
|
2018-05-09 23:54:42 +00:00
|
|
|
q0, err = ds.QueryByName(queries[0].Name)
|
2018-01-03 19:18:05 +00:00
|
|
|
require.Nil(t, err)
|
|
|
|
if assert.Len(t, q0.Packs, 1) {
|
|
|
|
assert.Equal(t, "p2", q0.Packs[0].Name)
|
|
|
|
}
|
2016-12-06 18:22:28 +00:00
|
|
|
|
2018-05-09 23:54:42 +00:00
|
|
|
q1, err = ds.QueryByName(queries[1].Name)
|
2016-12-06 18:22:28 +00:00
|
|
|
require.Nil(t, err)
|
2018-01-03 19:18:05 +00:00
|
|
|
assert.Empty(t, q1.Packs)
|
|
|
|
|
|
|
|
specs = []*kolide.PackSpec{
|
|
|
|
&kolide.PackSpec{
|
|
|
|
Name: "p1",
|
|
|
|
Queries: []kolide.PackSpecQuery{
|
|
|
|
kolide.PackSpecQuery{
|
|
|
|
QueryName: queries[1].Name,
|
|
|
|
Interval: 60,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&kolide.PackSpec{
|
|
|
|
Name: "p3",
|
|
|
|
Queries: []kolide.PackSpecQuery{
|
|
|
|
kolide.PackSpecQuery{
|
|
|
|
QueryName: queries[1].Name,
|
|
|
|
Interval: 60,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err = ds.ApplyPackSpecs(specs)
|
2016-12-06 18:22:28 +00:00
|
|
|
require.Nil(t, err)
|
|
|
|
|
2018-05-09 23:54:42 +00:00
|
|
|
q0, err = ds.QueryByName(queries[0].Name)
|
2016-12-06 18:22:28 +00:00
|
|
|
require.Nil(t, err)
|
2018-01-03 19:18:05 +00:00
|
|
|
if assert.Len(t, q0.Packs, 1) {
|
|
|
|
assert.Equal(t, "p2", q0.Packs[0].Name)
|
|
|
|
}
|
|
|
|
|
2018-05-09 23:54:42 +00:00
|
|
|
q1, err = ds.QueryByName(queries[1].Name)
|
2016-12-06 18:22:28 +00:00
|
|
|
require.Nil(t, err)
|
2018-01-03 19:18:05 +00:00
|
|
|
if assert.Len(t, q1.Packs, 2) {
|
|
|
|
sort.Slice(q1.Packs, func(i, j int) bool { return q1.Packs[i].Name < q1.Packs[j].Name })
|
|
|
|
assert.Equal(t, "p1", q1.Packs[0].Name)
|
|
|
|
assert.Equal(t, "p3", q1.Packs[1].Name)
|
|
|
|
}
|
2016-12-06 18:22:28 +00:00
|
|
|
|
2018-01-03 19:18:05 +00:00
|
|
|
specs = []*kolide.PackSpec{
|
|
|
|
&kolide.PackSpec{
|
|
|
|
Name: "p3",
|
|
|
|
Queries: []kolide.PackSpecQuery{
|
|
|
|
kolide.PackSpecQuery{
|
2018-06-22 00:06:44 +00:00
|
|
|
Name: "q0",
|
2018-01-03 19:18:05 +00:00
|
|
|
QueryName: queries[0].Name,
|
|
|
|
Interval: 60,
|
|
|
|
},
|
|
|
|
kolide.PackSpecQuery{
|
2018-06-22 00:06:44 +00:00
|
|
|
Name: "q1",
|
2018-01-03 19:18:05 +00:00
|
|
|
QueryName: queries[1].Name,
|
|
|
|
Interval: 60,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err = ds.ApplyPackSpecs(specs)
|
|
|
|
require.Nil(t, err)
|
2016-12-06 18:22:28 +00:00
|
|
|
|
2018-05-09 23:54:42 +00:00
|
|
|
q0, err = ds.QueryByName(queries[0].Name)
|
2016-12-06 18:22:28 +00:00
|
|
|
require.Nil(t, err)
|
2018-01-03 19:18:05 +00:00
|
|
|
if assert.Len(t, q0.Packs, 2) {
|
|
|
|
sort.Slice(q0.Packs, func(i, j int) bool { return q0.Packs[i].Name < q0.Packs[j].Name })
|
|
|
|
assert.Equal(t, "p2", q0.Packs[0].Name)
|
|
|
|
assert.Equal(t, "p3", q0.Packs[1].Name)
|
|
|
|
}
|
|
|
|
|
2018-05-09 23:54:42 +00:00
|
|
|
q1, err = ds.QueryByName(queries[1].Name)
|
2016-12-06 18:22:28 +00:00
|
|
|
require.Nil(t, err)
|
2018-01-03 19:18:05 +00:00
|
|
|
if assert.Len(t, q1.Packs, 2) {
|
|
|
|
sort.Slice(q1.Packs, func(i, j int) bool { return q1.Packs[i].Name < q1.Packs[j].Name })
|
|
|
|
assert.Equal(t, "p1", q1.Packs[0].Name)
|
|
|
|
assert.Equal(t, "p3", q1.Packs[1].Name)
|
|
|
|
}
|
2016-12-06 18:22:28 +00:00
|
|
|
}
|
2017-01-16 22:20:15 +00:00
|
|
|
|
|
|
|
func testDuplicateNewQuery(t *testing.T, ds kolide.Datastore) {
|
|
|
|
user := test.NewUser(t, ds, "Mike Arpaia", "marpaia", "mike@kolide.co", true)
|
|
|
|
q1, err := ds.NewQuery(&kolide.Query{
|
|
|
|
Name: "foo",
|
|
|
|
Query: "select * from time;",
|
2018-10-11 19:37:18 +00:00
|
|
|
AuthorID: &user.ID,
|
2017-01-16 22:20:15 +00:00
|
|
|
})
|
|
|
|
require.Nil(t, err)
|
|
|
|
assert.NotZero(t, q1.ID)
|
|
|
|
|
|
|
|
_, err = ds.NewQuery(&kolide.Query{
|
|
|
|
Name: "foo",
|
|
|
|
Query: "select * from osquery_info;",
|
|
|
|
})
|
|
|
|
|
|
|
|
// Note that we can't do the actual type assertion here because existsError
|
|
|
|
// is private to the individual datastore implementations
|
2021-04-05 20:28:43 +00:00
|
|
|
assert.Contains(t, err.Error(), "already exists")
|
2017-01-16 22:20:15 +00:00
|
|
|
}
|