From f68338c8883e62ea0e70595b7da72151f813d077 Mon Sep 17 00:00:00 2001 From: Bobby DeSimone Date: Tue, 28 May 2019 18:12:49 -0700 Subject: [PATCH] internal/httputil: add request id to error page (#144) --- cmd/pomerium/main.go | 16 ++++++------- internal/httputil/errors.go | 24 ++++++++++++------- .../logger.go => log/middleware.go} | 9 +------ .../logger_test.go => log/middleware_test.go} | 2 +- internal/{middleware => log}/wrap_writer.go | 3 +-- .../{middleware => log}/wrap_writer_test.go | 2 +- internal/templates/templates.go | 4 +++- 7 files changed, 31 insertions(+), 29 deletions(-) rename internal/{middleware/logger.go => log/middleware.go} (95%) rename internal/{middleware/logger_test.go => log/middleware_test.go} (98%) rename internal/{middleware => log}/wrap_writer.go (98%) rename internal/{middleware => log}/wrap_writer_test.go (91%) diff --git a/cmd/pomerium/main.go b/cmd/pomerium/main.go index a51ec976f..d85b0c924 100644 --- a/cmd/pomerium/main.go +++ b/cmd/pomerium/main.go @@ -143,9 +143,9 @@ func newProxyService(opt *config.Options, mux *http.ServeMux) (*proxy.Proxy, err func wrapMiddleware(o *config.Options, mux *http.ServeMux) http.Handler { c := middleware.NewChain() - c = c.Append(middleware.NewHandler(log.Logger)) - c = c.Append(middleware.AccessHandler(func(r *http.Request, status, size int, duration time.Duration) { - middleware.FromRequest(r).Debug(). + c = c.Append(log.NewHandler(log.Logger)) + c = c.Append(log.AccessHandler(func(r *http.Request, status, size int, duration time.Duration) { + log.FromRequest(r).Debug(). Dur("duration", duration). Int("size", size). Int("status", status). @@ -159,11 +159,11 @@ func wrapMiddleware(o *config.Options, mux *http.ServeMux) http.Handler { if o != nil && len(o.Headers) != 0 { c = c.Append(middleware.SetHeaders(o.Headers)) } - c = c.Append(middleware.ForwardedAddrHandler("fwd_ip")) - c = c.Append(middleware.RemoteAddrHandler("ip")) - c = c.Append(middleware.UserAgentHandler("user_agent")) - c = c.Append(middleware.RefererHandler("referer")) - c = c.Append(middleware.RequestIDHandler("req_id", "Request-Id")) + c = c.Append(log.ForwardedAddrHandler("fwd_ip")) + c = c.Append(log.RemoteAddrHandler("ip")) + c = c.Append(log.UserAgentHandler("user_agent")) + c = c.Append(log.RefererHandler("referer")) + c = c.Append(log.RequestIDHandler("req_id", "Request-Id")) c = c.Append(middleware.Healthcheck("/ping", version.UserAgent())) return c.Then(mux) } diff --git a/internal/httputil/errors.go b/internal/httputil/errors.go index e4818bb0d..87ab97b03 100644 --- a/internal/httputil/errors.go +++ b/internal/httputil/errors.go @@ -6,6 +6,7 @@ import ( "io" "net/http" + "github.com/pomerium/pomerium/internal/log" "github.com/pomerium/pomerium/internal/templates" ) @@ -31,11 +32,16 @@ func CodeForError(err error) int { // ErrorResponse renders an error page for errors given a message and a status code. // If no message is passed, defaults to the text of the status code. -func ErrorResponse(rw http.ResponseWriter, req *http.Request, message string, code int) { +func ErrorResponse(rw http.ResponseWriter, r *http.Request, message string, code int) { if message == "" { message = http.StatusText(code) } - if req.Header.Get("Accept") == "application/json" { + reqID := "" + id, ok := log.IDFromRequest(r) + if ok { + reqID = id + } + if r.Header.Get("Accept") == "application/json" { var response struct { Error string `json:"error"` } @@ -45,13 +51,15 @@ func ErrorResponse(rw http.ResponseWriter, req *http.Request, message string, co title := http.StatusText(code) rw.WriteHeader(code) t := struct { - Code int - Title string - Message string + Code int + Title string + Message string + RequestID string }{ - Code: code, - Title: title, - Message: message, + Code: code, + Title: title, + Message: message, + RequestID: reqID, } templates.New().ExecuteTemplate(rw, "error.html", t) } diff --git a/internal/middleware/logger.go b/internal/log/middleware.go similarity index 95% rename from internal/middleware/logger.go rename to internal/log/middleware.go index 66a2c20cc..4d47c42de 100644 --- a/internal/middleware/logger.go +++ b/internal/log/middleware.go @@ -1,4 +1,4 @@ -package middleware // import "github.com/pomerium/pomerium/internal/middleware" +package log // import "github.com/pomerium/pomerium/internal/log" import ( "context" @@ -9,16 +9,9 @@ import ( "strings" "time" - "github.com/pomerium/pomerium/internal/log" "github.com/rs/zerolog" ) -// FromRequest gets the logger in the request's context. -// This is a shortcut for log.Ctx(r.Context()) -func FromRequest(r *http.Request) *zerolog.Logger { - return log.Ctx(r.Context()) -} - // NewHandler injects log into requests context. func NewHandler(log zerolog.Logger) func(http.Handler) http.Handler { return func(next http.Handler) http.Handler { diff --git a/internal/middleware/logger_test.go b/internal/log/middleware_test.go similarity index 98% rename from internal/middleware/logger_test.go rename to internal/log/middleware_test.go index 0d771c392..6622474cd 100644 --- a/internal/middleware/logger_test.go +++ b/internal/log/middleware_test.go @@ -1,4 +1,4 @@ -package middleware // import "github.com/pomerium/pomerium/internal/middleware" +package log // import "github.com/pomerium/pomerium/internal/log" import ( "bytes" diff --git a/internal/middleware/wrap_writer.go b/internal/log/wrap_writer.go similarity index 98% rename from internal/middleware/wrap_writer.go rename to internal/log/wrap_writer.go index 9eefb0c1c..04fd4cf4d 100644 --- a/internal/middleware/wrap_writer.go +++ b/internal/log/wrap_writer.go @@ -1,4 +1,4 @@ -package middleware // import "github.com/pomerium/pomerium/internal/middleware" +package log // import "github.com/pomerium/pomerium/internal/log" // The original work was derived from Goji's middleware, source: // https://github.com/zenazn/goji/tree/master/web/middleware @@ -175,7 +175,6 @@ type http2FancyWriter struct { func (f *http2FancyWriter) Flush() { f.wroteHeader = true - fl := f.basicWriter.ResponseWriter.(http.Flusher) fl.Flush() } diff --git a/internal/middleware/wrap_writer_test.go b/internal/log/wrap_writer_test.go similarity index 91% rename from internal/middleware/wrap_writer_test.go rename to internal/log/wrap_writer_test.go index 705908d91..e26e6feed 100644 --- a/internal/middleware/wrap_writer_test.go +++ b/internal/log/wrap_writer_test.go @@ -1,4 +1,4 @@ -package middleware +package log // import "github.com/pomerium/pomerium/internal/log" import ( "net/http/httptest" diff --git a/internal/templates/templates.go b/internal/templates/templates.go index 52a5b4c4d..5c0a35175 100644 --- a/internal/templates/templates.go +++ b/internal/templates/templates.go @@ -266,7 +266,9 @@ func New() *template.Template {

{{.Title}}

{{.Message}}.

-

Troubleshoot your session.

+

Troubleshoot your session.
+ {{if .RequestID}} Request {{.RequestID}} {{end}} +