2016-09-26 18:48:55 +00:00
|
|
|
package service
|
2016-09-04 05:13:42 +00:00
|
|
|
|
|
|
|
import (
|
2017-03-15 15:55:30 +00:00
|
|
|
"context"
|
2022-02-08 16:47:48 +00:00
|
|
|
"database/sql"
|
|
|
|
"errors"
|
2022-01-19 19:07:58 +00:00
|
|
|
"fmt"
|
2021-08-02 22:06:27 +00:00
|
|
|
|
2021-07-13 19:54:22 +00:00
|
|
|
"github.com/fleetdm/fleet/v4/server/authz"
|
2021-11-22 14:13:26 +00:00
|
|
|
"github.com/fleetdm/fleet/v4/server/contexts/ctxerr"
|
2021-08-02 22:06:27 +00:00
|
|
|
"github.com/fleetdm/fleet/v4/server/contexts/logging"
|
2021-06-26 04:46:51 +00:00
|
|
|
"github.com/fleetdm/fleet/v4/server/contexts/viewer"
|
|
|
|
"github.com/fleetdm/fleet/v4/server/fleet"
|
|
|
|
"github.com/fleetdm/fleet/v4/server/ptr"
|
2016-09-04 05:13:42 +00:00
|
|
|
)
|
|
|
|
|
2022-01-31 21:35:22 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Get Query
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
type getQueryRequest struct {
|
|
|
|
ID uint `url:"id"`
|
2018-01-03 19:18:05 +00:00
|
|
|
}
|
|
|
|
|
2022-01-31 21:35:22 +00:00
|
|
|
type getQueryResponse struct {
|
|
|
|
Query *fleet.Query `json:"query,omitempty"`
|
|
|
|
Err error `json:"error,omitempty"`
|
2018-01-03 19:18:05 +00:00
|
|
|
}
|
|
|
|
|
2022-01-31 21:35:22 +00:00
|
|
|
func (r getQueryResponse) error() error { return r.Err }
|
2021-06-03 23:24:15 +00:00
|
|
|
|
2022-01-31 21:35:22 +00:00
|
|
|
func getQueryEndpoint(ctx context.Context, request interface{}, svc fleet.Service) (interface{}, error) {
|
|
|
|
req := request.(*getQueryRequest)
|
|
|
|
query, err := svc.GetQuery(ctx, req.ID)
|
|
|
|
if err != nil {
|
|
|
|
return getQueryResponse{Err: err}, nil
|
2018-01-03 19:18:05 +00:00
|
|
|
}
|
2022-01-31 21:35:22 +00:00
|
|
|
return getQueryResponse{query, nil}, nil
|
|
|
|
}
|
2018-01-03 19:18:05 +00:00
|
|
|
|
2022-01-31 21:35:22 +00:00
|
|
|
func (svc *Service) GetQuery(ctx context.Context, id uint) (*fleet.Query, error) {
|
|
|
|
if err := svc.authz.Authorize(ctx, &fleet.Query{}, fleet.ActionRead); err != nil {
|
|
|
|
return nil, err
|
2018-01-03 19:18:05 +00:00
|
|
|
}
|
|
|
|
|
2022-01-31 21:35:22 +00:00
|
|
|
return svc.ds.Query(ctx, id)
|
|
|
|
}
|
2020-12-15 02:13:34 +00:00
|
|
|
|
2022-01-31 21:35:22 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// List Queries
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2021-07-13 19:54:22 +00:00
|
|
|
|
2022-01-31 21:35:22 +00:00
|
|
|
type listQueriesRequest struct {
|
|
|
|
ListOptions fleet.ListOptions `url:"list_options"`
|
2018-01-03 19:18:05 +00:00
|
|
|
}
|
|
|
|
|
2022-01-31 21:35:22 +00:00
|
|
|
type listQueriesResponse struct {
|
|
|
|
Queries []fleet.Query `json:"queries"`
|
|
|
|
Err error `json:"error,omitempty"`
|
|
|
|
}
|
2021-06-03 23:24:15 +00:00
|
|
|
|
2022-01-31 21:35:22 +00:00
|
|
|
func (r listQueriesResponse) error() error { return r.Err }
|
|
|
|
|
|
|
|
func listQueriesEndpoint(ctx context.Context, request interface{}, svc fleet.Service) (interface{}, error) {
|
|
|
|
req := request.(*listQueriesRequest)
|
|
|
|
queries, err := svc.ListQueries(ctx, req.ListOptions)
|
2018-01-03 19:18:05 +00:00
|
|
|
if err != nil {
|
2022-01-31 21:35:22 +00:00
|
|
|
return listQueriesResponse{Err: err}, nil
|
2018-01-03 19:18:05 +00:00
|
|
|
}
|
|
|
|
|
2022-01-31 21:35:22 +00:00
|
|
|
resp := listQueriesResponse{Queries: []fleet.Query{}}
|
2018-01-03 19:18:05 +00:00
|
|
|
for _, query := range queries {
|
2022-01-31 21:35:22 +00:00
|
|
|
resp.Queries = append(resp.Queries, *query)
|
2018-01-03 19:18:05 +00:00
|
|
|
}
|
2022-01-31 21:35:22 +00:00
|
|
|
return resp, nil
|
2018-01-03 19:18:05 +00:00
|
|
|
}
|
|
|
|
|
2022-01-31 21:35:22 +00:00
|
|
|
func (svc *Service) ListQueries(ctx context.Context, opt fleet.ListOptions) ([]*fleet.Query, error) {
|
2021-06-06 22:07:29 +00:00
|
|
|
if err := svc.authz.Authorize(ctx, &fleet.Query{}, fleet.ActionRead); err != nil {
|
2021-06-03 23:24:15 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-01-31 21:35:22 +00:00
|
|
|
user := authz.UserFromContext(ctx)
|
|
|
|
onlyShowObserverCanRun := onlyShowObserverCanRunQueries(user)
|
|
|
|
|
|
|
|
queries, err := svc.ds.ListQueries(ctx, fleet.ListQueryOptions{
|
|
|
|
ListOptions: opt,
|
|
|
|
OnlyObserverCanRun: onlyShowObserverCanRun,
|
|
|
|
})
|
2018-05-08 01:54:29 +00:00
|
|
|
if err != nil {
|
2018-05-09 23:54:42 +00:00
|
|
|
return nil, err
|
2018-05-08 01:54:29 +00:00
|
|
|
}
|
2022-01-31 21:35:22 +00:00
|
|
|
|
|
|
|
return queries, nil
|
2018-05-08 01:54:29 +00:00
|
|
|
}
|
|
|
|
|
2021-09-20 16:07:51 +00:00
|
|
|
func onlyShowObserverCanRunQueries(user *fleet.User) bool {
|
|
|
|
if user.GlobalRole != nil && *user.GlobalRole == fleet.RoleObserver {
|
|
|
|
return true
|
|
|
|
} else if len(user.Teams) > 0 {
|
|
|
|
allObserver := true
|
|
|
|
for _, team := range user.Teams {
|
|
|
|
if team.Role != fleet.RoleObserver {
|
|
|
|
allObserver = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return allObserver
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-01-31 21:35:22 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Create Query
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2021-06-03 23:24:15 +00:00
|
|
|
|
2022-01-31 21:35:22 +00:00
|
|
|
type createQueryRequest struct {
|
|
|
|
fleet.QueryPayload
|
|
|
|
}
|
2021-09-20 16:07:51 +00:00
|
|
|
|
2022-01-31 21:35:22 +00:00
|
|
|
type createQueryResponse struct {
|
|
|
|
Query *fleet.Query `json:"query,omitempty"`
|
|
|
|
Err error `json:"error,omitempty"`
|
2016-09-04 05:13:42 +00:00
|
|
|
}
|
|
|
|
|
2022-01-31 21:35:22 +00:00
|
|
|
func (r createQueryResponse) error() error { return r.Err }
|
2021-06-03 23:24:15 +00:00
|
|
|
|
2022-01-31 21:35:22 +00:00
|
|
|
func createQueryEndpoint(ctx context.Context, request interface{}, svc fleet.Service) (interface{}, error) {
|
|
|
|
req := request.(*createQueryRequest)
|
|
|
|
query, err := svc.NewQuery(ctx, req.QueryPayload)
|
|
|
|
if err != nil {
|
|
|
|
return createQueryResponse{Err: err}, nil
|
|
|
|
}
|
|
|
|
return createQueryResponse{query, nil}, nil
|
2016-09-04 05:13:42 +00:00
|
|
|
}
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func (svc *Service) NewQuery(ctx context.Context, p fleet.QueryPayload) (*fleet.Query, error) {
|
2021-09-28 17:53:05 +00:00
|
|
|
user := authz.UserFromContext(ctx)
|
|
|
|
q := &fleet.Query{}
|
|
|
|
if user != nil {
|
|
|
|
q.AuthorID = ptr.Uint(user.ID)
|
|
|
|
}
|
|
|
|
if err := svc.authz.Authorize(ctx, q, fleet.ActionWrite); err != nil {
|
2021-06-03 23:24:15 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-01-19 19:07:58 +00:00
|
|
|
if err := p.Verify(); err != nil {
|
2022-09-19 17:53:44 +00:00
|
|
|
return nil, ctxerr.Wrap(ctx, &fleet.BadRequestError{
|
|
|
|
Message: fmt.Sprintf("query payload verification: %s", err),
|
2022-01-19 19:07:58 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
query := &fleet.Query{Saved: true}
|
2016-09-04 05:13:42 +00:00
|
|
|
|
|
|
|
if p.Name != nil {
|
|
|
|
query.Name = *p.Name
|
|
|
|
}
|
|
|
|
|
2016-10-31 21:05:02 +00:00
|
|
|
if p.Description != nil {
|
|
|
|
query.Description = *p.Description
|
|
|
|
}
|
|
|
|
|
2016-09-04 05:13:42 +00:00
|
|
|
if p.Query != nil {
|
|
|
|
query.Query = *p.Query
|
|
|
|
}
|
|
|
|
|
2021-08-02 22:06:27 +00:00
|
|
|
logging.WithExtras(ctx, "name", query.Name, "sql", query.Query)
|
|
|
|
|
2021-05-20 17:28:55 +00:00
|
|
|
if p.ObserverCanRun != nil {
|
|
|
|
query.ObserverCanRun = *p.ObserverCanRun
|
|
|
|
}
|
|
|
|
|
2016-12-07 20:22:31 +00:00
|
|
|
vc, ok := viewer.FromContext(ctx)
|
|
|
|
if ok {
|
2021-05-27 03:45:06 +00:00
|
|
|
query.AuthorID = ptr.Uint(vc.UserID())
|
2017-01-06 01:48:56 +00:00
|
|
|
query.AuthorName = vc.FullName()
|
2021-11-08 14:06:56 +00:00
|
|
|
query.AuthorEmail = vc.Email()
|
2016-12-07 20:22:31 +00:00
|
|
|
}
|
|
|
|
|
2021-09-14 12:11:07 +00:00
|
|
|
query, err := svc.ds.NewQuery(ctx, query)
|
2016-09-04 05:13:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-01-06 01:48:56 +00:00
|
|
|
|
2021-07-13 19:54:22 +00:00
|
|
|
if err := svc.ds.NewActivity(
|
2021-09-14 12:11:07 +00:00
|
|
|
ctx,
|
2021-07-13 19:54:22 +00:00
|
|
|
authz.UserFromContext(ctx),
|
2022-12-23 16:05:16 +00:00
|
|
|
fleet.ActivityTypeCreatedSavedQuery{
|
|
|
|
ID: query.ID,
|
|
|
|
Name: query.Name,
|
|
|
|
},
|
2021-07-13 19:54:22 +00:00
|
|
|
); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-10-31 22:00:04 +00:00
|
|
|
return query, nil
|
2016-09-04 05:13:42 +00:00
|
|
|
}
|
|
|
|
|
2022-01-31 21:35:22 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Modify Query
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
type modifyQueryRequest struct {
|
|
|
|
ID uint `json:"-" url:"id"`
|
|
|
|
fleet.QueryPayload
|
|
|
|
}
|
|
|
|
|
|
|
|
type modifyQueryResponse struct {
|
|
|
|
Query *fleet.Query `json:"query,omitempty"`
|
|
|
|
Err error `json:"error,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r modifyQueryResponse) error() error { return r.Err }
|
|
|
|
|
|
|
|
func modifyQueryEndpoint(ctx context.Context, request interface{}, svc fleet.Service) (interface{}, error) {
|
|
|
|
req := request.(*modifyQueryRequest)
|
|
|
|
query, err := svc.ModifyQuery(ctx, req.ID, req.QueryPayload)
|
|
|
|
if err != nil {
|
|
|
|
return modifyQueryResponse{Err: err}, nil
|
|
|
|
}
|
|
|
|
return modifyQueryResponse{query, nil}, nil
|
|
|
|
}
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func (svc *Service) ModifyQuery(ctx context.Context, id uint, p fleet.QueryPayload) (*fleet.Query, error) {
|
2021-09-28 17:53:05 +00:00
|
|
|
// First make sure the user can read queries
|
|
|
|
if err := svc.authz.Authorize(ctx, &fleet.Query{}, fleet.ActionRead); err != nil {
|
2021-06-03 23:24:15 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-01-19 19:07:58 +00:00
|
|
|
if err := p.Verify(); err != nil {
|
2022-09-19 17:53:44 +00:00
|
|
|
return nil, ctxerr.Wrap(ctx, &fleet.BadRequestError{
|
|
|
|
Message: fmt.Sprintf("query payload verification: %s", err),
|
2022-01-19 19:07:58 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-09-14 12:11:07 +00:00
|
|
|
query, err := svc.ds.Query(ctx, id)
|
2016-09-04 05:13:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-09-28 17:53:05 +00:00
|
|
|
// Then we make sure they can modify them
|
|
|
|
if err := svc.authz.Authorize(ctx, query, fleet.ActionWrite); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-09-04 05:13:42 +00:00
|
|
|
if p.Name != nil {
|
|
|
|
query.Name = *p.Name
|
|
|
|
}
|
|
|
|
|
2016-10-31 21:05:02 +00:00
|
|
|
if p.Description != nil {
|
|
|
|
query.Description = *p.Description
|
|
|
|
}
|
|
|
|
|
2016-09-04 05:13:42 +00:00
|
|
|
if p.Query != nil {
|
|
|
|
query.Query = *p.Query
|
|
|
|
}
|
|
|
|
|
2021-08-02 22:06:27 +00:00
|
|
|
logging.WithExtras(ctx, "name", query.Name, "sql", query.Query)
|
|
|
|
|
2021-05-20 17:28:55 +00:00
|
|
|
if p.ObserverCanRun != nil {
|
|
|
|
query.ObserverCanRun = *p.ObserverCanRun
|
|
|
|
}
|
|
|
|
|
2021-09-14 12:11:07 +00:00
|
|
|
if err := svc.ds.SaveQuery(ctx, query); err != nil {
|
2016-09-04 05:13:42 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-07-13 19:54:22 +00:00
|
|
|
if err := svc.ds.NewActivity(
|
2021-09-14 12:11:07 +00:00
|
|
|
ctx,
|
2021-07-13 19:54:22 +00:00
|
|
|
authz.UserFromContext(ctx),
|
2022-12-23 16:05:16 +00:00
|
|
|
fleet.ActivityTypeEditedSavedQuery{
|
|
|
|
ID: query.ID,
|
|
|
|
Name: query.Name,
|
|
|
|
},
|
2021-07-13 19:54:22 +00:00
|
|
|
); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-09-04 05:13:42 +00:00
|
|
|
return query, nil
|
|
|
|
}
|
|
|
|
|
2022-01-31 21:35:22 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Delete Query
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
type deleteQueryRequest struct {
|
|
|
|
Name string `url:"name"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type deleteQueryResponse struct {
|
|
|
|
Err error `json:"error,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r deleteQueryResponse) error() error { return r.Err }
|
|
|
|
|
|
|
|
func deleteQueryEndpoint(ctx context.Context, request interface{}, svc fleet.Service) (interface{}, error) {
|
|
|
|
req := request.(*deleteQueryRequest)
|
|
|
|
err := svc.DeleteQuery(ctx, req.Name)
|
|
|
|
if err != nil {
|
|
|
|
return deleteQueryResponse{Err: err}, nil
|
|
|
|
}
|
|
|
|
return deleteQueryResponse{}, nil
|
|
|
|
}
|
|
|
|
|
2021-06-01 00:07:51 +00:00
|
|
|
func (svc *Service) DeleteQuery(ctx context.Context, name string) error {
|
2021-09-28 17:53:05 +00:00
|
|
|
// First make sure the user can read queries
|
|
|
|
if err := svc.authz.Authorize(ctx, &fleet.Query{}, fleet.ActionRead); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
query, err := svc.ds.QueryByName(ctx, name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Then we make sure they can modify them
|
|
|
|
if err := svc.authz.Authorize(ctx, query, fleet.ActionWrite); err != nil {
|
2021-06-03 23:24:15 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-09-14 12:11:07 +00:00
|
|
|
if err := svc.ds.DeleteQuery(ctx, name); err != nil {
|
2021-07-13 19:54:22 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return svc.ds.NewActivity(
|
2021-09-14 12:11:07 +00:00
|
|
|
ctx,
|
2021-07-13 19:54:22 +00:00
|
|
|
authz.UserFromContext(ctx),
|
2022-12-23 16:05:16 +00:00
|
|
|
fleet.ActivityTypeDeletedSavedQuery{
|
|
|
|
Name: name,
|
|
|
|
},
|
2021-07-13 19:54:22 +00:00
|
|
|
)
|
2016-09-04 05:13:42 +00:00
|
|
|
}
|
2016-12-09 17:12:45 +00:00
|
|
|
|
2022-01-31 21:35:22 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Delete Query By ID
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
type deleteQueryByIDRequest struct {
|
|
|
|
ID uint `url:"id"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type deleteQueryByIDResponse struct {
|
|
|
|
Err error `json:"error,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r deleteQueryByIDResponse) error() error { return r.Err }
|
|
|
|
|
|
|
|
func deleteQueryByIDEndpoint(ctx context.Context, request interface{}, svc fleet.Service) (interface{}, error) {
|
|
|
|
req := request.(*deleteQueryByIDRequest)
|
|
|
|
err := svc.DeleteQueryByID(ctx, req.ID)
|
|
|
|
if err != nil {
|
|
|
|
return deleteQueryByIDResponse{Err: err}, nil
|
|
|
|
}
|
|
|
|
return deleteQueryByIDResponse{}, nil
|
|
|
|
}
|
|
|
|
|
2021-06-01 00:07:51 +00:00
|
|
|
func (svc *Service) DeleteQueryByID(ctx context.Context, id uint) error {
|
2021-09-28 17:53:05 +00:00
|
|
|
// First make sure the user can read queries
|
|
|
|
if err := svc.authz.Authorize(ctx, &fleet.Query{}, fleet.ActionRead); err != nil {
|
2021-06-03 23:24:15 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-09-14 12:11:07 +00:00
|
|
|
query, err := svc.ds.Query(ctx, id)
|
2018-06-15 14:13:11 +00:00
|
|
|
if err != nil {
|
2021-11-22 14:13:26 +00:00
|
|
|
return ctxerr.Wrap(ctx, err, "lookup query by ID")
|
2018-06-15 14:13:11 +00:00
|
|
|
}
|
|
|
|
|
2021-09-28 17:53:05 +00:00
|
|
|
// Then we make sure they can modify them
|
|
|
|
if err := svc.authz.Authorize(ctx, query, fleet.ActionWrite); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-09-14 12:11:07 +00:00
|
|
|
if err := svc.ds.DeleteQuery(ctx, query.Name); err != nil {
|
2021-11-22 14:13:26 +00:00
|
|
|
return ctxerr.Wrap(ctx, err, "delete query")
|
2021-07-13 19:54:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return svc.ds.NewActivity(
|
2021-09-14 12:11:07 +00:00
|
|
|
ctx,
|
2021-07-13 19:54:22 +00:00
|
|
|
authz.UserFromContext(ctx),
|
2022-12-23 16:05:16 +00:00
|
|
|
fleet.ActivityTypeDeletedSavedQuery{
|
|
|
|
Name: query.Name,
|
|
|
|
},
|
2021-07-13 19:54:22 +00:00
|
|
|
)
|
2018-06-15 14:13:11 +00:00
|
|
|
}
|
|
|
|
|
2022-01-31 21:35:22 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Delete Queries
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
type deleteQueriesRequest struct {
|
|
|
|
IDs []uint `json:"ids"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type deleteQueriesResponse struct {
|
|
|
|
Deleted uint `json:"deleted"`
|
|
|
|
Err error `json:"error,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r deleteQueriesResponse) error() error { return r.Err }
|
|
|
|
|
|
|
|
func deleteQueriesEndpoint(ctx context.Context, request interface{}, svc fleet.Service) (interface{}, error) {
|
|
|
|
req := request.(*deleteQueriesRequest)
|
|
|
|
deleted, err := svc.DeleteQueries(ctx, req.IDs)
|
|
|
|
if err != nil {
|
|
|
|
return deleteQueriesResponse{Err: err}, nil
|
|
|
|
}
|
|
|
|
return deleteQueriesResponse{Deleted: deleted}, nil
|
|
|
|
}
|
|
|
|
|
2021-06-01 00:07:51 +00:00
|
|
|
func (svc *Service) DeleteQueries(ctx context.Context, ids []uint) (uint, error) {
|
2021-09-28 17:53:05 +00:00
|
|
|
// First make sure the user can read queries
|
|
|
|
if err := svc.authz.Authorize(ctx, &fleet.Query{}, fleet.ActionRead); err != nil {
|
2021-06-03 23:24:15 +00:00
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2021-09-28 17:53:05 +00:00
|
|
|
for _, id := range ids {
|
|
|
|
query, err := svc.ds.Query(ctx, id)
|
|
|
|
if err != nil {
|
2021-11-22 14:13:26 +00:00
|
|
|
return 0, ctxerr.Wrap(ctx, err, "lookup query by ID")
|
2021-09-28 17:53:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Then we make sure they can modify them
|
|
|
|
if err := svc.authz.Authorize(ctx, query, fleet.ActionWrite); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-14 12:11:07 +00:00
|
|
|
n, err := svc.ds.DeleteQueries(ctx, ids)
|
2021-07-13 19:54:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = svc.ds.NewActivity(
|
2021-09-14 12:11:07 +00:00
|
|
|
ctx,
|
2021-07-13 19:54:22 +00:00
|
|
|
authz.UserFromContext(ctx),
|
2022-12-23 16:05:16 +00:00
|
|
|
fleet.ActivityTypeDeletedMultipleSavedQuery{
|
|
|
|
IDs: ids,
|
|
|
|
},
|
2021-07-13 19:54:22 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return n, nil
|
2016-12-09 17:12:45 +00:00
|
|
|
}
|
2022-01-31 21:35:22 +00:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Apply Query Spec
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
type applyQuerySpecsRequest struct {
|
|
|
|
Specs []*fleet.QuerySpec `json:"specs"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type applyQuerySpecsResponse struct {
|
|
|
|
Err error `json:"error,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r applyQuerySpecsResponse) error() error { return r.Err }
|
|
|
|
|
|
|
|
func applyQuerySpecsEndpoint(ctx context.Context, request interface{}, svc fleet.Service) (interface{}, error) {
|
|
|
|
req := request.(*applyQuerySpecsRequest)
|
|
|
|
err := svc.ApplyQuerySpecs(ctx, req.Specs)
|
|
|
|
if err != nil {
|
|
|
|
return applyQuerySpecsResponse{Err: err}, nil
|
|
|
|
}
|
|
|
|
return applyQuerySpecsResponse{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (svc *Service) ApplyQuerySpecs(ctx context.Context, specs []*fleet.QuerySpec) error {
|
2022-02-08 16:47:48 +00:00
|
|
|
// check that the user can create queries
|
2022-01-31 21:35:22 +00:00
|
|
|
if err := svc.authz.Authorize(ctx, &fleet.Query{}, fleet.ActionWrite); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
queries := []*fleet.Query{}
|
|
|
|
for _, spec := range specs {
|
|
|
|
queries = append(queries, queryFromSpec(spec))
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, query := range queries {
|
|
|
|
if err := query.Verify(); err != nil {
|
2022-09-19 17:53:44 +00:00
|
|
|
return ctxerr.Wrap(ctx, &fleet.BadRequestError{
|
|
|
|
Message: fmt.Sprintf("query payload verification: %s", err),
|
2022-01-31 21:35:22 +00:00
|
|
|
})
|
|
|
|
}
|
2022-02-08 16:47:48 +00:00
|
|
|
|
|
|
|
// check that the user can update the query if it already exists
|
|
|
|
query, err := svc.ds.QueryByName(ctx, query.Name)
|
|
|
|
if err != nil && !errors.Is(err, sql.ErrNoRows) {
|
|
|
|
return err
|
|
|
|
} else if err == nil {
|
|
|
|
if err := svc.authz.Authorize(ctx, query, fleet.ActionWrite); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
vc, ok := viewer.FromContext(ctx)
|
|
|
|
if !ok {
|
|
|
|
return ctxerr.New(ctx, "user must be authenticated to apply queries")
|
2022-01-31 21:35:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err := svc.ds.ApplyQueries(ctx, vc.UserID(), queries)
|
|
|
|
if err != nil {
|
|
|
|
return ctxerr.Wrap(ctx, err, "applying queries")
|
|
|
|
}
|
|
|
|
|
|
|
|
return svc.ds.NewActivity(
|
|
|
|
ctx,
|
|
|
|
authz.UserFromContext(ctx),
|
2022-12-23 16:05:16 +00:00
|
|
|
fleet.ActivityTypeAppliedSpecSavedQuery{
|
|
|
|
Specs: specs,
|
|
|
|
},
|
2022-01-31 21:35:22 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func queryFromSpec(spec *fleet.QuerySpec) *fleet.Query {
|
|
|
|
return &fleet.Query{
|
|
|
|
Name: spec.Name,
|
|
|
|
Description: spec.Description,
|
|
|
|
Query: spec.Query,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Get Query Specs
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
type getQuerySpecsResponse struct {
|
|
|
|
Specs []*fleet.QuerySpec `json:"specs"`
|
|
|
|
Err error `json:"error,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r getQuerySpecsResponse) error() error { return r.Err }
|
|
|
|
|
|
|
|
func getQuerySpecsEndpoint(ctx context.Context, request interface{}, svc fleet.Service) (interface{}, error) {
|
|
|
|
specs, err := svc.GetQuerySpecs(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return getQuerySpecsResponse{Err: err}, nil
|
|
|
|
}
|
|
|
|
return getQuerySpecsResponse{Specs: specs}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (svc *Service) GetQuerySpecs(ctx context.Context) ([]*fleet.QuerySpec, error) {
|
|
|
|
if err := svc.authz.Authorize(ctx, &fleet.Query{}, fleet.ActionRead); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
queries, err := svc.ds.ListQueries(ctx, fleet.ListQueryOptions{})
|
|
|
|
if err != nil {
|
|
|
|
return nil, ctxerr.Wrap(ctx, err, "getting queries")
|
|
|
|
}
|
|
|
|
|
|
|
|
specs := []*fleet.QuerySpec{}
|
|
|
|
for _, query := range queries {
|
|
|
|
specs = append(specs, specFromQuery(query))
|
|
|
|
}
|
|
|
|
return specs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func specFromQuery(query *fleet.Query) *fleet.QuerySpec {
|
|
|
|
return &fleet.QuerySpec{
|
|
|
|
Name: query.Name,
|
|
|
|
Description: query.Description,
|
|
|
|
Query: query.Query,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Get Query Spec
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
type getQuerySpecResponse struct {
|
|
|
|
Spec *fleet.QuerySpec `json:"specs,omitempty"`
|
|
|
|
Err error `json:"error,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r getQuerySpecResponse) error() error { return r.Err }
|
|
|
|
|
|
|
|
func getQuerySpecEndpoint(ctx context.Context, request interface{}, svc fleet.Service) (interface{}, error) {
|
|
|
|
req := request.(*getGenericSpecRequest)
|
|
|
|
spec, err := svc.GetQuerySpec(ctx, req.Name)
|
|
|
|
if err != nil {
|
|
|
|
return getQuerySpecResponse{Err: err}, nil
|
|
|
|
}
|
|
|
|
return getQuerySpecResponse{Spec: spec}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (svc *Service) GetQuerySpec(ctx context.Context, name string) (*fleet.QuerySpec, error) {
|
|
|
|
if err := svc.authz.Authorize(ctx, &fleet.Query{}, fleet.ActionRead); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
query, err := svc.ds.QueryByName(ctx, name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return specFromQuery(query), nil
|
|
|
|
}
|