2021-04-18 15:41:13 -04:00
|
|
|
// Copyright (c) 2015-2021 MinIO, Inc.
|
|
|
|
//
|
|
|
|
// This file is part of MinIO Object Storage stack
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2018-11-19 17:47:03 -05:00
|
|
|
|
|
|
|
package audit
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2023-09-04 15:57:37 -04:00
|
|
|
"github.com/minio/pkg/v2/logger/message/audit"
|
2023-02-22 00:21:17 -05:00
|
|
|
|
2021-06-01 17:59:40 -04:00
|
|
|
"github.com/minio/minio/internal/handlers"
|
|
|
|
xhttp "github.com/minio/minio/internal/http"
|
2018-11-19 17:47:03 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// Version - represents the current version of audit log structure.
|
|
|
|
const Version = "1"
|
|
|
|
|
2021-04-23 12:51:12 -04:00
|
|
|
// NewEntry - constructs an audit entry object with some fields filled
|
2023-02-22 00:21:17 -05:00
|
|
|
func NewEntry(deploymentID string) audit.Entry {
|
|
|
|
return audit.Entry{
|
2021-04-23 12:51:12 -04:00
|
|
|
Version: Version,
|
|
|
|
DeploymentID: deploymentID,
|
2021-12-23 18:33:54 -05:00
|
|
|
Time: time.Now().UTC(),
|
2021-04-23 12:51:12 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToEntry - constructs an audit entry from a http request
|
2023-02-22 00:21:17 -05:00
|
|
|
func ToEntry(w http.ResponseWriter, r *http.Request, reqClaims map[string]interface{}, deploymentID string) audit.Entry {
|
2021-04-23 12:51:12 -04:00
|
|
|
entry := NewEntry(deploymentID)
|
|
|
|
|
|
|
|
entry.RemoteHost = handlers.GetSourceIP(r)
|
|
|
|
entry.UserAgent = r.UserAgent()
|
|
|
|
entry.ReqClaims = reqClaims
|
2023-04-28 01:18:24 -04:00
|
|
|
entry.ReqHost = r.Host
|
|
|
|
entry.ReqPath = r.URL.Path
|
2021-04-23 12:51:12 -04:00
|
|
|
|
2020-09-10 14:37:22 -04:00
|
|
|
q := r.URL.Query()
|
|
|
|
reqQuery := make(map[string]string, len(q))
|
|
|
|
for k, v := range q {
|
2018-11-19 17:47:03 -05:00
|
|
|
reqQuery[k] = strings.Join(v, ",")
|
|
|
|
}
|
2021-04-23 12:51:12 -04:00
|
|
|
entry.ReqQuery = reqQuery
|
|
|
|
|
2020-09-10 14:37:22 -04:00
|
|
|
reqHeader := make(map[string]string, len(r.Header))
|
2018-11-19 17:47:03 -05:00
|
|
|
for k, v := range r.Header {
|
|
|
|
reqHeader[k] = strings.Join(v, ",")
|
|
|
|
}
|
2021-04-23 12:51:12 -04:00
|
|
|
entry.ReqHeader = reqHeader
|
|
|
|
|
2020-09-10 14:37:22 -04:00
|
|
|
wh := w.Header()
|
2021-04-23 12:51:12 -04:00
|
|
|
entry.RequestID = wh.Get(xhttp.AmzRequestID)
|
2020-09-10 14:37:22 -04:00
|
|
|
respHeader := make(map[string]string, len(wh))
|
|
|
|
for k, v := range wh {
|
2018-11-19 17:47:03 -05:00
|
|
|
respHeader[k] = strings.Join(v, ",")
|
|
|
|
}
|
2021-04-23 12:51:12 -04:00
|
|
|
entry.RespHeader = respHeader
|
|
|
|
|
2021-01-26 16:39:55 -05:00
|
|
|
if etag := respHeader[xhttp.ETag]; etag != "" {
|
|
|
|
respHeader[xhttp.ETag] = strings.Trim(etag, `"`)
|
|
|
|
}
|
2018-11-19 17:47:03 -05:00
|
|
|
|
|
|
|
return entry
|
|
|
|
}
|