control plane: add request id to all error pages (#2149)

* controlplane: add request id to all error pages

- use a single http error handler for both envoy and go control plane
- add http lib style status text for our custom statuses.

Signed-off-by: Bobby DeSimone <bobbydesimone@gmail.com>
This commit is contained in:
bobby 2021-04-28 15:04:44 -07:00 committed by GitHub
parent 91c7dc742f
commit 9215833a0b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 126 additions and 136 deletions

View file

@ -3,9 +3,9 @@ package httputil
import (
"html/template"
"net/http"
"net/url"
"github.com/pomerium/pomerium/internal/frontend"
"github.com/pomerium/pomerium/internal/log"
"github.com/pomerium/pomerium/internal/telemetry/requestid"
)
@ -15,8 +15,12 @@ var errorTemplate = template.Must(frontend.NewTemplates())
type HTTPError struct {
// HTTP status codes as registered with IANA.
Status int
// Err is the wrapped error
// Err is the wrapped error.
Err error
// DebugURL is the URL to the debug endpoint.
DebugURL *url.URL
// The request ID.
RequestID string
}
// NewError returns an error that contains a HTTP status and error.
@ -36,32 +40,35 @@ func (e *HTTPError) Unwrap() error { return e.Err }
// It does not otherwise end the request; the caller should ensure no further
// writes are done to w.
func (e *HTTPError) ErrorResponse(w http.ResponseWriter, r *http.Request) {
// indicate to clients that the error originates from Pomerium, not the app
w.Header().Set(HeaderPomeriumResponse, "true")
w.WriteHeader(e.Status)
log.FromRequest(r).Info().Err(e).Msg("httputil: ErrorResponse")
requestID := requestid.FromContext(r.Context())
reqID := e.RequestID
if e.RequestID == "" {
// if empty, try to grab from the request id from the request context
reqID = requestid.FromContext(r.Context())
}
response := struct {
Status int
Error string
StatusText string `json:"-"`
RequestID string `json:",omitempty"`
CanDebug bool `json:"-"`
Version string `json:"-"`
StatusText string `json:"-"`
RequestID string `json:",omitempty"`
CanDebug bool `json:"-"`
Version string `json:"-"`
DebugURL *url.URL `json:",omitempty"`
}{
Status: e.Status,
StatusText: http.StatusText(e.Status),
Error: e.Error(),
RequestID: requestID,
CanDebug: e.Status/100 == 4,
RequestID: reqID,
CanDebug: e.Status/100 == 4 && (e.DebugURL != nil || reqID != ""),
DebugURL: e.DebugURL,
}
// indicate to clients that the error originates from Pomerium, not the app
w.Header().Set(HeaderPomeriumResponse, "true")
if r.Header.Get("Accept") == "application/json" {
RenderJSON(w, e.Status, response)
return
}
w.Header().Set("Content-Type", "text/html; charset=UTF-8")
w.WriteHeader(e.Status)
errorTemplate.ExecuteTemplate(w, "error.html", response)
}