From 480bc8aa2a7fce397e551d10edbb61731e83d257 Mon Sep 17 00:00:00 2001 From: Nitish Tiwari Date: Tue, 31 Jul 2018 20:53:39 +0530 Subject: [PATCH] Use randString() to generate random string for S3 sign probe (#2498) newRandomID() func may return string with uppercase letters. Such strings will make invalid bucket name as S3 doesn't allow uppercase lettes in bucket names. randString() always returns strings with lowercase letters or numbers. Fixes #2496 --- cmd/config-host-add.go | 6 +++++- cmd/utils.go | 25 +++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/cmd/config-host-add.go b/cmd/config-host-add.go index fcfbde45..108096e2 100644 --- a/cmd/config-host-add.go +++ b/cmd/config-host-add.go @@ -17,6 +17,9 @@ package cmd import ( + "math/rand" + "time" + "github.com/fatih/color" "github.com/minio/cli" "github.com/minio/mc/pkg/console" @@ -149,6 +152,7 @@ func addHost(alias string, hostCfgV9 hostConfigV9) { // 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) { + probeBucketName := randString(60, rand.NewSource(time.Now().UnixNano()), "probe-bucket-sign-") // Test s3 connection for API auto probe s3Config := &Config{ // S3 connection parameters @@ -156,7 +160,7 @@ func probeS3Signature(accessKey, secretKey, url string) (string, *probe.Error) { AccessKey: accessKey, SecretKey: secretKey, Signature: "s3v4", - HostURL: urlJoinPath(url, "probe-bucket-sign-"+newRandomID(32)), + HostURL: urlJoinPath(url, probeBucketName), } s3Client, err := s3New(s3Config) diff --git a/cmd/utils.go b/cmd/utils.go index 9cf2b9fb..f4c34db0 100644 --- a/cmd/utils.go +++ b/cmd/utils.go @@ -50,6 +50,13 @@ func isErrIgnored(err *probe.Error) (ignored bool) { return ignored } +const ( + letterBytes = "abcdefghijklmnopqrstuvwxyz01234569" + letterIdxBits = 6 // 6 bits to represent a letter index + letterIdxMask = 1<= 0; { + if remain == 0 { + cache, remain = src.Int63(), letterIdxMax + } + if idx := int(cache & letterIdxMask); idx < len(letterBytes) { + b[i] = letterBytes[idx] + i-- + } + cache >>= letterIdxBits + remain-- + } + return prefix + string(b[0:30-len(prefix)]) +} + // dumpTlsCertificates prints some fields of the certificates received from the server. // Fields will be inspected by the user, so they must be conscise and useful func dumpTLSCertificates(t *tls.ConnectionState) {