2018-11-19 23:47:03 +01:00
|
|
|
/*
|
2019-09-23 22:34:28 +02:00
|
|
|
* MinIO Cloud Storage, (C) 2018, 2019 MinIO, Inc.
|
2018-11-19 23:47:03 +01:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package logger
|
|
|
|
|
|
|
|
import (
|
2019-09-23 22:34:28 +02:00
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2018-11-19 23:47:03 +01:00
|
|
|
"net/http"
|
2019-08-13 05:32:34 +02:00
|
|
|
"time"
|
2018-11-19 23:47:03 +01:00
|
|
|
|
2019-08-13 05:32:34 +02:00
|
|
|
"github.com/gorilla/mux"
|
2018-11-19 23:47:03 +01:00
|
|
|
"github.com/minio/minio/cmd/logger/message/audit"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ResponseWriter - is a wrapper to trap the http response status code.
|
|
|
|
type ResponseWriter struct {
|
|
|
|
http.ResponseWriter
|
2019-09-23 22:34:28 +02:00
|
|
|
StatusCode int
|
|
|
|
// Response body should be logged
|
|
|
|
LogBody bool
|
|
|
|
TimeToFirstByte time.Duration
|
|
|
|
StartTime time.Time
|
|
|
|
// number of bytes written
|
|
|
|
bytesWritten int
|
|
|
|
// Internal recording buffer
|
|
|
|
headers bytes.Buffer
|
|
|
|
body bytes.Buffer
|
|
|
|
// Indicate if headers are written in the log
|
|
|
|
headersLogged bool
|
2018-11-19 23:47:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewResponseWriter - returns a wrapped response writer to trap
|
|
|
|
// http status codes for auditiing purposes.
|
|
|
|
func NewResponseWriter(w http.ResponseWriter) *ResponseWriter {
|
2019-08-13 05:32:34 +02:00
|
|
|
return &ResponseWriter{
|
|
|
|
ResponseWriter: w,
|
2019-09-23 22:34:28 +02:00
|
|
|
StatusCode: http.StatusOK,
|
|
|
|
StartTime: time.Now().UTC(),
|
2019-08-13 05:32:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (lrw *ResponseWriter) Write(p []byte) (int, error) {
|
|
|
|
n, err := lrw.ResponseWriter.Write(p)
|
2019-09-23 22:34:28 +02:00
|
|
|
lrw.bytesWritten += n
|
|
|
|
if lrw.TimeToFirstByte == 0 {
|
|
|
|
lrw.TimeToFirstByte = time.Now().UTC().Sub(lrw.StartTime)
|
|
|
|
}
|
|
|
|
if !lrw.headersLogged {
|
|
|
|
// We assume the response code to be '200 OK' when WriteHeader() is not called,
|
|
|
|
// that way following Golang HTTP response behavior.
|
|
|
|
lrw.writeHeaders(&lrw.headers, http.StatusOK, lrw.Header())
|
|
|
|
lrw.headersLogged = true
|
|
|
|
}
|
|
|
|
if lrw.StatusCode >= http.StatusBadRequest || lrw.LogBody {
|
|
|
|
// Always logging error responses.
|
|
|
|
lrw.body.Write(p)
|
|
|
|
}
|
2019-08-13 05:32:34 +02:00
|
|
|
if err != nil {
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
return n, err
|
2018-11-19 23:47:03 +01:00
|
|
|
}
|
|
|
|
|
2019-09-23 22:34:28 +02:00
|
|
|
// Write the headers into the given buffer
|
|
|
|
func (lrw *ResponseWriter) writeHeaders(w io.Writer, statusCode int, headers http.Header) {
|
|
|
|
n, _ := fmt.Fprintf(w, "%d %s\n", statusCode, http.StatusText(statusCode))
|
|
|
|
lrw.bytesWritten += n
|
|
|
|
for k, v := range headers {
|
|
|
|
n, _ := fmt.Fprintf(w, "%s: %s\n", k, v[0])
|
|
|
|
lrw.bytesWritten += n
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// BodyPlaceHolder returns a dummy body placeholder
|
|
|
|
var BodyPlaceHolder = []byte("<BODY>")
|
|
|
|
|
|
|
|
// Body - Return response body.
|
|
|
|
func (lrw *ResponseWriter) Body() []byte {
|
|
|
|
// If there was an error response or body logging is enabled
|
|
|
|
// then we return the body contents
|
|
|
|
if lrw.StatusCode >= http.StatusBadRequest || lrw.LogBody {
|
|
|
|
return lrw.body.Bytes()
|
|
|
|
}
|
|
|
|
// ... otherwise we return the <BODY> place holder
|
|
|
|
return BodyPlaceHolder
|
|
|
|
}
|
|
|
|
|
2018-11-19 23:47:03 +01:00
|
|
|
// WriteHeader - writes http status code
|
|
|
|
func (lrw *ResponseWriter) WriteHeader(code int) {
|
2019-09-23 22:34:28 +02:00
|
|
|
lrw.StatusCode = code
|
|
|
|
if !lrw.headersLogged {
|
|
|
|
lrw.writeHeaders(&lrw.headers, code, lrw.ResponseWriter.Header())
|
|
|
|
lrw.headersLogged = true
|
|
|
|
}
|
2018-11-19 23:47:03 +01:00
|
|
|
lrw.ResponseWriter.WriteHeader(code)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Flush - Calls the underlying Flush.
|
|
|
|
func (lrw *ResponseWriter) Flush() {
|
|
|
|
lrw.ResponseWriter.(http.Flusher).Flush()
|
|
|
|
}
|
|
|
|
|
2019-09-23 22:34:28 +02:00
|
|
|
// Size - reutrns the number of bytes written
|
|
|
|
func (lrw *ResponseWriter) Size() int {
|
|
|
|
return lrw.bytesWritten
|
|
|
|
}
|
|
|
|
|
2018-11-19 23:47:03 +01:00
|
|
|
// AuditTargets is the list of enabled audit loggers
|
|
|
|
var AuditTargets = []Target{}
|
|
|
|
|
|
|
|
// AddAuditTarget adds a new audit logger target to the
|
|
|
|
// list of enabled loggers
|
|
|
|
func AddAuditTarget(t Target) {
|
|
|
|
AuditTargets = append(AuditTargets, t)
|
|
|
|
}
|
|
|
|
|
|
|
|
// AuditLog - logs audit logs to all audit targets.
|
2018-11-22 05:03:24 +01:00
|
|
|
func AuditLog(w http.ResponseWriter, r *http.Request, api string, reqClaims map[string]interface{}) {
|
2018-11-19 23:47:03 +01:00
|
|
|
var statusCode int
|
2019-08-13 05:32:34 +02:00
|
|
|
var timeToResponse time.Duration
|
|
|
|
var timeToFirstByte time.Duration
|
2018-11-19 23:47:03 +01:00
|
|
|
lrw, ok := w.(*ResponseWriter)
|
|
|
|
if ok {
|
2019-09-23 22:34:28 +02:00
|
|
|
statusCode = lrw.StatusCode
|
|
|
|
timeToResponse = time.Now().UTC().Sub(lrw.StartTime)
|
|
|
|
timeToFirstByte = lrw.TimeToFirstByte
|
2018-11-19 23:47:03 +01:00
|
|
|
}
|
2019-08-13 05:32:34 +02:00
|
|
|
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
bucket := vars["bucket"]
|
|
|
|
object := vars["object"]
|
|
|
|
|
2018-11-19 23:47:03 +01:00
|
|
|
// Send audit logs only to http targets.
|
|
|
|
for _, t := range AuditTargets {
|
2019-08-13 05:32:34 +02:00
|
|
|
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()
|
2019-10-12 03:50:54 +02:00
|
|
|
_ = t.Send(entry, string(All))
|
2018-11-19 23:47:03 +01:00
|
|
|
}
|
|
|
|
}
|