2016-08-19 18:24:59 +00:00
|
|
|
package kolide
|
2016-10-03 03:14:35 +00:00
|
|
|
|
|
|
|
import (
|
2017-03-15 15:55:30 +00:00
|
|
|
"context"
|
2016-10-03 03:14:35 +00:00
|
|
|
"time"
|
2020-04-07 22:12:32 +00:00
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2016-10-03 03:14:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type LabelStore interface {
|
2018-05-04 01:01:57 +00:00
|
|
|
// ApplyLabelSpecs applies a list of LabelSpecs to the datastore,
|
2018-01-10 19:38:20 +00:00
|
|
|
// creating and updating labels as necessary.
|
|
|
|
ApplyLabelSpecs(specs []*LabelSpec) error
|
|
|
|
// GetLabelSpecs returns all of the stored LabelSpecs.
|
|
|
|
GetLabelSpecs() ([]*LabelSpec, error)
|
2018-05-08 01:54:29 +00:00
|
|
|
// GetLabelSpec returns the spec for the named label.
|
|
|
|
GetLabelSpec(name string) (*LabelSpec, error)
|
2018-01-10 19:38:20 +00:00
|
|
|
|
2016-10-03 03:14:35 +00:00
|
|
|
// Label methods
|
2018-06-18 17:09:08 +00:00
|
|
|
NewLabel(Label *Label, opts ...OptionalArg) (*Label, error)
|
|
|
|
SaveLabel(label *Label) (*Label, error)
|
2018-05-04 18:05:55 +00:00
|
|
|
DeleteLabel(name string) error
|
2016-10-03 03:14:35 +00:00
|
|
|
Label(lid uint) (*Label, error)
|
2016-10-14 15:59:27 +00:00
|
|
|
ListLabels(opt ListOptions) ([]*Label, error)
|
2016-10-03 03:14:35 +00:00
|
|
|
|
|
|
|
// LabelQueriesForHost returns the label queries that should be executed
|
|
|
|
// for the given host. The cutoff is the minimum timestamp a query
|
|
|
|
// execution should have to be considered "fresh". Executions that are
|
|
|
|
// not fresh will be repeated. Results are returned in a map of label
|
|
|
|
// id -> query
|
|
|
|
LabelQueriesForHost(host *Host, cutoff time.Time) (map[string]string, error)
|
|
|
|
|
|
|
|
// RecordLabelQueryExecutions saves the results of label queries. The
|
|
|
|
// results map is a map of label id -> whether or not the label
|
|
|
|
// matches. The time parameter is the timestamp to save with the query
|
|
|
|
// execution.
|
2017-01-17 06:03:51 +00:00
|
|
|
RecordLabelQueryExecutions(host *Host, results map[uint]bool, t time.Time) error
|
2016-10-03 03:14:35 +00:00
|
|
|
|
|
|
|
// LabelsForHost returns the labels that the given host is in.
|
2016-10-14 15:59:27 +00:00
|
|
|
ListLabelsForHost(hid uint) ([]Label, error)
|
2016-11-02 14:59:53 +00:00
|
|
|
|
2017-02-17 19:19:27 +00:00
|
|
|
// ListHostsInLabel returns a slice of hosts in the label with the
|
|
|
|
// given ID.
|
2021-02-18 20:52:43 +00:00
|
|
|
ListHostsInLabel(lid uint, opt HostListOptions) ([]Host, error)
|
2017-02-17 19:19:27 +00:00
|
|
|
|
|
|
|
// ListUniqueHostsInLabels returns a slice of all of the hosts in the
|
|
|
|
// given label IDs. A host will only appear once in the results even if
|
|
|
|
// it is in multiple of the provided labels.
|
2016-11-02 14:59:53 +00:00
|
|
|
ListUniqueHostsInLabels(labels []uint) ([]Host, error)
|
|
|
|
|
2016-11-16 13:47:49 +00:00
|
|
|
SearchLabels(query string, omit ...uint) ([]Label, error)
|
2018-05-17 22:54:34 +00:00
|
|
|
|
|
|
|
// LabelIDsByName Retrieve the IDs associated with the given labels
|
|
|
|
LabelIDsByName(labels []string) ([]uint, error)
|
2016-10-03 03:14:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type LabelService interface {
|
2018-05-04 01:01:57 +00:00
|
|
|
// ApplyLabelSpecs applies a list of LabelSpecs to the datastore,
|
2018-01-10 19:38:20 +00:00
|
|
|
// creating and updating labels as necessary.
|
|
|
|
ApplyLabelSpecs(ctx context.Context, specs []*LabelSpec) error
|
|
|
|
// GetLabelSpecs returns all of the stored LabelSpecs.
|
|
|
|
GetLabelSpecs(ctx context.Context) ([]*LabelSpec, error)
|
2018-05-08 01:54:29 +00:00
|
|
|
// GetLabelSpec gets the spec for the label with the given name.
|
|
|
|
GetLabelSpec(ctx context.Context, name string) (*LabelSpec, error)
|
2018-01-10 19:38:20 +00:00
|
|
|
|
2018-06-18 17:09:08 +00:00
|
|
|
NewLabel(ctx context.Context, p LabelPayload) (label *Label, err error)
|
|
|
|
ModifyLabel(ctx context.Context, id uint, payload ModifyLabelPayload) (*Label, error)
|
2017-01-10 04:40:21 +00:00
|
|
|
ListLabels(ctx context.Context, opt ListOptions) (labels []*Label, err error)
|
|
|
|
GetLabel(ctx context.Context, id uint) (label *Label, err error)
|
2018-05-04 18:05:55 +00:00
|
|
|
|
|
|
|
DeleteLabel(ctx context.Context, name string) (err error)
|
2018-06-25 20:56:59 +00:00
|
|
|
// DeleteLabelByID is for backwards compatibility with the UI
|
|
|
|
DeleteLabelByID(ctx context.Context, id uint) (err error)
|
2017-02-12 04:27:43 +00:00
|
|
|
|
2020-03-30 02:19:54 +00:00
|
|
|
// ListHostsInLabel returns a slice of hosts in the label with the
|
|
|
|
// given ID.
|
2021-02-18 20:52:43 +00:00
|
|
|
ListHostsInLabel(ctx context.Context, lid uint, opt HostListOptions) ([]Host, error)
|
2020-03-30 02:19:54 +00:00
|
|
|
|
2020-04-22 20:54:32 +00:00
|
|
|
// LabelsForHost returns the labels that the given host is in.
|
|
|
|
ListLabelsForHost(ctx context.Context, hid uint) ([]Label, error)
|
|
|
|
|
2017-01-16 22:57:05 +00:00
|
|
|
// HostIDsForLabel returns ids of hosts that belong to the label identified
|
|
|
|
// by lid
|
|
|
|
HostIDsForLabel(lid uint) ([]uint, error)
|
2016-10-03 03:14:35 +00:00
|
|
|
}
|
|
|
|
|
2018-06-18 17:09:08 +00:00
|
|
|
// ModifyLabelPayload is used to change editable fields for a Label
|
|
|
|
type ModifyLabelPayload struct {
|
|
|
|
Name *string `json:"name"`
|
|
|
|
Description *string `json:"description"`
|
|
|
|
}
|
|
|
|
|
2016-10-03 03:14:35 +00:00
|
|
|
type LabelPayload struct {
|
2016-11-18 00:51:30 +00:00
|
|
|
Name *string `json:"name"`
|
2016-11-03 01:17:23 +00:00
|
|
|
Query *string `json:"query"`
|
|
|
|
Platform *string `json:"platform"`
|
|
|
|
Description *string `json:"description"`
|
2016-10-03 03:14:35 +00:00
|
|
|
}
|
|
|
|
|
2016-11-25 18:08:22 +00:00
|
|
|
// LabelType is used to catagorize the kind of label
|
|
|
|
type LabelType uint
|
|
|
|
|
|
|
|
const (
|
2017-01-09 20:02:21 +00:00
|
|
|
// LabelTypeRegular is for user created labels that can be modified.
|
|
|
|
LabelTypeRegular LabelType = iota
|
2019-01-24 17:39:32 +00:00
|
|
|
// LabelTypeBuiltIn is for labels built into Fleet that cannot be
|
2017-01-09 20:02:21 +00:00
|
|
|
// modified by users.
|
2016-11-25 18:08:22 +00:00
|
|
|
LabelTypeBuiltIn
|
|
|
|
)
|
|
|
|
|
2020-04-07 22:12:32 +00:00
|
|
|
func (t LabelType) MarshalJSON() ([]byte, error) {
|
|
|
|
switch t {
|
|
|
|
case LabelTypeRegular:
|
|
|
|
return []byte(`"regular"`), nil
|
|
|
|
case LabelTypeBuiltIn:
|
|
|
|
return []byte(`"builtin"`), nil
|
|
|
|
default:
|
|
|
|
return nil, errors.Errorf("invalid LabelType: %d", t)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *LabelType) UnmarshalJSON(b []byte) error {
|
|
|
|
switch string(b) {
|
2020-08-06 23:07:13 +00:00
|
|
|
case `"regular"`, "0":
|
2020-04-07 22:12:32 +00:00
|
|
|
*t = LabelTypeRegular
|
2020-08-06 23:07:13 +00:00
|
|
|
case `"builtin"`, "1":
|
2020-04-07 22:12:32 +00:00
|
|
|
*t = LabelTypeBuiltIn
|
|
|
|
default:
|
|
|
|
return errors.Errorf("invalid LabelType: %s", string(b))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// LabelMembershipType sets how the membership of the label is determined.
|
|
|
|
type LabelMembershipType uint
|
|
|
|
|
|
|
|
const (
|
|
|
|
// LabelTypeDynamic indicates that the label is populated dynamically (by
|
|
|
|
// the execution of a label query).
|
|
|
|
LabelMembershipTypeDynamic LabelMembershipType = iota
|
|
|
|
// LabelTypeManual indicates that the label is populated manually.
|
|
|
|
LabelMembershipTypeManual
|
|
|
|
)
|
|
|
|
|
|
|
|
func (t LabelMembershipType) MarshalJSON() ([]byte, error) {
|
|
|
|
switch t {
|
|
|
|
case LabelMembershipTypeDynamic:
|
|
|
|
return []byte(`"dynamic"`), nil
|
|
|
|
case LabelMembershipTypeManual:
|
|
|
|
return []byte(`"manual"`), nil
|
|
|
|
default:
|
|
|
|
return nil, errors.Errorf("invalid LabelMembershipType: %d", t)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *LabelMembershipType) UnmarshalJSON(b []byte) error {
|
|
|
|
switch string(b) {
|
|
|
|
case `"dynamic"`:
|
|
|
|
*t = LabelMembershipTypeDynamic
|
|
|
|
case `"manual"`:
|
|
|
|
*t = LabelMembershipTypeManual
|
|
|
|
default:
|
|
|
|
return errors.Errorf("invalid LabelMembershipType: %s", string(b))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-10-03 03:14:35 +00:00
|
|
|
type Label struct {
|
2016-11-16 13:47:49 +00:00
|
|
|
UpdateCreateTimestamps
|
2020-04-07 22:12:32 +00:00
|
|
|
ID uint `json:"id"`
|
|
|
|
Name string `json:"name"`
|
Add host details in API responses (#223)
Add label and pack information for the returned hosts in the single-host
API endpoints.
Example:
```
curl -k 'https://localhost:8080/api/v1/kolide/hosts/7' -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzZXNzaW9uX2tleSI6Ii9oNEZ4MUpEVmlvQWhtMC8wNUJKbzZpdldsUDZpMDhjQVBuZXRLeFIvWjNOUGgvMW9VdCsxQnFlNU1CVDVsMlU3ckVGMm5Sb1VxS3ZSUllzSmJJR2lBPT0ifQ.GQQsJgBU3JA1H1o4Y8fPjyfF78F_VY4c9AbrP5k0sCg'
{
"host": {
"created_at": "2021-01-16T00:22:33Z",
"updated_at": "2021-01-16T00:22:51Z",
"id": 7,
"detail_updated_at": "1970-01-02T00:00:00Z",
"label_updated_at": "1970-01-02T00:00:00Z",
"last_enrolled_at": "2021-01-16T00:22:33Z",
"seen_time": "2021-01-16T00:22:51Z",
"hostname": "55d91fc9c303",
"uuid": "853a4588-0000-0000-a061-7d494d04e9c4",
"platform": "ubuntu",
"osquery_version": "4.6.0",
"os_version": "Ubuntu 20.04.0",
"build": "",
"platform_like": "debian",
"code_name": "",
"uptime": 0,
"memory": 16794206208,
"cpu_type": "x86_64",
"cpu_subtype": "158",
"cpu_brand": "Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz\u0000\u0000\u0000\u0000\u0000\u0000\u0000",
"cpu_physical_cores": 8,
"cpu_logical_cores": 8,
"hardware_vendor": "",
"hardware_model": "",
"hardware_version": "",
"hardware_serial": "",
"computer_name": "55d91fc9c303",
"primary_ip": "",
"primary_mac": "",
"distributed_interval": 10,
"config_tls_refresh": 0,
"logger_tls_period": 10,
"enroll_secret_name": "default",
"labels": [
{
"created_at": "2020-12-22T01:22:47Z",
"updated_at": "2020-12-22T01:22:47Z",
"id": 6,
"name": "All Hosts",
"description": "All hosts which have enrolled in Fleet",
"query": "select 1;",
"label_type": "builtin",
"label_membership_type": "dynamic"
}
],
"packs": [
{
"created_at": "2021-01-20T16:36:42Z",
"updated_at": "2021-01-20T16:36:42Z",
"id": 2,
"name": "test"
}
],
"status": "offline",
"display_text": "55d91fc9c303"
}
}
```
2021-01-25 21:05:02 +00:00
|
|
|
Description string `json:"description,omitempty"`
|
2020-04-07 22:12:32 +00:00
|
|
|
Query string `json:"query"`
|
Add host details in API responses (#223)
Add label and pack information for the returned hosts in the single-host
API endpoints.
Example:
```
curl -k 'https://localhost:8080/api/v1/kolide/hosts/7' -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzZXNzaW9uX2tleSI6Ii9oNEZ4MUpEVmlvQWhtMC8wNUJKbzZpdldsUDZpMDhjQVBuZXRLeFIvWjNOUGgvMW9VdCsxQnFlNU1CVDVsMlU3ckVGMm5Sb1VxS3ZSUllzSmJJR2lBPT0ifQ.GQQsJgBU3JA1H1o4Y8fPjyfF78F_VY4c9AbrP5k0sCg'
{
"host": {
"created_at": "2021-01-16T00:22:33Z",
"updated_at": "2021-01-16T00:22:51Z",
"id": 7,
"detail_updated_at": "1970-01-02T00:00:00Z",
"label_updated_at": "1970-01-02T00:00:00Z",
"last_enrolled_at": "2021-01-16T00:22:33Z",
"seen_time": "2021-01-16T00:22:51Z",
"hostname": "55d91fc9c303",
"uuid": "853a4588-0000-0000-a061-7d494d04e9c4",
"platform": "ubuntu",
"osquery_version": "4.6.0",
"os_version": "Ubuntu 20.04.0",
"build": "",
"platform_like": "debian",
"code_name": "",
"uptime": 0,
"memory": 16794206208,
"cpu_type": "x86_64",
"cpu_subtype": "158",
"cpu_brand": "Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz\u0000\u0000\u0000\u0000\u0000\u0000\u0000",
"cpu_physical_cores": 8,
"cpu_logical_cores": 8,
"hardware_vendor": "",
"hardware_model": "",
"hardware_version": "",
"hardware_serial": "",
"computer_name": "55d91fc9c303",
"primary_ip": "",
"primary_mac": "",
"distributed_interval": 10,
"config_tls_refresh": 0,
"logger_tls_period": 10,
"enroll_secret_name": "default",
"labels": [
{
"created_at": "2020-12-22T01:22:47Z",
"updated_at": "2020-12-22T01:22:47Z",
"id": 6,
"name": "All Hosts",
"description": "All hosts which have enrolled in Fleet",
"query": "select 1;",
"label_type": "builtin",
"label_membership_type": "dynamic"
}
],
"packs": [
{
"created_at": "2021-01-20T16:36:42Z",
"updated_at": "2021-01-20T16:36:42Z",
"id": 2,
"name": "test"
}
],
"status": "offline",
"display_text": "55d91fc9c303"
}
}
```
2021-01-25 21:05:02 +00:00
|
|
|
Platform string `json:"platform,omitempty"`
|
2020-04-07 22:12:32 +00:00
|
|
|
LabelType LabelType `json:"label_type" db:"label_type"`
|
|
|
|
LabelMembershipType LabelMembershipType `json:"label_membership_type" db:"label_membership_type"`
|
Add host details in API responses (#223)
Add label and pack information for the returned hosts in the single-host
API endpoints.
Example:
```
curl -k 'https://localhost:8080/api/v1/kolide/hosts/7' -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzZXNzaW9uX2tleSI6Ii9oNEZ4MUpEVmlvQWhtMC8wNUJKbzZpdldsUDZpMDhjQVBuZXRLeFIvWjNOUGgvMW9VdCsxQnFlNU1CVDVsMlU3ckVGMm5Sb1VxS3ZSUllzSmJJR2lBPT0ifQ.GQQsJgBU3JA1H1o4Y8fPjyfF78F_VY4c9AbrP5k0sCg'
{
"host": {
"created_at": "2021-01-16T00:22:33Z",
"updated_at": "2021-01-16T00:22:51Z",
"id": 7,
"detail_updated_at": "1970-01-02T00:00:00Z",
"label_updated_at": "1970-01-02T00:00:00Z",
"last_enrolled_at": "2021-01-16T00:22:33Z",
"seen_time": "2021-01-16T00:22:51Z",
"hostname": "55d91fc9c303",
"uuid": "853a4588-0000-0000-a061-7d494d04e9c4",
"platform": "ubuntu",
"osquery_version": "4.6.0",
"os_version": "Ubuntu 20.04.0",
"build": "",
"platform_like": "debian",
"code_name": "",
"uptime": 0,
"memory": 16794206208,
"cpu_type": "x86_64",
"cpu_subtype": "158",
"cpu_brand": "Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz\u0000\u0000\u0000\u0000\u0000\u0000\u0000",
"cpu_physical_cores": 8,
"cpu_logical_cores": 8,
"hardware_vendor": "",
"hardware_model": "",
"hardware_version": "",
"hardware_serial": "",
"computer_name": "55d91fc9c303",
"primary_ip": "",
"primary_mac": "",
"distributed_interval": 10,
"config_tls_refresh": 0,
"logger_tls_period": 10,
"enroll_secret_name": "default",
"labels": [
{
"created_at": "2020-12-22T01:22:47Z",
"updated_at": "2020-12-22T01:22:47Z",
"id": 6,
"name": "All Hosts",
"description": "All hosts which have enrolled in Fleet",
"query": "select 1;",
"label_type": "builtin",
"label_membership_type": "dynamic"
}
],
"packs": [
{
"created_at": "2021-01-20T16:36:42Z",
"updated_at": "2021-01-20T16:36:42Z",
"id": 2,
"name": "test"
}
],
"status": "offline",
"display_text": "55d91fc9c303"
}
}
```
2021-01-25 21:05:02 +00:00
|
|
|
HostCount int `json:"host_count,omitempty" db:"host_count"`
|
2016-10-03 03:14:35 +00:00
|
|
|
}
|
|
|
|
|
2020-10-08 17:16:07 +00:00
|
|
|
const (
|
|
|
|
LabelKind = "label"
|
|
|
|
)
|
|
|
|
|
2016-10-03 03:14:35 +00:00
|
|
|
type LabelQueryExecution struct {
|
2016-11-16 13:47:49 +00:00
|
|
|
ID uint
|
2016-10-03 03:14:35 +00:00
|
|
|
UpdatedAt time.Time
|
|
|
|
Matches bool
|
2016-11-16 13:47:49 +00:00
|
|
|
LabelID uint
|
|
|
|
HostID uint
|
2016-10-03 03:14:35 +00:00
|
|
|
}
|
2018-01-10 19:38:20 +00:00
|
|
|
|
|
|
|
type LabelSpec struct {
|
2020-04-07 22:12:32 +00:00
|
|
|
ID uint
|
|
|
|
Name string `json:"name"`
|
|
|
|
Description string `json:"description"`
|
|
|
|
Query string `json:"query"`
|
|
|
|
Platform string `json:"platform,omitempty"`
|
|
|
|
LabelType LabelType `json:"label_type,omitempty" db:"label_type"`
|
|
|
|
LabelMembershipType LabelMembershipType `json:"label_membership_type" db:"label_membership_type"`
|
|
|
|
Hosts []string `json:"hosts,omitempty"`
|
2018-01-10 19:38:20 +00:00
|
|
|
}
|