Fix and refactor MC_HOSTS_ env handling code (#2365)

Existing MC_HOSTS_<alias> was incorrectly placed
inside s3Config initialization code path which
leads to a situation where we loose the URL path
information.

Fixes #2364
This commit is contained in:
Harshavardhana
2018-01-19 18:29:28 -08:00
committed by kannappanr
parent b94b8e67e9
commit 8fbbd0ee83
4 changed files with 70 additions and 81 deletions
+9 -16
View File
@@ -108,21 +108,6 @@ func newAdminFactory() func(config *Config) (*madmin.AdminClient, *probe.Error)
}
}
// newAdminClientFromAlias gives a new admin client interface for matching
// alias entry in the mc config file. If no matching host config entry
// is found, fs client is returned.
func newAdminClientFromAlias(alias string, urlStr string) (*madmin.AdminClient, *probe.Error) {
s3Config, err := buildS3Config(alias, urlStr)
if err != nil {
return nil, err
}
s3Client, err := s3AdminNew(s3Config)
if err != nil {
return nil, err.Trace(alias, urlStr)
}
return s3Client, nil
}
// newAdminClient gives a new client interface
func newAdminClient(aliasedURL string) (*madmin.AdminClient, *probe.Error) {
alias, urlStrFull, hostCfg, err := expandAlias(aliasedURL)
@@ -134,7 +119,15 @@ func newAdminClient(aliasedURL string) (*madmin.AdminClient, *probe.Error) {
if hostCfg == nil && urlRgx.MatchString(aliasedURL) {
return nil, errInvalidAliasedURL(aliasedURL).Trace(aliasedURL)
}
return newAdminClientFromAlias(alias, urlStrFull)
s3Config, err := buildS3Config(alias, urlStrFull, hostCfg)
if err != nil {
return nil, err.Trace(alias, urlStrFull)
}
s3Client, err := s3AdminNew(s3Config)
if err != nil {
return nil, err.Trace(alias, urlStrFull)
}
return s3Client, nil
}
// s3AdminNew returns an initialized minioAdmin structure. If debug is enabled,
+6 -2
View File
@@ -175,8 +175,12 @@ func uploadSourceToTargetURL(ctx context.Context, urls URLs, progress io.Reader)
// newClientFromAlias gives a new client interface for matching
// alias entry in the mc config file. If no matching host config entry
// is found, fs client is returned.
func newClientFromAlias(alias string, urlStr string) (Client, *probe.Error) {
s3Config, err := buildS3Config(alias, urlStr)
func newClientFromAlias(alias, urlStr string) (Client, *probe.Error) {
alias, _, hostCfg, err := expandAlias(alias)
if err != nil {
return nil, err.Trace(alias, urlStr)
}
s3Config, err := buildS3Config(alias, urlStr, hostCfg)
if err != nil {
// No matching host config. So we treat it like a
// filesystem.
+54
View File
@@ -17,6 +17,7 @@
package cmd
import (
"net/url"
"os"
"path/filepath"
"regexp"
@@ -175,11 +176,64 @@ func mustGetHostConfig(alias string) *hostConfigV8 {
return hostCfg
}
// parse url usually obtained from env.
func parseEnvURL(envURL string) (*url.URL, string, string, *probe.Error) {
u, e := url.Parse(envURL)
if e != nil {
return nil, "", "", probe.NewError(e).Trace(envURL)
}
var accessKey, secretKey string
// Check if username:password is provided in URL, with no
// access keys or secret we proceed and perform anonymous
// requests.
if u.User != nil {
accessKey = u.User.Username()
secretKey, _ = u.User.Password()
}
// 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 == false && u.RawQuery == "" && u.Fragment == "") {
return nil, "", "", errInvalidArgument().Trace(u.String())
}
// Now that we have validated the URL to be in expected style.
u.User = nil
return u, accessKey, secretKey, nil
}
const mcEnvHostsPrefix = "MC_HOSTS_"
func expandAliasFromEnv(envURL string) (*hostConfigV8, *probe.Error) {
u, accessKey, secretKey, err := parseEnvURL(envURL)
if err != nil {
return nil, err.Trace(envURL)
}
return &hostConfigV8{
URL: u.String(),
API: "S3v4",
AccessKey: accessKey,
SecretKey: secretKey,
}, nil
}
// expandAlias expands aliased URL if any match is found, returns as is otherwise.
func expandAlias(aliasedURL string) (alias string, urlStr string, hostCfg *hostConfigV8, err *probe.Error) {
// Extract alias from the URL.
alias, path := url2Alias(aliasedURL)
if envConfig, ok := os.LookupEnv(mcEnvHostsPrefix + alias); ok {
hostCfg, err = expandAliasFromEnv(envConfig)
if err != nil {
return "", "", nil, err.Trace(aliasedURL)
}
return alias, urlJoinPath(hostCfg.URL, path), hostCfg, nil
}
// Find the matching alias entry and expand the URL.
if hostCfg = mustGetHostConfig(alias); hostCfg != nil {
return alias, urlJoinPath(hostCfg.URL, path), hostCfg, nil
+1 -63
View File
@@ -21,13 +21,11 @@ import (
"fmt"
"io"
"math/rand"
"net/url"
"os"
"runtime"
"strings"
"time"
"github.com/minio/cli"
"github.com/minio/mc/pkg/console"
"github.com/minio/mc/pkg/probe"
)
@@ -97,69 +95,9 @@ func splitStr(path, sep string, n int) []string {
return splits
}
// parse url usually obtained from env.
func parseEnvURL(envURL string) (*url.URL, string, string, *probe.Error) {
u, e := url.Parse(envURL)
if e != nil {
return nil, "", "", probe.NewError(e).Trace(envURL)
}
var accessKey, secretKey string
// Check if username:password is provided in URL, with no
// access keys or secret we proceed and perform anonymous
// requests.
if u.User != nil {
accessKey = u.User.Username()
secretKey, _ = u.User.Password()
}
// 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 == false && u.RawQuery == "" && u.Fragment == "") {
return nil, "", "", errInvalidArgument().Trace(u.String())
}
// Now that we have validated the URL to be in expected style.
u.User = nil
return u, accessKey, secretKey, nil
}
func buildS3ConfigFromEnv(envURL string) (*Config, *probe.Error) {
u, accessKey, secretKey, err := parseEnvURL(envURL)
if err != nil {
return nil, err.Trace(envURL)
}
s3Config, err := newS3Config(cli.Args{u.String(), accessKey, secretKey})
if err != nil {
return nil, err.Trace(envURL)
}
s3Config.AppName = "mc"
s3Config.AppVersion = Version
s3Config.AppComments = []string{os.Args[0], runtime.GOOS, runtime.GOARCH}
s3Config.Debug = globalDebug
s3Config.Insecure = globalInsecure
return s3Config, nil
}
const mcEnvHostsPrefix = "MC_HOSTS_"
// buildS3Config fetches config related to the specified alias
// to create a new config structure
func buildS3Config(alias, urlStr string) (*Config, *probe.Error) {
// Check if we can get the values from ENV, if not use from `config.json`.
if alias == "" {
alias, _ = url2Alias(urlStr)
}
if envConfig, ok := os.LookupEnv(mcEnvHostsPrefix + alias); ok {
return buildS3ConfigFromEnv(envConfig)
}
hostCfg := mustGetHostConfig(alias)
func buildS3Config(alias, urlStr string, hostCfg *hostConfigV8) (*Config, *probe.Error) {
if hostCfg == nil {
return nil, probe.NewError(fmt.Errorf("The specified alias: %s not found", urlStr))
}