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
This commit is contained in:
Nitish Tiwari
2018-07-31 20:53:39 +05:30
committed by GitHub
parent a8c97b467d
commit 480bc8aa2a
2 changed files with 30 additions and 1 deletions
+5 -1
View File
@@ -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)
+25
View File
@@ -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<<letterIdxBits - 1 // All 1-bits, as many as letterIdxBits
letterIdxMax = 63 / letterIdxBits // # of letter indices fitting in 63 bits
)
// UTCNow - returns current UTC time.
func UTCNow() time.Time {
return time.Now().UTC()
@@ -67,6 +74,24 @@ func newRandomID(n int) string {
return string(sid)
}
// randString generates random names and prepends them with a known prefix.
func randString(n int, src rand.Source, prefix string) string {
b := make([]byte, n)
// A rand.Int63() generates 63 random bits, enough for letterIdxMax letters!
for i, cache, remain := n-1, src.Int63(), letterIdxMax; i >= 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) {