Files
bpmforge e0d82a4ed0 Kryptkeeper 1.0.0
First stable release. Notes: docs/release/RELEASE_NOTES_v1.0.0.md
Lab record: docs/release/LIVE_RESULTS_v1.0.0.md
2026-10-19 14:43:23 -04:00

134 lines
3.1 KiB
Go

// Copyright 2026 Kryptkeeper Authors
//
// SPDX-License-Identifier: Apache-2.0
package bootstrap
import (
"context"
"crypto/rand"
"encoding/hex"
"log/slog"
"net/http"
"strings"
"time"
"kryptkeeper/internal/logging"
)
type requestIDContextKey struct{}
const requestIDHeader = "X-Request-ID"
func withRequestID(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
requestID := normalizeRequestID(r.Header.Get(requestIDHeader))
if requestID == "" {
requestID = generateRequestID()
}
ctx := context.WithValue(r.Context(), requestIDContextKey{}, requestID)
ctx = logging.WithRequestID(ctx, requestID)
r = r.WithContext(ctx)
w.Header().Set(requestIDHeader, requestID)
next.ServeHTTP(w, r)
})
}
func requestIDFromContext(ctx context.Context) string {
requestID, _ := ctx.Value(requestIDContextKey{}).(string)
return requestID
}
func normalizeRequestID(value string) string {
trimmed := strings.TrimSpace(value)
if trimmed == "" {
return ""
}
if len(trimmed) > 128 {
return ""
}
for _, r := range trimmed {
if (r >= 'a' && r <= 'z') || (r >= 'A' && r <= 'Z') || (r >= '0' && r <= '9') {
continue
}
switch r {
case '-', '_', '.', ':':
continue
default:
return ""
}
}
return trimmed
}
func generateRequestID() string {
raw := make([]byte, 12)
if _, err := rand.Read(raw); err != nil {
return "req_fallback"
}
return "req_" + hex.EncodeToString(raw)
}
// statusRecorder captures status and byte count for access logging. It keeps
// http.Flusher and Unwrap so SSE endpoints and http.ResponseController keep
// working through the wrapper.
type statusRecorder struct {
http.ResponseWriter
status int
bytes int64
wroteHeader bool
}
func (s *statusRecorder) WriteHeader(code int) {
if !s.wroteHeader {
s.status = code
s.wroteHeader = true
}
s.ResponseWriter.WriteHeader(code)
}
func (s *statusRecorder) Write(b []byte) (int, error) {
if !s.wroteHeader {
s.status = http.StatusOK
s.wroteHeader = true
}
n, err := s.ResponseWriter.Write(b)
s.bytes += int64(n)
return n, err
}
func (s *statusRecorder) Flush() {
if f, ok := s.ResponseWriter.(http.Flusher); ok {
f.Flush()
}
}
func (s *statusRecorder) Unwrap() http.ResponseWriter { return s.ResponseWriter }
// withAccessLog emits one structured line per request. It logs r.URL.Path
// only: the query string routinely carries tokens.
func withAccessLog(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
rec := &statusRecorder{ResponseWriter: w, status: http.StatusOK}
next.ServeHTTP(rec, r)
level := slog.LevelInfo
switch {
case rec.status >= 500:
level = slog.LevelError
case r.URL.Path == "/healthz" || r.URL.Path == "/readyz" || r.URL.Path == "/metrics":
level = slog.LevelDebug
}
slog.LogAttrs(r.Context(), level, "http request",
slog.String("method", r.Method),
slog.String("path", r.URL.Path),
slog.Int("status", rec.status),
slog.Int64("latency_ms", time.Since(start).Milliseconds()),
slog.Int64("bytes", rec.bytes),
slog.String("tenant", auditRequestTenant(r)),
)
})
}