2016-09-26 18:48:55 +00:00
|
|
|
package service
|
2016-09-06 21:28:07 +00:00
|
|
|
|
|
|
|
import (
|
2017-03-15 15:55:30 +00:00
|
|
|
"context"
|
|
|
|
|
2020-11-11 17:59:12 +00:00
|
|
|
"github.com/fleetdm/fleet/server/kolide"
|
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/pkg/errors"
|
2016-09-06 21:28:07 +00:00
|
|
|
)
|
|
|
|
|
2020-03-30 02:19:54 +00:00
|
|
|
func (svc service) ListHosts(ctx context.Context, opt kolide.HostListOptions) ([]*kolide.Host, error) {
|
2016-10-14 15:59:27 +00:00
|
|
|
return svc.ds.ListHosts(opt)
|
2016-09-06 21:28:07 +00:00
|
|
|
}
|
|
|
|
|
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 (svc service) GetHost(ctx context.Context, id uint) (*kolide.HostDetail, error) {
|
|
|
|
host, err := svc.ds.Host(id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "get host")
|
|
|
|
}
|
|
|
|
|
|
|
|
return svc.getHostDetails(ctx, host)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (svc service) HostByIdentifier(ctx context.Context, identifier string) (*kolide.HostDetail, error) {
|
|
|
|
host, err := svc.ds.HostByIdentifier(identifier)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "get host by identifier")
|
|
|
|
}
|
|
|
|
|
|
|
|
return svc.getHostDetails(ctx, host)
|
2016-09-06 21:28:07 +00:00
|
|
|
}
|
|
|
|
|
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 (svc service) getHostDetails(ctx context.Context, host *kolide.Host) (*kolide.HostDetail, error) {
|
|
|
|
labels, err := svc.ds.ListLabelsForHost(host.ID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "get labels for host")
|
|
|
|
}
|
|
|
|
|
|
|
|
packPtrs, err := svc.ds.ListPacksForHost(host.ID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "get packs for host")
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO refactor List* APIs to be consistent so we don't have to do this
|
|
|
|
// transformation
|
|
|
|
packs := make([]kolide.Pack, 0, len(packPtrs))
|
|
|
|
for _, p := range packPtrs {
|
|
|
|
packs = append(packs, *p)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &kolide.HostDetail{Host: *host, Labels: labels, Packs: packs}, nil
|
2020-04-22 20:54:32 +00:00
|
|
|
}
|
|
|
|
|
2017-01-04 21:16:17 +00:00
|
|
|
func (svc service) GetHostSummary(ctx context.Context) (*kolide.HostSummary, error) {
|
2017-04-18 17:39:50 +00:00
|
|
|
online, offline, mia, new, err := svc.ds.GenerateHostStatusStatistics(svc.clock.Now())
|
2017-01-04 21:16:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2016-10-12 21:41:35 +00:00
|
|
|
}
|
2017-01-04 21:16:17 +00:00
|
|
|
return &kolide.HostSummary{
|
|
|
|
OnlineCount: online,
|
|
|
|
OfflineCount: offline,
|
|
|
|
MIACount: mia,
|
2017-01-20 13:57:47 +00:00
|
|
|
NewCount: new,
|
2017-01-04 21:16:17 +00:00
|
|
|
}, nil
|
2016-10-12 21:41:35 +00:00
|
|
|
}
|
|
|
|
|
2016-09-06 21:28:07 +00:00
|
|
|
func (svc service) DeleteHost(ctx context.Context, id uint) error {
|
2017-01-04 18:18:21 +00:00
|
|
|
return svc.ds.DeleteHost(id)
|
2016-09-06 21:28:07 +00:00
|
|
|
}
|