From da65cbae0693eb251d6df8311647dca59fb06141 Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Wed, 18 Nov 2015 23:57:28 -0800 Subject: [PATCH] config: Extensive changes to config and host handling - alias now expands for all match aliases and also any matching hosts. Example:- mc ls -r s3.amazonaws.com/ferenginar/bin - not globs, no hosts - only fully qualified URLs are stored in config. - everywhere use 'args2URLs()' getAliasURL should be used internally. --- access-main.go | 13 ++-- aliasconfig.go | 30 ++++++-- common_methods_test.go | 6 -- config-host-main.go | 154 +++++++++++++++++++++++------------------ config.go | 81 ++++++++++++++++++---- config_test.go | 6 +- diff-main.go | 17 +++-- hostconfig.go | 9 +-- mb-main.go | 6 +- mc_test.go | 52 +++++--------- share-download-main.go | 21 +++--- share-upload-main.go | 21 +++--- url.go | 11 ++- 13 files changed, 247 insertions(+), 180 deletions(-) diff --git a/access-main.go b/access-main.go index 58f2ad6d..34ce0e5a 100644 --- a/access-main.go +++ b/access-main.go @@ -160,14 +160,15 @@ func mainAccess(ctx *cli.Context) { // Additional command speific theme customization. console.SetColor("Access", color.New(color.FgGreen, color.Bold)) - config := mustGetMcConfig() switch ctx.Args().First() { case "set": perms := accessPerms(ctx.Args().Tail().First()) - for _, arg := range ctx.Args().Tail().Tail() { - targetURL := getAliasURL(arg, config.Aliases) + URLs, err := args2URLs(ctx.Args().Tail().Tail()) + fatalIf(err.Trace(ctx.Args().Tail().Tail()...), "Unable to convert args 2 URLs.") + + for _, targetURL := range URLs { err := doSetAccess(targetURL, perms) // Upon error, print and continue. if err != nil { @@ -183,8 +184,10 @@ func mainAccess(ctx *cli.Context) { }) } case "get": - for _, arg := range ctx.Args().Tail() { - targetURL := getAliasURL(arg, config.Aliases) + URLs, err := args2URLs(ctx.Args().Tail()) + fatalIf(err.Trace(ctx.Args().Tail()...), "Unable to convert args 2 URLs.") + + for _, targetURL := range URLs { perms, err := doGetAccess(targetURL) if err != nil { errorIf(err.Trace(targetURL), "Unable to get access permission for ‘"+targetURL+"’.") diff --git a/aliasconfig.go b/aliasconfig.go index 148e4d8b..df262fc4 100644 --- a/aliasconfig.go +++ b/aliasconfig.go @@ -21,6 +21,9 @@ import ( "regexp" "strings" "unicode/utf8" + + "github.com/minio/mc/pkg/client" + "github.com/minio/minio-xl/pkg/probe" ) // isValidAliasName - Check if aliasName is a valid alias. @@ -35,25 +38,38 @@ func normalizeAliasedURL(aliasedURL string) string { } // getAliasURL expands aliased (name/path) to full URL, used by url-parser. -func getAliasURL(aliasedURL string, aliases map[string]string) string { +func getAliasURL(aliasedURL string) (string, *probe.Error) { + config, err := getMcConfig() + if err != nil { + return aliasedURL, err.Trace(aliasedURL) + } + if strings.HasPrefix(aliasedURL, "https") || strings.HasPrefix(aliasedURL, "http") { + return aliasedURL, nil + } + for hostURL := range config.Hosts { + url := client.NewURL(hostURL) + if strings.Contains(aliasedURL, url.Host) { + return url.Scheme + url.SchemeSeparator + aliasedURL, nil + } + } normalizedAliasURL := normalizeAliasedURL(aliasedURL) - for aliasName, aliasValue := range aliases { + for aliasName, aliasValue := range config.Aliases { if strings.HasPrefix(normalizedAliasURL, aliasName) { // Match found. Expand it. splits := strings.SplitN(normalizedAliasURL, aliasName, 2) if len(splits) == 1 { - return aliasedURL // Not an aliased URL. Return as is. + return aliasedURL, nil // Not an aliased URL. Return as is. } if len(splits[0]) == 0 && len(splits[1]) == 0 { - return aliasValue // exact match. + return aliasValue, nil // exact match. } _, sepLen := utf8.DecodeRuneInString(splits[1]) if sepLen == 1 && !os.IsPathSeparator(splits[1][0]) && splits[1] != recursiveSeparator { - return aliasedURL // Do not expand for whole strings with alias prefix. + return aliasedURL, nil // Do not expand for whole strings with alias prefix. } // Matched, but path needs to be joined. - return urlJoinPath(aliasValue, splits[1]) + return urlJoinPath(aliasValue, splits[1]), nil } } - return aliasedURL // No matching alias found. Return as is. + return aliasedURL, nil // No matching alias found. Return as is. } diff --git a/common_methods_test.go b/common_methods_test.go index 7d401686..e066c68a 100644 --- a/common_methods_test.go +++ b/common_methods_test.go @@ -71,10 +71,4 @@ func (s *TestSuite) TestCommonMethods(c *C) { _, err = url2Client(objectPathServer) c.Assert(err, IsNil) - - _, err = url2Client("http://test.minio.io" + "/bucket/fail") - c.Assert(err, Not(IsNil)) - - _, err = url2Client("http://test.minio.io" + "/bucket/fail") - c.Assert(err, Not(IsNil)) } diff --git a/config-host-main.go b/config-host-main.go index 40108a9d..909e46a2 100644 --- a/config-host-main.go +++ b/config-host-main.go @@ -8,6 +8,7 @@ import ( "github.com/fatih/color" "github.com/minio/cli" + "github.com/minio/mc/pkg/client" "github.com/minio/mc/pkg/console" "github.com/minio/minio-xl/pkg/probe" "github.com/minio/minio-xl/pkg/quick" @@ -42,19 +43,19 @@ FLAGS: EXAMPLES: 1. Add host configuration for a URL, using default signature V4. For security reasons turn off bash history $ set +o history - $ mc config {{.Name}} add s3.amazonaws.com BKIKJAA5BMMU2RHO6IBB V7f1CwQqAcwo80UEIJEjc5gVQUSSx5ohQ9GSrr12 + $ mc config {{.Name}} add https://s3.amazonaws.com BKIKJAA5BMMU2RHO6IBB V7f1CwQqAcwo80UEIJEjc5gVQUSSx5ohQ9GSrr12 $ set -o history 2. Add host configuration for a URL, using signature V2. For security reasons turn off bash history $ set +o history - $ mc config {{.Name}} add storage.googleapis.com BKIKJAA5BMMU2RHO6IBB V7f1CwQqAcwo80UEIJEjc5gVQUSSx5ohQ9GSrr12 S3v2 + $ mc config {{.Name}} add https://storage.googleapis.com BKIKJAA5BMMU2RHO6IBB V7f1CwQqAcwo80UEIJEjc5gVQUSSx5ohQ9GSrr12 S3v2 $ set -o history 3. List all hosts. $ mc config {{.Name}} list 4. Remove host config. - $ mc config {{.Name}} remove s3.amazonaws.com + $ mc config {{.Name}} remove https://s3.amazonaws.com `, } @@ -147,7 +148,6 @@ func mainConfigHost(ctx *cli.Context) { listHosts() } } - func listHosts() { conf := new(configV6) conf.Version = globalMCConfigVersion @@ -169,12 +169,12 @@ func listHosts() { } } -func removeHost(hostGlob string) { - if strings.TrimSpace(hostGlob) == "" { - fatalIf(errDummy().Trace(), "Alias or URL cannot be empty.") +func removeHost(hostURL string) { + if !isValidHostURL(hostURL) { + fatalIf(errDummy().Trace(hostURL), "Invalid host URL: ‘"+hostURL+"’ provided. Valid options are [https://example.test.io, https://bucket.s3.amazonaws.com].") } - if strings.TrimSpace(hostGlob) == "dl.minio.io:9000" { - fatalIf(errDummy().Trace(), "‘"+hostGlob+"’ is reserved hostname and cannot be removed.") + if strings.TrimSpace(hostURL) == "https://dl.minio.io:9000" { + fatalIf(errDummy().Trace(hostURL), "‘"+hostURL+"’ is reserved hostname and cannot be removed.") } config, err := newConfig() fatalIf(err.Trace(globalMCConfigVersion), "Failed to initialize ‘quick’ configuration data structure.") @@ -185,17 +185,78 @@ func removeHost(hostGlob string) { // convert interface{} back to its original struct newConf := config.Data().(*configV6) - if _, ok := newConf.Hosts[hostGlob]; !ok { - fatalIf(errDummy().Trace(), fmt.Sprintf("Host glob ‘%s’ does not exist.", hostGlob)) + if _, ok := newConf.Hosts[hostURL]; !ok { + fatalIf(errDummy().Trace(hostURL), fmt.Sprintf("Host url ‘%s’ does not exist.", hostURL)) } - delete(newConf.Hosts, hostGlob) + delete(newConf.Hosts, hostURL) newConfig, err := quick.New(newConf) fatalIf(err.Trace(globalMCConfigVersion), "Failed to initialize ‘quick’ configuration data structure.") err = writeConfig(newConfig) - fatalIf(err.Trace(hostGlob), "Unable to save host glob ‘"+hostGlob+"’.") + fatalIf(err.Trace(hostURL), "Unable to save host url ‘"+hostURL+"’.") - printMsg(hostMessage{op: "remove", Host: hostGlob}) + printMsg(hostMessage{op: "remove", Host: hostURL}) +} + +// addHost - add new host url. +func addHost(newHostURL, accessKeyID, secretAccessKey, api string) { + if !isValidHostURL(newHostURL) { + fatalIf(errDummy().Trace(newHostURL), + "Invalid host URL: ‘"+newHostURL+"’ provided. Valid options are [https://example.test.io, https://bucket.s3.amazonaws.com].") + } + if len(accessKeyID) != 0 { + if !isValidAccessKey(accessKeyID) { + fatalIf(errInvalidArgument().Trace(accessKeyID), "Invalid access key id provided.") + } + } + if len(secretAccessKey) != 0 { + if !isValidSecretKey(secretAccessKey) { + fatalIf(errInvalidArgument().Trace(secretAccessKey), "Invalid secret access key provided.") + } + } + if strings.TrimSpace(api) == "" { + api = "S3v4" + } + if strings.TrimSpace(api) != "S3v2" && strings.TrimSpace(api) != "S3v4" { + fatalIf(errInvalidArgument().Trace(), "Unrecognized version name provided, supported inputs are ‘S3v4’, ‘S3v2’.") + } + config, err := newConfig() + fatalIf(err.Trace(globalMCConfigVersion), "Failed to initialize ‘quick’ configuration data structure.") + + configPath := mustGetMcConfigPath() + err = config.Load(configPath) + fatalIf(err.Trace(configPath), "Unable to load config path.") + + newConf := config.Data().(*configV6) + for savedHost := range newConf.Hosts { + if savedHost == newHostURL { + newConf.Hosts[savedHost] = hostConfig{ + AccessKeyID: accessKeyID, + SecretAccessKey: secretAccessKey, + API: api, + } + } else { + newConf.Hosts[newHostURL] = hostConfig{ + AccessKeyID: accessKeyID, + SecretAccessKey: secretAccessKey, + API: api, + } + } + } + + newConfig, err := quick.New(newConf) + fatalIf(err.Trace(globalMCConfigVersion), "Failed to initialize ‘quick’ configuration data structure.") + + err = writeConfig(newConfig) + fatalIf(err.Trace(newHostURL), "Unable to save new host url ‘"+newHostURL+"’.") + + printMsg(hostMessage{ + op: "add", + Host: newHostURL, + AccessKeyID: accessKeyID, + SecretAccessKey: secretAccessKey, + API: api, + }) } // isValidSecretKey - validate secret key @@ -216,62 +277,17 @@ func isValidAccessKey(accessKeyID string) bool { return regex.MatchString(accessKeyID) } -// addHost - add new host -func addHost(newHost, accessKeyID, secretAccessKey, api string) { - if strings.TrimSpace(newHost) == "" { - fatalIf(errDummy().Trace(), "Unable to proceed, empty arguments provided.") +// isValidHostURL - validate input host url. +func isValidHostURL(hostURL string) bool { + if strings.TrimSpace(hostURL) == "" { + return false } - if len(accessKeyID) != 0 { - if !isValidAccessKey(accessKeyID) { - fatalIf(errInvalidArgument().Trace(), "Invalid access key id provided.") - } + url := client.NewURL(hostURL) + if url.Scheme != "https" && url.Scheme != "http" { + return false } - if len(secretAccessKey) != 0 { - if !isValidSecretKey(secretAccessKey) { - fatalIf(errInvalidArgument().Trace(), "Invalid secret access key provided.") - } + if url.Path != "" && url.Path != "/" { + return false } - if strings.TrimSpace(api) == "" { - api = "S3v4" - } - if strings.TrimSpace(api) != "S3v2" && strings.TrimSpace(api) != "S3v4" { - fatalIf(errInvalidArgument().Trace(), "Unrecognized version name provided, supported inputs are ‘S3v4’, ‘S3v2’") - } - config, err := newConfig() - fatalIf(err.Trace(globalMCConfigVersion), "Failed to initialize ‘quick’ configuration data structure.") - - configPath := mustGetMcConfigPath() - err = config.Load(configPath) - fatalIf(err.Trace(configPath), "Unable to load config path") - - newConf := config.Data().(*configV6) - for savedHost := range newConf.Hosts { - if savedHost == newHost { - newConf.Hosts[savedHost] = hostConfig{ - AccessKeyID: accessKeyID, - SecretAccessKey: secretAccessKey, - API: api, - } - } else { - newConf.Hosts[newHost] = hostConfig{ - AccessKeyID: accessKeyID, - SecretAccessKey: secretAccessKey, - API: api, - } - } - } - - newConfig, err := quick.New(newConf) - fatalIf(err.Trace(globalMCConfigVersion), "Failed to initialize ‘quick’ configuration data structure.") - - err = writeConfig(newConfig) - fatalIf(err.Trace(newHost), "Unable to save new host ‘"+newHost+"’.") - - printMsg(hostMessage{ - op: "add", - Host: newHost, - AccessKeyID: accessKeyID, - SecretAccessKey: secretAccessKey, - API: api, - }) + return true } diff --git a/config.go b/config.go index 9dc24bbe..a094aa5b 100644 --- a/config.go +++ b/config.go @@ -23,6 +23,7 @@ import ( "strings" "sync" + "github.com/minio/mc/pkg/client" "github.com/minio/mc/pkg/console" "github.com/minio/minio-xl/pkg/probe" "github.com/minio/minio-xl/pkg/quick" @@ -131,13 +132,6 @@ func getMcConfig() (*configV6, *probe.Error) { return qconf.Data().(*configV6), nil } -// mustGetMcConfig - reads configuration file and returns configs, exits on error -func mustGetMcConfig() *configV6 { - config, err := getMcConfig() - fatalIf(err.Trace(), "Unable to read mc configuration.") - return config -} - // isMcConfigExists xreturns err if config doesn't exist func isMcConfigExists() bool { configFile, err := getMcConfigPath() @@ -215,11 +209,11 @@ func newConfigV6() *configV6 { } conf.Hosts[globalExampleHostURL] = exampleHostConf - conf.Hosts["localhost:9000"] = localHostConfig - conf.Hosts["dl.minio.io:9000"] = dlHostConfig - conf.Hosts["s3.amazonaws.com"] = s3HostConf - conf.Hosts["play.minio.io:9000"] = playHostConfig - conf.Hosts["storage.googleapis.com"] = googlHostConf + conf.Hosts["http://localhost:9000"] = localHostConfig + conf.Hosts["https://dl.minio.io:9000"] = dlHostConfig + conf.Hosts["https://s3.amazonaws.com"] = s3HostConf + conf.Hosts["https://play.minio.io:9000"] = playHostConfig + conf.Hosts["https://storage.googleapis.com"] = googlHostConf aliases := make(map[string]string) aliases["s3"] = "https://s3.amazonaws.com" @@ -240,6 +234,63 @@ func newConfig() (config quick.Config, err *probe.Error) { return config, nil } +func fixConfigV6ForHosts() { + if !isMcConfigExists() { + return + } + config, err := quick.New(newConfigV6()) + fatalIf(err.Trace(), "Unable to initialize config.") + + err = config.Load(mustGetMcConfigPath()) + fatalIf(err.Trace(), "Unable to load config.") + + if config.Data().(*configV6).Version == "6" { + newConfig := new(configV6) + newConfig.Aliases = make(map[string]string) + newConfig.Hosts = make(map[string]hostConfig) + newConfig.Version = "6" + newConfig.Aliases = config.Data().(*configV6).Aliases + + url := new(client.URL) + for host, hostCfg := range config.Data().(*configV6).Hosts { + // Already fixed - move on. + if strings.HasPrefix(host, "https") || strings.HasPrefix(host, "http") { + newConfig.Hosts[host] = hostCfg + continue + } + if host == "s3.amazonaws.com" || host == "storage.googleapis.com" { + console.Infoln("Found hosts, replacing " + host + " with https://" + host) + url.Host = host + url.Scheme = "https" + url.SchemeSeparator = "://" + newConfig.Hosts[url.String()] = hostCfg + delete(newConfig.Hosts, host) + } + if host == "localhost:9000" || host == "127.0.0.1:9000" { + console.Infoln("Found hosts, replacing " + host + " with http://" + host) + url.Host = host + url.Scheme = "http" + url.SchemeSeparator = "://" + newConfig.Hosts[url.String()] = hostCfg + delete(newConfig.Hosts, host) + } + if host == "play.minio.io:9000" || host == "dl.minio.io:9000" { + console.Infoln("Found hosts, replacing " + host + " with https://" + host) + url.Host = host + url.Scheme = "https" + url.SchemeSeparator = "://" + newConfig.Hosts[url.String()] = hostCfg + delete(newConfig.Hosts, host) + } + } + newConf, err := quick.New(newConfig) + fatalIf(err.Trace(), "Unable to initialize newly fixed config.") + + err = newConf.Save(mustGetMcConfigPath()) + fatalIf(err.Trace(mustGetMcConfigPath()), "Unable to save newly fixed config path.") + } +} + // fixConfigV6 - fix all the unnecessary glob URLs present in existing config version 6. func fixConfigV6() { if !isMcConfigExists() { @@ -258,6 +309,10 @@ func fixConfigV6() { newConfig.Version = "6" newConfig.Aliases = config.Data().(*configV6).Aliases for host, hostCfg := range config.Data().(*configV6).Hosts { + if strings.HasPrefix(host, "https") || strings.HasPrefix(host, "http") { + newConfig.Hosts[host] = hostCfg + continue + } if strings.Contains(host, "*s3*") || strings.Contains(host, "*.s3*") { console.Infoln("Found glob url, replacing " + host + " with s3.amazonaws.com") newConfig.Hosts["s3.amazonaws.com"] = hostCfg @@ -318,4 +373,6 @@ func fixConfig() { fixConfigV3() // Fix config V6 fixConfigV6() + // Fix config V6 for hosts + fixConfigV6ForHosts() } diff --git a/config_test.go b/config_test.go index 3c8d30bb..5e2afde0 100644 --- a/config_test.go +++ b/config_test.go @@ -63,7 +63,7 @@ func (s *TestSuite) TestConfigHostContext(c *C) { "config", "host", "add", - "*my-example.com", + "http://my-example.com", "AKIKJAA5BMMU2RHO6IBB", "V7f1CCwQqAcwo80UEIJEjc5gVQUSSx5ohQ9GSrr9", "S3v2", @@ -74,14 +74,14 @@ func (s *TestSuite) TestConfigHostContext(c *C) { // reset back console.IsExited = false - err = app.Run([]string{os.Args[0], "config", "host", "remove", "*my-example.com"}) + err = app.Run([]string{os.Args[0], "config", "host", "remove", "http://my-example.com"}) c.Assert(err, IsNil) c.Assert(console.IsExited, Equals, false) // reset back console.IsExited = false - err = app.Run([]string{os.Args[0], "config", "host", "remove", "dl.minio.io:9000"}) + err = app.Run([]string{os.Args[0], "config", "host", "remove", "http://dl.minio.io:9000"}) c.Assert(err, IsNil) c.Assert(console.IsExited, Equals, true) diff --git a/diff-main.go b/diff-main.go index 4a899a7d..c3e3801f 100644 --- a/diff-main.go +++ b/diff-main.go @@ -177,26 +177,25 @@ func mainDiff(ctx *cli.Context) { console.SetColor("DiffType", color.New(color.FgYellow, color.Bold)) console.SetColor("DiffSize", color.New(color.FgMagenta, color.Bold)) - config := mustGetMcConfig() - firstArg := ctx.Args().First() - secondArg := ctx.Args().Last() + URLs, err := args2URLs(ctx.Args()) + fatalIf(err.Trace(ctx.Args()...), "Unable to convert args 2 URLs") - firstURL := getAliasURL(firstArg, config.Aliases) - secondURL := getAliasURL(secondArg, config.Aliases) + firstURL := URLs[0] + secondURL := URLs[1] _, firstContent, err := url2Stat(firstURL) if err != nil { - fatalIf(err.Trace(), fmt.Sprintf("Unable to stat '%s'.", firstURL)) + fatalIf(err.Trace(firstURL), fmt.Sprintf("Unable to stat '%s'.", firstURL)) } if !firstContent.Type.IsDir() { - fatalIf(errInvalidArgument().Trace(), fmt.Sprintf("‘%s’ is not a folder.", firstURL)) + fatalIf(errInvalidArgument().Trace(firstURL), fmt.Sprintf("‘%s’ is not a folder.", firstURL)) } _, secondContent, err := url2Stat(secondURL) if err != nil { - fatalIf(err.Trace(), fmt.Sprintf("Unable to stat '%s'.", secondURL)) + fatalIf(err.Trace(secondURL), fmt.Sprintf("Unable to stat '%s'.", secondURL)) } if !secondContent.Type.IsDir() { - fatalIf(errInvalidArgument().Trace(), fmt.Sprintf("‘%s’ is not a folder.", secondURL)) + fatalIf(errInvalidArgument().Trace(secondURL), fmt.Sprintf("‘%s’ is not a folder.", secondURL)) } doDiffMain(firstURL, secondURL) } diff --git a/hostconfig.go b/hostconfig.go index f1a61699..be638cd1 100644 --- a/hostconfig.go +++ b/hostconfig.go @@ -47,12 +47,13 @@ func getHostConfig(URL string) (hostConfig, *probe.Error) { return hostCfg, nil } // if host is exact return quickly. - if _, ok := config.Hosts[url.Host]; ok { - return config.Hosts[url.Host], nil + if _, ok := config.Hosts[url.String()]; ok { + return config.Hosts[url.String()], nil } // if host matches a suffix return. - for savedURL, hostCfg := range config.Hosts { - if strings.HasSuffix(url.Host, savedURL) { + for savedURLStr, hostCfg := range config.Hosts { + savedURL := client.NewURL(savedURLStr) + if strings.HasSuffix(url.Host, savedURL.Host) { return hostCfg, nil } } diff --git a/mb-main.go b/mb-main.go index 65c5ac78..dcc04a7f 100644 --- a/mb-main.go +++ b/mb-main.go @@ -98,10 +98,10 @@ func mainMakeBucket(ctx *cli.Context) { // Additional command speific theme customization. console.SetColor("MakeBucket", color.New(color.FgGreen, color.Bold)) - config := mustGetMcConfig() - for _, arg := range ctx.Args() { - targetURL := getAliasURL(arg, config.Aliases) + URLs, err := args2URLs(ctx.Args()) + fatalIf(err.Trace(ctx.Args()...), "Unable to convert args to URLs.") + for _, targetURL := range URLs { // Instantiate client for URL. clnt, err := url2Client(targetURL) fatalIf(err.Trace(targetURL), "Invalid target ‘"+targetURL+"’.") diff --git a/mc_test.go b/mc_test.go index 2cebbfe0..460612a9 100644 --- a/mc_test.go +++ b/mc_test.go @@ -17,7 +17,6 @@ package main import ( - "fmt" "io/ioutil" "os" "path/filepath" @@ -29,7 +28,6 @@ import ( "net/http/httptest" "github.com/minio/cli" - "github.com/minio/mc/pkg/client" "github.com/minio/mc/pkg/console" "github.com/minio/minio-xl/pkg/quick" . "gopkg.in/check.v1" @@ -71,14 +69,12 @@ func (s *TestSuite) SetUpSuite(c *C) { config, err := newConfig() c.Assert(err, IsNil) - url := client.NewURL(server.URL) - config.Data().(*configV6).Hosts[url.Host] = hostConfig{ + config.Data().(*configV6).Hosts[server.URL] = hostConfig{ AccessKeyID: "WLGDGYAQYIGI833EV05A", SecretAccessKey: "BYvgJM101sHngl2uzjXS/OBF/aMxAN06JrJ3qJlF", API: "S3v4", } - fmt.Println(config.Data().(*configV6).Hosts) err = writeConfig(config) c.Assert(err, IsNil) @@ -152,11 +148,11 @@ func (s *TestSuite) TestNewConfigV6(c *C) { } wantHosts := []string{ - "localhost:9000", - "play.minio.io:9000", - "dl.minio.io:9000", - "s3.amazonaws.com", - "storage.googleapis.com", + "http://localhost:9000", + "https://play.minio.io:9000", + "https://dl.minio.io:9000", + "https://s3.amazonaws.com", + "https://storage.googleapis.com", } for _, host := range wantHosts { _, ok := data.Hosts[host] @@ -170,21 +166,6 @@ func (s *TestSuite) TestHostConfig(c *C) { c.Assert(hostcfg.AccessKeyID, Equals, globalAccessKeyID) c.Assert(hostcfg.SecretAccessKey, Equals, globalSecretAccessKey) c.Assert(hostcfg.API, Equals, "S3v4") - - _, err = getHostConfig("http://test.minio.io") - c.Assert(err, Not(IsNil)) -} - -func (s *TestSuite) TestArgs2URL(c *C) { - URLs := []string{"local", "s3", "play", "playgo", "play.go", "https://s3-us-west-2.amazonaws.com"} - expandedURLs, err := args2URLs(URLs) - c.Assert(err, IsNil) - c.Assert(expandedURLs[0], Equals, "http://localhost:9000") - c.Assert(expandedURLs[1], Equals, "https://s3.amazonaws.com") - c.Assert(expandedURLs[2], Equals, "https://play.minio.io:9000") - c.Assert(expandedURLs[3], Equals, "playgo") // Has no corresponding alias. So expect same value. - c.Assert(expandedURLs[4], Equals, "play.go") // Has no corresponding alias. So expect same value. - c.Assert(expandedURLs[5], Equals, "https://s3-us-west-2.amazonaws.com") } func (s *TestSuite) TestValidPERMS(c *C) { @@ -255,26 +236,25 @@ func (s *TestSuite) TestIsvalidAliasName(c *C) { } func (s *TestSuite) TestEmptyExpansions(c *C) { - url := getAliasURL("hello", nil) + url, err := getAliasURL("hello") + c.Assert(err, IsNil) c.Assert(url, Equals, "hello") - url = getAliasURL("minio://hello", nil) + url, err = getAliasURL("minio://hello") + c.Assert(err, IsNil) c.Assert(url, Equals, "minio://hello") - url = getAliasURL("$#\\", nil) + url, err = getAliasURL("$#\\") + c.Assert(err, IsNil) c.Assert(url, Equals, "$#\\") - url = getAliasURL("foo/bar", map[string]string{"foo": "http://foo"}) - c.Assert(url, Equals, "http://foo/bar") - - url = getAliasURL("myfoo/bar", nil) + url, err = getAliasURL("myfoo/bar") + c.Assert(err, IsNil) c.Assert(url, Equals, "myfoo/bar") - url = getAliasURL("", nil) + url, err = getAliasURL("") + c.Assert(err, IsNil) c.Assert(url, Equals, "") - - url = getAliasURL("hello", nil) - c.Assert(url, Equals, "hello") } func (s *TestSuite) TestHumanizedTime(c *C) { diff --git a/share-download-main.go b/share-download-main.go index 51be9738..45244fea 100644 --- a/share-download-main.go +++ b/share-download-main.go @@ -88,13 +88,13 @@ func checkShareDownloadSyntax(ctx *cli.Context) { fatalIf(errDummy().Trace(expiry.String()), "Expiry cannot be larger than 7 days.") } - for _, arg := range ctx.Args() { - config := mustGetMcConfig() - url := getAliasURL(arg, config.Aliases) // Expand alias. - _, _, err := url2Stat(url) - fatalIf(err.Trace(url), "Unable to stat ‘"+arg+"’.") - } + URLs, err := args2URLs(ctx.Args()) // expand alias. + fatalIf(err.Trace(ctx.Args()...), "Unable to convert args to URLs.") + for _, url := range URLs { + _, _, err := url2Stat(url) + fatalIf(err.Trace(url), "Unable to stat ‘"+url+"’.") + } } // doShareURL share files from target. @@ -162,7 +162,6 @@ func mainShareDownload(ctx *cli.Context) { checkShareDownloadSyntax(ctx) // Extract arguments. - config := mustGetMcConfig() isRecursive := ctx.Bool("recursive") expiry := shareDefaultExpiry if ctx.String("expire") != "" { @@ -171,9 +170,11 @@ func mainShareDownload(ctx *cli.Context) { fatalIf(probe.NewError(e), "Unable to parse expire=‘"+ctx.String("expire")+"’.") } - for _, url := range ctx.Args() { - targetURL := getAliasURL(url, config.Aliases) // Expand alias. + URLs, err := args2URLs(ctx.Args()) // expand alias. + fatalIf(err.Trace(ctx.Args()...), "Unable to convert args to URLs.") + + for _, targetURL := range URLs { err := doShareDownloadURL(targetURL, isRecursive, expiry) - errorIf(err.Trace(targetURL), "Unable to share target ‘"+url+"’.") + fatalIf(err.Trace(targetURL), "Unable to share target ‘"+targetURL+"’.") } } diff --git a/share-upload-main.go b/share-upload-main.go index aa4609d0..b7416cd0 100644 --- a/share-upload-main.go +++ b/share-upload-main.go @@ -84,13 +84,13 @@ func checkShareUploadSyntax(ctx *cli.Context) { fatalIf(errDummy().Trace(expiry.String()), "Expiry cannot be larger than 7 days.") } - for _, arg := range ctx.Args() { - config := mustGetMcConfig() - url := getAliasURL(arg, config.Aliases) // Expand alias. - _, _, err := url2Stat(url) - fatalIf(err.Trace(url), "Unable to stat ‘"+arg+"’.") - } + URLs, err := args2URLs(ctx.Args()) // expand alias. + fatalIf(err.Trace(ctx.Args()...), "Unable to convert args to URLs.") + for _, url := range URLs { + _, _, err := url2Stat(url) + fatalIf(err.Trace(url), "Unable to stat ‘"+url+"’.") + } } // makeCurlCmd constructs curl command-line. @@ -168,7 +168,6 @@ func mainShareUpload(ctx *cli.Context) { checkShareUploadSyntax(ctx) isRecursive := ctx.Bool("recursive") - config := mustGetMcConfig() expireArg := ctx.String("expire") expiry := shareDefaultExpiry contentType := ctx.String("content-type") @@ -178,9 +177,11 @@ func mainShareUpload(ctx *cli.Context) { fatalIf(probe.NewError(e), "Unable to parse expire=‘"+expireArg+"’.") } - for _, url := range ctx.Args() { - targetURL := getAliasURL(url, config.Aliases) + URLs, err := args2URLs(ctx.Args()) // expand alias. + fatalIf(err.Trace(ctx.Args()...), "Unable to convert args to URLs.") + + for _, targetURL := range URLs { err := doShareUploadURL(targetURL, isRecursive, expiry, contentType) - fatalIf(err.Trace(targetURL), "Unable to generate curl command for upload ‘"+url+"’.") + fatalIf(err.Trace(targetURL), "Unable to generate curl command for upload ‘"+targetURL+"’.") } } diff --git a/url.go b/url.go index 32ce80d0..8060e2d2 100644 --- a/url.go +++ b/url.go @@ -55,15 +55,14 @@ func stripRecursiveURL(urlStr string) string { // args2URLs extracts source and target URLs from command-line args. func args2URLs(args []string) ([]string, *probe.Error) { - config, err := getMcConfig() - if err != nil { - return nil, err.Trace() - - } // Convert arguments to URLs: expand alias, fix format... URLs := []string{} for _, arg := range args { - URLs = append(URLs, getAliasURL(arg, config.Aliases)) + aliasedURL, err := getAliasURL(arg) + if err != nil { + return nil, err.Trace(arg) + } + URLs = append(URLs, aliasedURL) } return URLs, nil }