|
package mailgun |
|
|
|
import ( |
|
"context" |
|
"crypto/hmac" |
|
"crypto/sha256" |
|
"encoding/hex" |
|
"fmt" |
|
"io" |
|
"mime" |
|
"net/http" |
|
"net/mail" |
|
"strings" |
|
"time" |
|
|
|
"github.com/google/uuid" |
|
"github.com/pkg/errors" |
|
"github.com/target/goalert/alert" |
|
"github.com/target/goalert/auth" |
|
"github.com/target/goalert/auth/authtoken" |
|
"github.com/target/goalert/config" |
|
"github.com/target/goalert/integrationkey" |
|
"github.com/target/goalert/permission" |
|
"github.com/target/goalert/retry" |
|
"github.com/target/goalert/util/errutil" |
|
"github.com/target/goalert/util/log" |
|
"github.com/target/goalert/validation" |
|
"github.com/target/goalert/validation/validate" |
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func httpError(ctx context.Context, w http.ResponseWriter, err error) bool { |
|
if err == nil { |
|
return false |
|
} |
|
|
|
var clientErr interface { |
|
ClientError() bool |
|
} |
|
|
|
if errors.As(err, &clientErr) && clientErr.ClientError() { |
|
log.Debug(ctx, err) |
|
http.Error(w, err.Error(), http.StatusNotAcceptable) |
|
return true |
|
} |
|
|
|
if errutil.IsLimitError(err) { |
|
|
|
log.Debug(ctx, err) |
|
http.Error(w, err.Error(), http.StatusNotAcceptable) |
|
return true |
|
} |
|
|
|
log.Log(ctx, err) |
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) |
|
return true |
|
} |
|
|
|
|
|
|
|
|
|
func validSignature(ctx context.Context, req *http.Request, apikey string) bool { |
|
h := hmac.New(sha256.New, []byte(apikey)) |
|
_, _ = io.WriteString(h, req.FormValue("timestamp")) |
|
_, _ = io.WriteString(h, req.FormValue("token")) |
|
|
|
calculatedSignature := h.Sum(nil) |
|
signature, err := hex.DecodeString(req.FormValue("signature")) |
|
if err != nil { |
|
return false |
|
} |
|
|
|
return hmac.Equal(signature, calculatedSignature) |
|
} |
|
|
|
type ingressHandler struct { |
|
alerts *alert.Store |
|
intKeys *integrationkey.Store |
|
} |
|
|
|
func (h *ingressHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
|
ctx := r.Context() |
|
cfg := config.FromContext(ctx) |
|
if !cfg.Mailgun.Enable { |
|
http.Error(w, "not enabled", http.StatusServiceUnavailable) |
|
return |
|
} |
|
|
|
ct := r.Header.Get("Content-Type") |
|
|
|
|
|
if ct == "" { |
|
ct = "application/octet-stream" |
|
} |
|
typ, _, err := mime.ParseMediaType(ct) |
|
if err != nil { |
|
http.Error(w, err.Error(), http.StatusNotAcceptable) |
|
return |
|
} |
|
|
|
switch typ { |
|
case "application/x-www-form-urlencoded": |
|
err = r.ParseForm() |
|
case "multipart/form-data", "multipart/mixed": |
|
err = r.ParseMultipartForm(32 << 20) |
|
} |
|
if err != nil { |
|
http.Error(w, err.Error(), http.StatusNotAcceptable) |
|
return |
|
} |
|
|
|
if !validSignature(ctx, r, cfg.Mailgun.APIKey) { |
|
log.Log(ctx, errors.New("invalid Mailgun signature")) |
|
auth.Delay(ctx) |
|
http.Error(w, "Invalid Signature", http.StatusNotAcceptable) |
|
return |
|
} |
|
|
|
recipient := r.FormValue("recipient") |
|
|
|
m, err := mail.ParseAddress(recipient) |
|
if err != nil { |
|
err = validation.NewFieldError("recipient", "must be valid email: "+err.Error()) |
|
} |
|
if httpError(ctx, w, err) { |
|
return |
|
} |
|
recipient = m.Address |
|
|
|
ctx = log.WithFields(ctx, log.Fields{ |
|
"Recipient": recipient, |
|
"FromAddress": r.FormValue("from"), |
|
}) |
|
|
|
|
|
parts := strings.SplitN(recipient, "@", 2) |
|
domain := strings.ToLower(parts[1]) |
|
if domain != cfg.Mailgun.EmailDomain { |
|
httpError(ctx, w, validation.NewFieldError("domain", "invalid domain")) |
|
return |
|
} |
|
|
|
|
|
parts = strings.SplitN(parts[0], "+", 2) |
|
err = validate.UUID("recipient", parts[0]) |
|
if httpError(ctx, w, errors.Wrap(err, "bad mailbox name")) { |
|
return |
|
} |
|
|
|
tokID, err := uuid.Parse(parts[0]) |
|
if httpError(ctx, w, err) { |
|
return |
|
} |
|
|
|
tok := authtoken.Token{ID: tokID} |
|
var dedupStr string |
|
if len(parts) > 1 { |
|
dedupStr = parts[1] |
|
} |
|
|
|
ctx = log.WithField(ctx, "IntegrationKey", tok.ID.String()) |
|
|
|
summary := validate.SanitizeText(r.FormValue("subject"), alert.MaxSummaryLength) |
|
details := fmt.Sprintf("From: %s\n\n%s", r.FormValue("from"), r.FormValue("body-plain")) |
|
details = validate.SanitizeText(details, alert.MaxDetailsLength) |
|
newAlert := &alert.Alert{ |
|
Summary: summary, |
|
Details: details, |
|
Status: alert.StatusTriggered, |
|
Source: alert.SourceEmail, |
|
Dedup: alert.NewUserDedup(dedupStr), |
|
} |
|
|
|
err = retry.DoTemporaryError(func(_ int) error { |
|
if newAlert.ServiceID == "" { |
|
ctx, err = h.intKeys.Authorize(ctx, tok, integrationkey.TypeEmail) |
|
newAlert.ServiceID = permission.ServiceID(ctx) |
|
} |
|
if err != nil { |
|
return err |
|
} |
|
_, _, err = h.alerts.CreateOrUpdate(ctx, newAlert) |
|
err = errors.Wrap(err, "create/update alert") |
|
err = errutil.MapDBError(err) |
|
return err |
|
}, |
|
retry.Log(ctx), |
|
retry.Limit(12), |
|
retry.FibBackoff(time.Second), |
|
) |
|
|
|
httpError(ctx, w, err) |
|
} |
|
|
|
|
|
|
|
|
|
func IngressWebhooks(aDB *alert.Store, intDB *integrationkey.Store) http.HandlerFunc { |
|
return (&ingressHandler{ |
|
alerts: aDB, |
|
intKeys: intDB, |
|
}).ServeHTTP |
|
} |
|
|