github.com/minio/minio@v0.0.0-20240328213742-3f72439b8a27/internal/logger/message/audit/entry.go (about)

     1  // Copyright (c) 2015-2021 MinIO, Inc.
     2  //
     3  // This file is part of MinIO Object Storage stack
     4  //
     5  // This program is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU Affero General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // This program is distributed in the hope that it will be useful
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  // GNU Affero General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Affero General Public License
    16  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17  
    18  package audit
    19  
    20  import (
    21  	"net/http"
    22  	"strings"
    23  	"time"
    24  
    25  	"github.com/minio/pkg/v2/logger/message/audit"
    26  
    27  	"github.com/minio/minio/internal/handlers"
    28  	xhttp "github.com/minio/minio/internal/http"
    29  )
    30  
    31  // Version - represents the current version of audit log structure.
    32  const Version = "1"
    33  
    34  // NewEntry - constructs an audit entry object with some fields filled
    35  func NewEntry(deploymentID string) audit.Entry {
    36  	return audit.Entry{
    37  		Version:      Version,
    38  		DeploymentID: deploymentID,
    39  		Time:         time.Now().UTC(),
    40  	}
    41  }
    42  
    43  // ToEntry - constructs an audit entry from a http request
    44  func ToEntry(w http.ResponseWriter, r *http.Request, reqClaims map[string]interface{}, deploymentID string) audit.Entry {
    45  	entry := NewEntry(deploymentID)
    46  
    47  	entry.RemoteHost = handlers.GetSourceIP(r)
    48  	entry.UserAgent = r.UserAgent()
    49  	entry.ReqClaims = reqClaims
    50  	entry.ReqHost = r.Host
    51  	entry.ReqPath = r.URL.Path
    52  
    53  	q := r.URL.Query()
    54  	reqQuery := make(map[string]string, len(q))
    55  	for k, v := range q {
    56  		reqQuery[k] = strings.Join(v, ",")
    57  	}
    58  	entry.ReqQuery = reqQuery
    59  
    60  	reqHeader := make(map[string]string, len(r.Header))
    61  	for k, v := range r.Header {
    62  		reqHeader[k] = strings.Join(v, ",")
    63  	}
    64  	entry.ReqHeader = reqHeader
    65  
    66  	wh := w.Header()
    67  	entry.RequestID = wh.Get(xhttp.AmzRequestID)
    68  	respHeader := make(map[string]string, len(wh))
    69  	for k, v := range wh {
    70  		respHeader[k] = strings.Join(v, ",")
    71  	}
    72  	entry.RespHeader = respHeader
    73  
    74  	if etag := respHeader[xhttp.ETag]; etag != "" {
    75  		respHeader[xhttp.ETag] = strings.Trim(etag, `"`)
    76  	}
    77  
    78  	return entry
    79  }