diff --git a/cmd/client-admin.go b/cmd/client-admin.go index 993f8923..92f200a4 100644 --- a/cmd/client-admin.go +++ b/cmd/client-admin.go @@ -18,6 +18,7 @@ package cmd import ( "crypto/tls" + "fmt" "hash/fnv" "net" "net/http" @@ -119,10 +120,13 @@ func newAdminClient(aliasedURL string) (*madmin.AdminClient, *probe.Error) { if hostCfg == nil && urlRgx.MatchString(aliasedURL) { return nil, errInvalidAliasedURL(aliasedURL).Trace(aliasedURL) } - s3Config, err := buildS3Config(alias, urlStrFull, hostCfg) - if err != nil { - return nil, err.Trace(alias, urlStrFull) + + if hostCfg == nil { + return nil, probe.NewError(fmt.Errorf("The specified alias: %s not found", urlStrFull)) } + + s3Config := newS3Config(urlStrFull, hostCfg) + s3Client, err := s3AdminNew(s3Config) if err != nil { return nil, err.Trace(alias, urlStrFull) diff --git a/cmd/common-methods.go b/cmd/common-methods.go index a0c966c4..2d6d73b5 100644 --- a/cmd/common-methods.go +++ b/cmd/common-methods.go @@ -180,8 +180,8 @@ func newClientFromAlias(alias, urlStr string) (Client, *probe.Error) { if err != nil { return nil, err.Trace(alias, urlStr) } - s3Config, err := buildS3Config(alias, urlStr, hostCfg) - if err != nil { + + if hostCfg == nil { // No matching host config. So we treat it like a // filesystem. fsClient, fsErr := fsNew(urlStr) @@ -191,6 +191,8 @@ func newClientFromAlias(alias, urlStr string) (Client, *probe.Error) { return fsClient, nil } + s3Config := newS3Config(urlStr, hostCfg) + s3Client, err := s3New(s3Config) if err != nil { return nil, err.Trace(alias, urlStr) diff --git a/cmd/config-host-add.go b/cmd/config-host-add.go index 044f5bbf..b1d1d861 100644 --- a/cmd/config-host-add.go +++ b/cmd/config-host-add.go @@ -123,27 +123,12 @@ func addHost(alias string, hostCfgV8 hostConfigV8) { }) } -func newS3Config(args cli.Args) (*Config, *probe.Error) { - var ( - url = args.Get(0) - accessKey = args.Get(1) - secretKey = args.Get(2) - api = args.Get(3) - ) - - // If api is provided we do not auto probe signature, this is - // required in situations when signature type is provided by the user. - if api != "" { - return &Config{ - Insecure: globalInsecure, - AccessKey: accessKey, - SecretKey: secretKey, - Signature: api, - HostURL: url, - }, nil - } - +// probeS3Signature - auto probe S3 server signature: issue a Stat call +// using v4 signature then v2 in case of failure. +func probeS3Signature(accessKey, secretKey, url string) (string, *probe.Error) { + // Test s3 connection for API auto probe s3Config := &Config{ + // S3 connection parameters Insecure: globalInsecure, AccessKey: accessKey, SecretKey: secretKey, @@ -153,7 +138,7 @@ func newS3Config(args cli.Args) (*Config, *probe.Error) { s3Client, err := s3New(s3Config) if err != nil { - return nil, err.Trace(args...) + return "", err } if _, err = s3Client.Stat(false, false); err != nil { @@ -165,21 +150,52 @@ func newS3Config(args cli.Args) (*Config, *probe.Error) { s3Config.Signature = "s3v2" s3Client, err = s3New(s3Config) if err != nil { - return nil, err.Trace(args...) + return "", err } if _, err = s3Client.Stat(false, false); err != nil { switch err.ToGoError().(type) { case BucketDoesNotExist: // Bucket doesn't exist, means signature probing worked with V2. default: - return nil, err.Trace(args...) + return "", err } } } } - // Set the host url with original URL. - s3Config.HostURL = url + return s3Config.Signature, nil +} + +// buildS3Config constructs an S3 Config and does +// signature auto-probe when needed. +func buildS3Config(args cli.Args) (*Config, *probe.Error) { + var ( + url = args.Get(0) + accessKey = args.Get(1) + secretKey = args.Get(2) + api = args.Get(3) + ) + + s3Config := newS3Config(url, &hostConfigV8{ + AccessKey: accessKey, + SecretKey: secretKey, + URL: url, + }) + + // If api is provided we do not auto probe signature, this is + // required in situations when signature type is provided by the user. + if api != "" { + s3Config.Signature = api + return s3Config, nil + } + + // Probe S3 signature version + api, err := probeS3Signature(accessKey, secretKey, url) + if err != nil { + return nil, err.Trace(args...) + } + + s3Config.Signature = api // Success. return s3Config, nil @@ -190,7 +206,7 @@ func mainConfigHostAdd(ctx *cli.Context) error { console.SetColor("HostMessage", color.New(color.FgGreen)) - s3Config, err := newS3Config(ctx.Args().Tail()) + s3Config, err := buildS3Config(ctx.Args().Tail()) fatalIf(err.Trace(ctx.Args()...), "Unable to initialize new config from the provided credentials") addHost(ctx.Args().Get(0), hostConfigV8{ diff --git a/cmd/utils.go b/cmd/utils.go index 09a24171..a0b8d80e 100644 --- a/cmd/utils.go +++ b/cmd/utils.go @@ -18,7 +18,6 @@ package cmd import ( "crypto/tls" - "fmt" "io" "math/rand" "os" @@ -95,28 +94,27 @@ func splitStr(path, sep string, n int) []string { return splits } -// buildS3Config fetches config related to the specified alias -// to create a new config structure -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)) - } - +// newS3Config simply creates a new Config struct using the passed +// parameters. +func newS3Config(urlStr string, hostCfg *hostConfigV8) *Config { // We have a valid alias and hostConfig. We populate the // credentials from the match found in the config file. s3Config := new(Config) - s3Config.HostURL = urlStr - s3Config.AccessKey = hostCfg.AccessKey - s3Config.SecretKey = hostCfg.SecretKey - s3Config.Signature = hostCfg.API 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 + s3Config.HostURL = urlStr + if hostCfg != nil { + s3Config.AccessKey = hostCfg.AccessKey + s3Config.SecretKey = hostCfg.SecretKey + s3Config.Signature = hostCfg.API + } + + return s3Config } // lineTrunc - truncates a string to the given maximum length by