github.com/minio/minio@v0.0.0-20240328213742-3f72439b8a27/internal/config/subnet/config.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 subnet 19 20 import ( 21 "net/http" 22 "net/url" 23 "os" 24 "strings" 25 "sync" 26 27 "github.com/minio/minio/internal/config" 28 "github.com/minio/pkg/v2/env" 29 xnet "github.com/minio/pkg/v2/net" 30 ) 31 32 const ( 33 baseURL = "https://subnet.min.io" 34 baseURLDev = "http://localhost:9000" 35 ) 36 37 // DefaultKVS - default KV config for subnet settings 38 var DefaultKVS = config.KVS{ 39 config.KV{ 40 Key: config.License, // Deprecated Dec 2021 41 Value: "", 42 }, 43 config.KV{ 44 Key: config.APIKey, 45 Value: "", 46 }, 47 config.KV{ 48 Key: config.Proxy, 49 Value: "", 50 }, 51 } 52 53 // Config represents the subnet related configuration 54 type Config struct { 55 // The subnet license token - Deprecated Dec 2021 56 License string `json:"license"` 57 58 // The subnet api key 59 APIKey string `json:"apiKey"` 60 61 // The HTTP(S) proxy URL to use for connecting to SUBNET 62 Proxy string `json:"proxy"` 63 64 // Transport configured with proxy_url if set optionally. 65 transport http.RoundTripper 66 67 // The subnet base URL 68 BaseURL string 69 } 70 71 var configLock sync.RWMutex 72 73 // Registered indicates if cluster is registered or not 74 func (c *Config) Registered() bool { 75 configLock.RLock() 76 defer configLock.RUnlock() 77 78 return len(c.APIKey) > 0 79 } 80 81 // ApplyEnv - applies the current subnet config to Console UI specific environment variables. 82 func (c *Config) ApplyEnv() { 83 configLock.RLock() 84 defer configLock.RUnlock() 85 86 if c.License != "" { 87 os.Setenv("CONSOLE_SUBNET_LICENSE", c.License) 88 } 89 if c.APIKey != "" { 90 os.Setenv("CONSOLE_SUBNET_API_KEY", c.APIKey) 91 } 92 if c.Proxy != "" { 93 os.Setenv("CONSOLE_SUBNET_PROXY", c.Proxy) 94 } 95 os.Setenv("CONSOLE_SUBNET_URL", c.BaseURL) 96 } 97 98 // Update - in-place update with new license and registration information. 99 func (c *Config) Update(ncfg Config, isDevEnv bool) { 100 configLock.Lock() 101 defer configLock.Unlock() 102 103 c.License = ncfg.License 104 c.APIKey = ncfg.APIKey 105 c.Proxy = ncfg.Proxy 106 c.transport = ncfg.transport 107 c.BaseURL = baseURL 108 109 if isDevEnv { 110 c.BaseURL = os.Getenv("_MINIO_SUBNET_URL") 111 if c.BaseURL == "" { 112 c.BaseURL = baseURLDev 113 } 114 } 115 } 116 117 // LookupConfig - lookup config and override with valid environment settings if any. 118 func LookupConfig(kvs config.KVS, transport http.RoundTripper) (cfg Config, err error) { 119 if err = config.CheckValidKeys(config.SubnetSubSys, kvs, DefaultKVS); err != nil { 120 return cfg, err 121 } 122 123 var proxyURL *xnet.URL 124 proxy := env.Get(config.EnvMinIOSubnetProxy, kvs.Get(config.Proxy)) 125 if len(proxy) > 0 { 126 proxyURL, err = xnet.ParseHTTPURL(proxy) 127 if err != nil { 128 return cfg, err 129 } 130 131 } 132 133 cfg.License = strings.TrimSpace(env.Get(config.EnvMinIOSubnetLicense, kvs.Get(config.License))) 134 cfg.APIKey = strings.TrimSpace(env.Get(config.EnvMinIOSubnetAPIKey, kvs.Get(config.APIKey))) 135 cfg.Proxy = proxy 136 137 if transport == nil { 138 // when transport is nil, it means we are just validating the 139 // inputs not performing any network calls. 140 return cfg, nil 141 } 142 143 // Make sure to clone the transport before editing the ProxyURL 144 if proxyURL != nil { 145 ctransport := transport.(*http.Transport).Clone() 146 ctransport.Proxy = http.ProxyURL((*url.URL)(proxyURL)) 147 cfg.transport = ctransport 148 } else { 149 cfg.transport = transport 150 } 151 152 return cfg, nil 153 }