fix: Audit tests on the correct response writer type (#9445)

This commit is contained in:
Anis Elleuch
2020-04-30 06:17:36 +01:00
committed by GitHub
parent c2529260e7
commit d090a17ed0
8 changed files with 191 additions and 139 deletions

View File

@@ -26,6 +26,8 @@ import (
"github.com/gorilla/mux"
"github.com/minio/minio/cmd/logger/message/audit"
stats "github.com/minio/minio/cmd/http/stats"
)
// ResponseWriter - is a wrapper to trap the http response status code.
@@ -132,14 +134,22 @@ func AddAuditTarget(t Target) {
// AuditLog - logs audit logs to all audit targets.
func AuditLog(w http.ResponseWriter, r *http.Request, api string, reqClaims map[string]interface{}) {
var statusCode int
var timeToResponse time.Duration
var timeToFirstByte time.Duration
lrw, ok := w.(*ResponseWriter)
// Fast exit if there is not audit target configured
if len(AuditTargets) == 0 {
return
}
var (
statusCode int
timeToResponse time.Duration
timeToFirstByte time.Duration
)
st, ok := w.(*stats.RecordAPIStats)
if ok {
statusCode = lrw.StatusCode
timeToResponse = time.Now().UTC().Sub(lrw.StartTime)
timeToFirstByte = lrw.TimeToFirstByte
statusCode = st.RespStatusCode
timeToResponse = time.Now().UTC().Sub(st.StartTime)
timeToFirstByte = st.TTFB
}
vars := mux.Vars(r)
@@ -149,16 +159,17 @@ func AuditLog(w http.ResponseWriter, r *http.Request, api string, reqClaims map[
object = vars["object"]
}
entry := audit.ToEntry(w, r, reqClaims, globalDeploymentID)
entry.API.Name = api
entry.API.Bucket = bucket
entry.API.Object = object
entry.API.Status = http.StatusText(statusCode)
entry.API.StatusCode = statusCode
entry.API.TimeToFirstByte = timeToFirstByte.String()
entry.API.TimeToResponse = timeToResponse.String()
// Send audit logs only to http targets.
for _, t := range AuditTargets {
entry := audit.ToEntry(w, r, reqClaims, globalDeploymentID)
entry.API.Name = api
entry.API.Bucket = bucket
entry.API.Object = object
entry.API.Status = http.StatusText(statusCode)
entry.API.StatusCode = statusCode
entry.API.TimeToFirstByte = timeToFirstByte.String()
entry.API.TimeToResponse = timeToResponse.String()
_ = t.Send(entry, string(All))
}
}