|
@@ -7,9 +7,14 @@ import (
|
|
|
"github.com/OliveTin/OliveTin/internal/auth"
|
|
"github.com/OliveTin/OliveTin/internal/auth"
|
|
|
"github.com/OliveTin/OliveTin/internal/config"
|
|
"github.com/OliveTin/OliveTin/internal/config"
|
|
|
"github.com/OliveTin/OliveTin/internal/executor"
|
|
"github.com/OliveTin/OliveTin/internal/executor"
|
|
|
|
|
+ "github.com/google/uuid"
|
|
|
log "github.com/sirupsen/logrus"
|
|
log "github.com/sirupsen/logrus"
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
|
|
+// CorrelationIDHeader is the response header carrying the ID that appears in
|
|
|
|
|
+// OliveTin's log lines for the same webhook request.
|
|
|
|
|
+const CorrelationIDHeader = "X-OliveTin-Log-Correlation-ID"
|
|
|
|
|
+
|
|
|
type ActionWebhookConfig struct {
|
|
type ActionWebhookConfig struct {
|
|
|
Action *config.Action
|
|
Action *config.Action
|
|
|
Config config.WebhookConfig
|
|
Config config.WebhookConfig
|
|
@@ -33,7 +38,11 @@ func (h *WebhookHandler) HandleWebhook(w http.ResponseWriter, r *http.Request) {
|
|
|
return
|
|
return
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- payload, err := h.readPayload(r)
|
|
|
|
|
|
|
+ correlationID := uuid.NewString()
|
|
|
|
|
+ w.Header().Set(CorrelationIDHeader, correlationID)
|
|
|
|
|
+ reqLog := log.WithFields(webhookRequestFields(r, correlationID))
|
|
|
|
|
+
|
|
|
|
|
+ payload, err := h.readPayload(r, reqLog)
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
http.Error(w, "Failed to read payload", http.StatusBadRequest)
|
|
http.Error(w, "Failed to read payload", http.StatusBadRequest)
|
|
|
return
|
|
return
|
|
@@ -41,13 +50,13 @@ func (h *WebhookHandler) HandleWebhook(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
|
|
matchingActions := h.findMatchingActions(r, payload)
|
|
matchingActions := h.findMatchingActions(r, payload)
|
|
|
if len(matchingActions) == 0 {
|
|
if len(matchingActions) == 0 {
|
|
|
- log.WithFields(webhookRequestFields(r)).Infof("Webhook: no matching actions")
|
|
|
|
|
|
|
+ reqLog.Infof("Webhook: no matching actions")
|
|
|
h.writeOKResponse(w, "no matching webhook actions")
|
|
h.writeOKResponse(w, "no matching webhook actions")
|
|
|
return
|
|
return
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- processed := h.processMatchingActions(matchingActions, r, payload)
|
|
|
|
|
- log.WithFields(webhookRequestFields(r)).WithFields(log.Fields{
|
|
|
|
|
|
|
+ processed := h.processMatchingActions(matchingActions, r, payload, reqLog)
|
|
|
|
|
+ reqLog.WithFields(log.Fields{
|
|
|
"matched": len(matchingActions),
|
|
"matched": len(matchingActions),
|
|
|
"processed": processed,
|
|
"processed": processed,
|
|
|
}).Infof("Webhook: matched")
|
|
}).Infof("Webhook: matched")
|
|
@@ -55,19 +64,20 @@ func (h *WebhookHandler) HandleWebhook(w http.ResponseWriter, r *http.Request) {
|
|
|
h.writeOKResponse(w, "webhook actions")
|
|
h.writeOKResponse(w, "webhook actions")
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func webhookRequestFields(r *http.Request) log.Fields {
|
|
|
|
|
|
|
+func webhookRequestFields(r *http.Request, correlationID string) log.Fields {
|
|
|
return log.Fields{
|
|
return log.Fields{
|
|
|
- "method": r.Method,
|
|
|
|
|
- "path": r.URL.Path,
|
|
|
|
|
- "query": r.URL.RawQuery,
|
|
|
|
|
|
|
+ "correlationId": correlationID,
|
|
|
|
|
+ "method": r.Method,
|
|
|
|
|
+ "path": r.URL.Path,
|
|
|
|
|
+ "query": r.URL.RawQuery,
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func (h *WebhookHandler) readPayload(r *http.Request) ([]byte, error) {
|
|
|
|
|
|
|
+func (h *WebhookHandler) readPayload(r *http.Request, reqLog *log.Entry) ([]byte, error) {
|
|
|
maxSize := int64(1024 * 1024)
|
|
maxSize := int64(1024 * 1024)
|
|
|
payload, err := io.ReadAll(io.LimitReader(r.Body, maxSize))
|
|
payload, err := io.ReadAll(io.LimitReader(r.Body, maxSize))
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
- log.WithFields(log.Fields{
|
|
|
|
|
|
|
+ reqLog.WithFields(log.Fields{
|
|
|
"error": err,
|
|
"error": err,
|
|
|
}).Warnf("Failed to read webhook payload")
|
|
}).Warnf("Failed to read webhook payload")
|
|
|
return nil, err
|
|
return nil, err
|
|
@@ -83,10 +93,10 @@ func (h *WebhookHandler) writeOKResponse(w http.ResponseWriter, context string)
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func (h *WebhookHandler) processMatchingActions(matchingActions []ActionWebhookConfig, r *http.Request, payload []byte) int {
|
|
|
|
|
|
|
+func (h *WebhookHandler) processMatchingActions(matchingActions []ActionWebhookConfig, r *http.Request, payload []byte, reqLog *log.Entry) int {
|
|
|
processed := 0
|
|
processed := 0
|
|
|
for _, actionConfig := range matchingActions {
|
|
for _, actionConfig := range matchingActions {
|
|
|
- if h.processWebhook(actionConfig, r, payload) {
|
|
|
|
|
|
|
+ if h.processWebhook(actionConfig, r, payload, reqLog) {
|
|
|
processed++
|
|
processed++
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -125,10 +135,10 @@ func (h *WebhookHandler) findMatchingWebhooksForAction(action *config.Action, r
|
|
|
return matches
|
|
return matches
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func (h *WebhookHandler) processWebhook(actionConfig ActionWebhookConfig, r *http.Request, payload []byte) bool {
|
|
|
|
|
|
|
+func (h *WebhookHandler) processWebhook(actionConfig ActionWebhookConfig, r *http.Request, payload []byte, reqLog *log.Entry) bool {
|
|
|
verifier := NewAuthVerifier(actionConfig.Config)
|
|
verifier := NewAuthVerifier(actionConfig.Config)
|
|
|
if !verifier.Verify(r, payload) {
|
|
if !verifier.Verify(r, payload) {
|
|
|
- log.WithFields(log.Fields{
|
|
|
|
|
|
|
+ reqLog.WithFields(log.Fields{
|
|
|
"actionTitle": actionConfig.Action.Title,
|
|
"actionTitle": actionConfig.Action.Title,
|
|
|
"authType": actionConfig.Config.AuthType,
|
|
"authType": actionConfig.Config.AuthType,
|
|
|
}).Warnf("Webhook authentication failed")
|
|
}).Warnf("Webhook authentication failed")
|
|
@@ -139,7 +149,7 @@ func (h *WebhookHandler) processWebhook(actionConfig ActionWebhookConfig, r *htt
|
|
|
|
|
|
|
|
args, err := matcher.ExtractArguments()
|
|
args, err := matcher.ExtractArguments()
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
- log.WithFields(log.Fields{
|
|
|
|
|
|
|
+ reqLog.WithFields(log.Fields{
|
|
|
"actionTitle": actionConfig.Action.Title,
|
|
"actionTitle": actionConfig.Action.Title,
|
|
|
"error": err,
|
|
"error": err,
|
|
|
}).Warnf("Failed to extract webhook arguments")
|
|
}).Warnf("Failed to extract webhook arguments")
|
|
@@ -148,21 +158,21 @@ func (h *WebhookHandler) processWebhook(actionConfig ActionWebhookConfig, r *htt
|
|
|
|
|
|
|
|
justification, err := matcher.ExtractJustification()
|
|
justification, err := matcher.ExtractJustification()
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
- log.WithFields(log.Fields{
|
|
|
|
|
|
|
+ reqLog.WithFields(log.Fields{
|
|
|
"actionTitle": actionConfig.Action.Title,
|
|
"actionTitle": actionConfig.Action.Title,
|
|
|
"error": err,
|
|
"error": err,
|
|
|
}).Warnf("Failed to extract webhook justification")
|
|
}).Warnf("Failed to extract webhook justification")
|
|
|
return false
|
|
return false
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- h.executeAction(actionConfig.Action, args, justification)
|
|
|
|
|
|
|
+ h.executeAction(actionConfig.Action, args, justification, reqLog)
|
|
|
return true
|
|
return true
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func (h *WebhookHandler) executeAction(action *config.Action, args map[string]string, justification string) {
|
|
|
|
|
|
|
+func (h *WebhookHandler) executeAction(action *config.Action, args map[string]string, justification string, reqLog *log.Entry) {
|
|
|
binding := h.executor.FindBindingWithNoEntity(action)
|
|
binding := h.executor.FindBindingWithNoEntity(action)
|
|
|
if binding == nil {
|
|
if binding == nil {
|
|
|
- log.WithFields(log.Fields{
|
|
|
|
|
|
|
+ reqLog.WithFields(log.Fields{
|
|
|
"actionTitle": action.Title,
|
|
"actionTitle": action.Title,
|
|
|
}).Warnf("Action binding not found, skipping execution")
|
|
}).Warnf("Action binding not found, skipping execution")
|
|
|
return
|
|
return
|