2016-09-26 18:48:55 +00:00
|
|
|
package service
|
2016-09-06 21:28:07 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2021-07-02 15:59:42 +00:00
|
|
|
"time"
|
2016-09-06 21:28:07 +00:00
|
|
|
|
2021-08-04 13:40:04 +00:00
|
|
|
"github.com/WatchBeam/clock"
|
|
|
|
"github.com/fleetdm/fleet/v4/server/datastore/mysql"
|
2021-06-26 04:46:51 +00:00
|
|
|
"github.com/fleetdm/fleet/v4/server/fleet"
|
|
|
|
"github.com/fleetdm/fleet/v4/server/mock"
|
|
|
|
"github.com/fleetdm/fleet/v4/server/ptr"
|
|
|
|
"github.com/fleetdm/fleet/v4/server/test"
|
2016-09-06 21:28:07 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
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
|
|
|
"github.com/stretchr/testify/require"
|
2016-09-06 21:28:07 +00:00
|
|
|
)
|
|
|
|
|
2016-10-07 17:26:48 +00:00
|
|
|
func TestListHosts(t *testing.T) {
|
2021-08-04 13:40:04 +00:00
|
|
|
ds := mysql.CreateMySQLDS(t)
|
|
|
|
defer ds.Close()
|
2016-09-06 21:28:07 +00:00
|
|
|
|
2021-06-03 23:24:15 +00:00
|
|
|
svc := newTestService(ds, nil, nil)
|
2016-09-06 21:28:07 +00:00
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
hosts, err := svc.ListHosts(test.UserContext(test.UserAdmin), fleet.HostListOptions{})
|
2016-09-06 21:28:07 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Len(t, hosts, 0)
|
|
|
|
|
2021-08-04 13:40:04 +00:00
|
|
|
storedTime := time.Now().UTC()
|
2021-07-02 15:59:42 +00:00
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
_, err = ds.NewHost(&fleet.Host{
|
2021-08-04 13:40:04 +00:00
|
|
|
Hostname: "foo",
|
|
|
|
SeenTime: storedTime,
|
|
|
|
DetailUpdatedAt: time.Now(),
|
|
|
|
LabelUpdatedAt: time.Now(),
|
2016-09-06 21:28:07 +00:00
|
|
|
})
|
2021-08-04 13:40:04 +00:00
|
|
|
require.NoError(t, err)
|
2016-09-06 21:28:07 +00:00
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
hosts, err = svc.ListHosts(test.UserContext(test.UserAdmin), fleet.HostListOptions{})
|
2021-08-04 13:40:04 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, hosts, 1)
|
|
|
|
format := "%Y-%m-%d %HH:%MM:%SS %Z"
|
|
|
|
assert.Equal(t, storedTime.Format(format), hosts[0].SeenTime.Format(format))
|
2016-09-06 21:28:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeleteHost(t *testing.T) {
|
2021-08-04 13:40:04 +00:00
|
|
|
ds := mysql.CreateMySQLDS(t)
|
|
|
|
defer ds.Close()
|
2016-09-06 21:28:07 +00:00
|
|
|
|
2021-06-03 23:24:15 +00:00
|
|
|
svc := newTestService(ds, nil, nil)
|
2016-09-06 21:28:07 +00:00
|
|
|
|
2021-08-04 13:40:04 +00:00
|
|
|
mockClock := clock.NewMockClock()
|
|
|
|
host := test.NewHost(t, ds, "foo", "192.168.1.10", "1", "1", mockClock.Now())
|
2016-09-06 21:28:07 +00:00
|
|
|
assert.NotZero(t, host.ID)
|
|
|
|
|
2021-08-04 13:40:04 +00:00
|
|
|
err := svc.DeleteHost(test.UserContext(test.UserAdmin), host.ID)
|
2016-09-06 21:28:07 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
filter := fleet.TeamFilter{User: test.UserAdmin}
|
|
|
|
hosts, err := ds.ListHosts(filter, fleet.HostListOptions{})
|
2016-09-06 21:28:07 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Len(t, hosts, 0)
|
|
|
|
}
|
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
|
|
|
|
|
|
|
func TestHostDetails(t *testing.T) {
|
|
|
|
ds := new(mock.Store)
|
2021-06-01 00:07:51 +00:00
|
|
|
svc := &Service{ds: ds}
|
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
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
host := &fleet.Host{ID: 3}
|
|
|
|
expectedLabels := []*fleet.Label{
|
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
|
|
|
{
|
|
|
|
Name: "foobar",
|
|
|
|
Description: "the foobar label",
|
|
|
|
},
|
|
|
|
}
|
2021-06-06 22:07:29 +00:00
|
|
|
ds.ListLabelsForHostFunc = func(hid uint) ([]*fleet.Label, error) {
|
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
|
|
|
return expectedLabels, nil
|
|
|
|
}
|
2021-06-06 22:07:29 +00:00
|
|
|
expectedPacks := []*fleet.Pack{
|
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
|
|
|
{
|
|
|
|
Name: "pack1",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "pack2",
|
|
|
|
},
|
|
|
|
}
|
2021-06-06 22:07:29 +00:00
|
|
|
ds.ListPacksForHostFunc = func(hid uint) ([]*fleet.Pack, error) {
|
2021-05-26 01:53:22 +00:00
|
|
|
return expectedPacks, nil
|
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
|
|
|
}
|
2021-06-06 22:07:29 +00:00
|
|
|
ds.LoadHostSoftwareFunc = func(host *fleet.Host) error {
|
2021-04-26 15:44:22 +00:00
|
|
|
return nil
|
|
|
|
}
|
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
|
|
|
|
2021-06-03 23:24:15 +00:00
|
|
|
hostDetail, err := svc.getHostDetails(test.UserContext(test.UserAdmin), host)
|
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
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, expectedLabels, hostDetail.Labels)
|
|
|
|
assert.Equal(t, expectedPacks, hostDetail.Packs)
|
|
|
|
}
|
2021-05-13 20:09:22 +00:00
|
|
|
|
|
|
|
func TestRefetchHost(t *testing.T) {
|
|
|
|
ds := new(mock.Store)
|
2021-06-03 23:24:15 +00:00
|
|
|
svc := newTestService(ds, nil, nil)
|
2021-05-13 20:09:22 +00:00
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
host := &fleet.Host{ID: 3}
|
2021-05-13 20:09:22 +00:00
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
ds.HostFunc = func(hid uint) (*fleet.Host, error) {
|
2021-05-13 20:09:22 +00:00
|
|
|
return host, nil
|
|
|
|
}
|
2021-06-06 22:07:29 +00:00
|
|
|
ds.SaveHostFunc = func(host *fleet.Host) error {
|
2021-05-13 20:09:22 +00:00
|
|
|
assert.True(t, host.RefetchRequested)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-06-03 23:24:15 +00:00
|
|
|
require.NoError(t, svc.RefetchHost(test.UserContext(test.UserAdmin), host.ID))
|
2021-08-11 22:01:37 +00:00
|
|
|
require.NoError(t, svc.RefetchHost(test.UserContext(test.UserObserver), host.ID))
|
|
|
|
require.NoError(t, svc.RefetchHost(test.UserContext(test.UserMaintainer), host.ID))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRefetchHostUserInTeams(t *testing.T) {
|
|
|
|
ds := new(mock.Store)
|
|
|
|
svc := newTestService(ds, nil, nil)
|
|
|
|
|
|
|
|
host := &fleet.Host{ID: 3, TeamID: ptr.Uint(4)}
|
|
|
|
|
|
|
|
ds.HostFunc = func(hid uint) (*fleet.Host, error) {
|
|
|
|
return host, nil
|
|
|
|
}
|
|
|
|
ds.SaveHostFunc = func(host *fleet.Host) error {
|
|
|
|
assert.True(t, host.RefetchRequested)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
maintainer := &fleet.User{
|
|
|
|
Teams: []fleet.UserTeam{
|
|
|
|
{
|
|
|
|
Team: fleet.Team{ID: 4},
|
|
|
|
Role: fleet.RoleMaintainer,
|
|
|
|
},
|
|
|
|
}}
|
|
|
|
require.NoError(t, svc.RefetchHost(test.UserContext(maintainer), host.ID))
|
|
|
|
|
|
|
|
observer := &fleet.User{
|
|
|
|
Teams: []fleet.UserTeam{
|
|
|
|
{
|
|
|
|
Team: fleet.Team{ID: 4},
|
|
|
|
Role: fleet.RoleObserver,
|
|
|
|
},
|
|
|
|
}}
|
|
|
|
require.NoError(t, svc.RefetchHost(test.UserContext(observer), host.ID))
|
2021-05-13 20:09:22 +00:00
|
|
|
}
|
2021-05-26 04:29:52 +00:00
|
|
|
|
|
|
|
func TestAddHostsToTeamByFilter(t *testing.T) {
|
|
|
|
ds := new(mock.Store)
|
2021-06-03 23:24:15 +00:00
|
|
|
svc := newTestService(ds, nil, nil)
|
2021-05-26 04:29:52 +00:00
|
|
|
|
|
|
|
expectedHostIDs := []uint{1, 2, 4}
|
|
|
|
expectedTeam := (*uint)(nil)
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
ds.ListHostsFunc = func(filter fleet.TeamFilter, opt fleet.HostListOptions) ([]*fleet.Host, error) {
|
|
|
|
var hosts []*fleet.Host
|
2021-05-26 04:29:52 +00:00
|
|
|
for _, id := range expectedHostIDs {
|
2021-06-06 22:07:29 +00:00
|
|
|
hosts = append(hosts, &fleet.Host{ID: id})
|
2021-05-26 04:29:52 +00:00
|
|
|
}
|
|
|
|
return hosts, nil
|
|
|
|
}
|
|
|
|
ds.AddHostsToTeamFunc = func(teamID *uint, hostIDs []uint) error {
|
|
|
|
assert.Equal(t, expectedTeam, teamID)
|
|
|
|
assert.Equal(t, expectedHostIDs, hostIDs)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
require.NoError(t, svc.AddHostsToTeamByFilter(test.UserContext(test.UserAdmin), expectedTeam, fleet.HostListOptions{}, nil))
|
2021-05-26 04:29:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAddHostsToTeamByFilterLabel(t *testing.T) {
|
|
|
|
ds := new(mock.Store)
|
2021-06-03 23:24:15 +00:00
|
|
|
svc := newTestService(ds, nil, nil)
|
2021-05-26 04:29:52 +00:00
|
|
|
|
|
|
|
expectedHostIDs := []uint{6}
|
|
|
|
expectedTeam := ptr.Uint(1)
|
|
|
|
expectedLabel := ptr.Uint(2)
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
ds.ListHostsInLabelFunc = func(filter fleet.TeamFilter, lid uint, opt fleet.HostListOptions) ([]*fleet.Host, error) {
|
2021-05-26 04:29:52 +00:00
|
|
|
assert.Equal(t, *expectedLabel, lid)
|
2021-06-06 22:07:29 +00:00
|
|
|
var hosts []*fleet.Host
|
2021-05-26 04:29:52 +00:00
|
|
|
for _, id := range expectedHostIDs {
|
2021-06-06 22:07:29 +00:00
|
|
|
hosts = append(hosts, &fleet.Host{ID: id})
|
2021-05-26 04:29:52 +00:00
|
|
|
}
|
|
|
|
return hosts, nil
|
|
|
|
}
|
|
|
|
ds.AddHostsToTeamFunc = func(teamID *uint, hostIDs []uint) error {
|
|
|
|
assert.Equal(t, expectedHostIDs, hostIDs)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
require.NoError(t, svc.AddHostsToTeamByFilter(test.UserContext(test.UserAdmin), expectedTeam, fleet.HostListOptions{}, expectedLabel))
|
2021-05-26 04:29:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAddHostsToTeamByFilterEmptyHosts(t *testing.T) {
|
|
|
|
ds := new(mock.Store)
|
2021-06-03 23:24:15 +00:00
|
|
|
svc := newTestService(ds, nil, nil)
|
2021-05-26 04:29:52 +00:00
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
ds.ListHostsFunc = func(filter fleet.TeamFilter, opt fleet.HostListOptions) ([]*fleet.Host, error) {
|
|
|
|
return []*fleet.Host{}, nil
|
2021-05-26 04:29:52 +00:00
|
|
|
}
|
|
|
|
ds.AddHostsToTeamFunc = func(teamID *uint, hostIDs []uint) error {
|
|
|
|
t.Error("add hosts func should not have been called")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
require.NoError(t, svc.AddHostsToTeamByFilter(test.UserContext(test.UserAdmin), nil, fleet.HostListOptions{}, nil))
|
2021-05-26 04:29:52 +00:00
|
|
|
}
|