2018-05-07 19:44:40 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
|
|
|
|
"github.com/kolide/fleet/server/kolide"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
2018-05-09 00:10:01 +00:00
|
|
|
// ApplyQueries sends the list of Queries to be applied (upserted) to the
|
2018-05-07 19:44:40 +00:00
|
|
|
// Fleet instance.
|
2018-05-09 00:10:01 +00:00
|
|
|
func (c *Client) ApplyQueries(specs []*kolide.QuerySpec) error {
|
2018-05-07 19:44:40 +00:00
|
|
|
req := applyQuerySpecsRequest{Specs: specs}
|
2018-05-07 23:30:52 +00:00
|
|
|
response, err := c.AuthenticatedDo("POST", "/api/v1/kolide/spec/queries", req)
|
2018-05-07 19:44:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "POST /api/v1/kolide/spec/queries")
|
|
|
|
}
|
|
|
|
defer response.Body.Close()
|
|
|
|
|
|
|
|
if response.StatusCode != http.StatusOK {
|
|
|
|
return errors.Errorf("apply query spec got HTTP %d, expected 200", response.StatusCode)
|
|
|
|
}
|
|
|
|
|
|
|
|
var responseBody applyQuerySpecsResponse
|
|
|
|
err = json.NewDecoder(response.Body).Decode(&responseBody)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "decode apply query spec response")
|
|
|
|
}
|
|
|
|
|
|
|
|
if responseBody.Err != nil {
|
|
|
|
return errors.Errorf("apply query spec: %s", responseBody.Err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-05-09 00:10:01 +00:00
|
|
|
// GetQuery retrieves the list of all Queries.
|
|
|
|
func (c *Client) GetQuery(name string) (*kolide.QuerySpec, error) {
|
|
|
|
verb, path := "GET", "/api/v1/kolide/spec/queries/"+url.QueryEscape(name)
|
|
|
|
response, err := c.AuthenticatedDo(verb, path, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "%s %s", verb, path)
|
|
|
|
}
|
|
|
|
defer response.Body.Close()
|
|
|
|
|
|
|
|
switch response.StatusCode {
|
|
|
|
case http.StatusNotFound:
|
|
|
|
return nil, notFoundErr{}
|
|
|
|
}
|
|
|
|
|
|
|
|
if response.StatusCode != http.StatusOK {
|
|
|
|
return nil, errors.Errorf("get query spec got HTTP %d, expected 200", response.StatusCode)
|
|
|
|
}
|
|
|
|
|
|
|
|
var responseBody getQuerySpecResponse
|
|
|
|
err = json.NewDecoder(response.Body).Decode(&responseBody)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "decode get query spec response")
|
|
|
|
}
|
|
|
|
|
|
|
|
if responseBody.Err != nil {
|
|
|
|
return nil, errors.Errorf("get query spec: %s", responseBody.Err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return responseBody.Spec, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetQueries retrieves the list of all Queries.
|
|
|
|
func (c *Client) GetQueries() ([]*kolide.QuerySpec, error) {
|
2018-05-07 23:30:52 +00:00
|
|
|
response, err := c.AuthenticatedDo("GET", "/api/v1/kolide/spec/queries", nil)
|
2018-05-07 19:44:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "GET /api/v1/kolide/spec/queries")
|
|
|
|
}
|
|
|
|
defer response.Body.Close()
|
|
|
|
|
|
|
|
if response.StatusCode != http.StatusOK {
|
|
|
|
return nil, errors.Errorf("get query spec got HTTP %d, expected 200", response.StatusCode)
|
|
|
|
}
|
|
|
|
|
|
|
|
var responseBody getQuerySpecsResponse
|
|
|
|
err = json.NewDecoder(response.Body).Decode(&responseBody)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "decode get query spec response")
|
|
|
|
}
|
|
|
|
|
|
|
|
if responseBody.Err != nil {
|
|
|
|
return nil, errors.Errorf("get query spec: %s", responseBody.Err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return responseBody.Specs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteQuery deletes the query with the matching name.
|
|
|
|
func (c *Client) DeleteQuery(name string) error {
|
|
|
|
verb, path := "DELETE", "/api/v1/kolide/queries/"+url.QueryEscape(name)
|
2018-05-07 23:30:52 +00:00
|
|
|
response, err := c.AuthenticatedDo(verb, path, nil)
|
2018-05-07 19:44:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "%s %s", verb, path)
|
|
|
|
}
|
|
|
|
defer response.Body.Close()
|
|
|
|
|
2018-05-08 02:07:00 +00:00
|
|
|
switch response.StatusCode {
|
|
|
|
case http.StatusNotFound:
|
|
|
|
return notFoundErr{}
|
|
|
|
}
|
|
|
|
|
2018-05-07 19:44:40 +00:00
|
|
|
if response.StatusCode != http.StatusOK {
|
|
|
|
return errors.Errorf("get query spec got HTTP %d, expected 200", response.StatusCode)
|
|
|
|
}
|
|
|
|
|
|
|
|
var responseBody deleteQueryResponse
|
|
|
|
err = json.NewDecoder(response.Body).Decode(&responseBody)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "decode get query spec response")
|
|
|
|
}
|
|
|
|
|
|
|
|
if responseBody.Err != nil {
|
|
|
|
return errors.Errorf("get query spec: %s", responseBody.Err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|