fleet/server/webhooks/host_status.go
Victor Lyuboslavsky 592a7450e3
Enabling setting host status webhook at the team level via REST API and fleetctl apply/gitops. (#17186)
Enabling setting host status webhook at the team level via REST API and
fleetctl apply/gitops.
#14916

Example payload:
```json
{
    "data": {
        "days_unseen": 3,
        "host_ids": [
            10724,
            10726,
            10738,
            10739,
            10740,
            10741,
            10742,
            10744,
            10745,
            10746,
            10747,
            10748,
            10749
        ],
        "team_id": 3,
        "total_hosts": 15,
        "unseen_hosts": 13
    },
    "text": "More than 86.67% of your hosts have not checked into Fleet for more than 3 days. You've been sent this message because the Host status webhook is enabled in your Fleet instance."
}
```

# Checklist for submitter

- [x] Changes file added for user-visible changes in `changes/` or
`orbit/changes/`.
See [Changes
files](https://fleetdm.com/docs/contributing/committing-changes#changes-files)
for more information.
- [x] Added/updated tests
- [x] Manual QA for all new/changed functionality
2024-03-04 12:35:27 -06:00

104 lines
3.1 KiB
Go

package webhooks
import (
"context"
"fmt"
"github.com/fleetdm/fleet/v4/server"
"github.com/fleetdm/fleet/v4/server/contexts/ctxerr"
"github.com/fleetdm/fleet/v4/server/fleet"
kitlog "github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/hashicorp/go-multierror"
)
func TriggerHostStatusWebhook(
ctx context.Context,
ds fleet.Datastore,
logger kitlog.Logger,
) error {
multiErr := &multierror.Error{}
multiErr = multierror.Append(multiErr, triggerGlobalHostStatusWebhook(ctx, ds, logger))
multiErr = multierror.Append(multiErr, triggerTeamHostStatusWebhook(ctx, ds, logger))
return multiErr.ErrorOrNil()
}
func triggerGlobalHostStatusWebhook(ctx context.Context, ds fleet.Datastore, logger kitlog.Logger) error {
appConfig, err := ds.AppConfig(ctx)
if err != nil {
return ctxerr.Wrap(ctx, err, "getting app config")
}
if !appConfig.WebhookSettings.HostStatusWebhook.Enable {
return nil
}
level.Debug(logger).Log("global", "true", "enable_host_status_webhook", "true")
return processWebhook(ctx, ds, nil, appConfig.WebhookSettings.HostStatusWebhook)
}
func processWebhook(ctx context.Context, ds fleet.Datastore, teamID *uint, settings fleet.HostStatusWebhookSettings) error {
total, unseen, err := ds.TotalAndUnseenHostsSince(ctx, teamID, settings.DaysCount)
if err != nil {
return ctxerr.Wrap(ctx, err, "getting total and unseen hosts")
}
unseenCount := len(unseen)
percentUnseen := float64(unseenCount) * 100.0 / float64(total)
if percentUnseen >= settings.HostPercentage {
url := settings.DestinationURL
message := fmt.Sprintf(
"More than %.2f%% of your hosts have not checked into Fleet for more than %d days. "+
"You've been sent this message because the Host status webhook is enabled in your Fleet instance.",
percentUnseen, settings.DaysCount,
)
payload := map[string]interface{}{
"text": message,
"data": map[string]interface{}{
"unseen_hosts": unseenCount,
"total_hosts": total,
"days_unseen": settings.DaysCount,
"host_ids": unseen,
},
}
if teamID != nil {
payload["data"].(map[string]interface{})["team_id"] = *teamID
}
err = server.PostJSONWithTimeout(ctx, url, &payload)
if err != nil {
return ctxerr.Wrapf(ctx, err, "posting to %s", url)
}
}
return nil
}
func triggerTeamHostStatusWebhook(ctx context.Context, ds fleet.Datastore, logger kitlog.Logger) error {
teams, err := ds.TeamsSummary(ctx)
if err != nil {
return ctxerr.Wrap(ctx, err, "getting teams summary")
}
// We try to send a webhook for each team. If one team fails, we continue.
multiErr := &multierror.Error{}
for _, teamSummary := range teams {
id := teamSummary.ID
team, err := ds.Team(ctx, id)
if err != nil {
multiErr = multierror.Append(multiErr, ctxerr.Wrap(ctx, err, "getting team"))
continue
}
if !team.Config.WebhookSettings.HostStatusWebhook.Enable {
continue
}
level.Debug(logger).Log("team", id, "enable_host_status_webhook", "true")
err = processWebhook(ctx, ds, &id, team.Config.WebhookSettings.HostStatusWebhook)
if err != nil {
multiErr = multierror.Append(multiErr, ctxerr.Wrap(ctx, err, "processing webhook"))
}
}
return multiErr.ErrorOrNil()
}