-
Notifications
You must be signed in to change notification settings - Fork 553
/
config.go
348 lines (304 loc) · 9.72 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
// Copyright (c) 2015-2022 MinIO, Inc.
//
// This file is part of MinIO Object Storage stack
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package cmd
import (
"bufio"
"fmt"
"net/url"
"os"
"path/filepath"
"regexp"
"runtime"
"strings"
"github.com/minio/mc/pkg/probe"
"github.com/minio/pkg/v3/env"
"github.com/mitchellh/go-homedir"
)
// mcCustomConfigDir contains the whole path to config dir. Only access via get/set functions.
var mcCustomConfigDir string
// setMcConfigDir - set a custom MinIO Client config folder.
func setMcConfigDir(configDir string) {
mcCustomConfigDir = configDir
}
// getMcConfigDir - construct MinIO Client config folder.
func getMcConfigDir() (string, *probe.Error) {
if mcCustomConfigDir != "" {
return mcCustomConfigDir, nil
}
homeDir, e := homedir.Dir()
if e != nil {
return "", probe.NewError(e)
}
configDir := filepath.Join(homeDir, defaultMCConfigDir())
return configDir, nil
}
// Return default default mc config directory.
// Generally you want to use getMcConfigDir which returns custom overrides.
func defaultMCConfigDir() string {
if runtime.GOOS == "windows" {
// For windows the path is slightly different
cmd := filepath.Base(os.Args[0])
if strings.HasSuffix(strings.ToLower(cmd), ".exe") {
cmd = cmd[:strings.LastIndex(cmd, ".")]
}
return fmt.Sprintf("%s\\", cmd)
}
return fmt.Sprintf(".%s/", filepath.Base(os.Args[0]))
}
// mustGetMcConfigDir - construct MinIO Client config folder or fail
func mustGetMcConfigDir() (configDir string) {
configDir, err := getMcConfigDir()
fatalIf(err.Trace(), "Unable to get mcConfigDir.")
return configDir
}
// createMcConfigDir - create MinIO Client config folder
func createMcConfigDir() *probe.Error {
p, err := getMcConfigDir()
if err != nil {
return err.Trace()
}
if e := os.MkdirAll(p, 0o700); e != nil {
return probe.NewError(e)
}
return nil
}
// getMcConfigPath - construct MinIO Client configuration path
func getMcConfigPath() (string, *probe.Error) {
if mcCustomConfigDir != "" {
return filepath.Join(mcCustomConfigDir, globalMCConfigFile), nil
}
dir, err := getMcConfigDir()
if err != nil {
return "", err.Trace()
}
return filepath.Join(dir, globalMCConfigFile), nil
}
// mustGetMcConfigPath - similar to getMcConfigPath, ignores errors
func mustGetMcConfigPath() string {
path, err := getMcConfigPath()
fatalIf(err.Trace(), "Unable to get mcConfigPath.")
return path
}
// newMcConfig - initializes a new version '10' config.
func newMcConfig() *configV10 {
cfg := newConfigV10()
cfg.loadDefaults()
return cfg
}
// loadMcConfigCached - returns loadMcConfig with a closure for config cache.
func loadMcConfigFactory() func() (*configV10, *probe.Error) {
// Load once and cache in a closure.
cfgCache, err := loadConfigV10()
// loadMcConfig - reads configuration file and returns config.
return func() (*configV10, *probe.Error) {
return cfgCache, err
}
}
// loadMcConfig - returns configuration, initialized later.
var loadMcConfig func() (*configV10, *probe.Error)
// saveMcConfig - saves configuration file and returns error if any.
func saveMcConfig(config *configV10) *probe.Error {
if config == nil {
return errInvalidArgument().Trace()
}
err := createMcConfigDir()
if err != nil {
return err.Trace(mustGetMcConfigDir())
}
// Save the config.
if err := saveConfigV10(config); err != nil {
return err.Trace(mustGetMcConfigPath())
}
// Refresh the config cache.
loadMcConfig = loadMcConfigFactory()
return nil
}
// isMcConfigExists returns err if config doesn't exist.
func isMcConfigExists() bool {
configFile, err := getMcConfigPath()
if err != nil {
return false
}
if _, e := os.Stat(configFile); e != nil {
return false
}
return true
}
// cleanAlias removes any forbidden trailing slashes or backslashes
// before any validation to avoid annoying mc complaints.
func cleanAlias(s string) string {
s = strings.TrimSuffix(s, "/")
s = strings.TrimSuffix(s, "\\")
return s
}
// isValidAlias - Check if alias valid.
func isValidAlias(alias string) bool {
return regexp.MustCompile("^[a-zA-Z][a-zA-Z0-9-_]*$").MatchString(alias)
}
// getAliasConfig retrieves host specific configuration such as access keys, signature type.
func getAliasConfig(alias string) (*aliasConfigV10, *probe.Error) {
mcCfg, err := loadMcConfig()
if err != nil {
return nil, err.Trace(alias)
}
// if host is exact return quickly.
if _, ok := mcCfg.Aliases[alias]; ok {
hostCfg := mcCfg.Aliases[alias]
hostCfg.Src = mustGetMcConfigPath()
return &hostCfg, nil
}
// return error if cannot be matched.
return nil, errNoMatchingHost(alias).Trace(alias)
}
// mustGetHostConfig retrieves host specific configuration such as access keys, signature type.
func mustGetHostConfig(alias string) *aliasConfigV10 {
// look for it in the environment variable first.
aliasCfg, _ := expandAliasFromEnv(env.Get(mcEnvHostPrefix+alias, ""))
// If alias is not found,
// look for it in the customized configuration.
if aliasCfg == nil {
aliasCfg = aliasToConfigMap[alias]
}
if aliasCfg == nil {
aliasCfg, _ = getAliasConfig(alias)
}
return aliasCfg
}
var (
hostKeys = regexp.MustCompile("^(https?://)(.*?):(.*)@(.*?)$")
hostKeyTokens = regexp.MustCompile("^(https?://)(.*?):(.*?):(.*)@(.*?)$")
)
// parse url usually obtained from env.
func parseEnvURLStr(envURL string) (*url.URL, string, string, string, *probe.Error) {
var accessKey, secretKey, sessionToken string
var parsedURL string
if hostKeyTokens.MatchString(envURL) {
parts := hostKeyTokens.FindStringSubmatch(envURL)
if len(parts) != 6 {
return nil, "", "", "", errInvalidArgument().Trace(envURL)
}
accessKey = parts[2]
secretKey = parts[3]
sessionToken = parts[4]
parsedURL = fmt.Sprintf("%s%s", parts[1], parts[5])
} else if hostKeys.MatchString(envURL) {
parts := hostKeys.FindStringSubmatch(envURL)
if len(parts) != 5 {
return nil, "", "", "", errInvalidArgument().Trace(envURL)
}
accessKey = parts[2]
secretKey = parts[3]
parsedURL = fmt.Sprintf("%s%s", parts[1], parts[4])
}
var u *url.URL
var e error
if parsedURL != "" {
u, e = url.Parse(parsedURL)
} else {
u, e = url.Parse(envURL)
}
if e != nil {
return nil, "", "", "", probe.NewError(e)
}
// Look for if URL has invalid values and return error.
if !((u.Scheme == "http" || u.Scheme == "https") &&
(u.Path == "/" || u.Path == "") && u.Opaque == "" &&
!u.ForceQuery && u.RawQuery == "" && u.Fragment == "") {
return nil, "", "", "", errInvalidArgument().Trace(u.String())
}
if accessKey == "" && secretKey == "" {
if u.User != nil {
accessKey = u.User.Username()
secretKey, _ = u.User.Password()
}
}
return u, accessKey, secretKey, sessionToken, nil
}
const (
mcEnvHostPrefix = "MC_HOST_"
mcEnvConfigFile = "MC_CONFIG_ENV_FILE"
)
var aliasToConfigMap = make(map[string]*aliasConfigV10)
func readAliasesFromFile(envConfigFile string) *probe.Error {
r, e := os.Open(envConfigFile)
if e != nil {
return probe.NewError(e).Trace(envConfigFile)
}
defer r.Close()
scanner := bufio.NewScanner(r)
for scanner.Scan() {
envLine := scanner.Text()
strs := strings.SplitN(envLine, "=", 2)
if len(strs) != 2 {
return probe.NewError(fmt.Errorf("parsing error at %s", envLine)).Trace(envConfigFile)
}
alias := strings.TrimPrefix(strs[0], mcEnvHostPrefix)
if len(alias) == 0 {
return probe.NewError(fmt.Errorf("parsing error at %s", envLine)).Trace(envConfigFile)
}
aliasConfig, err := expandAliasFromEnv(strs[1])
if err != nil {
return err.Trace(envLine)
}
aliasConfig.Src = envConfigFile
aliasToConfigMap[alias] = aliasConfig
}
if e := scanner.Err(); e != nil {
return probe.NewError(e).Trace(envConfigFile)
}
return nil
}
func expandAliasFromEnv(envURL string) (*aliasConfigV10, *probe.Error) {
u, accessKey, secretKey, sessionToken, err := parseEnvURLStr(envURL)
if err != nil {
return nil, err.Trace(envURL)
}
return &aliasConfigV10{
URL: u.String(),
API: "S3v4",
AccessKey: accessKey,
SecretKey: secretKey,
SessionToken: sessionToken,
Src: "env",
}, nil
}
// expandAlias expands aliased URL if any match is found, returns as is otherwise.
func expandAlias(aliasedURL string) (alias, urlStr string, aliasCfg *aliasConfigV10, err *probe.Error) {
// Extract alias from the URL.
alias, path := url2Alias(aliasedURL)
if env.IsSet(mcEnvHostPrefix + alias) {
aliasCfg, err = expandAliasFromEnv(env.Get(mcEnvHostPrefix+alias, ""))
if err != nil {
return "", "", nil, err.Trace(aliasedURL)
}
return alias, urlJoinPath(aliasCfg.URL, path), aliasCfg, nil
}
aliasCfg = aliasToConfigMap[alias]
if aliasCfg != nil {
return alias, urlJoinPath(aliasCfg.URL, path), aliasCfg, nil
}
// Find the matching alias entry and expand the URL.
if aliasCfg = mustGetHostConfig(alias); aliasCfg != nil {
return alias, urlJoinPath(aliasCfg.URL, path), aliasCfg, nil
}
return "", aliasedURL, nil, nil // No matching entry found. Return original URL as is.
}
// mustExpandAlias expands aliased URL if any match is found, returns as is otherwise.
func mustExpandAlias(aliasedURL string) (alias, urlStr string, aliasCfg *aliasConfigV10) {
alias, urlStr, aliasCfg, _ = expandAlias(aliasedURL)
return alias, urlStr, aliasCfg
}