mirror of
https://github.com/minio/mc.git
synced 2025-11-13 12:22:45 +03:00
Adding auth tests
This commit is contained in:
41
auth.go
41
auth.go
@@ -16,42 +16,21 @@
|
|||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
// exact key length
|
import (
|
||||||
const (
|
"log"
|
||||||
accessKeyLength = 20
|
"regexp"
|
||||||
secretAccessLength = 40
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// isValidSecretKey - validate secret key
|
// isValidSecretKey - validate secret key
|
||||||
func isValidSecretKey(secretAccesskey string) bool {
|
func isValidSecretKey(secretAccessKey string) bool {
|
||||||
if len(secretAccesskey) != secretAccessLength {
|
log.Println(secretAccessKey)
|
||||||
return false
|
regex := regexp.MustCompile("^.{40}$")
|
||||||
}
|
return regex.MatchString(secretAccessKey)
|
||||||
return true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// isValidAccessKey - validate access key
|
// isValidAccessKey - validate access key
|
||||||
func isValidAccessKey(accessKeyID string) bool {
|
func isValidAccessKey(accessKeyID string) bool {
|
||||||
if len(accessKeyID) != accessKeyLength {
|
regex := regexp.MustCompile("^[A-Z0-9\\-\\.\\_\\~]{20}$")
|
||||||
return false
|
regex.MatchString(accessKeyID)
|
||||||
}
|
return regex.MatchString(accessKeyID)
|
||||||
// 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
|
|
||||||
}
|
}
|
||||||
|
|||||||
86
auth_test.go
Normal file
86
auth_test.go
Normal file
@@ -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)
|
||||||
|
}
|
||||||
@@ -24,9 +24,11 @@ import (
|
|||||||
. "github.com/minio-io/check"
|
. "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)
|
bar := startBar(1024)
|
||||||
c.Assert(bar, Not(IsNil))
|
c.Assert(bar, Not(IsNil))
|
||||||
c.Assert(bar.Units, Equals, pb.U_BYTES)
|
c.Assert(bar.Units, Equals, pb.U_BYTES)
|
||||||
|
|||||||
@@ -46,8 +46,8 @@ var (
|
|||||||
const (
|
const (
|
||||||
// do not pass accesskeyid and secretaccesskey through cli
|
// do not pass accesskeyid and secretaccesskey through cli
|
||||||
// users should manually edit them, add a stub entry
|
// users should manually edit them, add a stub entry
|
||||||
accessKeyID = "YOUR-ACCESS-KEY-ID-HERE"
|
globalAccessKeyID = "YOUR-ACCESS-KEY-ID-HERE"
|
||||||
secretAccesskey = "YOUR-SECRET-ACCESS-KEY-HERE"
|
globalSecretAccessKey = "YOUR-SECRET-ACCESS-KEY-HERE"
|
||||||
)
|
)
|
||||||
|
|
||||||
func getMcConfigDir() (string, error) {
|
func getMcConfigDir() (string, error) {
|
||||||
@@ -163,8 +163,8 @@ func newConfig() (config qdb.Store) {
|
|||||||
localAuth := make(map[string]string)
|
localAuth := make(map[string]string)
|
||||||
|
|
||||||
hosts := make(map[string]map[string]string)
|
hosts := make(map[string]map[string]string)
|
||||||
s3Auth["Auth.AccessKeyID"] = accessKeyID
|
s3Auth["Auth.AccessKeyID"] = globalAccessKeyID
|
||||||
s3Auth["Auth.SecretAccessKey"] = secretAccesskey
|
s3Auth["Auth.SecretAccessKey"] = globalSecretAccessKey
|
||||||
hosts["http*://s3*.amazonaws.com"] = s3Auth
|
hosts["http*://s3*.amazonaws.com"] = s3Auth
|
||||||
|
|
||||||
// local minio server can have this empty until webcli is ready
|
// local minio server can have this empty until webcli is ready
|
||||||
|
|||||||
Reference in New Issue
Block a user