* docs(autoscan): add arr webhook intake spec and implementation plan Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * feat(autoscan): add webhook intake schema migration Adds delivery_mode to autoscan_sources, the autoscan_webhook_endpoints table, and delivery_mode/provider_event_type on autoscan_events. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * feat(autoscan): add built-in arr-webhook source identity Host-discovered scan-source entry so webhook-mode sources need no plugin installation; composite lister appends it to plugin discovery. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * feat(autoscan): persist delivery mode, webhook endpoints, event metadata Sources carry delivery_mode; autoscan_webhook_endpoints CRUD with SHA-256 token lookup and AAD-bound encrypted redisplay; events record delivery_mode/provider_event_type; CreateEvent gains SkipRunningCheck so webhook deliveries are never dropped by the poll exclusion. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * feat(autoscan): share the consume path and add webhook IngestChanges Extracts consumeSourceChanges from PollOnce (marker semantics preserved, existing poll tests unchanged); PollOnce skips webhook sources; IngestChanges feeds deliveries through the shared pipeline without markers and without the running-event exclusion. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * feat(autoscan): add Sonarr/Radarr webhook payload parser Host-side arrwebhook package: provider inference, import/rename/delete path extraction with vanished-path-friendly previous paths, subtree fallback, exact-path dedupe, and no-op unknown events. Fixture-backed. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * feat(autoscan): add public webhook delivery route and admin endpoint management Public POST /api/v1/autoscan/webhooks/{token} with per-IP rate limiting, 256KiB body cap, 202-for-noop semantics, and token/body kept out of logs; admin create/rotate/delete endpoint routes; source responses carry delivery mode + webhook status/URL; create/update validate delivery mode against source identity. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * feat(web): add webhook delivery mode to Autoscan admin UI Webhook sources get a generate/copy/rotate webhook URL section, provider selector, delivery status, and a connection-free Add-source flow; activity rows badge webhook deliveries with the arr event type. Path rewrites stay editable in both modes. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(api): redact secret path params from request and activity logs The request logger and activity-log middleware recorded raw URLs, so bearer credentials in secret path segments (autoscan webhook {token}, webhook-sync {secret}) were persisted to app logs and activity_log. Redact the secret segment via the chi route params in both sinks. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(autoscan): make webhook delivery reliable --------- Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
104 lines
2.8 KiB
Go
104 lines
2.8 KiB
Go
package middleware
|
|
|
|
import (
|
|
"bufio"
|
|
"fmt"
|
|
"log/slog"
|
|
"net"
|
|
"net/http"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
chimw "github.com/go-chi/chi/v5/middleware"
|
|
|
|
"github.com/Silo-Server/silo-server/internal/activitylog"
|
|
"github.com/Silo-Server/silo-server/internal/clientip"
|
|
)
|
|
|
|
func RequestLogger(nodeID string) func(http.Handler) http.Handler {
|
|
return func(next http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
for _, prefix := range []string{"/api/v1/health", "/api/v1/ready", "/api/v1/admin/logs"} {
|
|
if strings.HasPrefix(r.URL.Path, prefix) {
|
|
next.ServeHTTP(w, r)
|
|
return
|
|
}
|
|
}
|
|
|
|
start := time.Now()
|
|
wrapped := &requestStatusWriter{ResponseWriter: w, status: http.StatusOK}
|
|
lc := activitylog.GetLogContext(r.Context())
|
|
if lc == nil {
|
|
lc = &activitylog.LogContext{}
|
|
r = r.WithContext(activitylog.SetLogContext(r.Context(), lc))
|
|
}
|
|
playbackLC := activitylog.GetPlaybackLogContext(r.Context())
|
|
if playbackLC == nil {
|
|
playbackLC = &activitylog.PlaybackLogContext{}
|
|
r = r.WithContext(activitylog.SetPlaybackLogContext(r.Context(), playbackLC))
|
|
}
|
|
|
|
next.ServeHTTP(wrapped, r)
|
|
|
|
pathPattern := r.URL.Path
|
|
if routeCtx := chi.RouteContext(r.Context()); routeCtx != nil {
|
|
if route := routeCtx.RoutePattern(); route != "" {
|
|
pathPattern = route
|
|
}
|
|
}
|
|
|
|
attrs := []any{
|
|
"component", "api",
|
|
"request_id", chimw.GetReqID(r.Context()),
|
|
"method", r.Method,
|
|
"path", activitylog.RedactSecretPathParams(r, r.URL.Path),
|
|
"path_pattern", pathPattern,
|
|
"status", wrapped.status,
|
|
"duration_ms", time.Since(start).Milliseconds(),
|
|
"client_ip", clientip.FromContext(r.Context()),
|
|
"user_agent", r.UserAgent(),
|
|
"node_id", nodeID,
|
|
}
|
|
if lc.UserID != nil {
|
|
attrs = append(attrs, "user_id", *lc.UserID)
|
|
}
|
|
if lc.SessionID != "" {
|
|
attrs = append(attrs, "session_id", lc.SessionID)
|
|
}
|
|
if playbackLC.PlaybackSessionID != "" {
|
|
attrs = append(attrs, "playback_session_id", playbackLC.PlaybackSessionID)
|
|
}
|
|
slog.InfoContext(r.Context(), "api request", append([]any{"component", "api"}, attrs...)...)
|
|
})
|
|
}
|
|
}
|
|
|
|
type requestStatusWriter struct {
|
|
http.ResponseWriter
|
|
status int
|
|
wroteHeader bool
|
|
}
|
|
|
|
func (w *requestStatusWriter) WriteHeader(status int) {
|
|
if !w.wroteHeader {
|
|
w.status = status
|
|
w.wroteHeader = true
|
|
}
|
|
w.ResponseWriter.WriteHeader(status)
|
|
}
|
|
|
|
func (w *requestStatusWriter) Write(b []byte) (int, error) {
|
|
if !w.wroteHeader {
|
|
w.wroteHeader = true
|
|
}
|
|
return w.ResponseWriter.Write(b)
|
|
}
|
|
|
|
func (w *requestStatusWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
|
|
if hj, ok := w.ResponseWriter.(http.Hijacker); ok {
|
|
return hj.Hijack()
|
|
}
|
|
return nil, nil, fmt.Errorf("underlying ResponseWriter does not implement http.Hijacker")
|
|
}
|