diff --git a/cmd/config-utils.go b/cmd/config-utils.go index 92104d83..39f4ee63 100644 --- a/cmd/config-utils.go +++ b/cmd/config-utils.go @@ -16,52 +16,45 @@ package cmd -import ( - "regexp" - "strings" -) +import "strings" var validAPIs = []string{"S3v4", "S3v2"} -// isValidSecretKey - validate secret key. -func isValidSecretKey(secretKey string) bool { - if secretKey == "" { - return true - } - regex := regexp.MustCompile(`.{8,40}$`) - return regex.MatchString(secretKey) && !strings.ContainsAny(secretKey, "$%^~`!|&*#@") +const ( + accessKeyMinLen = 5 + accessKeyMaxLen = 20 + secretKeyMinLen = 8 + secretKeyMaxLen = 40 +) + +// isValidAccessKey - validate access key for right length. +func isValidAccessKey(accessKey string) bool { + return len(accessKey) >= accessKeyMinLen && len(accessKey) <= accessKeyMaxLen } -// isValidAccessKey - validate access key. -func isValidAccessKey(accessKey string) bool { - if accessKey == "" { - return true - } - regex := regexp.MustCompile(`.{5,40}$`) - return regex.MatchString(accessKey) && !strings.ContainsAny(accessKey, "$%^~`!|&*#@") +// isValidSecretKey - validate secret key for right length. +func isValidSecretKey(secretKey string) bool { + return len(secretKey) >= secretKeyMinLen && len(secretKey) <= secretKeyMaxLen } // isValidHostURL - validate input host url. -func isValidHostURL(hostURL string) bool { - if strings.TrimSpace(hostURL) == "" { - return false +func isValidHostURL(hostURL string) (ok bool) { + if strings.TrimSpace(hostURL) != "" { + url := newClientURL(hostURL) + if url.Scheme == "https" || url.Scheme == "http" { + if url.Path == "/" { + ok = true + } + } } - url := newClientURL(hostURL) - if url.Scheme != "https" && url.Scheme != "http" { - return false - } - if url.Path != "" && url.Path != "/" { - return false - } - return true + return ok } // isValidAPI - Validates if API signature string of supported type. -func isValidAPI(api string) bool { +func isValidAPI(api string) (ok bool) { switch strings.ToLower(api) { case "s3v2", "s3v4": - return true - default: - return false + ok = true } + return ok } diff --git a/cmd/config-utils_test.go b/cmd/config-utils_test.go new file mode 100644 index 00000000..e34b8e3f --- /dev/null +++ b/cmd/config-utils_test.go @@ -0,0 +1,75 @@ +/* + * Minio Client (C) 2016 Minio, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package cmd + +import "testing" + +// Tests valid host URL functionality. +func TestValidHostURL(t *testing.T) { + testCases := []struct { + hostURL string + isHost bool + }{ + { + hostURL: "https://localhost:9000", + isHost: true, + }, + { + hostURL: "/", + isHost: false, + }, + } + + for _, testCase := range testCases { + isHost := isValidHostURL(testCase.hostURL) + if testCase.isHost != isHost { + t.Fatalf("Expected %t, got %t", testCase.isHost, isHost) + } + } +} + +func TestIsValidAPI(t *testing.T) { + equalAssert(isValidAPI("s3V2"), true, t) + equalAssert(isValidAPI("S3v2"), true, t) + equalAssert(isValidAPI("s3"), false, t) +} + +func equalAssert(ok1, ok2 bool, t *testing.T) { + if ok1 != ok2 { + t.Errorf("Expected %t, got %t", ok2, ok1) + } +} + +// Tests valid and invalid secret keys. +func TestValidSecretKeys(t *testing.T) { + equalAssert(isValidSecretKey("password"), true, t) + equalAssert(isValidSecretKey("BYvgJM101sHngl2uzjXS/OBF/aMxAN06JrJ3qJlF"), true, t) + + equalAssert(isValidSecretKey("aaa"), false, t) + equalAssert(isValidSecretKey("password%%"), true, t) +} + +// Tests valid and invalid access keys. +func TestValidAccessKeys(t *testing.T) { + equalAssert(isValidAccessKey("c67W2-r4MAyAYScRl"), true, t) + equalAssert(isValidAccessKey("EXOb76bfeb1234562iu679f11588"), false, t) + equalAssert(isValidAccessKey("BYvgJM101sHngl2uzjXS/OBF/aMxAN06JrJ3qJlF"), false, t) + equalAssert(isValidAccessKey("admin"), true, t) + + equalAssert(isValidAccessKey("aaa"), false, t) + equalAssert(isValidAccessKey("$$%%%%%3333"), true, t) +} diff --git a/cmd/mc_test.go b/cmd/mc_test.go index 80648b21..11aee25c 100644 --- a/cmd/mc_test.go +++ b/cmd/mc_test.go @@ -61,26 +61,6 @@ func (s *TestSuite) TestValidPERMS(c *C) { c.Assert(string(perms), Equals, "upload") } -// Tests valid and invalid secret keys. -func (s *TestSuite) TestValidSecretKeys(c *C) { - c.Assert(isValidSecretKey("password"), Equals, true) - c.Assert(isValidSecretKey("BYvgJM101sHngl2uzjXS/OBF/aMxAN06JrJ3qJlF"), Equals, true) - - c.Assert(isValidSecretKey("aaa"), Equals, false) - c.Assert(isValidSecretKey("password%%"), Equals, false) -} - -// Tests valid and invalid access keys. -func (s *TestSuite) TestValidAccessKeys(c *C) { - c.Assert(isValidAccessKey("c67W2-r4MAyAYScRl"), Equals, true) - c.Assert(isValidAccessKey("EXOb76bfeb1234562iu679f11588"), Equals, true) - c.Assert(isValidAccessKey("BYvgJM101sHngl2uzjXS/OBF/aMxAN06JrJ3qJlF"), Equals, true) - c.Assert(isValidAccessKey("admin"), Equals, true) - - c.Assert(isValidAccessKey("aaa"), Equals, false) - c.Assert(isValidAccessKey("$$%%%%%3333"), Equals, false) -} - func (s *TestSuite) TestInvalidPERMS(c *C) { perms := accessPerms("invalid") c.Assert(perms.isValidAccessPERM(), Equals, false)