// Package alertwebhook delivers operator alerts to the notify webhook. package alertwebhook import ( "bytes" "context" "encoding/json" "errors" "fmt" "net/http" "net/url" "time" "github.com/samcharles93/archie-core/internal/domain/alert" "github.com/samcharles93/archie-core/internal/webhookguard" ) // SignatureHeader carries the payload's HMAC-SHA256 when a secret is set. const SignatureHeader = "X-Archie-Signature" const timeout = 10 * time.Second type payload struct { Event string `json:"event"` Attempt int64 `json:"attempt"` Alert body `json:"alert"` } type body struct { ID int64 `json:"id"` Org string `json:"org"` Kind string `json:"kind"` Subject string `json:"subject"` Severity string `json:"severity"` Title string `json:"title"` Body string `json:"body"` Link string `json:"link"` State string `json:"state"` FirstRaised time.Time `json:"first_raised"` LastSeen time.Time `json:"last_seen"` Count int64 `json:"count"` } // Payload is the JSON body sent for a due delivery. func Payload(due alert.Due) ([]byte, error) { a := due.Alert return json.Marshal(payload{ Event: due.Event, Attempt: due.Attempt, Alert: body{ ID: a.ID, Org: string(a.Org), Kind: a.Kind, Subject: a.Subject, Severity: string(a.Severity), Title: a.Title, Body: a.Body, Link: a.Link, State: string(a.State), FirstRaised: a.FirstRaised, LastSeen: a.LastSeen, Count: a.Count, }, }) } // Send posts due to webhook, signed with secret when it is not empty. The // outcome's error names what went wrong without the webhook URL, which may // carry a token. func Send(ctx context.Context, client *http.Client, webhook, secret string, due alert.Due) alert.Outcome { out := alert.Outcome{DeliveryID: due.ID} data, err := Payload(due) if err != nil { out.Error = "encode payload" return out } ctx, cancel := context.WithTimeout(ctx, timeout) defer cancel() req, err := http.NewRequestWithContext(ctx, http.MethodPost, webhook, bytes.NewReader(data)) if err != nil { out.Error = "invalid webhook URL" return out } req.Header.Set("Content-Type", "application/json") req.Header.Set("X-Archie-Event", due.Event) req.Header.Set("X-Archie-Delivery", fmt.Sprint(due.ID)) if secret != "" { req.Header.Set(SignatureHeader, webhookguard.SignHMAC(data, secret)) } resp, err := client.Do(req) if err != nil { if urlErr, ok := errors.AsType[*url.Error](err); ok { err = urlErr.Err } out.Error = err.Error() return out } _ = resp.Body.Close() out.StatusCode = resp.StatusCode if resp.StatusCode < 200 || resp.StatusCode > 299 { out.Error = fmt.Sprintf("webhook answered %d", resp.StatusCode) } return out }