2019-01-03 22:19:23 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2020-10-01 23:14:18 +00:00
|
|
|
"fmt"
|
2019-01-03 22:19:23 +00:00
|
|
|
"net/http"
|
|
|
|
|
2021-07-21 17:03:10 +00:00
|
|
|
"github.com/fleetdm/fleet/v4/server/fleet"
|
|
|
|
"github.com/fleetdm/fleet/v4/server/ptr"
|
2019-01-03 22:19:23 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
// GetHosts retrieves the list of all Hosts
|
2020-03-31 23:14:26 +00:00
|
|
|
func (c *Client) GetHosts() ([]HostResponse, error) {
|
2021-02-10 20:13:11 +00:00
|
|
|
response, err := c.AuthenticatedDo("GET", "/api/v1/fleet/hosts", "", nil)
|
2019-01-03 22:19:23 +00:00
|
|
|
if err != nil {
|
2021-02-10 20:13:11 +00:00
|
|
|
return nil, errors.Wrap(err, "GET /api/v1/fleet/hosts")
|
2019-01-03 22:19:23 +00:00
|
|
|
}
|
|
|
|
defer response.Body.Close()
|
|
|
|
|
|
|
|
if response.StatusCode != http.StatusOK {
|
|
|
|
return nil, errors.Errorf(
|
|
|
|
"get hosts received status %d %s",
|
|
|
|
response.StatusCode,
|
|
|
|
extractServerErrorText(response.Body),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
var responseBody listHostsResponse
|
|
|
|
err = json.NewDecoder(response.Body).Decode(&responseBody)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "decode list hosts response")
|
|
|
|
}
|
|
|
|
if responseBody.Err != nil {
|
|
|
|
return nil, errors.Errorf("list hosts: %s", responseBody.Err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return responseBody.Hosts, nil
|
|
|
|
}
|
2020-04-22 20:54:32 +00:00
|
|
|
|
|
|
|
// HostByIdentifier retrieves a host by the uuid, osquery_host_id, hostname, or
|
|
|
|
// node_key.
|
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 (c *Client) HostByIdentifier(identifier string) (*HostDetailResponse, error) {
|
2021-02-10 20:13:11 +00:00
|
|
|
response, err := c.AuthenticatedDo("GET", "/api/v1/fleet/hosts/identifier/"+identifier, "", nil)
|
2020-04-22 20:54:32 +00:00
|
|
|
if err != nil {
|
2021-02-10 20:13:11 +00:00
|
|
|
return nil, errors.Wrap(err, "GET /api/v1/fleet/hosts/identifier")
|
2020-04-22 20:54:32 +00:00
|
|
|
}
|
|
|
|
defer response.Body.Close()
|
|
|
|
|
|
|
|
if response.StatusCode != http.StatusOK {
|
|
|
|
return nil, errors.Errorf(
|
|
|
|
"get host by identifier received status %d %s",
|
|
|
|
response.StatusCode,
|
|
|
|
extractServerErrorText(response.Body),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
var responseBody getHostResponse
|
|
|
|
err = json.NewDecoder(response.Body).Decode(&responseBody)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "decode host response")
|
|
|
|
}
|
|
|
|
if responseBody.Err != nil {
|
|
|
|
return nil, errors.Errorf("get host by identifier: %s", responseBody.Err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return responseBody.Host, nil
|
|
|
|
}
|
2020-10-01 23:14:18 +00:00
|
|
|
|
|
|
|
// DeleteHost deletes the host with the matching id.
|
|
|
|
func (c *Client) DeleteHost(id uint) error {
|
|
|
|
verb := "DELETE"
|
2021-02-10 20:13:11 +00:00
|
|
|
path := fmt.Sprintf("/api/v1/fleet/hosts/%d", id)
|
2020-11-05 04:45:16 +00:00
|
|
|
response, err := c.AuthenticatedDo(verb, path, "", nil)
|
2020-10-01 23:14:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "%s %s", verb, path)
|
|
|
|
}
|
|
|
|
defer response.Body.Close()
|
|
|
|
|
|
|
|
switch response.StatusCode {
|
|
|
|
case http.StatusNotFound:
|
|
|
|
return notFoundErr{}
|
|
|
|
}
|
|
|
|
if response.StatusCode != http.StatusOK {
|
|
|
|
return errors.Errorf(
|
|
|
|
"delete host received status %d %s",
|
|
|
|
response.StatusCode,
|
|
|
|
extractServerErrorText(response.Body),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
var responseBody deleteHostResponse
|
|
|
|
err = json.NewDecoder(response.Body).Decode(&responseBody)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "decode delete host response")
|
|
|
|
}
|
|
|
|
|
|
|
|
if responseBody.Err != nil {
|
|
|
|
return errors.Errorf("delete host: %s", responseBody.Err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2021-07-21 17:03:10 +00:00
|
|
|
|
|
|
|
func (c *Client) translateTransferHostsToIDs(hosts []string, label string, team string) ([]uint, uint, uint, error) {
|
|
|
|
verb, path := "POST", "/api/v1/fleet/translate"
|
|
|
|
var responseBody translatorResponse
|
|
|
|
|
|
|
|
var translatePayloads []fleet.TranslatePayload
|
|
|
|
for _, host := range hosts {
|
|
|
|
translatedPayload, err := encodeTranslatedPayload(fleet.TranslatorTypeHost, host)
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, 0, err
|
|
|
|
}
|
|
|
|
translatePayloads = append(translatePayloads, translatedPayload)
|
|
|
|
}
|
|
|
|
|
|
|
|
if label != "" {
|
|
|
|
translatedPayload, err := encodeTranslatedPayload(fleet.TranslatorTypeLabel, label)
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, 0, err
|
|
|
|
}
|
|
|
|
translatePayloads = append(translatePayloads, translatedPayload)
|
|
|
|
}
|
|
|
|
|
|
|
|
translatedPayload, err := encodeTranslatedPayload(fleet.TranslatorTypeTeam, team)
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, 0, err
|
|
|
|
}
|
|
|
|
translatePayloads = append(translatePayloads, translatedPayload)
|
|
|
|
|
|
|
|
params := translatorRequest{List: translatePayloads}
|
|
|
|
|
|
|
|
err = c.authenticatedRequest(¶ms, verb, path, &responseBody)
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var hostIDs []uint
|
|
|
|
var labelID uint
|
|
|
|
var teamID uint
|
|
|
|
|
|
|
|
for _, payload := range responseBody.List {
|
|
|
|
switch payload.Type {
|
|
|
|
case fleet.TranslatorTypeLabel:
|
|
|
|
labelID = payload.Payload.ID
|
|
|
|
case fleet.TranslatorTypeTeam:
|
|
|
|
teamID = payload.Payload.ID
|
|
|
|
case fleet.TranslatorTypeHost:
|
|
|
|
hostIDs = append(hostIDs, payload.Payload.ID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return hostIDs, labelID, teamID, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func encodeTranslatedPayload(translatorType string, identifier string) (fleet.TranslatePayload, error) {
|
|
|
|
translatedPayload := fleet.TranslatePayload{
|
|
|
|
Type: translatorType,
|
|
|
|
Payload: fleet.StringIdentifierToIDPayload{Identifier: identifier},
|
|
|
|
}
|
|
|
|
return translatedPayload, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) TransferHosts(hosts []string, label string, status, searchQuery string, team string) error {
|
|
|
|
hostIDs, labelID, teamID, err := c.translateTransferHostsToIDs(hosts, label, team)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(hosts) != 0 {
|
|
|
|
verb, path := "POST", "/api/v1/fleet/hosts/transfer"
|
|
|
|
var responseBody addHostsToTeamResponse
|
|
|
|
params := addHostsToTeamRequest{TeamID: ptr.Uint(teamID), HostIDs: hostIDs}
|
|
|
|
return c.authenticatedRequest(params, verb, path, &responseBody)
|
|
|
|
}
|
|
|
|
|
|
|
|
var labelIDPtr *uint
|
|
|
|
if label != "" {
|
|
|
|
labelIDPtr = &labelID
|
|
|
|
}
|
|
|
|
|
|
|
|
verb, path := "POST", "/api/v1/fleet/hosts/transfer/filter"
|
|
|
|
var responseBody addHostsToTeamByFilterResponse
|
|
|
|
params := addHostsToTeamByFilterRequest{TeamID: ptr.Uint(teamID), Filters: struct {
|
|
|
|
MatchQuery string `json:"query"`
|
|
|
|
Status fleet.HostStatus `json:"status"`
|
|
|
|
LabelID *uint `json:"label_id"`
|
|
|
|
}{MatchQuery: searchQuery, Status: fleet.HostStatus(status), LabelID: labelIDPtr}}
|
|
|
|
return c.authenticatedRequest(params, verb, path, &responseBody)
|
|
|
|
}
|