From 5c8152d2c219ce2911176043ec8db5b6990e0cd5 Mon Sep 17 00:00:00 2001 From: Vincent Boutour Date: Sat, 13 Jan 2024 11:07:49 +0100 Subject: [PATCH] refactor: Using appropriate level for logging Signed-off-by: Vincent Boutour --- go.mod | 10 +++++----- go.sum | 24 ++++++++++++------------ pkg/httperror/error.go | 14 ++++++++++++++ pkg/query/ip.go | 19 +++++++++++++++++++ pkg/renderer/handlers.go | 18 +++++++++++------- 5 files changed, 61 insertions(+), 24 deletions(-) create mode 100644 pkg/query/ip.go diff --git a/go.mod b/go.mod index 58db4e91..41215b78 100644 --- a/go.mod +++ b/go.mod @@ -42,16 +42,16 @@ require ( github.com/klauspost/cpuid/v2 v2.2.6 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/redis/go-redis/extra/rediscmd/v9 v9.0.5 // indirect - github.com/tdewolff/parse/v2 v2.7.8 // indirect + github.com/tdewolff/parse/v2 v2.7.9 // indirect go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.21.0 // indirect go.opentelemetry.io/proto/otlp v1.0.0 // indirect - golang.org/x/crypto v0.17.0 // indirect - golang.org/x/net v0.19.0 // indirect + golang.org/x/crypto v0.18.0 // indirect + golang.org/x/net v0.20.0 // indirect golang.org/x/sync v0.4.0 // indirect golang.org/x/sys v0.16.0 // indirect golang.org/x/text v0.14.0 // indirect - google.golang.org/genproto/googleapis/api v0.0.0-20240102182953-50ed04b92917 // indirect - google.golang.org/genproto/googleapis/rpc v0.0.0-20240102182953-50ed04b92917 // indirect + google.golang.org/genproto/googleapis/api v0.0.0-20240108191215-35c7eff3a6b1 // indirect + google.golang.org/genproto/googleapis/rpc v0.0.0-20240108191215-35c7eff3a6b1 // indirect google.golang.org/grpc v1.60.1 // indirect google.golang.org/protobuf v1.32.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect diff --git a/go.sum b/go.sum index ed7ed45f..9c87d7f2 100644 --- a/go.sum +++ b/go.sum @@ -72,8 +72,8 @@ github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcU github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/tdewolff/minify/v2 v2.20.14 h1:sktSuVixRwk0ryQjqvKBu/uYS+MWmkwEFMEWtFZ+TdE= github.com/tdewolff/minify/v2 v2.20.14/go.mod h1:qnIJbnG2dSzk7LIa/UUwgN2OjS8ir6RRlqc0T/1q2xY= -github.com/tdewolff/parse/v2 v2.7.8 h1:1cnVqa8L63xFkc2vfRsZTM6Qy35nJpTvQ2Uvdv3vbvs= -github.com/tdewolff/parse/v2 v2.7.8/go.mod h1:3FbJWZp3XT9OWVN3Hmfp0p/a08v4h8J9W1aghka0soA= +github.com/tdewolff/parse/v2 v2.7.9 h1:4u8nNXNmEGCRVd/slZmZHFL1mv/EVEpHMhSinxdDCqw= +github.com/tdewolff/parse/v2 v2.7.9/go.mod h1:3FbJWZp3XT9OWVN3Hmfp0p/a08v4h8J9W1aghka0soA= github.com/tdewolff/test v1.0.11-0.20231101010635-f1265d231d52/go.mod h1:6DAvZliBAAnD7rhVgwaM7DE5/d9NMOAJ09SqYqeK4QE= github.com/tdewolff/test v1.0.11-0.20240106005702-7de5f7df4739 h1:IkjBCtQOOjIn03u/dMQK9g+Iw9ewps4mCl1nB8Sscbo= github.com/tdewolff/test v1.0.11-0.20240106005702-7de5f7df4739/go.mod h1:XPuWBzvdUzhCuxWO1ojpXsyzsA5bFoS3tO/Q3kFuTG8= @@ -108,10 +108,10 @@ go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto= go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE= go.uber.org/mock v0.4.0 h1:VcM4ZOtdbR4f6VXfiOpwpVJDL6lCReaZ6mw31wqh7KU= go.uber.org/mock v0.4.0/go.mod h1:a6FSlNadKUHUa9IP5Vyt1zh4fC7uAwxMutEAscFbkZc= -golang.org/x/crypto v0.17.0 h1:r8bRNjWL3GshPW3gkd+RpvzWrZAwPS49OmTGZ/uhM4k= -golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4= -golang.org/x/net v0.19.0 h1:zTwKpTd2XuCqf8huc7Fo2iSy+4RHPd10s4KzeTnVr1c= -golang.org/x/net v0.19.0/go.mod h1:CfAk/cbD4CthTvqiEl8NpboMuiuOYsAr/7NOjZJtv1U= +golang.org/x/crypto v0.18.0 h1:PGVlW0xEltQnzFZ55hkuX5+KLyrMYhHld1YHO4AKcdc= +golang.org/x/crypto v0.18.0/go.mod h1:R0j02AL6hcrfOiy9T4ZYp/rcWeMxM3L6QYxlOuEG1mg= +golang.org/x/net v0.20.0 h1:aCL9BSgETF1k+blQaYUBx9hJ9LOGP3gAVemcZlf1Kpo= +golang.org/x/net v0.20.0/go.mod h1:z8BVo6PvndSri0LbOE3hAn0apkU+1YvI6E70E9jsnvY= golang.org/x/sync v0.4.0 h1:zxkM55ReGkDlKSM+Fu41A+zmbZuaPVbGMzvvdUPznYQ= golang.org/x/sync v0.4.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y= golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= @@ -120,12 +120,12 @@ golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -google.golang.org/genproto v0.0.0-20231212172506-995d672761c0 h1:YJ5pD9rF8o9Qtta0Cmy9rdBwkSjrTCT6XTiUQVOtIos= -google.golang.org/genproto v0.0.0-20231212172506-995d672761c0/go.mod h1:l/k7rMz0vFTBPy+tFSGvXEd3z+BcoG1k7EHbqm+YBsY= -google.golang.org/genproto/googleapis/api v0.0.0-20240102182953-50ed04b92917 h1:rcS6EyEaoCO52hQDupoSfrxI3R6C2Tq741is7X8OvnM= -google.golang.org/genproto/googleapis/api v0.0.0-20240102182953-50ed04b92917/go.mod h1:CmlNWB9lSezaYELKS5Ym1r44VrrbPUa7JTvw+6MbpJ0= -google.golang.org/genproto/googleapis/rpc v0.0.0-20240102182953-50ed04b92917 h1:6G8oQ016D88m1xAKljMlBOOGWDZkes4kMhgGFlf8WcQ= -google.golang.org/genproto/googleapis/rpc v0.0.0-20240102182953-50ed04b92917/go.mod h1:xtjpI3tXFPP051KaWnhvxkiubL/6dJ18vLVf7q2pTOU= +google.golang.org/genproto v0.0.0-20240102182953-50ed04b92917 h1:nz5NESFLZbJGPFxDT/HCn+V1mZ8JGNoY4nUpmW/Y2eg= +google.golang.org/genproto v0.0.0-20240102182953-50ed04b92917/go.mod h1:pZqR+glSb11aJ+JQcczCvgf47+duRuzNSKqE8YAQnV0= +google.golang.org/genproto/googleapis/api v0.0.0-20240108191215-35c7eff3a6b1 h1:OPXtXn7fNMaXwO3JvOmF1QyTc00jsSFFz1vXXBOdCDo= +google.golang.org/genproto/googleapis/api v0.0.0-20240108191215-35c7eff3a6b1/go.mod h1:B5xPO//w8qmBDjGReYLpR6UJPnkldGkCSMoH/2vxJeg= +google.golang.org/genproto/googleapis/rpc v0.0.0-20240108191215-35c7eff3a6b1 h1:gphdwh0npgs8elJ4T6J+DQJHPVF7RsuJHCfwztUb4J4= +google.golang.org/genproto/googleapis/rpc v0.0.0-20240108191215-35c7eff3a6b1/go.mod h1:daQN87bsDqDoe316QbbvX60nMoJQa4r6Ds0ZuoAe5yA= google.golang.org/grpc v1.60.1 h1:26+wFr+cNqSGFcOXcabYC0lUVJVRa2Sb2ortSK7VrEU= google.golang.org/grpc v1.60.1/go.mod h1:OlCHIeLYqSSsLi6i49B5QGdzaMZK9+M7LXN2FKz4eGM= google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= diff --git a/pkg/httperror/error.go b/pkg/httperror/error.go index 6e8d073c..ac70f0e0 100644 --- a/pkg/httperror/error.go +++ b/pkg/httperror/error.go @@ -71,6 +71,20 @@ func HandleError(ctx context.Context, w http.ResponseWriter, err error) bool { return true } +func Log(ctx context.Context, err error, status int, message string) { + if err == nil { + return + } + + fields := []any{"error", err, "status", status} + + if status >= http.StatusInternalServerError { + slog.ErrorContext(ctx, message, fields...) + } else { + slog.WarnContext(ctx, message, fields...) + } +} + func ErrorStatus(err error) (status int, message string) { status = http.StatusInternalServerError if err == nil { diff --git a/pkg/query/ip.go b/pkg/query/ip.go new file mode 100644 index 00000000..c96084a5 --- /dev/null +++ b/pkg/query/ip.go @@ -0,0 +1,19 @@ +package query + +import "net/http" + +var ipHeaders = []string{ + "Cf-Connecting-Ip", + "X-Forwarded-For", + "X-Real-Ip", +} + +func GetIP(r *http.Request) string { + for _, header := range ipHeaders { + if ip := r.Header.Get(header); len(ip) != 0 { + return ip + } + } + + return r.RemoteAddr +} diff --git a/pkg/renderer/handlers.go b/pkg/renderer/handlers.go index c1870f57..9455db31 100644 --- a/pkg/renderer/handlers.go +++ b/pkg/renderer/handlers.go @@ -33,8 +33,6 @@ func (s Service) Redirect(w http.ResponseWriter, r *http.Request, pathname strin } func (s Service) Error(w http.ResponseWriter, r *http.Request, content map[string]any, err error) { - slog.ErrorContext(r.Context(), err.Error()) - content = s.feedContent(content) status, message := httperror.ErrorStatus(err) @@ -42,12 +40,16 @@ func (s Service) Error(w http.ResponseWriter, r *http.Request, content map[strin content["Message"] = NewErrorMessage(message) } + ctx := r.Context() + + httperror.Log(ctx, err, status, message) + nonce := owasp.Nonce() owasp.WriteNonce(w, nonce) content["nonce"] = nonce - if err = templates.ResponseHTMLTemplate(r.Context(), s.tracer, s.tpl.Lookup("error"), w, content, status); err != nil { - httperror.InternalServerError(r.Context(), w, err) + if err = templates.ResponseHTMLTemplate(ctx, s.tracer, s.tpl.Lookup("error"), w, content, status); err != nil { + httperror.InternalServerError(ctx, w, err) } } @@ -91,12 +93,14 @@ func (s Service) render(w http.ResponseWriter, r *http.Request, templateFunc Tem responder = templates.ResponseHTMLTemplateRaw } + ctx := r.Context() + if s.generatedMeter != nil { - s.generatedMeter.Add(r.Context(), 1, metric.WithAttributes(attribute.String("template", page.Template))) + s.generatedMeter.Add(ctx, 1, metric.WithAttributes(attribute.String("template", page.Template))) } - if err = responder(r.Context(), s.tracer, s.tpl.Lookup(page.Template), w, page.Content, page.Status); err != nil { - httperror.InternalServerError(r.Context(), w, err) + if err = responder(ctx, s.tracer, s.tpl.Lookup(page.Template), w, page.Content, page.Status); err != nil { + httperror.InternalServerError(ctx, w, err) } }