2016-10-06 00:10:44 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
2017-03-15 15:55:30 +00:00
|
|
|
"context"
|
2016-12-06 17:37:22 +00:00
|
|
|
"time"
|
|
|
|
|
2021-06-26 04:46:51 +00:00
|
|
|
"github.com/fleetdm/fleet/v4/server/fleet"
|
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/go-kit/kit/endpoint"
|
2016-10-06 00:10:44 +00:00
|
|
|
)
|
|
|
|
|
2020-03-31 23:14:26 +00:00
|
|
|
// HostResponse is the response struct that contains the full host information
|
|
|
|
// along with the host online status and the "display text" to be used when
|
|
|
|
// rendering in the UI.
|
|
|
|
type HostResponse struct {
|
2021-06-06 22:07:29 +00:00
|
|
|
*fleet.Host
|
|
|
|
Status fleet.HostStatus `json:"status"`
|
2021-06-24 00:32:19 +00:00
|
|
|
DisplayText string `json:"display_text"`
|
2021-06-06 22:07:29 +00:00
|
|
|
Labels []fleet.Label `json:"labels,omitempty"`
|
2017-01-10 05:05:18 +00:00
|
|
|
}
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func hostResponseForHost(ctx context.Context, svc fleet.Service, host *fleet.Host) (*HostResponse, error) {
|
2020-03-31 23:14:26 +00:00
|
|
|
return &HostResponse{
|
2021-05-26 01:53:22 +00:00
|
|
|
Host: host,
|
2017-04-18 17:39:50 +00:00
|
|
|
Status: host.Status(time.Now()),
|
2021-06-24 00:32:19 +00:00
|
|
|
DisplayText: host.Hostname,
|
2017-01-10 05:05:18 +00:00
|
|
|
}, nil
|
2016-10-12 21:41:35 +00:00
|
|
|
}
|
|
|
|
|
2021-08-31 15:37:03 +00:00
|
|
|
// HostDetailResponse is the response struct that contains the full host information
|
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
|
|
|
// with the HostDetail details.
|
|
|
|
type HostDetailResponse struct {
|
2021-06-06 22:07:29 +00:00
|
|
|
fleet.HostDetail
|
|
|
|
Status fleet.HostStatus `json:"status"`
|
2021-06-24 00:32:19 +00:00
|
|
|
DisplayText string `json:"display_text"`
|
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
|
|
|
func hostDetailResponseForHost(ctx context.Context, svc fleet.Service, host *fleet.HostDetail) (*HostDetailResponse, 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 &HostDetailResponse{
|
|
|
|
HostDetail: *host,
|
2020-04-22 20:54:32 +00:00
|
|
|
Status: host.Status(time.Now()),
|
2021-06-24 00:32:19 +00:00
|
|
|
DisplayText: host.Hostname,
|
2020-04-22 20:54:32 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2016-10-06 00:10:44 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Get Host
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
type getHostRequest struct {
|
|
|
|
ID uint `json:"id"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type getHostResponse struct {
|
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
|
|
|
Host *HostDetailResponse `json:"host"`
|
|
|
|
Err error `json:"error,omitempty"`
|
2016-10-06 00:10:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r getHostResponse) error() error { return r.Err }
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func makeGetHostEndpoint(svc fleet.Service) endpoint.Endpoint {
|
2016-10-06 00:10:44 +00:00
|
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
|
|
|
req := request.(getHostRequest)
|
|
|
|
host, err := svc.GetHost(ctx, req.ID)
|
|
|
|
if err != nil {
|
|
|
|
return getHostResponse{Err: err}, nil
|
|
|
|
}
|
2017-01-10 05:05:18 +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
|
|
|
resp, err := hostDetailResponseForHost(ctx, svc, host)
|
2017-01-10 05:05:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return getHostResponse{Err: err}, nil
|
|
|
|
}
|
|
|
|
|
2017-01-04 21:16:17 +00:00
|
|
|
return getHostResponse{
|
2017-01-10 05:05:18 +00:00
|
|
|
Host: resp,
|
2017-01-04 21:16:17 +00:00
|
|
|
}, nil
|
2016-10-06 00:10:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-22 20:54:32 +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
|
|
|
// Get Host By Identifier
|
2020-04-22 20:54:32 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
type hostByIdentifierRequest struct {
|
|
|
|
Identifier string `json:"identifier"`
|
|
|
|
}
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func makeHostByIdentifierEndpoint(svc fleet.Service) endpoint.Endpoint {
|
2020-04-22 20:54:32 +00:00
|
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
|
|
|
req := request.(hostByIdentifierRequest)
|
|
|
|
host, err := svc.HostByIdentifier(ctx, req.Identifier)
|
|
|
|
if err != nil {
|
|
|
|
return getHostResponse{Err: err}, 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
|
|
|
resp, err := hostDetailResponseForHost(ctx, svc, host)
|
2020-04-22 20:54:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return getHostResponse{Err: err}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return getHostResponse{
|
|
|
|
Host: resp,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-06 00:10:44 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// List Hosts
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-10-13 18:21:47 +00:00
|
|
|
type listHostsRequest struct {
|
2021-06-06 22:07:29 +00:00
|
|
|
ListOptions fleet.HostListOptions
|
2016-10-13 18:21:47 +00:00
|
|
|
}
|
|
|
|
|
2016-10-07 17:26:48 +00:00
|
|
|
type listHostsResponse struct {
|
2020-03-31 23:14:26 +00:00
|
|
|
Hosts []HostResponse `json:"hosts"`
|
2016-10-12 21:41:35 +00:00
|
|
|
Err error `json:"error,omitempty"`
|
2016-10-06 00:10:44 +00:00
|
|
|
}
|
|
|
|
|
2016-10-07 17:26:48 +00:00
|
|
|
func (r listHostsResponse) error() error { return r.Err }
|
2016-10-06 00:10:44 +00:00
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func makeListHostsEndpoint(svc fleet.Service) endpoint.Endpoint {
|
2016-10-06 00:10:44 +00:00
|
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
2016-10-13 18:21:47 +00:00
|
|
|
req := request.(listHostsRequest)
|
|
|
|
hosts, err := svc.ListHosts(ctx, req.ListOptions)
|
2016-10-06 00:10:44 +00:00
|
|
|
if err != nil {
|
2016-10-07 17:26:48 +00:00
|
|
|
return listHostsResponse{Err: err}, nil
|
2016-10-06 00:10:44 +00:00
|
|
|
}
|
|
|
|
|
2020-03-31 23:14:26 +00:00
|
|
|
hostResponses := make([]HostResponse, len(hosts))
|
2017-01-10 05:05:18 +00:00
|
|
|
for i, host := range hosts {
|
2017-04-18 17:39:50 +00:00
|
|
|
h, err := hostResponseForHost(ctx, svc, host)
|
2017-01-10 05:05:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return listHostsResponse{Err: err}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
hostResponses[i] = *h
|
2017-01-04 21:16:17 +00:00
|
|
|
}
|
2017-01-10 05:05:18 +00:00
|
|
|
return listHostsResponse{Hosts: hostResponses}, nil
|
2017-01-04 21:16:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Get Host Summary
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
type getHostSummaryResponse struct {
|
2021-06-06 22:07:29 +00:00
|
|
|
fleet.HostSummary
|
2017-01-04 21:16:17 +00:00
|
|
|
Err error `json:"error,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r getHostSummaryResponse) error() error { return r.Err }
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func makeGetHostSummaryEndpoint(svc fleet.Service) endpoint.Endpoint {
|
2017-01-04 21:16:17 +00:00
|
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
|
|
|
summary, err := svc.GetHostSummary(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return getHostSummaryResponse{Err: err}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := getHostSummaryResponse{
|
|
|
|
HostSummary: *summary,
|
2016-10-06 00:10:44 +00:00
|
|
|
}
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Delete Host
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
type deleteHostRequest struct {
|
|
|
|
ID uint `json:"id"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type deleteHostResponse struct {
|
|
|
|
Err error `json:"error,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r deleteHostResponse) error() error { return r.Err }
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func makeDeleteHostEndpoint(svc fleet.Service) endpoint.Endpoint {
|
2016-10-06 00:10:44 +00:00
|
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
|
|
|
req := request.(deleteHostRequest)
|
|
|
|
err := svc.DeleteHost(ctx, req.ID)
|
|
|
|
if err != nil {
|
|
|
|
return deleteHostResponse{Err: err}, nil
|
|
|
|
}
|
|
|
|
return deleteHostResponse{}, nil
|
|
|
|
}
|
|
|
|
}
|
2021-05-17 19:23:21 +00:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Add Hosts to Team
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
type addHostsToTeamRequest struct {
|
2021-05-26 04:29:52 +00:00
|
|
|
TeamID *uint `json:"team_id"`
|
2021-05-17 19:23:21 +00:00
|
|
|
HostIDs []uint `json:"hosts"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type addHostsToTeamResponse struct {
|
|
|
|
Err error `json:"error,omitempty"`
|
|
|
|
}
|
|
|
|
|
2021-05-26 04:29:52 +00:00
|
|
|
func (r addHostsToTeamResponse) error() error { return r.Err }
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func makeAddHostsToTeamEndpoint(svc fleet.Service) endpoint.Endpoint {
|
2021-05-17 19:23:21 +00:00
|
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
|
|
|
req := request.(addHostsToTeamRequest)
|
2021-05-26 04:29:52 +00:00
|
|
|
err := svc.AddHostsToTeam(ctx, req.TeamID, req.HostIDs)
|
2021-05-17 19:23:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return addHostsToTeamResponse{Err: err}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return addHostsToTeamResponse{}, err
|
|
|
|
}
|
|
|
|
}
|
2021-05-19 17:16:54 +00:00
|
|
|
|
2021-05-26 04:29:52 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Add Hosts to Team by Filter
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
type addHostsToTeamByFilterRequest struct {
|
|
|
|
TeamID *uint `json:"team_id"`
|
|
|
|
Filters struct {
|
2021-06-24 00:32:19 +00:00
|
|
|
MatchQuery string `json:"query"`
|
2021-06-06 22:07:29 +00:00
|
|
|
Status fleet.HostStatus `json:"status"`
|
2021-06-24 00:32:19 +00:00
|
|
|
LabelID *uint `json:"label_id"`
|
2021-05-26 04:29:52 +00:00
|
|
|
} `json:"filters"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type addHostsToTeamByFilterResponse struct {
|
|
|
|
Err error `json:"error,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r addHostsToTeamByFilterResponse) error() error { return r.Err }
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func makeAddHostsToTeamByFilterEndpoint(svc fleet.Service) endpoint.Endpoint {
|
2021-05-26 04:29:52 +00:00
|
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
|
|
|
req := request.(addHostsToTeamByFilterRequest)
|
2021-06-06 22:07:29 +00:00
|
|
|
listOpt := fleet.HostListOptions{
|
|
|
|
ListOptions: fleet.ListOptions{
|
2021-05-26 04:29:52 +00:00
|
|
|
MatchQuery: req.Filters.MatchQuery,
|
|
|
|
},
|
|
|
|
StatusFilter: req.Filters.Status,
|
|
|
|
}
|
|
|
|
err := svc.AddHostsToTeamByFilter(ctx, req.TeamID, listOpt, req.Filters.LabelID)
|
|
|
|
if err != nil {
|
|
|
|
return addHostsToTeamByFilterResponse{Err: err}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return addHostsToTeamByFilterResponse{}, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-13 20:09:22 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Refetch Host
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
type refetchHostRequest struct {
|
|
|
|
ID uint `json:"id"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type refetchHostResponse struct {
|
|
|
|
Err error `json:"error,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r refetchHostResponse) error() error { return r.Err }
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func makeRefetchHostEndpoint(svc fleet.Service) endpoint.Endpoint {
|
2021-05-13 20:09:22 +00:00
|
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
|
|
|
req := request.(refetchHostRequest)
|
|
|
|
err := svc.RefetchHost(ctx, req.ID)
|
|
|
|
if err != nil {
|
|
|
|
return refetchHostResponse{Err: err}, nil
|
|
|
|
}
|
|
|
|
return refetchHostResponse{}, nil
|
|
|
|
}
|
|
|
|
}
|