diff --git a/auth.go b/auth.go index b4c93214..7ba3a773 100644 --- a/auth.go +++ b/auth.go @@ -16,42 +16,21 @@ package main -// exact key length -const ( - accessKeyLength = 20 - secretAccessLength = 40 +import ( + "log" + "regexp" ) // isValidSecretKey - validate secret key -func isValidSecretKey(secretAccesskey string) bool { - if len(secretAccesskey) != secretAccessLength { - return false - } - return true +func isValidSecretKey(secretAccessKey string) bool { + log.Println(secretAccessKey) + regex := regexp.MustCompile("^.{40}$") + return regex.MatchString(secretAccessKey) } // isValidAccessKey - validate access key func isValidAccessKey(accessKeyID string) bool { - if len(accessKeyID) != accessKeyLength { - return false - } - // Is alphanumeric? - isalnum := func(c rune) bool { - return '0' <= c && c <= '9' || 'A' <= c && c <= 'Z' || 'a' <= c && c <= 'z' - } - for _, char := range accessKeyID { - if isalnum(char) { - continue - } - switch char { - case '-': - case '.': - case '_': - case '~': - continue - default: - return false - } - } - return true + regex := regexp.MustCompile("^[A-Z0-9\\-\\.\\_\\~]{20}$") + regex.MatchString(accessKeyID) + return regex.MatchString(accessKeyID) } diff --git a/auth_test.go b/auth_test.go new file mode 100644 index 00000000..337cb542 --- /dev/null +++ b/auth_test.go @@ -0,0 +1,86 @@ +/* + * Mini Copy, (C) 2015 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 main + +import . "github.com/minio-io/check" + +type AuthSuite struct{} + +var _ = Suite(&AuthSuite{}) + +func (s *AuthSuite) TestAuthAccessKeyLength(c *C) { + // short + result := isValidSecretKey("123456789012345678901234567890123456789") + c.Assert(result, Equals, false) + + // long + result = isValidSecretKey("12345678901234567890123456789012345678901") + c.Assert(result, Equals, false) + + // 40 characters long + result = isValidSecretKey("1234567890123456789012345678901234567890") + c.Assert(result, Equals, true) +} +func (s *AuthSuite) TestValidAccessKeyLength(c *C) { + // short + result := isValidAccessKey("1234567890123456789") + c.Assert(result, Equals, false) + + // long + result = isValidAccessKey("123456789012345678901") + c.Assert(result, Equals, false) + + // 40 characters long + result = isValidAccessKey("12345678901234567890") + c.Assert(result, Equals, true) + + // alphanumberic characters long + result = isValidAccessKey("ABCDEFGHIJ1234567890") + c.Assert(result, Equals, true) + + // alphanumberic characters long + result = isValidAccessKey("A1B2C3D4E5F6G7H8I9J0") + c.Assert(result, Equals, true) + + // alphanumberic lower case characters long + result = isValidAccessKey("a1b2c3d4e5f6g7h8i9j0") + c.Assert(result, Equals, false) + + // alphanumberic with - + result = isValidAccessKey("A1B2C3D4-5F6G7H8I9J0") + c.Assert(result, Equals, true) + + // alphanumberic with . + result = isValidAccessKey("A1B2C3D4E5F6G7H8I.J0") + c.Assert(result, Equals, true) + + // alphanumberic with _ + result = isValidAccessKey("A1B2C3D4E_F6G7H8I9J0") + c.Assert(result, Equals, true) + + // alphanumberic with ~ + result = isValidAccessKey("A1B2C3D4E~F6G7H8I9J0") + c.Assert(result, Equals, true) + + // with all classes + result = isValidAccessKey("A1B2C3D4E~F.G_H8~9J0") + c.Assert(result, Equals, true) + + // with all classes and an extra invalid + result = isValidAccessKey("A1B2$3D4E~F.G_H8~9J0") + c.Assert(result, Equals, false) +} diff --git a/cmd-common_test.go b/cmd-common_test.go index 3678efcb..83ddded0 100644 --- a/cmd-common_test.go +++ b/cmd-common_test.go @@ -24,9 +24,11 @@ import ( . "github.com/minio-io/check" ) -var _ = Suite(&MySuite{}) +type StatusBarSuite struct{} -func (s *MySuite) TestStatusBar(c *C) { +var _ = Suite(&StatusBarSuite{}) + +func (s *StatusBarSuite) TestStatusBar(c *C) { bar := startBar(1024) c.Assert(bar, Not(IsNil)) c.Assert(bar.Units, Equals, pb.U_BYTES) diff --git a/cmd-config.go b/cmd-config.go index bee60faf..ebfd0b68 100644 --- a/cmd-config.go +++ b/cmd-config.go @@ -46,8 +46,8 @@ var ( const ( // do not pass accesskeyid and secretaccesskey through cli // users should manually edit them, add a stub entry - accessKeyID = "YOUR-ACCESS-KEY-ID-HERE" - secretAccesskey = "YOUR-SECRET-ACCESS-KEY-HERE" + globalAccessKeyID = "YOUR-ACCESS-KEY-ID-HERE" + globalSecretAccessKey = "YOUR-SECRET-ACCESS-KEY-HERE" ) func getMcConfigDir() (string, error) { @@ -163,8 +163,8 @@ func newConfig() (config qdb.Store) { localAuth := make(map[string]string) hosts := make(map[string]map[string]string) - s3Auth["Auth.AccessKeyID"] = accessKeyID - s3Auth["Auth.SecretAccessKey"] = secretAccesskey + s3Auth["Auth.AccessKeyID"] = globalAccessKeyID + s3Auth["Auth.SecretAccessKey"] = globalSecretAccessKey hosts["http*://s3*.amazonaws.com"] = s3Auth // local minio server can have this empty until webcli is ready