storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/cmd/config/api/api.go (about)

     1  /*
     2   * MinIO Cloud Storage, (C) 2020 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 api
    18  
    19  import (
    20  	"encoding/json"
    21  	"errors"
    22  	"strconv"
    23  	"strings"
    24  	"time"
    25  
    26  	"storj.io/minio/cmd/config"
    27  	"storj.io/minio/pkg/env"
    28  )
    29  
    30  // API sub-system constants
    31  const (
    32  	apiRequestsMax             = "requests_max"
    33  	apiRequestsDeadline        = "requests_deadline"
    34  	apiClusterDeadline         = "cluster_deadline"
    35  	apiCorsAllowOrigin         = "cors_allow_origin"
    36  	apiRemoteTransportDeadline = "remote_transport_deadline"
    37  	apiListQuorum              = "list_quorum"
    38  	apiExtendListCacheLife     = "extend_list_cache_life"
    39  	apiReplicationWorkers      = "replication_workers"
    40  
    41  	EnvAPIRequestsMax             = "MINIO_API_REQUESTS_MAX"
    42  	EnvAPIRequestsDeadline        = "MINIO_API_REQUESTS_DEADLINE"
    43  	EnvAPIClusterDeadline         = "MINIO_API_CLUSTER_DEADLINE"
    44  	EnvAPICorsAllowOrigin         = "MINIO_API_CORS_ALLOW_ORIGIN"
    45  	EnvAPIRemoteTransportDeadline = "MINIO_API_REMOTE_TRANSPORT_DEADLINE"
    46  	EnvAPIListQuorum              = "MINIO_API_LIST_QUORUM"
    47  	EnvAPIExtendListCacheLife     = "MINIO_API_EXTEND_LIST_CACHE_LIFE"
    48  	EnvAPISecureCiphers           = "MINIO_API_SECURE_CIPHERS"
    49  	EnvAPIReplicationWorkers      = "MINIO_API_REPLICATION_WORKERS"
    50  )
    51  
    52  // Deprecated key and ENVs
    53  const (
    54  	apiReadyDeadline    = "ready_deadline"
    55  	EnvAPIReadyDeadline = "MINIO_API_READY_DEADLINE"
    56  )
    57  
    58  // DefaultKVS - default storage class config
    59  var (
    60  	DefaultKVS = config.KVS{
    61  		config.KV{
    62  			Key:   apiRequestsMax,
    63  			Value: "0",
    64  		},
    65  		config.KV{
    66  			Key:   apiRequestsDeadline,
    67  			Value: "10s",
    68  		},
    69  		config.KV{
    70  			Key:   apiClusterDeadline,
    71  			Value: "10s",
    72  		},
    73  		config.KV{
    74  			Key:   apiCorsAllowOrigin,
    75  			Value: "*",
    76  		},
    77  		config.KV{
    78  			Key:   apiRemoteTransportDeadline,
    79  			Value: "2h",
    80  		},
    81  		config.KV{
    82  			Key:   apiListQuorum,
    83  			Value: "strict",
    84  		},
    85  		config.KV{
    86  			Key:   apiExtendListCacheLife,
    87  			Value: "0s",
    88  		},
    89  		config.KV{
    90  			Key:   apiReplicationWorkers,
    91  			Value: "500",
    92  		},
    93  	}
    94  )
    95  
    96  // Config storage class configuration
    97  type Config struct {
    98  	RequestsMax             int           `json:"requests_max"`
    99  	RequestsDeadline        time.Duration `json:"requests_deadline"`
   100  	ClusterDeadline         time.Duration `json:"cluster_deadline"`
   101  	CorsAllowOrigin         []string      `json:"cors_allow_origin"`
   102  	RemoteTransportDeadline time.Duration `json:"remote_transport_deadline"`
   103  	ListQuorum              string        `json:"list_strict_quorum"`
   104  	ExtendListLife          time.Duration `json:"extend_list_cache_life"`
   105  	ReplicationWorkers      int           `json:"replication_workers"`
   106  }
   107  
   108  // UnmarshalJSON - Validate SS and RRS parity when unmarshalling JSON.
   109  func (sCfg *Config) UnmarshalJSON(data []byte) error {
   110  	type Alias Config
   111  	aux := &struct {
   112  		*Alias
   113  	}{
   114  		Alias: (*Alias)(sCfg),
   115  	}
   116  	return json.Unmarshal(data, &aux)
   117  }
   118  
   119  // GetListQuorum interprets list quorum values and returns appropriate
   120  // acceptable quorum expected for list operations
   121  func (sCfg Config) GetListQuorum() int {
   122  	switch sCfg.ListQuorum {
   123  	case "reduced":
   124  		return 2
   125  	case "disk":
   126  		// smallest possible value, generally meant for testing.
   127  		return 1
   128  	case "strict":
   129  		return -1
   130  	}
   131  	// Defaults to 3 drives per set, defaults to "optimal" value
   132  	return 3
   133  }
   134  
   135  // LookupConfig - lookup api config and override with valid environment settings if any.
   136  func LookupConfig(kvs config.KVS) (cfg Config, err error) {
   137  	// remove this since we have removed this already.
   138  	kvs.Delete(apiReadyDeadline)
   139  
   140  	if err = config.CheckValidKeys(config.APISubSys, kvs, DefaultKVS); err != nil {
   141  		return cfg, err
   142  	}
   143  
   144  	// Check environment variables parameters
   145  	requestsMax, err := strconv.Atoi(env.Get(EnvAPIRequestsMax, kvs.Get(apiRequestsMax)))
   146  	if err != nil {
   147  		return cfg, err
   148  	}
   149  
   150  	if requestsMax < 0 {
   151  		return cfg, errors.New("invalid API max requests value")
   152  	}
   153  
   154  	requestsDeadline, err := time.ParseDuration(env.Get(EnvAPIRequestsDeadline, kvs.Get(apiRequestsDeadline)))
   155  	if err != nil {
   156  		return cfg, err
   157  	}
   158  
   159  	clusterDeadline, err := time.ParseDuration(env.Get(EnvAPIClusterDeadline, kvs.Get(apiClusterDeadline)))
   160  	if err != nil {
   161  		return cfg, err
   162  	}
   163  
   164  	corsAllowOrigin := strings.Split(env.Get(EnvAPICorsAllowOrigin, kvs.Get(apiCorsAllowOrigin)), ",")
   165  
   166  	remoteTransportDeadline, err := time.ParseDuration(env.Get(EnvAPIRemoteTransportDeadline, kvs.Get(apiRemoteTransportDeadline)))
   167  	if err != nil {
   168  		return cfg, err
   169  	}
   170  
   171  	listQuorum := env.Get(EnvAPIListQuorum, kvs.Get(apiListQuorum))
   172  	switch listQuorum {
   173  	case "strict", "optimal", "reduced", "disk":
   174  	default:
   175  		return cfg, errors.New("invalid value for list strict quorum")
   176  	}
   177  
   178  	listLife, err := time.ParseDuration(env.Get(EnvAPIExtendListCacheLife, kvs.Get(apiExtendListCacheLife)))
   179  	if err != nil {
   180  		return cfg, err
   181  	}
   182  
   183  	replicationWorkers, err := strconv.Atoi(env.Get(EnvAPIReplicationWorkers, kvs.Get(apiReplicationWorkers)))
   184  	if err != nil {
   185  		return cfg, err
   186  	}
   187  
   188  	if replicationWorkers <= 0 {
   189  		return cfg, config.ErrInvalidReplicationWorkersValue(nil).Msg("Minimum number of replication workers should be 1")
   190  	}
   191  
   192  	return Config{
   193  		RequestsMax:             requestsMax,
   194  		RequestsDeadline:        requestsDeadline,
   195  		ClusterDeadline:         clusterDeadline,
   196  		CorsAllowOrigin:         corsAllowOrigin,
   197  		RemoteTransportDeadline: remoteTransportDeadline,
   198  		ListQuorum:              listQuorum,
   199  		ExtendListLife:          listLife,
   200  		ReplicationWorkers:      replicationWorkers,
   201  	}, nil
   202  }