2016-11-29 18:20:06 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/go-kit/kit/endpoint"
|
2016-12-23 01:31:45 +00:00
|
|
|
kitlog "github.com/go-kit/kit/log"
|
2016-11-29 18:20:06 +00:00
|
|
|
"github.com/kolide/kolide-ose/server/contexts/viewer"
|
|
|
|
"github.com/kolide/kolide-ose/server/kolide"
|
|
|
|
"github.com/kolide/kolide-ose/server/websocket"
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
)
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Create Distributed Query Campaign
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
type createDistributedQueryCampaignRequest struct {
|
|
|
|
Query string `json:"query"`
|
|
|
|
Selected struct {
|
|
|
|
Labels []uint `json:"labels"`
|
|
|
|
Hosts []uint `json:"hosts"`
|
|
|
|
} `json:"selected"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type createDistributedQueryCampaignResponse struct {
|
|
|
|
Campaign *kolide.DistributedQueryCampaign `json:"campaign,omitempty"`
|
|
|
|
Err error `json:"error,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r createDistributedQueryCampaignResponse) error() error { return r.Err }
|
|
|
|
|
|
|
|
func makeCreateDistributedQueryCampaignEndpoint(svc kolide.Service) endpoint.Endpoint {
|
|
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
|
|
|
req := request.(createDistributedQueryCampaignRequest)
|
|
|
|
campaign, err := svc.NewDistributedQueryCampaign(ctx, req.Query, req.Selected.Hosts, req.Selected.Labels)
|
|
|
|
if err != nil {
|
|
|
|
return createQueryResponse{Err: err}, nil
|
|
|
|
}
|
|
|
|
return createDistributedQueryCampaignResponse{campaign, nil}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Stream Distributed Query Campaign Results and Metadata
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-12-23 01:31:45 +00:00
|
|
|
func makeStreamDistributedQueryCampaignResultsHandler(svc kolide.Service, jwtKey string, logger kitlog.Logger) http.HandlerFunc {
|
2016-11-29 18:20:06 +00:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// Upgrade to websocket connection
|
|
|
|
conn, err := websocket.Upgrade(w, r)
|
|
|
|
if err != nil {
|
2016-12-23 01:31:45 +00:00
|
|
|
logger.Log("err", err, "msg", "could not upgrade to websocket")
|
2016-11-29 18:20:06 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
// Receive the auth bearer token
|
|
|
|
token, err := conn.ReadAuthToken()
|
|
|
|
if err != nil {
|
2016-12-23 01:31:45 +00:00
|
|
|
logger.Log("err", err, "msg", "failed to read auth token")
|
2016-11-29 18:20:06 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Authenticate with the token
|
|
|
|
vc, err := authViewer(context.Background(), jwtKey, string(token), svc)
|
|
|
|
if err != nil || !vc.CanPerformActions() {
|
2016-12-23 01:31:45 +00:00
|
|
|
logger.Log("err", err, "msg", "unauthorized viewer")
|
2016-11-29 18:20:06 +00:00
|
|
|
conn.WriteJSONError("unauthorized")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := viewer.NewContext(context.Background(), *vc)
|
|
|
|
|
|
|
|
campaignID, err := idFromRequest(r, "id")
|
|
|
|
if err != nil {
|
2016-12-23 01:31:45 +00:00
|
|
|
logger.Log("err", err, "invalid campaign ID")
|
2016-11-29 18:20:06 +00:00
|
|
|
conn.WriteJSONError("invalid campaign ID")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
svc.StreamCampaignResults(ctx, conn, campaignID)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|