2016-09-04 05:13:42 +00:00
|
|
|
package kolide
|
|
|
|
|
|
|
|
import (
|
2017-03-15 15:55:30 +00:00
|
|
|
"context"
|
2016-09-04 05:13:42 +00:00
|
|
|
)
|
|
|
|
|
2017-01-11 20:33:30 +00:00
|
|
|
// PackStore is the datastore interface for managing query packs.
|
2016-09-04 05:13:42 +00:00
|
|
|
type PackStore interface {
|
2018-01-10 19:38:20 +00:00
|
|
|
// ApplyPackSpecs applies a list of PackSpecs to the datastore,
|
|
|
|
// creating and updating packs as necessary.
|
2018-01-03 19:18:05 +00:00
|
|
|
ApplyPackSpecs(specs []*PackSpec) error
|
2018-01-10 19:38:20 +00:00
|
|
|
// GetPackSpecs returns all of the stored PackSpecs.
|
2018-01-03 19:18:05 +00:00
|
|
|
GetPackSpecs() ([]*PackSpec, error)
|
|
|
|
|
2017-01-11 20:33:30 +00:00
|
|
|
// DeletePack deletes a pack record from the datastore.
|
2016-10-03 03:14:35 +00:00
|
|
|
DeletePack(pid uint) error
|
2017-01-11 20:33:30 +00:00
|
|
|
|
|
|
|
// Pack retrieves a pack from the datastore by ID.
|
2016-10-03 03:14:35 +00:00
|
|
|
Pack(pid uint) (*Pack, error)
|
2017-01-11 20:33:30 +00:00
|
|
|
|
|
|
|
// ListPacks lists all packs in the datastore.
|
2016-10-14 15:59:27 +00:00
|
|
|
ListPacks(opt ListOptions) ([]*Pack, error)
|
2017-01-13 18:35:25 +00:00
|
|
|
// PackByName fetches pack if it exists, if the pack
|
|
|
|
// exists the bool return value is true
|
2017-05-30 19:42:00 +00:00
|
|
|
PackByName(name string, opts ...OptionalArg) (*Pack, bool, error)
|
2016-09-04 05:13:42 +00:00
|
|
|
|
2017-01-11 20:33:30 +00:00
|
|
|
// ListLabelsForPack lists all labels that are associated with a pack.
|
2016-11-22 21:56:05 +00:00
|
|
|
ListLabelsForPack(pid uint) ([]*Label, error)
|
|
|
|
|
2018-01-10 19:38:20 +00:00
|
|
|
// ListPacksForHost lists the packs that a host should execute.
|
|
|
|
ListPacksForHost(hid uint) (packs []*Pack, err error)
|
2017-01-11 20:33:30 +00:00
|
|
|
|
2018-01-10 19:38:20 +00:00
|
|
|
// ListHostsInPack lists the IDs of all hosts that are associated with a pack
|
|
|
|
// through labels.
|
2017-03-01 19:56:13 +00:00
|
|
|
ListHostsInPack(pid uint, opt ListOptions) ([]uint, error)
|
2017-01-11 20:33:30 +00:00
|
|
|
|
2017-03-01 19:56:13 +00:00
|
|
|
// ListExplicitHostsInPack lists the IDs of hosts that have been manually
|
|
|
|
// associated with a query pack.
|
|
|
|
ListExplicitHostsInPack(pid uint, opt ListOptions) ([]uint, error)
|
2016-09-04 05:13:42 +00:00
|
|
|
}
|
|
|
|
|
2017-01-11 20:33:30 +00:00
|
|
|
// PackService is the service interface for managing query packs.
|
2016-09-04 05:13:42 +00:00
|
|
|
type PackService interface {
|
2018-01-10 19:38:20 +00:00
|
|
|
// ApplyPackSpecs applies a list of PackSpecs to the datastore,
|
|
|
|
// creating and updating packs as necessary.
|
|
|
|
ApplyPackSpecs(ctx context.Context, specs []*PackSpec) error
|
|
|
|
// GetPackSpecs returns all of the stored PackSpecs.
|
|
|
|
GetPackSpecs(ctx context.Context) ([]*PackSpec, error)
|
|
|
|
|
2017-01-11 20:33:30 +00:00
|
|
|
// ListPacks lists all packs in the application.
|
2017-01-10 04:40:21 +00:00
|
|
|
ListPacks(ctx context.Context, opt ListOptions) (packs []*Pack, err error)
|
2017-01-11 20:33:30 +00:00
|
|
|
|
|
|
|
// GetPack retrieves a pack by ID.
|
2017-01-10 04:40:21 +00:00
|
|
|
GetPack(ctx context.Context, id uint) (pack *Pack, err error)
|
2017-01-11 20:33:30 +00:00
|
|
|
|
|
|
|
// DeletePack deletes a pack record from the datastore.
|
2017-01-10 04:40:21 +00:00
|
|
|
DeletePack(ctx context.Context, id uint) (err error)
|
2016-09-04 05:13:42 +00:00
|
|
|
|
2017-01-11 20:33:30 +00:00
|
|
|
// ListLabelsForPack lists all labels that are associated with a pack.
|
2017-01-10 04:40:21 +00:00
|
|
|
ListLabelsForPack(ctx context.Context, pid uint) (labels []*Label, err error)
|
2016-10-17 19:30:47 +00:00
|
|
|
|
2017-01-11 20:33:30 +00:00
|
|
|
// ListPacksForHost lists the packs that a host should execute.
|
2017-01-10 04:40:21 +00:00
|
|
|
ListPacksForHost(ctx context.Context, hid uint) (packs []*Pack, err error)
|
2017-01-11 20:33:30 +00:00
|
|
|
|
2017-03-01 19:56:13 +00:00
|
|
|
// ListHostsInPack lists the IDs of all hosts that are associated with a pack,
|
|
|
|
// both through labels and manual associations.
|
|
|
|
ListHostsInPack(ctx context.Context, pid uint, opt ListOptions) (hosts []uint, err error)
|
2017-01-11 20:33:30 +00:00
|
|
|
|
2017-03-01 19:56:13 +00:00
|
|
|
// ListExplicitHostsInPack lists the IDs of hosts that have been manually associated
|
2017-01-11 20:33:30 +00:00
|
|
|
// with a query pack.
|
2017-03-01 19:56:13 +00:00
|
|
|
ListExplicitHostsInPack(ctx context.Context, pid uint, opt ListOptions) (hosts []uint, err error)
|
2016-09-04 05:13:42 +00:00
|
|
|
}
|
|
|
|
|
2017-01-11 20:33:30 +00:00
|
|
|
// Pack is the structure which represents an osquery query pack.
|
2016-09-04 05:13:42 +00:00
|
|
|
type Pack struct {
|
2016-11-16 13:47:49 +00:00
|
|
|
UpdateCreateTimestamps
|
|
|
|
DeleteFields
|
2016-11-21 19:49:36 +00:00
|
|
|
ID uint `json:"id"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Description string `json:"description"`
|
|
|
|
Platform string `json:"platform"`
|
2016-11-22 21:56:05 +00:00
|
|
|
Disabled bool `json:"disabled"`
|
2016-11-21 19:49:36 +00:00
|
|
|
}
|
|
|
|
|
2017-01-11 20:33:30 +00:00
|
|
|
// PackPayload is the struct which is used to create/update packs.
|
2016-11-21 19:49:36 +00:00
|
|
|
type PackPayload struct {
|
Add host_ids and label_ids fields to the packs API (#737)
This PR adds the `host_ids` and `label_ids` field to the packs HTTP API so that one can operate on the hosts/labels which a pack is scheduled to be executed on. This replaces (and deletes) the `/api/v1/kolide/packs/123/labels/456` API in favor of `PATCH /api/v1/packs/123` and specifying the `label_ids` field. This also allows for bulk operations.
Consider the following API examples:
## Creating a pack with a known set of hosts and labels
The key addition is the `host_ids` and `label_ids` field in both the request and the response.
### Request
```
POST /api/v1/kolide/packs
```
```json
{
"name": "My new pack",
"description": "The newest of the packs",
"host_ids": [1, 2, 3],
"label_ids": [1, 3, 5]
}
```
### Response
```json
{
"pack": {
"id": 123,
"name": "My new pack",
"description": "The newest of the packs",
"platform": "",
"created_by": 1,
"disabled": false,
"query_count": 0,
"total_hosts_count": 5,
"host_ids": [1, 2, 3],
"label_ids": [1, 3, 5]
}
}
```
## Modifying the hosts and/or labels that a pack is scheduled to execute on
### Request
```
PATCH /api/v1/kolide/packs/123
```
```json
{
"host_ids": [1, 2, 3, 4, 5],
"label_ids": [1, 3, 5, 7]
}
```
### Response
```json
{
"pack": {
"id": 123,
"name": "My new pack",
"description": "The newest of the packs",
"platform": "",
"created_by": 1,
"disabled": false,
"query_count": 0,
"total_hosts_count": 5,
"host_ids": [1, 2, 3, 4, 5],
"label_ids": [1, 3, 5, 7]
}
}
```
close #633
2017-01-03 17:32:06 +00:00
|
|
|
Name *string `json:"name"`
|
|
|
|
Description *string `json:"description"`
|
|
|
|
Platform *string `json:"platform"`
|
|
|
|
Disabled *bool `json:"disabled"`
|
|
|
|
HostIDs *[]uint `json:"host_ids"`
|
|
|
|
LabelIDs *[]uint `json:"label_ids"`
|
2016-09-04 05:13:42 +00:00
|
|
|
}
|
|
|
|
|
2018-01-03 19:18:05 +00:00
|
|
|
type PackSpec struct {
|
|
|
|
ID uint
|
|
|
|
Name string `json:"name"`
|
|
|
|
Description string `json:"description"`
|
|
|
|
Platform string `json:"platform"`
|
|
|
|
Targets PackSpecTargets `json:"targets"`
|
|
|
|
Queries []PackSpecQuery `json:"queries"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type PackSpecTargets struct {
|
|
|
|
Labels []string `json:"labels"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type PackSpecQuery struct {
|
|
|
|
QueryName string `json:"query" db:"query_name"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Description string `json:"description"`
|
|
|
|
Interval uint `json:"interval"`
|
|
|
|
Snapshot *bool `json:"snapshot,omitempty"`
|
|
|
|
Removed *bool `json:"removed,omitempty"`
|
|
|
|
Shard *uint `json:"shard,omitempty"`
|
|
|
|
Platform *string `json:"platform,omitempty"`
|
|
|
|
Version *string `json:"version,omitempty"`
|
|
|
|
}
|
|
|
|
|
2017-01-11 20:33:30 +00:00
|
|
|
// PackTarget associates a pack with either a host or a label
|
2016-09-04 05:13:42 +00:00
|
|
|
type PackTarget struct {
|
2016-11-16 17:48:43 +00:00
|
|
|
ID uint
|
2016-11-16 13:47:49 +00:00
|
|
|
PackID uint
|
2016-11-02 14:59:53 +00:00
|
|
|
Target
|
2016-09-04 05:13:42 +00:00
|
|
|
}
|