feat: enhanced logging

This commit is contained in:
Michael Bruns
2025-10-30 07:55:50 +01:00
committed by GitHub
parent 1af4c723f4
commit 24abd52e23
+22 -8
View File
@@ -25,6 +25,11 @@ type Handler struct {
func NewHandler(c *Config) (http.Handler, error) { func NewHandler(c *Config) (http.Handler, error) {
ls := webdav.NewMemLS() ls := webdav.NewMemLS()
logFunc := func(r *http.Request, err error) {
lZap := getRequestLogger(r, c.BehindProxy)
lZap.Debug("handle webdav request", zap.String("method", r.Method), zap.String("path", r.URL.Path), zap.Error(err))
}
h := &Handler{ h := &Handler{
noPassword: c.NoPassword, noPassword: c.NoPassword,
behindProxy: c.BehindProxy, behindProxy: c.BehindProxy,
@@ -42,6 +47,7 @@ func NewHandler(c *Config) (http.Handler, error) {
LockSystem: ls, LockSystem: ls,
directory: c.Directory, directory: c.Directory,
}, },
Logger: logFunc,
}, },
}, },
users: map[string]*handlerUser{}, users: map[string]*handlerUser{},
@@ -60,6 +66,7 @@ func NewHandler(c *Config) (http.Handler, error) {
LockSystem: ls, LockSystem: ls,
directory: u.Directory, directory: u.Directory,
}, },
Logger: logFunc,
}, },
} }
} }
@@ -90,13 +97,12 @@ func NewHandler(c *Config) (http.Handler, error) {
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
user := h.user user := h.user
lZap := getRequestLogger(r, h.behindProxy)
// Authentication // Authentication
if len(h.users) > 0 { if len(h.users) > 0 {
w.Header().Set("WWW-Authenticate", `Basic realm="Restricted"`) w.Header().Set("WWW-Authenticate", `Basic realm="Restricted"`)
// Retrieve the real client IP address using the updated helper function
remoteAddr := getRealRemoteIP(r, h.behindProxy)
// Gets the correct user for this request. // Gets the correct user for this request.
username, password, ok := r.BasicAuth() username, password, ok := r.BasicAuth()
if !ok { if !ok {
@@ -107,26 +113,26 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
user, ok = h.users[username] user, ok = h.users[username]
if !ok { if !ok {
// Log invalid username // Log invalid username
zap.L().Info("invalid username", zap.String("username", username), zap.String("remote_address", remoteAddr)) lZap.Info("invalid username", zap.String("username", username))
http.Error(w, "Not authorized", http.StatusUnauthorized) http.Error(w, "Not authorized", http.StatusUnauthorized)
return return
} }
if !h.noPassword && !user.checkPassword(password) { if !h.noPassword && !user.checkPassword(password) {
// Log invalid password // Log invalid password
zap.L().Info("invalid password", zap.String("username", username), zap.String("remote_address", remoteAddr)) lZap.Info("invalid password", zap.String("username", username))
http.Error(w, "Not authorized", http.StatusUnauthorized) http.Error(w, "Not authorized", http.StatusUnauthorized)
return return
} }
// Log successful authorization // Log successful authorization
zap.L().Info("user authorized", zap.String("username", username), zap.String("remote_address", remoteAddr)) lZap.Info("user authorized", zap.String("username", username))
} }
// Convert the HTTP request into an internal request type // Convert the HTTP request into an internal request type
req, err := newRequest(r, h.user.Prefix) req, err := newRequest(r, h.user.Prefix)
if err != nil { if err != nil {
zap.L().Info("invalid request path or destination", zap.Error(err)) lZap.Info("invalid request path or destination", zap.Error(err))
http.Error(w, "Invalid request path or destination", http.StatusBadRequest) http.Error(w, "Invalid request path or destination", http.StatusBadRequest)
return return
} }
@@ -137,7 +143,7 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return !os.IsNotExist(err) return !os.IsNotExist(err)
}) })
zap.L().Debug("allowed & method & path", zap.Bool("allowed", allowed), zap.String("method", r.Method), zap.String("path", r.URL.Path)) lZap.Debug("allowed & method & path", zap.Bool("allowed", allowed), zap.String("method", r.Method), zap.String("path", r.URL.Path))
if !allowed { if !allowed {
w.WriteHeader(http.StatusForbidden) w.WriteHeader(http.StatusForbidden)
@@ -174,6 +180,14 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
user.ServeHTTP(w, r) user.ServeHTTP(w, r)
} }
// getRequestLogger creates a zap.Logger using the request remote ip.
func getRequestLogger(r *http.Request, behindProxy bool) *zap.Logger {
// Retrieve the real client IP address using the updated helper function
remoteAddr := getRealRemoteIP(r, behindProxy)
return zap.L().With(zap.String("remote_address", remoteAddr))
}
// getRealRemoteIP retrieves the client's actual IP address, considering reverse proxies. // getRealRemoteIP retrieves the client's actual IP address, considering reverse proxies.
func getRealRemoteIP(r *http.Request, behindProxy bool) string { func getRealRemoteIP(r *http.Request, behindProxy bool) string {
if behindProxy { if behindProxy {