From bdf0c07a41fc347c049eef9e8b63ef516d789801 Mon Sep 17 00:00:00 2001 From: Clement Michaud Date: Wed, 11 Dec 2019 08:52:02 +0100 Subject: [PATCH] Display correct RemoteIP in logs. --- internal/logging/logger.go | 10 ---------- internal/middlewares/authelia_context.go | 18 +++++++++++++----- internal/middlewares/log_request.go | 4 ++-- 3 files changed, 15 insertions(+), 17 deletions(-) diff --git a/internal/logging/logger.go b/internal/logging/logger.go index a90453542..569b43bda 100644 --- a/internal/logging/logger.go +++ b/internal/logging/logger.go @@ -3,7 +3,6 @@ package logging import ( logrus_stack "github.com/Gurpartap/logrus-stack" "github.com/sirupsen/logrus" - "github.com/valyala/fasthttp" ) func init() { @@ -17,15 +16,6 @@ func Logger() *logrus.Logger { return logrus.StandardLogger() } -// NewRequestLogger create a new request logger for the given request. -func NewRequestLogger(ctx *fasthttp.RequestCtx) *logrus.Entry { - return logrus.WithFields(logrus.Fields{ - "method": string(ctx.Method()), - "path": string(ctx.Path()), - "remote_ip": ctx.RemoteIP().String(), - }) -} - // SetLevel set the level of the logger. func SetLevel(level logrus.Level) { logrus.SetLevel(level) diff --git a/internal/middlewares/authelia_context.go b/internal/middlewares/authelia_context.go index 8ade46fe0..cfed540b1 100644 --- a/internal/middlewares/authelia_context.go +++ b/internal/middlewares/authelia_context.go @@ -7,20 +7,28 @@ import ( "strings" "github.com/asaskevich/govalidator" - "github.com/clems4ever/authelia/internal/session" - "github.com/clems4ever/authelia/internal/configuration/schema" - "github.com/clems4ever/authelia/internal/logging" + "github.com/clems4ever/authelia/internal/session" + "github.com/sirupsen/logrus" "github.com/valyala/fasthttp" ) +// NewRequestLogger create a new request logger for the given request. +func NewRequestLogger(ctx *AutheliaCtx) *logrus.Entry { + return logrus.WithFields(logrus.Fields{ + "method": string(ctx.Method()), + "path": string(ctx.Path()), + "remote_ip": ctx.RemoteIP().String(), + }) +} + // NewAutheliaCtx instantiate an AutheliaCtx out of a RequestCtx. func NewAutheliaCtx(ctx *fasthttp.RequestCtx, configuration schema.Configuration, providers Providers) (*AutheliaCtx, error) { autheliaCtx := new(AutheliaCtx) autheliaCtx.RequestCtx = ctx autheliaCtx.Providers = providers autheliaCtx.Configuration = configuration - autheliaCtx.Logger = logging.NewRequestLogger(ctx) + autheliaCtx.Logger = NewRequestLogger(autheliaCtx) userSession, err := providers.SessionProvider.GetSession(ctx) if err != nil { @@ -153,7 +161,7 @@ func (c *AutheliaCtx) SetJSONBody(value interface{}) error { // RemoteIP return the remote IP taking X-Forwarded-For header into account if provided. func (c *AutheliaCtx) RemoteIP() net.IP { - XForwardedFor := c.RequestCtx.Request.Header.Peek("X-Forwarded-For") + XForwardedFor := c.Request.Header.Peek("X-Forwarded-For") if XForwardedFor != nil { ips := strings.Split(string(XForwardedFor), ",") diff --git a/internal/middlewares/log_request.go b/internal/middlewares/log_request.go index 659d58833..7a1e9cd83 100644 --- a/internal/middlewares/log_request.go +++ b/internal/middlewares/log_request.go @@ -1,14 +1,14 @@ package middlewares import ( - "github.com/clems4ever/authelia/internal/logging" "github.com/valyala/fasthttp" ) // LogRequestMiddleware logs the query that is being treated. func LogRequestMiddleware(next fasthttp.RequestHandler) fasthttp.RequestHandler { return func(ctx *fasthttp.RequestCtx) { - logger := logging.NewRequestLogger(ctx) + autheliaCtx := &AutheliaCtx{RequestCtx: ctx} + logger := NewRequestLogger(autheliaCtx) logger.Trace("Request hit") next(ctx)