* feat(observability): OpenTelemetry logs+traces with secret redaction Part of #265. Adds opt-in OpenTelemetry (logs + traces) alongside the existing stderr + opslog pipeline, plus secret redaction on all sinks. Default-off: with no OTEL_* / SILO_OTEL_ENABLED config, behavior is unchanged. Bootstrap (internal/telemetry): - Setup() builds one shared resource, a TracerProvider (parent-based trace-id ratio sampler), a LoggerProvider, and the W3C TraceContext+Baggage propagator from env. It installs NO MeterProvider — metrics stay on Prometheus, and the built-in no-op global MeterProvider keeps the trace instrumentation libs from double-emitting. Shutdown is deferred with a flush timeout. - Logs are bridged via otelslog fan-out (slog.MultiHandler), level-gated by the shared LevelVar and best-effort so a failing collector can't break the console or DB branches. stderr + opslog stay untouched. Secret redaction (internal/logredact): - A slog.Handler masks secret-keyed attributes (password, token, api_key, authorization, cookie, ...) — including .With-bound attrs, nested groups, secret-keyed group subtrees, and values behind a LogValuer — on the console and OTLP sinks, with a no-op fast path when a record has no secret keys. opslog.shouldRedact delegates to logredact.SecretKey so all sinks share one marker list. Rotation is infra-managed (no custom file sink): container runtime for stderr, collector/backend for OTLP, opslog partition-pruning for the DB. Documented in docs/architecture/observability.md. Verification: go build ./..., go vet, gofmt -l — clean; go test ./internal/telemetry/ ./internal/logredact/ -race pass. AI-use disclosure: implemented with AI assistance (Claude Code), including adversarial reviews that hardened the bootstrap and fixed two redaction leak paths; reviewed by the author. * refactor(observability): slog context+component sweep, sloglint gate (phase 3) Part of #265. Builds on the OTel bootstrap + redaction commit. Standardizes every log call site onto the context-carrying slog variants so records correlate with the active OpenTelemetry trace, and locks the standard in with a machine gate so future code (human- or AI-authored) can't drift back. - Call-site sweep: converted the remaining slog.<Level>(...) calls to the slog.<Level>Context(ctx, ...) form wherever a context.Context is in scope (background/init calls with no ctx are left as-is), across 183 files. Applied via a type-aware AST codemod. Log levels and message strings are preserved verbatim; a component attr (canonical per-package name) is added to direct package-level slog calls. Bound-logger calls keep their existing .With bindings. The main.go and telemetry package conversions rode with their file in the previous commit to keep each file within a single commit. - Enforcement (.golangci.yml): enable sloglint with context=scope, static-msg, key-naming-case=snake, no-mixed-args. After the sweep all four report zero violations repo-wide (tests included), so make lint / CI now blocks any regression to the non-context form. The gate ships with the sweep because it cannot be green until the legacy sites are converted. Metrics remain on Prometheus; no behavior change to /metrics or Grafana. Verification: go build ./..., go vet ./..., gofmt -l — clean; sloglint (all 4 rules) 0 violations repo-wide; log levels verified unchanged. AI-use disclosure: implemented with AI assistance (Claude Code), including the codemod; reviewed by the author. * fix(observability): honor per-signal OTLP protocol and secret WithGroup names Two Codex review findings on PR #290: - telemetry: OTEL_EXPORTER_OTLP_{TRACES,LOGS}_PROTOCOL now override the generic OTEL_EXPORTER_OTLP_PROTOCOL per signal, so mixed collector setups (e.g. HTTP logs + gRPC traces) build the right exporter. - logredact: entering a group whose name is secret-bearing (e.g. WithGroup("authorization")) now masks every leaf in that subtree, matching how slog.Group("authorization", ...) is masked as a whole. * fix(observability): address review feedback on telemetry bootstrap - Telemetry setup failure no longer kills boot: Setup returns usable no-op providers alongside the error and main logs and continues with telemetry disabled, honoring the best-effort contract. - Honor OTEL_TRACES_SAMPLER (always_on/off, traceidratio, parentbased_* variants); unsupported values fall back to parentbased_traceidratio. - Attach node identity as semconv service.instance.id instead of the non-semconv node.name. - Rename opslog retention-scope log attrs to target_component/target_level so they no longer collide with the canonical component routing key, and tag those lines with component=opslog. - Fix stale levelGated comment casing; use WarnContext in the telemetry shutdown defer; document the LogValuer double-resolve on the redaction slow path. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> --------- Co-authored-by: Quick <31828688+Quick104@users.noreply.github.com> Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
369 lines
11 KiB
Go
369 lines
11 KiB
Go
package handlers
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"io"
|
|
"log/slog"
|
|
"net/http"
|
|
"path/filepath"
|
|
"strconv"
|
|
"strings"
|
|
|
|
apimw "github.com/Silo-Server/silo-server/internal/api/middleware"
|
|
"github.com/Silo-Server/silo-server/internal/subtitles"
|
|
"github.com/go-chi/chi/v5"
|
|
)
|
|
|
|
const (
|
|
subtitleUploadMaxSize = subtitles.MaxUploadSize
|
|
// Allow multipart framing and small form fields above the file size cap.
|
|
subtitleUploadMaxBodySize = subtitleUploadMaxSize + (256 << 10)
|
|
)
|
|
|
|
func parseSubtitleMultipartForm(w http.ResponseWriter, r *http.Request) bool {
|
|
r.Body = http.MaxBytesReader(w, r.Body, subtitleUploadMaxBodySize)
|
|
if err := r.ParseMultipartForm(subtitleUploadMaxSize); err != nil {
|
|
var maxBytesErr *http.MaxBytesError
|
|
if errors.As(err, &maxBytesErr) {
|
|
writeError(w, http.StatusRequestEntityTooLarge, "too_large", "Subtitle file must be under 5 MB")
|
|
} else {
|
|
writeError(w, http.StatusBadRequest, "bad_request", "Invalid multipart form")
|
|
}
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
// SubtitleMediaResolver looks up media metadata for subtitle search.
|
|
type SubtitleMediaResolver interface {
|
|
GetMediaFileWithMetadata(ctx context.Context, fileID int) (*MediaFileMetadata, error)
|
|
}
|
|
|
|
// MediaFileMetadata combines file info with parent item metadata for search.
|
|
type MediaFileMetadata struct {
|
|
FileID int
|
|
FilePath string
|
|
FileSize int64
|
|
FileHash string // OSHash (16-char hex)
|
|
Resolution string
|
|
VideoCodec string
|
|
AudioCodec string
|
|
Title string
|
|
Year int
|
|
IMDbID string
|
|
Season int
|
|
Episode int
|
|
}
|
|
|
|
// SubtitleSearchHandler handles user-facing subtitle search operations.
|
|
type SubtitleSearchHandler struct {
|
|
manager *subtitles.Manager
|
|
repo subtitles.Repository
|
|
mediaResolver SubtitleMediaResolver
|
|
FileAuthorizer *MediaFileAuthorizer
|
|
}
|
|
|
|
// NewSubtitleSearchHandler creates a new SubtitleSearchHandler.
|
|
func NewSubtitleSearchHandler(
|
|
manager *subtitles.Manager,
|
|
repo subtitles.Repository,
|
|
mediaResolver SubtitleMediaResolver,
|
|
) *SubtitleSearchHandler {
|
|
return &SubtitleSearchHandler{
|
|
manager: manager,
|
|
repo: repo,
|
|
mediaResolver: mediaResolver,
|
|
}
|
|
}
|
|
|
|
type searchSubtitlesRequest struct {
|
|
MediaFileID int `json:"media_file_id"`
|
|
Languages []string `json:"languages"`
|
|
}
|
|
|
|
type downloadSubtitleRequest struct {
|
|
MediaFileID int `json:"media_file_id"`
|
|
Provider string `json:"provider"`
|
|
SubtitleID string `json:"subtitle_id"`
|
|
Language string `json:"language"`
|
|
ReleaseName string `json:"release_name"`
|
|
Format string `json:"format"`
|
|
Score float64 `json:"score"`
|
|
HearingImpaired bool `json:"hearing_impaired"`
|
|
}
|
|
|
|
func (h *SubtitleSearchHandler) authorizeMediaFile(w http.ResponseWriter, r *http.Request, fileID int) bool {
|
|
return authorizeMediaFileAccess(w, r, h.FileAuthorizer, fileID)
|
|
}
|
|
|
|
// HandleSearch handles POST /api/v1/subtitles/search
|
|
func (h *SubtitleSearchHandler) HandleSearch(w http.ResponseWriter, r *http.Request) {
|
|
var req searchSubtitlesRequest
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
writeError(w, http.StatusBadRequest, "invalid_request", "Invalid request body")
|
|
return
|
|
}
|
|
|
|
if !h.authorizeMediaFile(w, r, req.MediaFileID) {
|
|
return
|
|
}
|
|
|
|
meta, err := h.mediaResolver.GetMediaFileWithMetadata(r.Context(), req.MediaFileID)
|
|
if err != nil {
|
|
writeError(w, http.StatusInternalServerError, "metadata_error", "Failed to look up media metadata")
|
|
return
|
|
}
|
|
if meta == nil {
|
|
writeError(w, http.StatusNotFound, "not_found", "Media file not found")
|
|
return
|
|
}
|
|
|
|
releaseInfo := subtitles.ParseReleaseInfo(meta.FilePath)
|
|
searchReq := subtitles.SearchRequest{
|
|
IMDbID: meta.IMDbID,
|
|
Title: meta.Title,
|
|
Year: meta.Year,
|
|
Season: meta.Season,
|
|
Episode: meta.Episode,
|
|
Languages: req.Languages,
|
|
Filename: filepath.Base(meta.FilePath),
|
|
FileHash: meta.FileHash,
|
|
MediaInfo: &subtitles.MediaMatchInfo{
|
|
ReleaseGroup: releaseInfo.ReleaseGroup,
|
|
Resolution: firstNonEmpty(meta.Resolution, releaseInfo.Resolution),
|
|
VideoCodec: firstNonEmpty(meta.VideoCodec, releaseInfo.VideoCodec),
|
|
AudioCodec: firstNonEmpty(meta.AudioCodec, releaseInfo.AudioCodec),
|
|
Source: releaseInfo.Source,
|
|
},
|
|
}
|
|
|
|
resp, err := h.manager.Search(r.Context(), searchReq)
|
|
if err != nil {
|
|
writeError(w, http.StatusInternalServerError, "search_error", "Subtitle search failed")
|
|
return
|
|
}
|
|
|
|
writeJSON(w, http.StatusOK, resp)
|
|
}
|
|
|
|
// HandleDownload handles POST /api/v1/subtitles/download
|
|
func (h *SubtitleSearchHandler) HandleDownload(w http.ResponseWriter, r *http.Request) {
|
|
var req downloadSubtitleRequest
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
writeError(w, http.StatusBadRequest, "invalid_request", "Invalid request body")
|
|
return
|
|
}
|
|
|
|
if !h.authorizeMediaFile(w, r, req.MediaFileID) {
|
|
return
|
|
}
|
|
|
|
userID := apimw.GetUserID(r.Context())
|
|
|
|
sub, err := h.manager.Download(r.Context(), subtitles.DownloadRequest{
|
|
ProviderName: req.Provider,
|
|
SubtitleID: req.SubtitleID,
|
|
MediaFileID: req.MediaFileID,
|
|
UserID: &userID,
|
|
Language: req.Language,
|
|
ReleaseName: req.ReleaseName,
|
|
Score: req.Score,
|
|
HearingImpaired: req.HearingImpaired,
|
|
})
|
|
if err != nil {
|
|
slog.ErrorContext(r.Context(), "subtitle download failed", "component", "api", "provider", req.Provider, "subtitle_id", req.SubtitleID, "error", err)
|
|
writeError(w, http.StatusInternalServerError, "download_error", "Failed to download subtitle")
|
|
return
|
|
}
|
|
|
|
writeJSON(w, http.StatusOK, map[string]interface{}{"subtitle": sub})
|
|
}
|
|
|
|
// HandleUpload handles POST /api/v1/subtitles/upload
|
|
func (h *SubtitleSearchHandler) HandleUpload(w http.ResponseWriter, r *http.Request) {
|
|
if !parseSubtitleMultipartForm(w, r) {
|
|
return
|
|
}
|
|
|
|
mediaFileID, err := strconv.Atoi(strings.TrimSpace(r.FormValue("media_file_id")))
|
|
if err != nil || mediaFileID <= 0 {
|
|
writeError(w, http.StatusBadRequest, "bad_request", "Invalid media_file_id")
|
|
return
|
|
}
|
|
|
|
if !h.authorizeMediaFile(w, r, mediaFileID) {
|
|
return
|
|
}
|
|
|
|
file, header, err := r.FormFile("file")
|
|
if err != nil {
|
|
writeError(w, http.StatusBadRequest, "bad_request", "Missing subtitle file")
|
|
return
|
|
}
|
|
defer file.Close()
|
|
|
|
data, err := io.ReadAll(io.LimitReader(file, subtitleUploadMaxSize+1))
|
|
if err != nil {
|
|
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to read upload")
|
|
return
|
|
}
|
|
if len(data) > subtitleUploadMaxSize {
|
|
writeError(w, http.StatusRequestEntityTooLarge, "too_large", "Subtitle file must be under 5 MB")
|
|
return
|
|
}
|
|
|
|
releaseName := strings.TrimSpace(r.FormValue("release_name"))
|
|
hearingImpaired := parseBoolFormValue(r.FormValue("hearing_impaired"))
|
|
userID := apimw.GetUserID(r.Context())
|
|
|
|
userLanguage := strings.TrimSpace(r.FormValue("language"))
|
|
preferUserLanguage := parseBoolFormValue(r.FormValue("language_override"))
|
|
|
|
sub, err := h.manager.Upload(r.Context(), subtitles.UploadRequest{
|
|
MediaFileID: mediaFileID,
|
|
UserID: &userID,
|
|
Language: userLanguage,
|
|
PreferUserLanguage: preferUserLanguage,
|
|
Filename: header.Filename,
|
|
ReleaseName: releaseName,
|
|
HearingImpaired: hearingImpaired,
|
|
Data: data,
|
|
})
|
|
if err != nil {
|
|
switch {
|
|
case strings.Contains(err.Error(), "unsupported subtitle format"),
|
|
strings.Contains(err.Error(), "missing file extension"),
|
|
strings.Contains(err.Error(), "empty subtitle file"),
|
|
strings.Contains(err.Error(), "could not detect subtitle language"),
|
|
strings.Contains(err.Error(), "invalid subtitle language"):
|
|
writeError(w, http.StatusBadRequest, "bad_request", err.Error())
|
|
case strings.Contains(err.Error(), "exceeds maximum size"):
|
|
writeError(w, http.StatusRequestEntityTooLarge, "too_large", "Subtitle file must be under 5 MB")
|
|
default:
|
|
slog.ErrorContext(r.Context(), "subtitle upload failed", "component", "api", "media_file_id", mediaFileID, "error", err)
|
|
writeError(w, http.StatusInternalServerError, "upload_error", "Failed to upload subtitle")
|
|
}
|
|
return
|
|
}
|
|
|
|
writeJSON(w, http.StatusOK, map[string]interface{}{"subtitle": sub})
|
|
}
|
|
|
|
// HandleDetectLanguage handles POST /api/v1/subtitles/detect-language
|
|
func (h *SubtitleSearchHandler) HandleDetectLanguage(w http.ResponseWriter, r *http.Request) {
|
|
if !parseSubtitleMultipartForm(w, r) {
|
|
return
|
|
}
|
|
|
|
file, header, err := r.FormFile("file")
|
|
if err != nil {
|
|
writeError(w, http.StatusBadRequest, "bad_request", "Missing subtitle file")
|
|
return
|
|
}
|
|
defer file.Close()
|
|
|
|
data, err := io.ReadAll(io.LimitReader(file, subtitleUploadMaxSize+1))
|
|
if err != nil {
|
|
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to read upload")
|
|
return
|
|
}
|
|
if len(data) > subtitleUploadMaxSize {
|
|
writeError(w, http.StatusRequestEntityTooLarge, "too_large", "Subtitle file must be under 5 MB")
|
|
return
|
|
}
|
|
|
|
format, err := subtitles.FormatFromFilename(header.Filename)
|
|
if err != nil {
|
|
writeError(w, http.StatusBadRequest, "bad_request", err.Error())
|
|
return
|
|
}
|
|
|
|
userLanguage := strings.TrimSpace(r.FormValue("language"))
|
|
detected, err := subtitles.ResolveUploadLanguage(header.Filename, format, data, userLanguage, false)
|
|
if err != nil {
|
|
writeError(w, http.StatusBadRequest, "bad_request", err.Error())
|
|
return
|
|
}
|
|
|
|
writeJSON(w, http.StatusOK, detected)
|
|
}
|
|
|
|
// HandleList handles GET /api/v1/subtitles/{media_file_id}
|
|
func (h *SubtitleSearchHandler) HandleList(w http.ResponseWriter, r *http.Request) {
|
|
mediaFileID, err := strconv.Atoi(chi.URLParam(r, "media_file_id"))
|
|
if err != nil {
|
|
writeError(w, http.StatusBadRequest, "invalid_id", "Invalid media file ID")
|
|
return
|
|
}
|
|
|
|
if !h.authorizeMediaFile(w, r, mediaFileID) {
|
|
return
|
|
}
|
|
|
|
subs, err := h.repo.ListDownloadedSubtitles(r.Context(), mediaFileID)
|
|
if err != nil {
|
|
writeError(w, http.StatusInternalServerError, "list_error", "Failed to list subtitles")
|
|
return
|
|
}
|
|
|
|
writeJSON(w, http.StatusOK, map[string]interface{}{"subtitles": subs})
|
|
}
|
|
|
|
// HandleDelete handles DELETE /api/v1/subtitles/{id}
|
|
func (h *SubtitleSearchHandler) HandleDelete(w http.ResponseWriter, r *http.Request) {
|
|
id, err := strconv.Atoi(chi.URLParam(r, "id"))
|
|
if err != nil {
|
|
writeError(w, http.StatusBadRequest, "invalid_id", "Invalid subtitle ID")
|
|
return
|
|
}
|
|
|
|
sub, err := h.repo.GetDownloadedSubtitle(r.Context(), id)
|
|
if err != nil {
|
|
writeError(w, http.StatusInternalServerError, "lookup_error", "Failed to look up subtitle")
|
|
return
|
|
}
|
|
if sub == nil {
|
|
writeError(w, http.StatusNotFound, "not_found", "Subtitle not found")
|
|
return
|
|
}
|
|
|
|
if !h.authorizeMediaFile(w, r, sub.MediaFileID) {
|
|
return
|
|
}
|
|
|
|
claims := apimw.GetClaims(r.Context())
|
|
isAdmin := claims != nil && claims.Role == "admin"
|
|
isOwner := sub.DownloadedBy != nil && claims != nil && *sub.DownloadedBy == claims.UserID
|
|
if !isAdmin && !isOwner {
|
|
writeError(w, http.StatusForbidden, "forbidden", "Not authorized to delete this subtitle")
|
|
return
|
|
}
|
|
|
|
if err := h.manager.DeleteSubtitle(r.Context(), id); err != nil {
|
|
writeError(w, http.StatusInternalServerError, "delete_error", "Failed to delete subtitle")
|
|
return
|
|
}
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
}
|
|
|
|
func firstNonEmpty(values ...string) string {
|
|
for _, v := range values {
|
|
if v != "" {
|
|
return v
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func parseBoolFormValue(value string) bool {
|
|
switch strings.ToLower(strings.TrimSpace(value)) {
|
|
case "1", "true", "yes", "on":
|
|
return true
|
|
default:
|
|
return false
|
|
}
|
|
}
|