github.com/minio/mc@v0.0.0-20240503112107-b471de8d1882/cmd/config-v10.go (about)

     1  // Copyright (c) 2015-2022 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 cmd
    19  
    20  import (
    21  	"sync"
    22  
    23  	"github.com/minio/mc/pkg/probe"
    24  	"github.com/minio/pkg/v2/quick"
    25  )
    26  
    27  const (
    28  	defaultAccessKey = "YOUR-ACCESS-KEY-HERE"
    29  	defaultSecretKey = "YOUR-SECRET-KEY-HERE"
    30  )
    31  
    32  var (
    33  	// set once during first load.
    34  	cacheCfgV10 *configV10
    35  	// All access to mc config file should be synchronized.
    36  	cfgMutex = &sync.RWMutex{}
    37  )
    38  
    39  // aliasConfig configuration of an alias.
    40  type aliasConfigV10 struct {
    41  	URL          string `json:"url"`
    42  	AccessKey    string `json:"accessKey"`
    43  	SecretKey    string `json:"secretKey"`
    44  	SessionToken string `json:"sessionToken,omitempty"`
    45  	API          string `json:"api"`
    46  	Path         string `json:"path"`
    47  	License      string `json:"license,omitempty"`
    48  	APIKey       string `json:"apiKey,omitempty"`
    49  }
    50  
    51  // configV10 config version.
    52  type configV10 struct {
    53  	Version string                    `json:"version"`
    54  	Aliases map[string]aliasConfigV10 `json:"aliases"`
    55  }
    56  
    57  // newConfigV10 - new config version.
    58  func newConfigV10() *configV10 {
    59  	cfg := new(configV10)
    60  	cfg.Version = globalMCConfigVersion
    61  	cfg.Aliases = make(map[string]aliasConfigV10)
    62  	return cfg
    63  }
    64  
    65  // SetAlias sets host config if not empty.
    66  func (c *configV10) setAlias(alias string, cfg aliasConfigV10) {
    67  	if _, ok := c.Aliases[alias]; !ok {
    68  		c.Aliases[alias] = cfg
    69  	}
    70  }
    71  
    72  // load default values for missing entries.
    73  func (c *configV10) loadDefaults() {
    74  	// MinIO server running locally.
    75  	c.setAlias("local", aliasConfigV10{
    76  		URL:       "http://localhost:9000",
    77  		AccessKey: "",
    78  		SecretKey: "",
    79  		API:       "S3v4",
    80  		Path:      "auto",
    81  	})
    82  
    83  	// Amazon S3 cloud storage service.
    84  	c.setAlias("s3", aliasConfigV10{
    85  		URL:       "https://s3.amazonaws.com",
    86  		AccessKey: defaultAccessKey,
    87  		SecretKey: defaultSecretKey,
    88  		API:       "S3v4",
    89  		Path:      "dns",
    90  	})
    91  
    92  	// Google cloud storage service.
    93  	c.setAlias("gcs", aliasConfigV10{
    94  		URL:       "https://storage.googleapis.com",
    95  		AccessKey: defaultAccessKey,
    96  		SecretKey: defaultSecretKey,
    97  		API:       "S3v2",
    98  		Path:      "dns",
    99  	})
   100  
   101  	// MinIO anonymous server for demo.
   102  	c.setAlias("play", aliasConfigV10{
   103  		URL:       "https://play.min.io",
   104  		AccessKey: "Q3AM3UQ867SPQQA43P2F",
   105  		SecretKey: "zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG",
   106  		API:       "S3v4",
   107  		Path:      "auto",
   108  	})
   109  }
   110  
   111  // loadConfigV10 - loads a new config.
   112  func loadConfigV10() (*configV10, *probe.Error) {
   113  	cfgMutex.RLock()
   114  	defer cfgMutex.RUnlock()
   115  
   116  	// If already cached, return the cached value.
   117  	if cacheCfgV10 != nil {
   118  		return cacheCfgV10, nil
   119  	}
   120  
   121  	if !isMcConfigExists() {
   122  		return nil, errInvalidArgument().Trace()
   123  	}
   124  
   125  	// Initialize a new config loader.
   126  	qc, e := quick.NewConfig(newConfigV10(), nil)
   127  	if e != nil {
   128  		return nil, probe.NewError(e)
   129  	}
   130  
   131  	// Load config at configPath, fails if config is not
   132  	// accessible, malformed or version missing.
   133  	if e = qc.Load(mustGetMcConfigPath()); e != nil {
   134  		return nil, probe.NewError(e)
   135  	}
   136  
   137  	cfgV10 := qc.Data().(*configV10)
   138  
   139  	// Cache config.
   140  	cacheCfgV10 = cfgV10
   141  
   142  	// Success.
   143  	return cfgV10, nil
   144  }
   145  
   146  // saveConfigV10 - saves an updated config.
   147  func saveConfigV10(cfgV10 *configV10) *probe.Error {
   148  	cfgMutex.Lock()
   149  	defer cfgMutex.Unlock()
   150  
   151  	qs, e := quick.NewConfig(cfgV10, nil)
   152  	if e != nil {
   153  		return probe.NewError(e)
   154  	}
   155  
   156  	// update the cache.
   157  	cacheCfgV10 = cfgV10
   158  
   159  	e = qs.Save(mustGetMcConfigPath())
   160  	if e != nil {
   161  		return probe.NewError(e).Trace(mustGetMcConfigPath())
   162  	}
   163  	return nil
   164  }