2016-09-26 18:48:55 +00:00
|
|
|
package service
|
2016-08-28 03:59:17 +00:00
|
|
|
|
|
|
|
import (
|
2017-03-15 15:55:30 +00:00
|
|
|
"context"
|
2016-08-28 03:59:17 +00:00
|
|
|
"encoding/json"
|
2017-05-09 00:43:48 +00:00
|
|
|
"io"
|
2016-08-28 03:59:17 +00:00
|
|
|
"net/http"
|
2018-11-21 01:19:24 +00:00
|
|
|
"net/url"
|
2016-09-04 05:13:42 +00:00
|
|
|
"strconv"
|
2021-02-18 20:52:43 +00:00
|
|
|
"strings"
|
2016-08-28 03:59:17 +00:00
|
|
|
|
2021-06-26 04:46:51 +00:00
|
|
|
"github.com/fleetdm/fleet/v4/server/fleet"
|
2021-08-24 20:24:52 +00:00
|
|
|
"github.com/fleetdm/fleet/v4/server/ptr"
|
2021-02-16 23:25:34 +00:00
|
|
|
"github.com/gorilla/mux"
|
2018-11-21 01:19:24 +00:00
|
|
|
"github.com/pkg/errors"
|
2016-08-28 03:59:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// errBadRoute is used for mux errors
|
|
|
|
errBadRoute = errors.New("bad route")
|
|
|
|
)
|
|
|
|
|
|
|
|
func encodeResponse(ctx context.Context, w http.ResponseWriter, response interface{}) error {
|
2017-05-17 15:58:40 +00:00
|
|
|
// The has to happen first, if an error happens we'll redirect to an error
|
|
|
|
// page and the error will be logged
|
|
|
|
if page, ok := response.(htmlPage); ok {
|
|
|
|
w.Header().Set("Content-Type", "text/html; charset=UTF-8")
|
|
|
|
_, err := io.WriteString(w, page.html())
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-08-28 03:59:17 +00:00
|
|
|
if e, ok := response.(errorer); ok && e.error() != nil {
|
|
|
|
encodeError(ctx, e.error(), w)
|
|
|
|
return nil
|
|
|
|
}
|
2016-09-15 14:52:17 +00:00
|
|
|
|
|
|
|
if e, ok := response.(statuser); ok {
|
|
|
|
w.WriteHeader(e.status())
|
|
|
|
if e.status() == http.StatusNoContent {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-09 12:34:53 +00:00
|
|
|
enc := json.NewEncoder(w)
|
|
|
|
enc.SetIndent("", " ")
|
|
|
|
return enc.Encode(response)
|
2016-08-28 03:59:17 +00:00
|
|
|
}
|
|
|
|
|
2016-09-15 14:52:17 +00:00
|
|
|
// statuser allows response types to implement a custom
|
|
|
|
// http success status - default is 200 OK
|
|
|
|
type statuser interface {
|
|
|
|
status() int
|
|
|
|
}
|
|
|
|
|
2017-05-09 00:43:48 +00:00
|
|
|
// loads a html page
|
|
|
|
type htmlPage interface {
|
|
|
|
html() string
|
|
|
|
error() error
|
|
|
|
}
|
|
|
|
|
2016-09-04 05:13:42 +00:00
|
|
|
func idFromRequest(r *http.Request, name string) (uint, error) {
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
id, ok := vars[name]
|
|
|
|
if !ok {
|
|
|
|
return 0, errBadRoute
|
|
|
|
}
|
|
|
|
uid, err := strconv.Atoi(id)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return uint(uid), nil
|
|
|
|
}
|
|
|
|
|
2018-05-04 18:05:55 +00:00
|
|
|
func nameFromRequest(r *http.Request, varName string) (string, error) {
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
name, ok := vars[varName]
|
|
|
|
if !ok {
|
|
|
|
return "", errBadRoute
|
|
|
|
}
|
2018-11-21 01:19:24 +00:00
|
|
|
unescaped, err := url.PathUnescape(name)
|
|
|
|
if err != nil {
|
|
|
|
return "", errors.Wrap(err, "unescape name in path")
|
|
|
|
}
|
|
|
|
return unescaped, nil
|
2018-05-04 18:05:55 +00:00
|
|
|
}
|
|
|
|
|
2016-10-13 18:21:47 +00:00
|
|
|
// default number of items to include per page
|
|
|
|
const defaultPerPage = 20
|
|
|
|
|
|
|
|
// listOptionsFromRequest parses the list options from the request parameters
|
2021-06-06 22:07:29 +00:00
|
|
|
func listOptionsFromRequest(r *http.Request) (fleet.ListOptions, error) {
|
2016-10-13 18:21:47 +00:00
|
|
|
var err error
|
|
|
|
|
|
|
|
pageString := r.URL.Query().Get("page")
|
|
|
|
perPageString := r.URL.Query().Get("per_page")
|
2016-10-17 14:01:14 +00:00
|
|
|
orderKey := r.URL.Query().Get("order_key")
|
|
|
|
orderDirectionString := r.URL.Query().Get("order_direction")
|
2016-10-13 18:21:47 +00:00
|
|
|
|
2021-08-24 17:35:03 +00:00
|
|
|
var page int
|
2016-10-13 18:21:47 +00:00
|
|
|
if pageString != "" {
|
|
|
|
page, err = strconv.Atoi(pageString)
|
|
|
|
if err != nil {
|
2021-06-06 22:07:29 +00:00
|
|
|
return fleet.ListOptions{}, errors.New("non-int page value")
|
2016-10-13 18:21:47 +00:00
|
|
|
}
|
|
|
|
if page < 0 {
|
2021-06-06 22:07:29 +00:00
|
|
|
return fleet.ListOptions{}, errors.New("negative page value")
|
2016-10-13 18:21:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We default to 0 for per_page so that not specifying any paging
|
|
|
|
// information gets all results
|
2021-08-24 17:35:03 +00:00
|
|
|
var perPage int
|
2016-10-13 18:21:47 +00:00
|
|
|
if perPageString != "" {
|
|
|
|
perPage, err = strconv.Atoi(perPageString)
|
|
|
|
if err != nil {
|
2021-06-06 22:07:29 +00:00
|
|
|
return fleet.ListOptions{}, errors.New("non-int per_page value")
|
2016-10-13 18:21:47 +00:00
|
|
|
}
|
|
|
|
if perPage <= 0 {
|
2021-06-06 22:07:29 +00:00
|
|
|
return fleet.ListOptions{}, errors.New("invalid per_page value")
|
2016-10-13 18:21:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if perPage == 0 && pageString != "" {
|
|
|
|
// We explicitly set a non-zero default if a page is specified
|
|
|
|
// (because the client probably intended for paging, and
|
|
|
|
// leaving the 0 would turn that off)
|
|
|
|
perPage = defaultPerPage
|
|
|
|
}
|
|
|
|
|
2016-10-17 14:01:14 +00:00
|
|
|
if orderKey == "" && orderDirectionString != "" {
|
2021-06-06 22:07:29 +00:00
|
|
|
return fleet.ListOptions{},
|
2016-10-17 14:01:14 +00:00
|
|
|
errors.New("order_key must be specified with order_direction")
|
|
|
|
}
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
var orderDirection fleet.OrderDirection
|
2016-10-17 14:01:14 +00:00
|
|
|
switch orderDirectionString {
|
|
|
|
case "desc":
|
2021-06-06 22:07:29 +00:00
|
|
|
orderDirection = fleet.OrderDescending
|
2016-10-17 14:01:14 +00:00
|
|
|
case "asc":
|
2021-06-06 22:07:29 +00:00
|
|
|
orderDirection = fleet.OrderAscending
|
2016-10-17 14:01:14 +00:00
|
|
|
case "":
|
2021-06-06 22:07:29 +00:00
|
|
|
orderDirection = fleet.OrderAscending
|
2016-10-17 14:01:14 +00:00
|
|
|
default:
|
2021-06-06 22:07:29 +00:00
|
|
|
return fleet.ListOptions{},
|
2016-10-17 14:01:14 +00:00
|
|
|
errors.New("unknown order_direction: " + orderDirectionString)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-03-18 00:24:34 +00:00
|
|
|
query := r.URL.Query().Get("query")
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
return fleet.ListOptions{
|
2016-10-17 14:01:14 +00:00
|
|
|
Page: uint(page),
|
|
|
|
PerPage: uint(perPage),
|
|
|
|
OrderKey: orderKey,
|
|
|
|
OrderDirection: orderDirection,
|
2021-03-18 00:24:34 +00:00
|
|
|
MatchQuery: query,
|
2016-10-17 14:01:14 +00:00
|
|
|
}, nil
|
2016-10-13 18:21:47 +00:00
|
|
|
}
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func hostListOptionsFromRequest(r *http.Request) (fleet.HostListOptions, error) {
|
2021-02-18 20:52:43 +00:00
|
|
|
opt, err := listOptionsFromRequest(r)
|
|
|
|
if err != nil {
|
2021-06-06 22:07:29 +00:00
|
|
|
return fleet.HostListOptions{}, err
|
2021-02-18 20:52:43 +00:00
|
|
|
}
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
hopt := fleet.HostListOptions{ListOptions: opt}
|
2021-02-18 20:52:43 +00:00
|
|
|
|
|
|
|
status := r.URL.Query().Get("status")
|
2021-06-06 22:07:29 +00:00
|
|
|
switch fleet.HostStatus(status) {
|
|
|
|
case fleet.StatusNew, fleet.StatusOnline, fleet.StatusOffline, fleet.StatusMIA:
|
|
|
|
hopt.StatusFilter = fleet.HostStatus(status)
|
2021-02-18 20:52:43 +00:00
|
|
|
case "":
|
|
|
|
// No error when unset
|
|
|
|
default:
|
|
|
|
return hopt, errors.Errorf("invalid status %s", status)
|
|
|
|
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return hopt, err
|
|
|
|
}
|
|
|
|
|
|
|
|
additionalInfoFiltersString := r.URL.Query().Get("additional_info_filters")
|
|
|
|
if additionalInfoFiltersString != "" {
|
|
|
|
hopt.AdditionalFilters = strings.Split(additionalInfoFiltersString, ",")
|
|
|
|
}
|
|
|
|
|
2021-08-11 14:40:56 +00:00
|
|
|
team_id := r.URL.Query().Get("team_id")
|
|
|
|
if team_id != "" {
|
|
|
|
id, err := strconv.Atoi(team_id)
|
|
|
|
if err != nil {
|
|
|
|
return hopt, err
|
|
|
|
}
|
|
|
|
tid := uint(id)
|
|
|
|
hopt.TeamFilter = &tid
|
|
|
|
}
|
|
|
|
|
2021-08-24 20:24:52 +00:00
|
|
|
policy_id := r.URL.Query().Get("policy_id")
|
|
|
|
if policy_id != "" {
|
|
|
|
id, err := strconv.Atoi(policy_id)
|
|
|
|
if err != nil {
|
|
|
|
return hopt, err
|
|
|
|
}
|
|
|
|
pid := uint(id)
|
|
|
|
hopt.PolicyIDFilter = &pid
|
|
|
|
}
|
|
|
|
|
|
|
|
policy_response := r.URL.Query().Get("policy_response")
|
|
|
|
if policy_response != "" {
|
|
|
|
var v *bool
|
|
|
|
switch policy_response {
|
|
|
|
case "passing":
|
|
|
|
v = ptr.Bool(true)
|
|
|
|
case "failing":
|
|
|
|
v = ptr.Bool(false)
|
|
|
|
}
|
|
|
|
hopt.PolicyResponseFilter = v
|
|
|
|
}
|
|
|
|
|
2021-02-18 20:52:43 +00:00
|
|
|
return hopt, nil
|
|
|
|
}
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func userListOptionsFromRequest(r *http.Request) (fleet.UserListOptions, error) {
|
2021-04-22 03:54:09 +00:00
|
|
|
opt, err := listOptionsFromRequest(r)
|
|
|
|
if err != nil {
|
2021-06-06 22:07:29 +00:00
|
|
|
return fleet.UserListOptions{}, err
|
2021-04-22 03:54:09 +00:00
|
|
|
}
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
uopt := fleet.UserListOptions{ListOptions: opt}
|
2021-04-22 03:54:09 +00:00
|
|
|
|
|
|
|
if tid := r.URL.Query().Get("team_id"); tid != "" {
|
|
|
|
teamID, err := strconv.ParseUint(tid, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return uopt, errors.Wrap(err, "parse team_id as int")
|
|
|
|
}
|
|
|
|
uopt.TeamID = uint(teamID)
|
|
|
|
}
|
|
|
|
|
|
|
|
return uopt, nil
|
|
|
|
}
|
|
|
|
|
2016-09-04 05:13:42 +00:00
|
|
|
func decodeNoParamsRequest(ctx context.Context, r *http.Request) (interface{}, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2018-05-08 01:54:29 +00:00
|
|
|
|
|
|
|
type getGenericSpecRequest struct {
|
|
|
|
Name string
|
|
|
|
}
|
|
|
|
|
|
|
|
func decodeGetGenericSpecRequest(ctx context.Context, r *http.Request) (interface{}, error) {
|
|
|
|
name, err := nameFromRequest(r, "name")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var req getGenericSpecRequest
|
|
|
|
req.Name = name
|
|
|
|
return req, nil
|
|
|
|
}
|