Refactor {build,new}S3Config to remove a duplicate code (#2329)

This commit is contained in:
A. Elleuch
2018-01-24 12:06:34 -08:00
committed by kannappanr
parent 793cda2ca0
commit 99ee2a5042
4 changed files with 64 additions and 44 deletions
+7 -3
View File
@@ -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)
+4 -2
View File
@@ -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)
+42 -26
View File
@@ -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{
+11 -13
View File
@@ -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