fleet/server/kolide/packs.go

126 lines
4.3 KiB
Go
Raw Normal View History

package kolide
import (
"context"
)
// PackStore is the datastore interface for managing query packs.
type PackStore interface {
// ApplyPackSpecs applies a list of PackSpecs to the datastore,
// creating and updating packs as necessary.
ApplyPackSpecs(specs []*PackSpec) error
// GetPackSpecs returns all of the stored PackSpecs.
GetPackSpecs() ([]*PackSpec, error)
// DeletePack deletes a pack record from the datastore.
DeletePack(pid uint) error
// Pack retrieves a pack from the datastore by ID.
Pack(pid uint) (*Pack, error)
// ListPacks lists all packs in the datastore.
ListPacks(opt ListOptions) ([]*Pack, error)
// PackByName fetches pack if it exists, if the pack
// exists the bool return value is true
PackByName(name string, opts ...OptionalArg) (*Pack, bool, error)
// ListLabelsForPack lists all labels that are associated with a pack.
ListLabelsForPack(pid uint) ([]*Label, error)
// ListPacksForHost lists the packs that a host should execute.
ListPacksForHost(hid uint) (packs []*Pack, err error)
// ListHostsInPack lists the IDs of all hosts that are associated with a pack
// through labels.
ListHostsInPack(pid uint, opt ListOptions) ([]uint, error)
// ListExplicitHostsInPack lists the IDs of hosts that have been manually
// associated with a query pack.
ListExplicitHostsInPack(pid uint, opt ListOptions) ([]uint, error)
}
// PackService is the service interface for managing query packs.
type PackService interface {
// 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)
// ListPacks lists all packs in the application.
ListPacks(ctx context.Context, opt ListOptions) (packs []*Pack, err error)
// GetPack retrieves a pack by ID.
GetPack(ctx context.Context, id uint) (pack *Pack, err error)
// DeletePack deletes a pack record from the datastore.
DeletePack(ctx context.Context, id uint) (err error)
// ListLabelsForPack lists all labels that are associated with a pack.
ListLabelsForPack(ctx context.Context, pid uint) (labels []*Label, err error)
// ListPacksForHost lists the packs that a host should execute.
ListPacksForHost(ctx context.Context, hid uint) (packs []*Pack, err error)
// 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)
// ListExplicitHostsInPack lists the IDs of hosts that have been manually associated
// with a query pack.
ListExplicitHostsInPack(ctx context.Context, pid uint, opt ListOptions) (hosts []uint, err error)
}
// Pack is the structure which represents an osquery query pack.
type Pack struct {
UpdateCreateTimestamps
DeleteFields
ID uint `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
Platform string `json:"platform"`
Disabled bool `json:"disabled"`
}
// PackPayload is the struct which is used to create/update packs.
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"`
}
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"`
}
// PackTarget associates a pack with either a host or a label
type PackTarget struct {
2016-11-16 17:48:43 +00:00
ID uint
PackID uint
2016-11-02 14:59:53 +00:00
Target
}