mirror of
https://github.com/empayre/fleet.git
synced 2024-11-06 08:55:24 +00:00
parent
94106f87e4
commit
97de2bc5aa
@ -328,13 +328,13 @@ func createDevLabels(ds kolide.Datastore, config config.KolideConfig) {
|
||||
CreatedAt: time.Date(2016, time.October, 27, 8, 31, 16, 0, time.UTC),
|
||||
UpdatedAt: time.Date(2016, time.October, 27, 8, 31, 16, 0, time.UTC),
|
||||
Name: "dev_label_apache",
|
||||
QueryID: 4,
|
||||
Query: "select * from processes where nae like '%Apache%'",
|
||||
},
|
||||
{
|
||||
CreatedAt: time.Now().Add(-1 * time.Hour),
|
||||
UpdatedAt: time.Now(),
|
||||
Name: "dev_label_darwin",
|
||||
QueryID: 5,
|
||||
Query: "select * from osquery_info where build_platform='darwin'",
|
||||
},
|
||||
}
|
||||
|
||||
|
@ -33,42 +33,6 @@ func testLabels(t *testing.T, db kolide.Datastore) {
|
||||
assert.Nil(t, err)
|
||||
assert.Empty(t, labels)
|
||||
|
||||
labelQueries := []kolide.Query{
|
||||
kolide.Query{
|
||||
Name: "query1",
|
||||
Query: "query1",
|
||||
Platform: "darwin",
|
||||
},
|
||||
kolide.Query{
|
||||
Name: "query2",
|
||||
Query: "query2",
|
||||
Platform: "darwin",
|
||||
},
|
||||
kolide.Query{
|
||||
Name: "query3",
|
||||
Query: "query3",
|
||||
Platform: "darwin",
|
||||
},
|
||||
kolide.Query{
|
||||
Name: "query4",
|
||||
Query: "query4",
|
||||
Platform: "darwin",
|
||||
},
|
||||
}
|
||||
|
||||
for _, query := range labelQueries {
|
||||
newQuery, err := db.NewQuery(&query)
|
||||
assert.Nil(t, err)
|
||||
assert.NotZero(t, newQuery.ID)
|
||||
}
|
||||
|
||||
// this one should not show up
|
||||
_, err = db.NewQuery(&kolide.Query{
|
||||
Platform: "not_darwin",
|
||||
Query: "query5",
|
||||
})
|
||||
assert.Nil(t, err)
|
||||
|
||||
// No queries should be returned before labels added
|
||||
queries, err = db.LabelQueriesForHost(host, baseTime)
|
||||
assert.Nil(t, err)
|
||||
@ -77,20 +41,24 @@ func testLabels(t *testing.T, db kolide.Datastore) {
|
||||
newLabels := []kolide.Label{
|
||||
// Note these are intentionally out of order
|
||||
kolide.Label{
|
||||
Name: "label3",
|
||||
QueryID: 3,
|
||||
Name: "label3",
|
||||
Query: "query3",
|
||||
Platform: "darwin",
|
||||
},
|
||||
kolide.Label{
|
||||
Name: "label1",
|
||||
QueryID: 1,
|
||||
Name: "label1",
|
||||
Query: "query1",
|
||||
Platform: "darwin",
|
||||
},
|
||||
kolide.Label{
|
||||
Name: "label2",
|
||||
QueryID: 2,
|
||||
Name: "label2",
|
||||
Query: "query2",
|
||||
Platform: "darwin",
|
||||
},
|
||||
kolide.Label{
|
||||
Name: "label4",
|
||||
QueryID: 4,
|
||||
Name: "label4",
|
||||
Query: "query4",
|
||||
Platform: "darwin",
|
||||
},
|
||||
}
|
||||
|
||||
@ -172,29 +140,15 @@ func testLabels(t *testing.T, db kolide.Datastore) {
|
||||
}
|
||||
|
||||
func testManagingLabelsOnPacks(t *testing.T, ds kolide.Datastore) {
|
||||
mysqlQuery := &kolide.Query{
|
||||
Name: "MySQL",
|
||||
Query: "select pid from processes where name = 'mysqld';",
|
||||
}
|
||||
mysqlQuery, err := ds.NewQuery(mysqlQuery)
|
||||
assert.Nil(t, err)
|
||||
|
||||
osqueryRunningQuery := &kolide.Query{
|
||||
Name: "Is osquery currently running?",
|
||||
Query: "select pid from processes where name = 'osqueryd';",
|
||||
}
|
||||
osqueryRunningQuery, err = ds.NewQuery(osqueryRunningQuery)
|
||||
assert.Nil(t, err)
|
||||
|
||||
monitoringPack := &kolide.Pack{
|
||||
Name: "monitoring",
|
||||
}
|
||||
err = ds.NewPack(monitoringPack)
|
||||
err := ds.NewPack(monitoringPack)
|
||||
assert.Nil(t, err)
|
||||
|
||||
mysqlLabel := &kolide.Label{
|
||||
Name: "MySQL Monitoring",
|
||||
QueryID: mysqlQuery.ID,
|
||||
Name: "MySQL Monitoring",
|
||||
Query: "select pid from processes where name = 'mysqld';",
|
||||
}
|
||||
mysqlLabel, err = ds.NewLabel(mysqlLabel)
|
||||
assert.Nil(t, err)
|
||||
@ -208,8 +162,8 @@ func testManagingLabelsOnPacks(t *testing.T, ds kolide.Datastore) {
|
||||
assert.Equal(t, "MySQL Monitoring", labels[0].Name)
|
||||
|
||||
osqueryLabel := &kolide.Label{
|
||||
Name: "Osquery Monitoring",
|
||||
QueryID: osqueryRunningQuery.ID,
|
||||
Name: "Osquery Monitoring",
|
||||
Query: "select pid from processes where name = 'osqueryd';",
|
||||
}
|
||||
osqueryLabel, err = ds.NewLabel(osqueryLabel)
|
||||
assert.Nil(t, err)
|
||||
@ -294,8 +248,8 @@ func testListHostsInLabel(t *testing.T, db kolide.Datastore) {
|
||||
require.Nil(t, err)
|
||||
|
||||
l1, err := db.NewLabel(&kolide.Label{
|
||||
Name: "label foo",
|
||||
QueryID: 1,
|
||||
Name: "label foo",
|
||||
Query: "query1",
|
||||
})
|
||||
require.Nil(t, err)
|
||||
require.NotZero(t, l1.ID)
|
||||
@ -367,16 +321,16 @@ func testListUniqueHostsInLabels(t *testing.T, db kolide.Datastore) {
|
||||
require.Nil(t, err)
|
||||
|
||||
l1, err := db.NewLabel(&kolide.Label{
|
||||
Name: "label foo",
|
||||
QueryID: 1,
|
||||
Name: "label foo",
|
||||
Query: "query1",
|
||||
})
|
||||
require.Nil(t, err)
|
||||
require.NotZero(t, l1.ID)
|
||||
l1ID := fmt.Sprintf("%d", l1.ID)
|
||||
|
||||
l2, err := db.NewLabel(&kolide.Label{
|
||||
Name: "label bar",
|
||||
QueryID: 2,
|
||||
Name: "label bar",
|
||||
Query: "query2",
|
||||
})
|
||||
require.Nil(t, err)
|
||||
require.NotZero(t, l2.ID)
|
||||
|
@ -24,16 +24,6 @@ func (orm gormDB) NewLabel(label *kolide.Label) (*kolide.Label, error) {
|
||||
return label, nil
|
||||
}
|
||||
|
||||
func (orm gormDB) SaveLabel(label *kolide.Label) error {
|
||||
if label == nil {
|
||||
return errors.New(
|
||||
"error saving label",
|
||||
"nil pointer passed to SaveLabel",
|
||||
)
|
||||
}
|
||||
return orm.DB.Save(label).Error
|
||||
}
|
||||
|
||||
func (orm gormDB) DeleteLabel(lid uint) error {
|
||||
err := orm.DB.Where("id = ?", lid).Delete(&kolide.Label{}).Error
|
||||
if err != nil {
|
||||
@ -68,13 +58,12 @@ func (orm gormDB) LabelQueriesForHost(host *kolide.Host, cutoff time.Time) (map[
|
||||
)
|
||||
}
|
||||
rows, err := orm.DB.Raw(`
|
||||
SELECT l.id, q.query
|
||||
FROM labels l JOIN queries q
|
||||
ON l.query_id = q.id
|
||||
WHERE q.platform = ?
|
||||
AND q.id NOT IN /* subtract the set of executions that are recent enough */
|
||||
SELECT l.id, l.query
|
||||
from labels l
|
||||
WHERE l.platform = ?
|
||||
AND l.id NOT IN /* subtract the set of executions that are recent enough */
|
||||
(
|
||||
SELECT l.query_id
|
||||
SELECT l.id
|
||||
FROM labels l
|
||||
JOIN label_query_executions lqe
|
||||
ON lqe.label_id = l.id
|
||||
|
@ -46,32 +46,24 @@ func (orm *inmem) ListLabelsForHost(hid uint) ([]kolide.Label, error) {
|
||||
|
||||
func (orm *inmem) LabelQueriesForHost(host *kolide.Host, cutoff time.Time) (map[string]string, error) {
|
||||
// Get post-cutoff executions for host
|
||||
execedQueryIDs := map[uint]uint{} // Map queryID -> labelID
|
||||
execedIDs := map[uint]bool{}
|
||||
|
||||
orm.mtx.Lock()
|
||||
for _, lqe := range orm.labelQueryExecutions {
|
||||
if lqe.HostID == host.ID && (lqe.UpdatedAt == cutoff || lqe.UpdatedAt.After(cutoff)) {
|
||||
label := orm.labels[lqe.LabelID]
|
||||
execedQueryIDs[label.QueryID] = label.ID
|
||||
execedIDs[lqe.LabelID] = true
|
||||
}
|
||||
}
|
||||
|
||||
queries := map[string]string{}
|
||||
for _, label := range orm.labels {
|
||||
if (label.Platform == "" || strings.Contains(label.Platform, host.Platform)) && !execedIDs[label.ID] {
|
||||
queries[strconv.Itoa(int(label.ID))] = label.Query
|
||||
}
|
||||
}
|
||||
orm.mtx.Unlock()
|
||||
|
||||
queryToLabel := map[uint]uint{} // Map queryID -> labelID
|
||||
for _, label := range orm.labels {
|
||||
queryToLabel[label.QueryID] = label.ID
|
||||
}
|
||||
|
||||
resQueries := map[string]string{}
|
||||
for _, query := range orm.queries {
|
||||
_, execed := execedQueryIDs[query.ID]
|
||||
labelID := queryToLabel[query.ID]
|
||||
if query.Platform == host.Platform && !execed {
|
||||
resQueries[strconv.Itoa(int(labelID))] = query.Query
|
||||
}
|
||||
}
|
||||
|
||||
return resQueries, nil
|
||||
return queries, nil
|
||||
}
|
||||
|
||||
func (orm *inmem) getLabelByIDString(id string) (*kolide.Label, error) {
|
||||
@ -183,18 +175,6 @@ func (orm *inmem) ListLabels(opt kolide.ListOptions) ([]*kolide.Label, error) {
|
||||
return labels, nil
|
||||
}
|
||||
|
||||
func (orm *inmem) SaveLabel(label *kolide.Label) error {
|
||||
orm.mtx.Lock()
|
||||
if _, ok := orm.labels[label.ID]; !ok {
|
||||
return ErrNotFound
|
||||
}
|
||||
|
||||
orm.labels[label.ID] = label
|
||||
orm.mtx.Unlock()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (orm *inmem) SearchLabels(query string, omit []uint) ([]kolide.Label, error) {
|
||||
omitLookup := map[uint]bool{}
|
||||
for _, o := range omit {
|
||||
|
@ -9,7 +9,6 @@ import (
|
||||
type LabelStore interface {
|
||||
// Label methods
|
||||
NewLabel(Label *Label) (*Label, error)
|
||||
SaveLabel(Label *Label) error
|
||||
DeleteLabel(lid uint) error
|
||||
Label(lid uint) (*Label, error)
|
||||
ListLabels(opt ListOptions) ([]*Label, error)
|
||||
@ -40,21 +39,24 @@ type LabelService interface {
|
||||
ListLabels(ctx context.Context, opt ListOptions) ([]*Label, error)
|
||||
GetLabel(ctx context.Context, id uint) (*Label, error)
|
||||
NewLabel(ctx context.Context, p LabelPayload) (*Label, error)
|
||||
ModifyLabel(ctx context.Context, id uint, p LabelPayload) (*Label, error)
|
||||
DeleteLabel(ctx context.Context, id uint) error
|
||||
}
|
||||
|
||||
type LabelPayload struct {
|
||||
Name *string
|
||||
QueryID *uint `json:"query_id"`
|
||||
Name *string
|
||||
Query *string `json:"query"`
|
||||
Platform *string `json:"platform"`
|
||||
Description *string `json:"description"`
|
||||
}
|
||||
|
||||
type Label struct {
|
||||
ID uint `json:"id" gorm:"primary_key"`
|
||||
CreatedAt time.Time `json:"-"`
|
||||
UpdatedAt time.Time `json:"-"`
|
||||
Name string `json:"name" gorm:"not null;unique_index:idx_label_unique_name"` // there is a fulltext index on this field
|
||||
QueryID uint `json:"query_id"`
|
||||
ID uint `json:"id" gorm:"primary_key"`
|
||||
CreatedAt time.Time `json:"-"`
|
||||
UpdatedAt time.Time `json:"-"`
|
||||
Name string `json:"name" gorm:"not null;unique_index:idx_label_unique_name"` // there is a fulltext index on this field
|
||||
Description string `json:"description"`
|
||||
Query string `json:"query" gorm:"not null"`
|
||||
Platform string `json:"platform"`
|
||||
}
|
||||
|
||||
type LabelQueryExecution struct {
|
||||
|
@ -14,8 +14,14 @@ type getLabelRequest struct {
|
||||
ID uint
|
||||
}
|
||||
|
||||
type labelResponse struct {
|
||||
kolide.Label
|
||||
DisplayText string `json:"display_text"`
|
||||
Count uint `json:"count"`
|
||||
}
|
||||
|
||||
type getLabelResponse struct {
|
||||
Label *kolide.Label `json:"label"`
|
||||
Label labelResponse `json:"label"`
|
||||
Err error `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
@ -28,7 +34,17 @@ func makeGetLabelEndpoint(svc kolide.Service) endpoint.Endpoint {
|
||||
if err != nil {
|
||||
return getLabelResponse{Err: err}, nil
|
||||
}
|
||||
return getLabelResponse{label, nil}, nil
|
||||
count, err := svc.CountHostsInTargets(ctx, nil, []uint{label.ID})
|
||||
if err != nil {
|
||||
return getLabelResponse{Err: err}, nil
|
||||
}
|
||||
return getLabelResponse{
|
||||
Label: labelResponse{
|
||||
*label,
|
||||
label.Name,
|
||||
count,
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
|
||||
@ -41,8 +57,8 @@ type listLabelsRequest struct {
|
||||
}
|
||||
|
||||
type listLabelsResponse struct {
|
||||
Labels []kolide.Label `json:"labels"`
|
||||
Err error `json:"error,omitempty"`
|
||||
Labels []labelResponse `json:"labels"`
|
||||
Err error `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
func (r listLabelsResponse) error() error { return r.Err }
|
||||
@ -55,9 +71,19 @@ func makeListLabelsEndpoint(svc kolide.Service) endpoint.Endpoint {
|
||||
return listLabelsResponse{Err: err}, nil
|
||||
}
|
||||
|
||||
resp := listLabelsResponse{Labels: []kolide.Label{}}
|
||||
resp := listLabelsResponse{}
|
||||
for _, label := range labels {
|
||||
resp.Labels = append(resp.Labels, *label)
|
||||
count, err := svc.CountHostsInTargets(ctx, nil, []uint{label.ID})
|
||||
if err != nil {
|
||||
return listLabelsResponse{Err: err}, nil
|
||||
}
|
||||
resp.Labels = append(resp.Labels,
|
||||
labelResponse{
|
||||
*label,
|
||||
label.Name,
|
||||
count,
|
||||
},
|
||||
)
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
@ -72,7 +98,7 @@ type createLabelRequest struct {
|
||||
}
|
||||
|
||||
type createLabelResponse struct {
|
||||
Label *kolide.Label `json:"label"`
|
||||
Label labelResponse `json:"label"`
|
||||
Err error `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
@ -85,34 +111,17 @@ func makeCreateLabelEndpoint(svc kolide.Service) endpoint.Endpoint {
|
||||
if err != nil {
|
||||
return createLabelResponse{Err: err}, nil
|
||||
}
|
||||
return createLabelResponse{label, nil}, nil
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Modify Label
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
type modifyLabelRequest struct {
|
||||
ID uint
|
||||
payload kolide.LabelPayload
|
||||
}
|
||||
|
||||
type modifyLabelResponse struct {
|
||||
Label *kolide.Label `json:"label"`
|
||||
Err error `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
func (r modifyLabelResponse) error() error { return r.Err }
|
||||
|
||||
func makeModifyLabelEndpoint(svc kolide.Service) endpoint.Endpoint {
|
||||
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
||||
req := request.(modifyLabelRequest)
|
||||
label, err := svc.ModifyLabel(ctx, req.ID, req.payload)
|
||||
count, err := svc.CountHostsInTargets(ctx, nil, []uint{label.ID})
|
||||
if err != nil {
|
||||
return modifyLabelResponse{Err: err}, nil
|
||||
return createLabelResponse{Err: err}, nil
|
||||
}
|
||||
return modifyLabelResponse{label, nil}, nil
|
||||
return createLabelResponse{
|
||||
Label: labelResponse{
|
||||
*label,
|
||||
label.Name,
|
||||
count,
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -52,7 +52,6 @@ type KolideEndpoints struct {
|
||||
GetLabel endpoint.Endpoint
|
||||
ListLabels endpoint.Endpoint
|
||||
CreateLabel endpoint.Endpoint
|
||||
ModifyLabel endpoint.Endpoint
|
||||
DeleteLabel endpoint.Endpoint
|
||||
AddLabelToPack endpoint.Endpoint
|
||||
GetLabelsForPack endpoint.Endpoint
|
||||
@ -105,7 +104,6 @@ func MakeKolideServerEndpoints(svc kolide.Service, jwtKey string) KolideEndpoint
|
||||
GetLabel: authenticatedUser(jwtKey, svc, makeGetLabelEndpoint(svc)),
|
||||
ListLabels: authenticatedUser(jwtKey, svc, makeListLabelsEndpoint(svc)),
|
||||
CreateLabel: authenticatedUser(jwtKey, svc, makeCreateLabelEndpoint(svc)),
|
||||
ModifyLabel: authenticatedUser(jwtKey, svc, makeModifyLabelEndpoint(svc)),
|
||||
DeleteLabel: authenticatedUser(jwtKey, svc, makeDeleteLabelEndpoint(svc)),
|
||||
AddLabelToPack: authenticatedUser(jwtKey, svc, makeAddLabelToPackEndpoint(svc)),
|
||||
GetLabelsForPack: authenticatedUser(jwtKey, svc, makeGetLabelsForPackEndpoint(svc)),
|
||||
@ -161,7 +159,6 @@ type kolideHandlers struct {
|
||||
GetLabel *kithttp.Server
|
||||
ListLabels *kithttp.Server
|
||||
CreateLabel *kithttp.Server
|
||||
ModifyLabel *kithttp.Server
|
||||
DeleteLabel *kithttp.Server
|
||||
AddLabelToPack *kithttp.Server
|
||||
GetLabelsForPack *kithttp.Server
|
||||
@ -216,7 +213,6 @@ func makeKolideKitHandlers(ctx context.Context, e KolideEndpoints, opts []kithtt
|
||||
GetLabel: newServer(e.GetLabel, decodeGetLabelRequest),
|
||||
ListLabels: newServer(e.ListLabels, decodeListLabelsRequest),
|
||||
CreateLabel: newServer(e.CreateLabel, decodeCreateLabelRequest),
|
||||
ModifyLabel: newServer(e.ModifyLabel, decodeModifyLabelRequest),
|
||||
DeleteLabel: newServer(e.DeleteLabel, decodeDeleteLabelRequest),
|
||||
AddLabelToPack: newServer(e.AddLabelToPack, decodeAddLabelToPackRequest),
|
||||
GetLabelsForPack: newServer(e.GetLabelsForPack, decodeGetLabelsForPackRequest),
|
||||
@ -290,7 +286,6 @@ func attachKolideAPIRoutes(r *mux.Router, h kolideHandlers) {
|
||||
r.Handle("/api/v1/kolide/labels/{id}", h.GetLabel).Methods("GET")
|
||||
r.Handle("/api/v1/kolide/labels", h.ListLabels).Methods("GET")
|
||||
r.Handle("/api/v1/kolide/labels", h.CreateLabel).Methods("POST")
|
||||
r.Handle("/api/v1/kolide/labels/{id}", h.ModifyLabel).Methods("PATCH")
|
||||
r.Handle("/api/v1/kolide/labels/{id}", h.DeleteLabel).Methods("DELETE")
|
||||
r.Handle("/api/v1/kolide/packs/{pid}/labels/{lid}", h.AddLabelToPack).Methods("POST")
|
||||
r.Handle("/api/v1/kolide/packs/{pid}/labels", h.GetLabelsForPack).Methods("GET")
|
||||
|
@ -167,10 +167,6 @@ func TestAPIRoutes(t *testing.T) {
|
||||
verb: "POST",
|
||||
uri: "/api/v1/kolide/labels",
|
||||
},
|
||||
{
|
||||
verb: "PATCH",
|
||||
uri: "/api/v1/kolide/labels/1",
|
||||
},
|
||||
{
|
||||
verb: "DELETE",
|
||||
uri: "/api/v1/kolide/labels/1",
|
||||
|
@ -21,8 +21,17 @@ func (svc service) NewLabel(ctx context.Context, p kolide.LabelPayload) (*kolide
|
||||
}
|
||||
label.Name = *p.Name
|
||||
|
||||
if p.QueryID != nil {
|
||||
label.QueryID = *p.QueryID
|
||||
if p.Query == nil {
|
||||
return nil, newInvalidArgumentError("query", "missing required argument")
|
||||
}
|
||||
label.Query = *p.Query
|
||||
|
||||
if p.Platform != nil {
|
||||
label.Platform = *p.Platform
|
||||
}
|
||||
|
||||
if p.Description != nil {
|
||||
label.Description = *p.Description
|
||||
}
|
||||
|
||||
label, err := svc.ds.NewLabel(label)
|
||||
@ -32,28 +41,6 @@ func (svc service) NewLabel(ctx context.Context, p kolide.LabelPayload) (*kolide
|
||||
return label, nil
|
||||
}
|
||||
|
||||
func (svc service) ModifyLabel(ctx context.Context, id uint, p kolide.LabelPayload) (*kolide.Label, error) {
|
||||
label, err := svc.ds.Label(id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if p.Name != nil {
|
||||
label.Name = *p.Name
|
||||
}
|
||||
|
||||
if p.QueryID != nil {
|
||||
label.QueryID = *p.QueryID
|
||||
}
|
||||
|
||||
err = svc.ds.SaveLabel(label)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return label, nil
|
||||
}
|
||||
|
||||
func (svc service) DeleteLabel(ctx context.Context, id uint) error {
|
||||
return svc.ds.DeleteLabel(id)
|
||||
}
|
||||
|
@ -23,8 +23,8 @@ func TestListLabels(t *testing.T) {
|
||||
assert.Len(t, labels, 0)
|
||||
|
||||
_, err = ds.NewLabel(&kolide.Label{
|
||||
Name: "foo",
|
||||
QueryID: 1,
|
||||
Name: "foo",
|
||||
Query: "select * from foo;",
|
||||
})
|
||||
assert.Nil(t, err)
|
||||
|
||||
@ -44,8 +44,8 @@ func TestGetLabel(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
label := &kolide.Label{
|
||||
Name: "foo",
|
||||
QueryID: 1,
|
||||
Name: "foo",
|
||||
Query: "select * from foo;",
|
||||
}
|
||||
label, err = ds.NewLabel(label)
|
||||
assert.Nil(t, err)
|
||||
@ -66,10 +66,10 @@ func TestNewLabel(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
name := "foo"
|
||||
queryID := uint(1)
|
||||
query := "select * from foo;"
|
||||
label, err := svc.NewLabel(ctx, kolide.LabelPayload{
|
||||
Name: &name,
|
||||
QueryID: &queryID,
|
||||
Name: &name,
|
||||
Query: &query,
|
||||
})
|
||||
assert.NotZero(t, label.ID)
|
||||
|
||||
@ -81,32 +81,6 @@ func TestNewLabel(t *testing.T) {
|
||||
assert.Equal(t, "foo", labels[0].Name)
|
||||
}
|
||||
|
||||
func TestModifyLabel(t *testing.T) {
|
||||
ds, err := datastore.New("inmem", "")
|
||||
assert.Nil(t, err)
|
||||
|
||||
svc, err := newTestService(ds)
|
||||
assert.Nil(t, err)
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
label := &kolide.Label{
|
||||
Name: "foo",
|
||||
QueryID: 1,
|
||||
}
|
||||
label, err = ds.NewLabel(label)
|
||||
assert.Nil(t, err)
|
||||
assert.NotZero(t, label.ID)
|
||||
|
||||
newName := "bar"
|
||||
labelVerify, err := svc.ModifyLabel(ctx, label.ID, kolide.LabelPayload{
|
||||
Name: &newName,
|
||||
})
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, label.ID, labelVerify.ID)
|
||||
assert.Equal(t, "bar", labelVerify.Name)
|
||||
}
|
||||
|
||||
func TestDeleteLabel(t *testing.T) {
|
||||
ds, err := datastore.New("inmem", "")
|
||||
assert.Nil(t, err)
|
||||
@ -117,8 +91,8 @@ func TestDeleteLabel(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
label := &kolide.Label{
|
||||
Name: "foo",
|
||||
QueryID: 1,
|
||||
Name: "foo",
|
||||
Query: "select * from foo;",
|
||||
}
|
||||
label, err = ds.NewLabel(label)
|
||||
assert.Nil(t, err)
|
||||
|
@ -4,11 +4,12 @@ import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"golang.org/x/net/context"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"golang.org/x/net/context"
|
||||
|
||||
"github.com/WatchBeam/clock"
|
||||
hostctx "github.com/kolide/kolide-ose/server/contexts/host"
|
||||
"github.com/kolide/kolide-ose/server/datastore"
|
||||
@ -270,60 +271,26 @@ func TestLabelQueries(t *testing.T) {
|
||||
assert.Nil(t, err)
|
||||
assert.Len(t, queries, 0)
|
||||
|
||||
// Add some queries and labels to ensure they are returned
|
||||
labelQueries := []kolide.Query{
|
||||
kolide.Query{
|
||||
ID: 1,
|
||||
Name: "query1",
|
||||
Platform: "darwin",
|
||||
Query: "query1",
|
||||
},
|
||||
kolide.Query{
|
||||
ID: 2,
|
||||
Name: "query2",
|
||||
Platform: "darwin",
|
||||
Query: "query2",
|
||||
},
|
||||
kolide.Query{
|
||||
ID: 3,
|
||||
Name: "query3",
|
||||
Platform: "darwin",
|
||||
Query: "query3",
|
||||
},
|
||||
}
|
||||
|
||||
expectQueries := make(map[string]string)
|
||||
|
||||
for _, query := range labelQueries {
|
||||
_, err := ds.NewQuery(&query)
|
||||
assert.Nil(t, err)
|
||||
expectQueries[fmt.Sprintf("kolide_label_query_%d", query.ID)] = query.Query
|
||||
}
|
||||
// this one should not show up
|
||||
_, err = ds.NewQuery(&kolide.Query{
|
||||
ID: 4,
|
||||
Name: "query4",
|
||||
Platform: "not_darwin",
|
||||
Query: "query4",
|
||||
})
|
||||
assert.Nil(t, err)
|
||||
|
||||
labels := []kolide.Label{
|
||||
kolide.Label{
|
||||
Name: "label1",
|
||||
QueryID: 1,
|
||||
Name: "label1",
|
||||
Query: "query1",
|
||||
Platform: "darwin",
|
||||
},
|
||||
kolide.Label{
|
||||
Name: "label2",
|
||||
QueryID: 2,
|
||||
Name: "label2",
|
||||
Query: "query2",
|
||||
Platform: "darwin",
|
||||
},
|
||||
kolide.Label{
|
||||
Name: "label3",
|
||||
QueryID: 3,
|
||||
Name: "label3",
|
||||
Query: "query3",
|
||||
Platform: "darwin,linux",
|
||||
},
|
||||
kolide.Label{
|
||||
Name: "label4",
|
||||
QueryID: 4,
|
||||
Name: "label4",
|
||||
Query: "query4",
|
||||
Platform: "linux",
|
||||
},
|
||||
}
|
||||
|
||||
@ -336,7 +303,6 @@ func TestLabelQueries(t *testing.T) {
|
||||
queries, err = svc.GetDistributedQueries(ctx)
|
||||
assert.Nil(t, err)
|
||||
assert.Len(t, queries, 3)
|
||||
assert.Equal(t, expectQueries, queries)
|
||||
|
||||
// Record a query execution
|
||||
err = svc.SubmitDistributedQueryResults(
|
||||
@ -370,7 +336,6 @@ func TestLabelQueries(t *testing.T) {
|
||||
queries, err = svc.GetDistributedQueries(ctx)
|
||||
assert.Nil(t, err)
|
||||
assert.Len(t, queries, 3)
|
||||
assert.Equal(t, expectQueries, queries)
|
||||
|
||||
// Record a query execution
|
||||
err = svc.SubmitDistributedQueryResults(
|
||||
@ -383,11 +348,9 @@ func TestLabelQueries(t *testing.T) {
|
||||
assert.Nil(t, err)
|
||||
|
||||
// Now these should no longer show up in the necessary to run queries
|
||||
delete(expectQueries, "kolide_label_query_2")
|
||||
delete(expectQueries, "kolide_label_query_3")
|
||||
queries, err = svc.GetDistributedQueries(ctx)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, expectQueries, queries)
|
||||
assert.Len(t, queries, 1)
|
||||
|
||||
// Verify that labels are set appropriately
|
||||
hostLabels, err = ds.ListLabelsForHost(host.ID)
|
||||
@ -438,13 +401,6 @@ func TestGetClientConfig(t *testing.T) {
|
||||
|
||||
// let's populate the database with some info
|
||||
|
||||
mysqlQuery := &kolide.Query{
|
||||
Name: "MySQL",
|
||||
Query: "select pid from processes where name = 'mysqld';",
|
||||
}
|
||||
mysqlQuery, err = ds.NewQuery(mysqlQuery)
|
||||
assert.Nil(t, err)
|
||||
|
||||
infoQuery := &kolide.Query{
|
||||
Name: "Info",
|
||||
Query: "select * from osquery_info;",
|
||||
@ -463,8 +419,8 @@ func TestGetClientConfig(t *testing.T) {
|
||||
assert.Nil(t, err)
|
||||
|
||||
mysqlLabel := &kolide.Label{
|
||||
Name: "MySQL Monitoring",
|
||||
QueryID: mysqlQuery.ID,
|
||||
Name: "MySQL Monitoring",
|
||||
Query: "select pid from processes where name = 'mysqld';",
|
||||
}
|
||||
mysqlLabel, err = ds.NewLabel(mysqlLabel)
|
||||
assert.Nil(t, err)
|
||||
@ -472,7 +428,11 @@ func TestGetClientConfig(t *testing.T) {
|
||||
err = ds.AddLabelToPack(mysqlLabel.ID, monitoringPack.ID)
|
||||
assert.Nil(t, err)
|
||||
|
||||
err = ds.RecordLabelQueryExecutions(host, map[string]bool{fmt.Sprintf("%d", mysqlQuery.ID): true}, mockClock.Now())
|
||||
err = ds.RecordLabelQueryExecutions(
|
||||
host,
|
||||
map[string]bool{fmt.Sprintf("%d", mysqlLabel.ID): true},
|
||||
mockClock.Now(),
|
||||
)
|
||||
assert.Nil(t, err)
|
||||
|
||||
// with a minimal setup of packs, labels, and queries, will our host get the
|
||||
|
@ -28,8 +28,8 @@ func TestSearchTargets(t *testing.T) {
|
||||
require.Nil(t, err)
|
||||
|
||||
l1, err := ds.NewLabel(&kolide.Label{
|
||||
Name: "label foo",
|
||||
QueryID: 1,
|
||||
Name: "label foo",
|
||||
Query: "query foo",
|
||||
})
|
||||
require.Nil(t, err)
|
||||
|
||||
@ -93,16 +93,16 @@ func TestCountHostsInTargets(t *testing.T) {
|
||||
require.Nil(t, err)
|
||||
|
||||
l1, err := ds.NewLabel(&kolide.Label{
|
||||
Name: "label foo",
|
||||
QueryID: 1,
|
||||
Name: "label foo",
|
||||
Query: "query foo",
|
||||
})
|
||||
require.Nil(t, err)
|
||||
require.NotZero(t, l1.ID)
|
||||
l1ID := fmt.Sprintf("%d", l1.ID)
|
||||
|
||||
l2, err := ds.NewLabel(&kolide.Label{
|
||||
Name: "label bar",
|
||||
QueryID: 1,
|
||||
Name: "label bar",
|
||||
Query: "query foo",
|
||||
})
|
||||
require.Nil(t, err)
|
||||
require.NotZero(t, l2.ID)
|
||||
@ -165,8 +165,8 @@ func TestSearchWithOmit(t *testing.T) {
|
||||
require.Nil(t, err)
|
||||
|
||||
l1, err := ds.NewLabel(&kolide.Label{
|
||||
Name: "label foo",
|
||||
QueryID: 1,
|
||||
Name: "label foo",
|
||||
Query: "query foo",
|
||||
})
|
||||
|
||||
{
|
||||
@ -225,8 +225,8 @@ func TestSearchHostsInLabels(t *testing.T) {
|
||||
require.Nil(t, err)
|
||||
|
||||
l1, err := ds.NewLabel(&kolide.Label{
|
||||
Name: "label foo",
|
||||
QueryID: 1,
|
||||
Name: "label foo",
|
||||
Query: "query foo",
|
||||
})
|
||||
require.Nil(t, err)
|
||||
require.NotZero(t, l1.ID)
|
||||
|
@ -15,19 +15,6 @@ func decodeCreateLabelRequest(ctx context.Context, r *http.Request) (interface{}
|
||||
return req, nil
|
||||
}
|
||||
|
||||
func decodeModifyLabelRequest(ctx context.Context, r *http.Request) (interface{}, error) {
|
||||
id, err := idFromRequest(r, "id")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var req modifyLabelRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&req.payload); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.ID = id
|
||||
return req, nil
|
||||
}
|
||||
|
||||
func decodeDeleteLabelRequest(ctx context.Context, r *http.Request) (interface{}, error) {
|
||||
id, err := idFromRequest(r, "id")
|
||||
if err != nil {
|
||||
|
@ -20,13 +20,15 @@ func TestDecodeCreateLabelRequest(t *testing.T) {
|
||||
|
||||
params := r.(createLabelRequest)
|
||||
assert.Equal(t, "foo", *params.payload.Name)
|
||||
assert.Equal(t, uint(4), *params.payload.QueryID)
|
||||
assert.Equal(t, "select * from foo;", *params.payload.Query)
|
||||
assert.Equal(t, "darwin", *params.payload.Platform)
|
||||
}).Methods("POST")
|
||||
|
||||
var body bytes.Buffer
|
||||
body.Write([]byte(`{
|
||||
"name": "foo",
|
||||
"query_id": 4
|
||||
"query": "select * from foo;",
|
||||
"platform": "darwin"
|
||||
}`))
|
||||
|
||||
router.ServeHTTP(
|
||||
@ -35,28 +37,6 @@ func TestDecodeCreateLabelRequest(t *testing.T) {
|
||||
)
|
||||
}
|
||||
|
||||
func TestDecodeModifyLabelRequest(t *testing.T) {
|
||||
router := mux.NewRouter()
|
||||
router.HandleFunc("/api/v1/kolide/labels/{id}", func(writer http.ResponseWriter, request *http.Request) {
|
||||
r, err := decodeModifyLabelRequest(context.Background(), request)
|
||||
assert.Nil(t, err)
|
||||
|
||||
params := r.(modifyLabelRequest)
|
||||
assert.Equal(t, "foo", *params.payload.Name)
|
||||
assert.Equal(t, uint(1), params.ID)
|
||||
}).Methods("PATCH")
|
||||
|
||||
var body bytes.Buffer
|
||||
body.Write([]byte(`{
|
||||
"name": "foo"
|
||||
}`))
|
||||
|
||||
router.ServeHTTP(
|
||||
httptest.NewRecorder(),
|
||||
httptest.NewRequest("PATCH", "/api/v1/kolide/labels/1", &body),
|
||||
)
|
||||
}
|
||||
|
||||
func TestDecodeDeleteLabelRequest(t *testing.T) {
|
||||
router := mux.NewRouter()
|
||||
router.HandleFunc("/api/v1/kolide/labels/{id}", func(writer http.ResponseWriter, request *http.Request) {
|
||||
|
Loading…
Reference in New Issue
Block a user