Allow audit logging to work while tracing (#8077)

It is observed that when `mc admin trace` is being
used due to ResponseWriter wrapper, we loose information
about statusCode,statusText for audit logging.

This PR fixes this behavior
master
Harshavardhana 5 years ago committed by GitHub
parent 7bf1caa0fe
commit a15bb19d37
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 13
      cmd/http-tracer.go

@ -29,6 +29,7 @@ import (
"strings" "strings"
"time" "time"
"github.com/minio/minio/cmd/logger"
trace "github.com/minio/minio/pkg/trace" trace "github.com/minio/minio/pkg/trace"
) )
@ -126,7 +127,7 @@ func (r *recordResponseWriter) Write(p []byte) (n int, err error) {
r.writeHeaders(&r.headers, http.StatusOK, r.ResponseWriter.Header()) r.writeHeaders(&r.headers, http.StatusOK, r.ResponseWriter.Header())
r.headersLogged = true r.headersLogged = true
} }
if (r.statusCode != http.StatusOK && r.statusCode != http.StatusPartialContent && r.statusCode != 0) || r.logBody { if r.statusCode >= http.StatusBadRequest || r.logBody {
// Always logging error responses. // Always logging error responses.
r.body.Write(p) r.body.Write(p)
} }
@ -146,7 +147,7 @@ func (r *recordResponseWriter) Flush() {
func (r *recordResponseWriter) Body() []byte { func (r *recordResponseWriter) Body() []byte {
// If there was an error response or body logging is enabled // If there was an error response or body logging is enabled
// then we return the body contents // then we return the body contents
if r.statusCode >= 400 || r.logBody { if r.statusCode >= http.StatusBadRequest || r.logBody {
return r.body.Bytes() return r.body.Bytes()
} }
// ... otherwise we return the <BODY> place holder // ... otherwise we return the <BODY> place holder
@ -206,7 +207,7 @@ func Trace(f http.HandlerFunc, logBody bool, w http.ResponseWriter, r *http.Requ
// Setup a http response body recorder // Setup a http response body recorder
respBodyRecorder := &recordResponseWriter{ResponseWriter: w, logBody: logBody} respBodyRecorder := &recordResponseWriter{ResponseWriter: w, logBody: logBody}
f(respBodyRecorder, r) f(logger.NewResponseWriter(respBodyRecorder), r)
rs := trace.ResponseInfo{ rs := trace.ResponseInfo{
Time: time.Now().UTC(), Time: time.Now().UTC(),
@ -222,6 +223,10 @@ func Trace(f http.HandlerFunc, logBody bool, w http.ResponseWriter, r *http.Requ
t.ReqInfo = rq t.ReqInfo = rq
t.RespInfo = rs t.RespInfo = rs
t.CallStats = trace.CallStats{Latency: rs.Time.Sub(rq.Time), InputBytes: reqBodyRecorder.Size(), OutputBytes: respBodyRecorder.Size()} t.CallStats = trace.CallStats{
Latency: rs.Time.Sub(rq.Time),
InputBytes: reqBodyRecorder.Size(),
OutputBytes: respBodyRecorder.Size(),
}
return t return t
} }

Loading…
Cancel
Save