Skip to content

Commit

Permalink
Move Logging to Middleware Package (#1070)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Nick Meves authored Mar 6, 2021
1 parent 220b370 commit 602dac7
Showing 16 changed files with 337 additions and 266 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@

## Changes since v7.0.1

- [#1070](https://github.com/oauth2-proxy/oauth2-proxy/pull/1070) Refactor logging middleware to middleware package (@NickMeves)
- [#1064](https://github.com/oauth2-proxy/oauth2-proxy/pull/1064) Add support for setting groups on session when using basic auth (@stefansedich)
- [#1056](https://github.com/oauth2-proxy/oauth2-proxy/pull/1056) Add option for custom logos on the sign in page (@JoelSpeed)
- [#1054](https://github.com/oauth2-proxy/oauth2-proxy/pull/1054) Update to Go 1.16 (@JoelSpeed)
108 changes: 0 additions & 108 deletions logging_handler.go

This file was deleted.

116 changes: 0 additions & 116 deletions logging_handler_test.go

This file was deleted.

10 changes: 8 additions & 2 deletions oauthproxy.go
Original file line number Diff line number Diff line change
@@ -250,9 +250,15 @@ func buildPreAuthChain(opts *options.Options) (alice.Chain, error) {
// To silence logging of health checks, register the health check handler before
// the logging handler
if opts.Logging.SilencePing {
chain = chain.Append(middleware.NewHealthCheck(healthCheckPaths, healthCheckUserAgents), LoggingHandler)
chain = chain.Append(
middleware.NewHealthCheck(healthCheckPaths, healthCheckUserAgents),
middleware.NewRequestLogger(),
)
} else {
chain = chain.Append(LoggingHandler, middleware.NewHealthCheck(healthCheckPaths, healthCheckUserAgents))
chain = chain.Append(
middleware.NewRequestLogger(),
middleware.NewHealthCheck(healthCheckPaths, healthCheckUserAgents),
)
}

chain = chain.Append(middleware.NewRequestMetricsWithDefaultRegistry())
3 changes: 3 additions & 0 deletions pkg/apis/middleware/scope.go
Original file line number Diff line number Diff line change
@@ -34,6 +34,9 @@ type RequestScope struct {
// SessionRevalidated indicates whether the session has been revalidated since
// it was loaded or not.
SessionRevalidated bool

// Upstream tracks which upstream was used for this request
Upstream string
}

// GetRequestScope returns the current request scope from the given request
12 changes: 12 additions & 0 deletions pkg/middleware/middleware_suite_test.go
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@ import (
"net/http"
"testing"

middlewareapi "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/middleware"
"github.com/oauth2-proxy/oauth2-proxy/v7/pkg/logger"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
@@ -19,6 +20,17 @@ func TestMiddlewareSuite(t *testing.T) {

func testHandler() http.Handler {
return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
rw.WriteHeader(200)
rw.Write([]byte("test"))
})
}

func testUpstreamHandler(upstream string) http.Handler {
return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
scope := middlewareapi.GetRequestScope(req)
scope.Upstream = upstream

rw.WriteHeader(200)
rw.Write([]byte("test"))
})
}
110 changes: 110 additions & 0 deletions pkg/middleware/request_logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package middleware

import (
"bufio"
"errors"
"net"
"net/http"
"time"

"github.com/justinas/alice"
middlewareapi "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/middleware"
"github.com/oauth2-proxy/oauth2-proxy/v7/pkg/logger"
)

// NewRequestLogger returns middleware which logs requests
// It uses a custom ResponseWriter to track status code & response size details
func NewRequestLogger() alice.Constructor {
return requestLogger
}

func requestLogger(next http.Handler) http.Handler {
return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
startTime := time.Now()
url := *req.URL

responseLogger := &loggingResponse{ResponseWriter: rw}
next.ServeHTTP(responseLogger, req)

scope := middlewareapi.GetRequestScope(req)
// If scope is nil, this will panic.
// A scope should always be injected before this handler is called.
logger.PrintReq(
getUser(scope),
scope.Upstream,
req,
url,
startTime,
responseLogger.Status(),
responseLogger.Size(),
)
})
}

func getUser(scope *middlewareapi.RequestScope) string {
session := scope.Session
if session != nil {
if session.Email != "" {
return session.Email
}
return session.User
}
return ""
}

// loggingResponse is a custom http.ResponseWriter that allows tracking certain
// details for request logging.
type loggingResponse struct {
http.ResponseWriter

status int
size int
}

// Write writes the response using the ResponseWriter
func (r *loggingResponse) Write(b []byte) (int, error) {
if r.status == 0 {
// The status will be StatusOK if WriteHeader has not been called yet
r.status = http.StatusOK
}
size, err := r.ResponseWriter.Write(b)
r.size += size
return size, err
}

// WriteHeader writes the status code for the Response
func (r *loggingResponse) WriteHeader(s int) {
r.ResponseWriter.WriteHeader(s)
r.status = s
}

// Hijack implements the `http.Hijacker` interface that actual ResponseWriters
// implement to support websockets
func (r *loggingResponse) Hijack() (net.Conn, *bufio.ReadWriter, error) {
if hj, ok := r.ResponseWriter.(http.Hijacker); ok {
return hj.Hijack()
}
return nil, nil, errors.New("http.Hijacker is not available on writer")
}

// Flush sends any buffered data to the client. Implements the `http.Flusher`
// interface
func (r *loggingResponse) Flush() {
if flusher, ok := r.ResponseWriter.(http.Flusher); ok {
if r.status == 0 {
// The status will be StatusOK if WriteHeader has not been called yet
r.status = http.StatusOK
}
flusher.Flush()
}
}

// Status returns the response status code
func (r *loggingResponse) Status() int {
return r.status
}

// Size returns the response size
func (r *loggingResponse) Size() int {
return r.size
}
Loading

0 comments on commit 602dac7

Please sign in to comment.