mirror of
https://github.com/opencloud-eu/opencloud.git
synced 2026-12-05 05:43:36 -05:00
chi's ClientIPFrom* header middlewares trust the header from anyone who can
reach us. Pick the middleware per request instead: headers are only read
when the peer is a trusted proxy, everyone else gets their network address.
- PROXY_TRUSTED_PROXIES, defaults to loopback and private ranges
- PROXY_CLIENT_IP_HEADER, defaults to X-Forwarded-For, e.g. CF-Connecting-IP
- drop the strategy setting and xff_trusted_hops
- pass the client address to the services in X-Forwarded-For instead of
X-Client-Ip, reva reads it e.g. for OCM
- same check in collaboration, traefik routes to it directly
- OC_TRUSTED_PROXIES and OC_CLIENT_IP_HEADER set both services
- the other services only drop RealIP, none of them reads the address
Taken from #3864 and https://github.com/opencloud-eu/opencloud/pull/3464#issuecomment-6494799883
# Conflicts:
# services/proxy/README.md
39 lines
1.2 KiB
Go
39 lines
1.2 KiB
Go
package middleware
|
|
|
|
import (
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/go-chi/chi/v5/middleware"
|
|
"github.com/opencloud-eu/opencloud/pkg/log"
|
|
ocmiddleware "github.com/opencloud-eu/opencloud/pkg/middleware"
|
|
"go.opentelemetry.io/otel/trace"
|
|
)
|
|
|
|
// AccessLog is a middleware to log http requests at info level logging.
|
|
func AccessLog(logger log.Logger) func(http.Handler) http.Handler {
|
|
return func(next http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
start := time.Now()
|
|
requestID := middleware.GetReqID(r.Context())
|
|
// add Request Id to all responses
|
|
w.Header().Set(middleware.RequestIDHeader, requestID)
|
|
wrap := middleware.NewWrapResponseWriter(w, r.ProtoMajor)
|
|
next.ServeHTTP(wrap, r)
|
|
|
|
spanContext := trace.SpanContextFromContext(r.Context())
|
|
logger.Info().
|
|
Str("proto", r.Proto).
|
|
Str(log.RequestIDString, requestID).
|
|
Str("traceid", spanContext.TraceID().String()).
|
|
Str("remote-addr", ocmiddleware.ClientAddress(r)).
|
|
Str("method", r.Method).
|
|
Int("status", wrap.Status()).
|
|
Str("path", r.URL.Path).
|
|
Dur("duration", time.Since(start)).
|
|
Int("bytes", wrap.BytesWritten()).
|
|
Msg("access-log")
|
|
})
|
|
}
|
|
}
|