storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/cmd/logger/message/audit/entry.go (about)

     1  /*
     2   * MinIO Cloud Storage, (C) 2018 MinIO, Inc.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package audit
    18  
    19  import (
    20  	"net/http"
    21  	"strings"
    22  	"time"
    23  
    24  	xhttp "storj.io/minio/cmd/http"
    25  	"storj.io/minio/pkg/handlers"
    26  )
    27  
    28  // Version - represents the current version of audit log structure.
    29  const Version = "1"
    30  
    31  // Entry - audit entry logs.
    32  type Entry struct {
    33  	Version      string `json:"version"`
    34  	DeploymentID string `json:"deploymentid,omitempty"`
    35  	Time         string `json:"time"`
    36  	Trigger      string `json:"trigger"`
    37  	API          struct {
    38  		Name            string `json:"name,omitempty"`
    39  		Bucket          string `json:"bucket,omitempty"`
    40  		Object          string `json:"object,omitempty"`
    41  		Status          string `json:"status,omitempty"`
    42  		StatusCode      int    `json:"statusCode,omitempty"`
    43  		TimeToFirstByte string `json:"timeToFirstByte,omitempty"`
    44  		TimeToResponse  string `json:"timeToResponse,omitempty"`
    45  	} `json:"api"`
    46  	RemoteHost string                 `json:"remotehost,omitempty"`
    47  	RequestID  string                 `json:"requestID,omitempty"`
    48  	UserAgent  string                 `json:"userAgent,omitempty"`
    49  	ReqClaims  map[string]interface{} `json:"requestClaims,omitempty"`
    50  	ReqQuery   map[string]string      `json:"requestQuery,omitempty"`
    51  	ReqHeader  map[string]string      `json:"requestHeader,omitempty"`
    52  	RespHeader map[string]string      `json:"responseHeader,omitempty"`
    53  	Tags       map[string]interface{} `json:"tags,omitempty"`
    54  }
    55  
    56  // NewEntry - constructs an audit entry object with some fields filled
    57  func NewEntry(deploymentID string) Entry {
    58  	return Entry{
    59  		Version:      Version,
    60  		DeploymentID: deploymentID,
    61  		Time:         time.Now().UTC().Format(time.RFC3339Nano),
    62  	}
    63  }
    64  
    65  // ToEntry - constructs an audit entry from a http request
    66  func ToEntry(w http.ResponseWriter, r *http.Request, reqClaims map[string]interface{}, deploymentID string) Entry {
    67  
    68  	entry := NewEntry(deploymentID)
    69  
    70  	entry.RemoteHost = handlers.GetSourceIP(r)
    71  	entry.UserAgent = r.UserAgent()
    72  	entry.ReqClaims = reqClaims
    73  
    74  	q := r.URL.Query()
    75  	reqQuery := make(map[string]string, len(q))
    76  	for k, v := range q {
    77  		reqQuery[k] = strings.Join(v, ",")
    78  	}
    79  	entry.ReqQuery = reqQuery
    80  
    81  	reqHeader := make(map[string]string, len(r.Header))
    82  	for k, v := range r.Header {
    83  		reqHeader[k] = strings.Join(v, ",")
    84  	}
    85  	entry.ReqHeader = reqHeader
    86  
    87  	wh := w.Header()
    88  	entry.RequestID = wh.Get(xhttp.AmzRequestID)
    89  	respHeader := make(map[string]string, len(wh))
    90  	for k, v := range wh {
    91  		respHeader[k] = strings.Join(v, ",")
    92  	}
    93  	entry.RespHeader = respHeader
    94  
    95  	if etag := respHeader[xhttp.ETag]; etag != "" {
    96  		respHeader[xhttp.ETag] = strings.Trim(etag, `"`)
    97  	}
    98  
    99  	return entry
   100  }