Display correct RemoteIP in logs.

pull/510/head
Clement Michaud 2019-12-11 08:52:02 +01:00 committed by Clément Michaud
parent 4dd6260ac8
commit bdf0c07a41
3 changed files with 15 additions and 17 deletions

View File

@ -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)

View File

@ -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), ",")

View File

@ -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)