2016-11-29 18:20:06 +00:00
|
|
|
package mysql
|
|
|
|
|
|
|
|
import (
|
2016-12-01 18:31:16 +00:00
|
|
|
"time"
|
2016-11-29 18:20:06 +00:00
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
"github.com/fleetdm/fleet/server/fleet"
|
2017-01-13 18:35:25 +00:00
|
|
|
"github.com/pkg/errors"
|
2016-11-29 18:20:06 +00:00
|
|
|
)
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func (d *Datastore) NewDistributedQueryCampaign(camp *fleet.DistributedQueryCampaign) (*fleet.DistributedQueryCampaign, error) {
|
2016-11-29 18:20:06 +00:00
|
|
|
|
|
|
|
sqlStatement := `
|
|
|
|
INSERT INTO distributed_query_campaigns (
|
|
|
|
query_id,
|
|
|
|
status,
|
|
|
|
user_id
|
|
|
|
)
|
|
|
|
VALUES(?,?,?)
|
|
|
|
`
|
|
|
|
result, err := d.db.Exec(sqlStatement, camp.QueryID, camp.Status, camp.UserID)
|
|
|
|
if err != nil {
|
2017-01-13 18:35:25 +00:00
|
|
|
return nil, errors.Wrap(err, "inserting distributed query campaign")
|
2016-11-29 18:20:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
id, _ := result.LastInsertId()
|
|
|
|
camp.ID = uint(id)
|
|
|
|
return camp, nil
|
|
|
|
}
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func (d *Datastore) DistributedQueryCampaign(id uint) (*fleet.DistributedQueryCampaign, error) {
|
2016-11-29 18:20:06 +00:00
|
|
|
sql := `
|
2020-10-22 17:51:26 +00:00
|
|
|
SELECT * FROM distributed_query_campaigns WHERE id = ?
|
2016-11-29 18:20:06 +00:00
|
|
|
`
|
2021-06-06 22:07:29 +00:00
|
|
|
campaign := &fleet.DistributedQueryCampaign{}
|
2016-11-29 18:20:06 +00:00
|
|
|
if err := d.db.Get(campaign, sql, id); err != nil {
|
2017-01-13 18:35:25 +00:00
|
|
|
return nil, errors.Wrap(err, "selecting distributed query campaign")
|
2016-11-29 18:20:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return campaign, nil
|
|
|
|
}
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func (d *Datastore) SaveDistributedQueryCampaign(camp *fleet.DistributedQueryCampaign) error {
|
2016-11-29 18:20:06 +00:00
|
|
|
sqlStatement := `
|
|
|
|
UPDATE distributed_query_campaigns SET
|
|
|
|
query_id = ?,
|
|
|
|
status = ?,
|
|
|
|
user_id = ?
|
|
|
|
WHERE id = ?
|
|
|
|
`
|
2017-03-30 22:03:48 +00:00
|
|
|
result, err := d.db.Exec(sqlStatement, camp.QueryID, camp.Status, camp.UserID, camp.ID)
|
2016-11-29 18:20:06 +00:00
|
|
|
if err != nil {
|
2017-01-13 18:35:25 +00:00
|
|
|
return errors.Wrap(err, "updating distributed query campaign")
|
2016-11-29 18:20:06 +00:00
|
|
|
}
|
2017-03-30 22:03:48 +00:00
|
|
|
rowsAffected, err := result.RowsAffected()
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "rows affected updating distributed query campaign")
|
|
|
|
}
|
|
|
|
if rowsAffected == 0 {
|
|
|
|
return notFound("DistributedQueryCampaign").WithID(camp.ID)
|
|
|
|
}
|
2016-11-29 18:20:06 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func (d *Datastore) DistributedQueryCampaignTargetIDs(id uint) (*fleet.HostTargets, error) {
|
2016-11-29 18:20:06 +00:00
|
|
|
sqlStatement := `
|
|
|
|
SELECT * FROM distributed_query_campaign_targets WHERE distributed_query_campaign_id = ?
|
|
|
|
`
|
2021-06-06 22:07:29 +00:00
|
|
|
targets := []fleet.DistributedQueryCampaignTarget{}
|
2016-11-29 18:20:06 +00:00
|
|
|
|
2021-05-27 20:18:00 +00:00
|
|
|
if err := d.db.Select(&targets, sqlStatement, id); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "select distributed campaign target")
|
2016-11-29 18:20:06 +00:00
|
|
|
}
|
|
|
|
|
2021-05-27 20:18:00 +00:00
|
|
|
hostIDs := []uint{}
|
|
|
|
labelIDs := []uint{}
|
|
|
|
teamIDs := []uint{}
|
2016-11-29 18:20:06 +00:00
|
|
|
for _, target := range targets {
|
2021-06-10 01:56:57 +00:00
|
|
|
switch target.Type {
|
|
|
|
case fleet.TargetHost:
|
2016-11-29 18:20:06 +00:00
|
|
|
hostIDs = append(hostIDs, target.TargetID)
|
2021-06-10 01:56:57 +00:00
|
|
|
case fleet.TargetLabel:
|
2016-11-29 18:20:06 +00:00
|
|
|
labelIDs = append(labelIDs, target.TargetID)
|
2021-06-10 01:56:57 +00:00
|
|
|
case fleet.TargetTeam:
|
|
|
|
teamIDs = append(teamIDs, target.TargetID)
|
|
|
|
default:
|
2021-05-27 20:18:00 +00:00
|
|
|
return nil, errors.Errorf("invalid target type: %d", target.Type)
|
2016-11-29 18:20:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
return &fleet.HostTargets{HostIDs: hostIDs, LabelIDs: labelIDs, TeamIDs: teamIDs}, nil
|
2016-11-29 18:20:06 +00:00
|
|
|
}
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func (d *Datastore) NewDistributedQueryCampaignTarget(target *fleet.DistributedQueryCampaignTarget) (*fleet.DistributedQueryCampaignTarget, error) {
|
2016-11-29 18:20:06 +00:00
|
|
|
sqlStatement := `
|
|
|
|
INSERT into distributed_query_campaign_targets (
|
|
|
|
type,
|
|
|
|
distributed_query_campaign_id,
|
|
|
|
target_id
|
|
|
|
)
|
|
|
|
VALUES (?,?,?)
|
|
|
|
`
|
|
|
|
result, err := d.db.Exec(sqlStatement, target.Type, target.DistributedQueryCampaignID, target.TargetID)
|
|
|
|
if err != nil {
|
2017-01-13 18:35:25 +00:00
|
|
|
return nil, errors.Wrap(err, "insert distributed campaign target")
|
2016-11-29 18:20:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
id, _ := result.LastInsertId()
|
|
|
|
target.ID = uint(id)
|
|
|
|
return target, nil
|
|
|
|
}
|
|
|
|
|
2020-04-07 01:10:20 +00:00
|
|
|
func (d *Datastore) CleanupDistributedQueryCampaigns(now time.Time) (expired uint, err error) {
|
|
|
|
// Expire old waiting/running campaigns
|
2016-12-01 18:31:16 +00:00
|
|
|
sqlStatement := `
|
|
|
|
UPDATE distributed_query_campaigns
|
|
|
|
SET status = ?
|
|
|
|
WHERE (status = ? AND created_at < ?)
|
|
|
|
OR (status = ? AND created_at < ?)
|
|
|
|
`
|
2021-06-06 22:07:29 +00:00
|
|
|
result, err := d.db.Exec(sqlStatement, fleet.QueryComplete,
|
|
|
|
fleet.QueryWaiting, now.Add(-1*time.Minute),
|
|
|
|
fleet.QueryRunning, now.Add(-24*time.Hour))
|
2016-12-01 18:31:16 +00:00
|
|
|
if err != nil {
|
2020-04-07 01:10:20 +00:00
|
|
|
return 0, errors.Wrap(err, "updating distributed query campaign")
|
2016-12-01 18:31:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
exp, err := result.RowsAffected()
|
|
|
|
if err != nil {
|
2020-04-07 01:10:20 +00:00
|
|
|
return 0, errors.Wrap(err, "rows effected updating distributed query campaign")
|
2016-12-01 18:31:16 +00:00
|
|
|
}
|
|
|
|
|
2020-04-07 01:10:20 +00:00
|
|
|
return uint(exp), nil
|
2016-12-01 18:31:16 +00:00
|
|
|
}
|