Move Logging to Middleware Package (#1070)

* Use a specialized ResponseWriter in middleware

* Track User & Upstream in RequestScope

* Wrap responses in our custom ResponseWriter

* Add tests for logging middleware

* Inject upstream metadata into request scope

* Use custom ResponseWriter only in logging middleware

* Assume RequestScope is never nil
This commit is contained in:
Nick Meves
2021-03-06 17:27:16 +00:00
committed by GitHub
parent 220b3708fc
commit 602dac7852
16 changed files with 337 additions and 266 deletions
+7 -1
View File
@@ -4,6 +4,8 @@ import (
"net/http"
"runtime"
"strings"
"github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/middleware"
)
const fileScheme = "file"
@@ -37,6 +39,10 @@ type fileServer struct {
// ServeHTTP proxies requests to the upstream provider while signing the
// request headers
func (u *fileServer) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
rw.Header().Set("GAP-Upstream-Address", u.upstream)
scope := middleware.GetRequestScope(req)
// If scope is nil, this will panic.
// A scope should always be injected before this handler is called.
scope.Upstream = u.upstream
u.handler.ServeHTTP(rw, req)
}