Ver Fonte

feat(webhooks): add log correlation ID to webhook requests

Each webhook POST now gets a UUID that is returned in the
X-OliveTin-Log-Correlation-ID response header and added as a
correlationId field on every webhook log line for that request
(match, no match, auth and extraction failures). Callers can use it
to find the matching OliveTin log lines.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
jamesread há 3 dias atrás
pai
commit
f1907bee21

+ 29 - 19
service/internal/webhooks/handler.go

@@ -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

+ 85 - 0
service/internal/webhooks/handler_correlation_test.go

@@ -0,0 +1,85 @@
+package webhooks
+
+import (
+	"net/http"
+	"net/http/httptest"
+	"strings"
+	"testing"
+
+	"github.com/OliveTin/OliveTin/internal/config"
+	"github.com/google/uuid"
+	log "github.com/sirupsen/logrus"
+	"github.com/sirupsen/logrus/hooks/test"
+	"github.com/stretchr/testify/assert"
+	"github.com/stretchr/testify/require"
+)
+
+func postWebhook(t *testing.T, cfg *config.Config) (*httptest.ResponseRecorder, *test.Hook) {
+	t.Helper()
+	hook := test.NewGlobal()
+	t.Cleanup(func() { log.StandardLogger().ReplaceHooks(make(log.LevelHooks)) })
+
+	req := httptest.NewRequestWithContext(t.Context(), http.MethodPost, "/webhooks?source=ci", strings.NewReader(`{"ref":"main"}`))
+	rec := httptest.NewRecorder()
+	NewWebhookHandler(cfg, nil).HandleWebhook(rec, req)
+
+	return rec, hook
+}
+
+func requireCorrelationID(t *testing.T, rec *httptest.ResponseRecorder) string {
+	t.Helper()
+	id := rec.Header().Get(CorrelationIDHeader)
+	_, err := uuid.Parse(id)
+	require.NoError(t, err, "header should be a bare UUID, got %q", id)
+	return id
+}
+
+func findLogEntry(hook *test.Hook, message string) *log.Entry {
+	for _, entry := range hook.AllEntries() {
+		if entry.Message == message {
+			return entry
+		}
+	}
+	return nil
+}
+
+func TestWebhookNoMatchReturnsCorrelationIDInHeaderAndLog(t *testing.T) {
+	rec, hook := postWebhook(t, config.DefaultConfig())
+
+	assert.Equal(t, http.StatusOK, rec.Code)
+	id := requireCorrelationID(t, rec)
+
+	entry := findLogEntry(hook, "Webhook: no matching actions")
+	require.NotNil(t, entry)
+	assert.Equal(t, id, entry.Data["correlationId"])
+}
+
+func TestWebhookMatchUsesSameCorrelationIDForEveryLogLine(t *testing.T) {
+	cfg := config.DefaultConfig()
+	cfg.Actions = []*config.Action{{
+		Title: "Deploy",
+		ExecOnWebhook: []config.WebhookConfig{{
+			AuthType: "bearer",
+			Secret:   "not-sent-by-this-request",
+		}},
+	}}
+
+	rec, hook := postWebhook(t, cfg)
+
+	id := requireCorrelationID(t, rec)
+
+	matched := findLogEntry(hook, "Webhook: matched")
+	require.NotNil(t, matched)
+	assert.Equal(t, id, matched.Data["correlationId"])
+
+	authFailed := findLogEntry(hook, "Webhook authentication failed")
+	require.NotNil(t, authFailed)
+	assert.Equal(t, id, authFailed.Data["correlationId"])
+}
+
+func TestWebhookCorrelationIDIsUniquePerRequest(t *testing.T) {
+	first, _ := postWebhook(t, config.DefaultConfig())
+	second, _ := postWebhook(t, config.DefaultConfig())
+
+	assert.NotEqual(t, requireCorrelationID(t, first), requireCorrelationID(t, second))
+}