2021-10-26 14:33:31 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2021-10-26 19:12:07 +00:00
|
|
|
"github.com/fleetdm/fleet/v4/server/contexts/logging"
|
2021-10-26 14:33:31 +00:00
|
|
|
"github.com/fleetdm/fleet/v4/server/fleet"
|
|
|
|
"github.com/fleetdm/fleet/v4/server/ptr"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
type runLiveQueryRequest struct {
|
|
|
|
QueryIDs []uint `json:"query_ids"`
|
|
|
|
HostIDs []uint `json:"host_ids"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type summaryPayload struct {
|
|
|
|
TargetedHostCount int `json:"targeted_host_count"`
|
|
|
|
RespondedHostCount int `json:"responded_host_count"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type runLiveQueryResponse struct {
|
|
|
|
Summary summaryPayload `json:"summary"`
|
|
|
|
Err error `json:"error,omitempty"`
|
|
|
|
|
2021-10-26 19:12:07 +00:00
|
|
|
Results []fleet.QueryCampaignResult `json:"live_query_results"`
|
2021-10-26 14:33:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r runLiveQueryResponse) error() error { return r.Err }
|
|
|
|
|
|
|
|
func runLiveQueryEndpoint(ctx context.Context, request interface{}, svc fleet.Service) (interface{}, error) {
|
|
|
|
req := request.(*runLiveQueryRequest)
|
|
|
|
|
|
|
|
period := os.Getenv("FLEET_LIVE_QUERY_REST_PERIOD")
|
|
|
|
if period == "" {
|
|
|
|
period = "90s"
|
|
|
|
}
|
|
|
|
duration, err := time.ParseDuration(period)
|
|
|
|
if err != nil {
|
|
|
|
duration = 90 * time.Second
|
2021-10-26 19:12:07 +00:00
|
|
|
logging.WithExtras(ctx, "live_query_rest_period_err", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
res := runLiveQueryResponse{
|
|
|
|
Summary: summaryPayload{
|
|
|
|
TargetedHostCount: len(req.HostIDs),
|
|
|
|
RespondedHostCount: 0,
|
|
|
|
},
|
2021-10-26 14:33:31 +00:00
|
|
|
}
|
|
|
|
|
2021-10-26 19:12:07 +00:00
|
|
|
queryResults, respondedHostCount := svc.RunLiveQueryDeadline(ctx, req.QueryIDs, req.HostIDs, duration)
|
|
|
|
res.Results = queryResults
|
|
|
|
res.Summary.RespondedHostCount = respondedHostCount
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (svc *Service) RunLiveQueryDeadline(ctx context.Context, queryIDs []uint, hostIDs []uint, deadline time.Duration) ([]fleet.QueryCampaignResult, int) {
|
|
|
|
wg := sync.WaitGroup{}
|
|
|
|
|
|
|
|
resultsCh := make(chan fleet.QueryCampaignResult)
|
|
|
|
|
|
|
|
counterMutex := sync.Mutex{}
|
|
|
|
respondedHostIDs := make(map[uint]struct{})
|
|
|
|
|
|
|
|
for _, queryID := range queryIDs {
|
2021-10-26 14:33:31 +00:00
|
|
|
queryID := queryID
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
2021-10-26 19:12:07 +00:00
|
|
|
campaign, err := svc.NewDistributedQueryCampaign(ctx, "", &queryID, fleet.HostTargets{HostIDs: hostIDs})
|
2021-10-26 14:33:31 +00:00
|
|
|
if err != nil {
|
2021-10-26 19:12:07 +00:00
|
|
|
resultsCh <- fleet.QueryCampaignResult{QueryID: queryID, Error: ptr.String(err.Error())}
|
2021-10-26 14:33:31 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
readChan, cancelFunc, err := svc.GetCampaignReader(ctx, campaign)
|
|
|
|
if err != nil {
|
2021-10-26 19:12:07 +00:00
|
|
|
resultsCh <- fleet.QueryCampaignResult{QueryID: queryID, Error: ptr.String(err.Error())}
|
2021-10-26 14:33:31 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer cancelFunc()
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
err := svc.CompleteCampaign(ctx, campaign)
|
|
|
|
if err != nil {
|
2021-10-26 19:12:07 +00:00
|
|
|
resultsCh <- fleet.QueryCampaignResult{QueryID: queryID, Error: ptr.String(err.Error())}
|
2021-10-26 14:33:31 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2021-10-26 19:12:07 +00:00
|
|
|
var results []fleet.QueryResult
|
|
|
|
timeout := time.After(deadline)
|
2021-10-26 14:33:31 +00:00
|
|
|
loop:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case res := <-readChan:
|
|
|
|
switch res := res.(type) {
|
|
|
|
case fleet.DistributedQueryResult:
|
2021-10-26 19:12:07 +00:00
|
|
|
results = append(results, fleet.QueryResult{HostID: res.Host.ID, Rows: res.Rows, Error: res.Error})
|
2021-10-26 14:33:31 +00:00
|
|
|
counterMutex.Lock()
|
2021-10-26 19:12:07 +00:00
|
|
|
respondedHostIDs[res.Host.ID] = struct{}{}
|
2021-10-26 14:33:31 +00:00
|
|
|
counterMutex.Unlock()
|
2021-10-26 19:12:07 +00:00
|
|
|
case error:
|
|
|
|
resultsCh <- fleet.QueryCampaignResult{QueryID: queryID, Error: ptr.String(res.Error())}
|
|
|
|
return
|
2021-10-26 14:33:31 +00:00
|
|
|
}
|
2021-10-26 19:12:07 +00:00
|
|
|
case <-timeout:
|
|
|
|
break loop
|
|
|
|
case <-ctx.Done():
|
2021-10-26 14:33:31 +00:00
|
|
|
break loop
|
|
|
|
}
|
|
|
|
}
|
2021-10-26 19:12:07 +00:00
|
|
|
resultsCh <- fleet.QueryCampaignResult{QueryID: queryID, Results: results}
|
2021-10-26 14:33:31 +00:00
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2021-10-26 19:12:07 +00:00
|
|
|
// Iterate collecting results until all the goroutines have returned
|
2021-10-26 14:33:31 +00:00
|
|
|
go func() {
|
|
|
|
wg.Wait()
|
|
|
|
close(resultsCh)
|
|
|
|
}()
|
|
|
|
|
2021-10-26 19:12:07 +00:00
|
|
|
var results []fleet.QueryCampaignResult
|
2021-10-26 14:33:31 +00:00
|
|
|
for result := range resultsCh {
|
2021-10-26 19:12:07 +00:00
|
|
|
results = append(results, result)
|
2021-10-26 14:33:31 +00:00
|
|
|
}
|
|
|
|
|
2021-10-26 19:12:07 +00:00
|
|
|
return results, len(respondedHostIDs)
|
2021-10-26 14:33:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (svc *Service) GetCampaignReader(ctx context.Context, campaign *fleet.DistributedQueryCampaign) (<-chan interface{}, context.CancelFunc, error) {
|
|
|
|
// Open the channel from which we will receive incoming query results
|
|
|
|
// (probably from the redis pubsub implementation)
|
|
|
|
cancelCtx, cancelFunc := context.WithCancel(ctx)
|
|
|
|
|
|
|
|
readChan, err := svc.resultStore.ReadChannel(cancelCtx, *campaign)
|
|
|
|
if err != nil {
|
|
|
|
cancelFunc()
|
|
|
|
return nil, nil, fmt.Errorf("cannot open read channel for campaign %d ", campaign.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
campaign.Status = fleet.QueryRunning
|
|
|
|
if err := svc.ds.SaveDistributedQueryCampaign(ctx, campaign); err != nil {
|
|
|
|
cancelFunc()
|
|
|
|
return nil, nil, errors.Wrap(err, "error saving campaign state")
|
|
|
|
}
|
|
|
|
|
|
|
|
return readChan, cancelFunc, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (svc *Service) CompleteCampaign(ctx context.Context, campaign *fleet.DistributedQueryCampaign) error {
|
|
|
|
campaign.Status = fleet.QueryComplete
|
|
|
|
err := svc.ds.SaveDistributedQueryCampaign(ctx, campaign)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "saving distributed campaign after complete")
|
|
|
|
}
|
|
|
|
err = svc.liveQueryStore.StopQuery(strconv.Itoa(int(campaign.ID)))
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "stopping query after after complete")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|