storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/cmd/web-handler-context.go (about)

     1  /*
     2   * MinIO Cloud Storage, (C) 2019 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 cmd
    18  
    19  import (
    20  	"context"
    21  	"encoding/json"
    22  	"fmt"
    23  	"net/http"
    24  
    25  	"storj.io/minio/cmd/logger"
    26  	"storj.io/minio/pkg/handlers"
    27  )
    28  
    29  const (
    30  	kmBucket   = "BucketName"
    31  	kmObject   = "ObjectName"
    32  	kmObjects  = "Objects"
    33  	kmPrefix   = "Prefix"
    34  	kmMarker   = "Marker"
    35  	kmUsername = "UserName"
    36  	kmHostname = "HostName"
    37  	kmPolicy   = "Policy"
    38  )
    39  
    40  // KeyValueMap extends builtin map to support setting and getting
    41  // select fields like BucketName, ObjectName, Prefix, etc.
    42  type KeyValueMap map[string]string
    43  
    44  // Bucket returns the BucketName
    45  func (km KeyValueMap) Bucket() string {
    46  	return km[kmBucket]
    47  }
    48  
    49  // Object returns the ObjectName
    50  func (km KeyValueMap) Object() string {
    51  	return km[kmObject]
    52  }
    53  
    54  // Prefix returns the Prefix
    55  func (km KeyValueMap) Prefix() string {
    56  	return km[kmPrefix]
    57  }
    58  
    59  // Username returns the Username
    60  func (km KeyValueMap) Username() string {
    61  	return km[kmUsername]
    62  }
    63  
    64  // Hostname returns the Hostname
    65  func (km KeyValueMap) Hostname() string {
    66  	return km[kmHostname]
    67  }
    68  
    69  // Policy returns the Policy
    70  func (km KeyValueMap) Policy() string {
    71  	return km[kmPolicy]
    72  }
    73  
    74  // Objects returns the Objects
    75  func (km KeyValueMap) Objects() []string {
    76  	var objects []string
    77  	_ = json.Unmarshal([]byte(km[kmObjects]), &objects)
    78  	return objects
    79  }
    80  
    81  // SetBucket sets the given bucket to the KeyValueMap
    82  func (km *KeyValueMap) SetBucket(bucket string) {
    83  	(*km)[kmBucket] = bucket
    84  }
    85  
    86  // SetPrefix sets the given prefix to the KeyValueMap
    87  func (km *KeyValueMap) SetPrefix(prefix string) {
    88  	(*km)[kmPrefix] = prefix
    89  }
    90  
    91  // SetObject sets the given object to the KeyValueMap
    92  func (km *KeyValueMap) SetObject(object string) {
    93  	(*km)[kmObject] = object
    94  }
    95  
    96  // SetMarker sets the given marker to the KeyValueMap
    97  func (km *KeyValueMap) SetMarker(marker string) {
    98  	(*km)[kmMarker] = marker
    99  }
   100  
   101  // SetPolicy sets the given policy to the KeyValueMap
   102  func (km *KeyValueMap) SetPolicy(policy string) {
   103  	(*km)[kmPolicy] = policy
   104  }
   105  
   106  // SetExpiry sets the expiry to the KeyValueMap
   107  func (km *KeyValueMap) SetExpiry(expiry int64) {
   108  	(*km)[kmPolicy] = fmt.Sprintf("%d", expiry)
   109  }
   110  
   111  // SetObjects sets the list of objects to the KeyValueMap
   112  func (km *KeyValueMap) SetObjects(objects []string) {
   113  	objsVal, err := json.Marshal(objects)
   114  	if err != nil {
   115  		// NB this can only happen when we can't marshal a Go
   116  		// slice to its json representation.
   117  		objsVal = []byte("[]")
   118  	}
   119  	(*km)[kmObjects] = string(objsVal)
   120  }
   121  
   122  // SetUsername sets the username to the KeyValueMap
   123  func (km *KeyValueMap) SetUsername(username string) {
   124  	(*km)[kmUsername] = username
   125  }
   126  
   127  // SetHostname sets the hostname to the KeyValueMap
   128  func (km *KeyValueMap) SetHostname(hostname string) {
   129  	(*km)[kmHostname] = hostname
   130  }
   131  
   132  // ToKeyValuer interface wraps ToKeyValue method that allows types to
   133  // marshal their values as a map of structure member names to their
   134  // values, as strings
   135  type ToKeyValuer interface {
   136  	ToKeyValue() KeyValueMap
   137  }
   138  
   139  // ToKeyValue implementation for WebGenericArgs
   140  func (args *WebGenericArgs) ToKeyValue() KeyValueMap {
   141  	return KeyValueMap{}
   142  }
   143  
   144  // ToKeyValue implementation for MakeBucketArgs
   145  func (args *MakeBucketArgs) ToKeyValue() KeyValueMap {
   146  	km := KeyValueMap{}
   147  	km.SetBucket(args.BucketName)
   148  	return km
   149  }
   150  
   151  // ToKeyValue implementation for RemoveBucketArgs
   152  func (args *RemoveBucketArgs) ToKeyValue() KeyValueMap {
   153  	km := KeyValueMap{}
   154  	km.SetBucket(args.BucketName)
   155  	return km
   156  }
   157  
   158  // ToKeyValue implementation for ListObjectsArgs
   159  func (args *ListObjectsArgs) ToKeyValue() KeyValueMap {
   160  	km := KeyValueMap{}
   161  	km.SetBucket(args.BucketName)
   162  	km.SetPrefix(args.Prefix)
   163  	km.SetMarker(args.Marker)
   164  	return km
   165  }
   166  
   167  // ToKeyValue implementation for RemoveObjectArgs
   168  func (args *RemoveObjectArgs) ToKeyValue() KeyValueMap {
   169  	km := KeyValueMap{}
   170  	km.SetBucket(args.BucketName)
   171  	km.SetObjects(args.Objects)
   172  	return km
   173  }
   174  
   175  // ToKeyValue implementation for LoginArgs
   176  func (args *LoginArgs) ToKeyValue() KeyValueMap {
   177  	km := KeyValueMap{}
   178  	km.SetUsername(args.Username)
   179  	return km
   180  }
   181  
   182  // ToKeyValue implementation for LoginSTSArgs
   183  func (args *LoginSTSArgs) ToKeyValue() KeyValueMap {
   184  	km := KeyValueMap{}
   185  	return km
   186  }
   187  
   188  // ToKeyValue implementation for GetBucketPolicyArgs
   189  func (args *GetBucketPolicyArgs) ToKeyValue() KeyValueMap {
   190  	km := KeyValueMap{}
   191  	km.SetBucket(args.BucketName)
   192  	km.SetPrefix(args.Prefix)
   193  	return km
   194  }
   195  
   196  // ToKeyValue implementation for ListAllBucketPoliciesArgs
   197  func (args *ListAllBucketPoliciesArgs) ToKeyValue() KeyValueMap {
   198  	km := KeyValueMap{}
   199  	km.SetBucket(args.BucketName)
   200  	return km
   201  }
   202  
   203  // ToKeyValue implementation for SetBucketPolicyWebArgs
   204  func (args *SetBucketPolicyWebArgs) ToKeyValue() KeyValueMap {
   205  	km := KeyValueMap{}
   206  	km.SetBucket(args.BucketName)
   207  	km.SetPrefix(args.Prefix)
   208  	km.SetPolicy(args.Policy)
   209  	return km
   210  }
   211  
   212  // ToKeyValue implementation for SetAuthArgs
   213  // SetAuthArgs doesn't implement the ToKeyValue interface that will be
   214  // used by logger subsystem down the line, to avoid leaking
   215  // credentials to an external log target
   216  func (args *SetAuthArgs) ToKeyValue() KeyValueMap {
   217  	return KeyValueMap{}
   218  }
   219  
   220  // ToKeyValue implementation for PresignedGetArgs
   221  func (args *PresignedGetArgs) ToKeyValue() KeyValueMap {
   222  	km := KeyValueMap{}
   223  	km.SetHostname(args.HostName)
   224  	km.SetBucket(args.BucketName)
   225  	km.SetObject(args.ObjectName)
   226  	km.SetExpiry(args.Expiry)
   227  	return km
   228  }
   229  
   230  // newWebContext creates a context with ReqInfo values from the given
   231  // http request and api name.
   232  func newWebContext(r *http.Request, args ToKeyValuer, api string) context.Context {
   233  	argsMap := args.ToKeyValue()
   234  	bucket := argsMap.Bucket()
   235  	object := argsMap.Object()
   236  	prefix := argsMap.Prefix()
   237  
   238  	if prefix != "" {
   239  		object = prefix
   240  	}
   241  	reqInfo := &logger.ReqInfo{
   242  		DeploymentID: globalDeploymentID,
   243  		RemoteHost:   handlers.GetSourceIP(r),
   244  		Host:         getHostName(r),
   245  		UserAgent:    r.UserAgent(),
   246  		API:          api,
   247  		BucketName:   bucket,
   248  		ObjectName:   object,
   249  	}
   250  	return logger.SetReqInfo(GlobalContext, reqInfo)
   251  }