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"
|
|
|
|
|
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"
|
|
|
|
|
2022-01-03 04:28:52 -05:00
|
|
|
// ObjectVersion object version key/versionId
|
|
|
|
type ObjectVersion struct {
|
|
|
|
ObjectName string `json:"objectName"`
|
2022-01-03 12:26:26 -05:00
|
|
|
VersionID string `json:"versionId,omitempty"`
|
2022-01-03 04:28:52 -05:00
|
|
|
}
|
|
|
|
|
2018-11-19 17:47:03 -05:00
|
|
|
// Entry - audit entry logs.
|
|
|
|
type Entry struct {
|
2021-12-23 18:33:54 -05:00
|
|
|
Version string `json:"version"`
|
|
|
|
DeploymentID string `json:"deploymentid,omitempty"`
|
|
|
|
Time time.Time `json:"time"`
|
2022-07-12 13:43:32 -04:00
|
|
|
Event string `json:"event"`
|
|
|
|
// deprecated replaced by 'Event', kept here for some
|
|
|
|
// time for backward compatibility with k8s Operator.
|
|
|
|
Trigger string `json:"trigger"`
|
|
|
|
API struct {
|
2022-01-03 04:28:52 -05:00
|
|
|
Name string `json:"name,omitempty"`
|
|
|
|
Bucket string `json:"bucket,omitempty"`
|
|
|
|
Object string `json:"object,omitempty"`
|
|
|
|
Objects []ObjectVersion `json:"objects,omitempty"`
|
|
|
|
Status string `json:"status,omitempty"`
|
|
|
|
StatusCode int `json:"statusCode,omitempty"`
|
|
|
|
InputBytes int64 `json:"rx"`
|
|
|
|
OutputBytes int64 `json:"tx"`
|
2022-09-01 15:51:04 -04:00
|
|
|
HeaderBytes int64 `json:"txHeaders,omitempty"`
|
2022-01-03 04:28:52 -05:00
|
|
|
TimeToFirstByte string `json:"timeToFirstByte,omitempty"`
|
|
|
|
TimeToResponse string `json:"timeToResponse,omitempty"`
|
2018-11-19 17:47:03 -05:00
|
|
|
} `json:"api"`
|
2018-11-21 23:03:24 -05:00
|
|
|
RemoteHost string `json:"remotehost,omitempty"`
|
|
|
|
RequestID string `json:"requestID,omitempty"`
|
|
|
|
UserAgent string `json:"userAgent,omitempty"`
|
|
|
|
ReqClaims map[string]interface{} `json:"requestClaims,omitempty"`
|
|
|
|
ReqQuery map[string]string `json:"requestQuery,omitempty"`
|
|
|
|
ReqHeader map[string]string `json:"requestHeader,omitempty"`
|
|
|
|
RespHeader map[string]string `json:"responseHeader,omitempty"`
|
2021-01-26 16:21:51 -05:00
|
|
|
Tags map[string]interface{} `json:"tags,omitempty"`
|
2022-05-04 03:45:27 -04:00
|
|
|
|
2023-02-08 14:05:26 -05:00
|
|
|
AccessKey string `json:"accessKey,omitempty"`
|
|
|
|
ParentUser string `json:"parentUser,omitempty"`
|
|
|
|
|
2022-05-04 03:45:27 -04:00
|
|
|
Error string `json:"error,omitempty"`
|
2018-11-19 17:47:03 -05:00
|
|
|
}
|
|
|
|
|
2021-04-23 12:51:12 -04:00
|
|
|
// NewEntry - constructs an audit entry object with some fields filled
|
|
|
|
func NewEntry(deploymentID string) Entry {
|
|
|
|
return Entry{
|
|
|
|
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
|
2019-08-12 23:32:34 -04:00
|
|
|
func ToEntry(w http.ResponseWriter, r *http.Request, reqClaims map[string]interface{}, deploymentID string) Entry {
|
2021-04-23 12:51:12 -04:00
|
|
|
entry := NewEntry(deploymentID)
|
|
|
|
|
|
|
|
entry.RemoteHost = handlers.GetSourceIP(r)
|
|
|
|
entry.UserAgent = r.UserAgent()
|
|
|
|
entry.ReqClaims = reqClaims
|
|
|
|
|
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
|
|
|
|
}
|