mirror of https://github.com/minio/minio.git
Golint cleanup pkg/utils/crypto/keys
This commit is contained in:
parent
b33e2d2f9b
commit
66e31445ff
|
@ -92,7 +92,7 @@ func (web *webUiApi) accessHandler(w http.ResponseWriter, req *http.Request) {
|
||||||
var user = config.User{}
|
var user = config.User{}
|
||||||
user.Name = username
|
user.Name = username
|
||||||
|
|
||||||
accesskey, err = keys.GetRandomAlphaNumeric(keys.MINIO_ACCESS_ID)
|
accesskey, err = keys.GenerateRandomAlphaNumeric(keys.MinioAccessID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
w.Write([]byte(err.Error()))
|
w.Write([]byte(err.Error()))
|
||||||
|
@ -100,7 +100,7 @@ func (web *webUiApi) accessHandler(w http.ResponseWriter, req *http.Request) {
|
||||||
}
|
}
|
||||||
user.AccessKey = string(accesskey)
|
user.AccessKey = string(accesskey)
|
||||||
|
|
||||||
secretkey, err = keys.GetRandomBase64(keys.MINIO_SECRET_ID)
|
secretkey, err = keys.GenerateRandomBase64(keys.MinioSecretID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
w.Write([]byte(err.Error()))
|
w.Write([]byte(err.Error()))
|
||||||
|
|
|
@ -46,8 +46,8 @@ func (s *MySuite) TestConfig(c *C) {
|
||||||
}
|
}
|
||||||
conf.configLock = new(sync.RWMutex)
|
conf.configLock = new(sync.RWMutex)
|
||||||
|
|
||||||
accesskey, _ := keys.GetRandomAlphaNumeric(keys.MINIO_ACCESS_ID)
|
accesskey, _ := keys.GenerateRandomAlphaNumeric(keys.MINIO_ACCESS_ID)
|
||||||
secretkey, _ := keys.GetRandomBase64(keys.MINIO_SECRET_ID)
|
secretkey, _ := keys.GenerateRandomBase64(keys.MINIO_SECRET_ID)
|
||||||
|
|
||||||
user := User{
|
user := User{
|
||||||
Name: "gnubot",
|
Name: "gnubot",
|
||||||
|
@ -62,8 +62,8 @@ func (s *MySuite) TestConfig(c *C) {
|
||||||
err = conf.ReadConfig()
|
err = conf.ReadConfig()
|
||||||
c.Assert(err, IsNil)
|
c.Assert(err, IsNil)
|
||||||
|
|
||||||
accesskey, _ = keys.GetRandomAlphaNumeric(keys.MINIO_ACCESS_ID)
|
accesskey, _ = keys.GenerateRandomAlphaNumeric(keys.MINIO_ACCESS_ID)
|
||||||
secretkey, _ = keys.GetRandomBase64(keys.MINIO_SECRET_ID)
|
secretkey, _ = keys.GenerateRandomBase64(keys.MINIO_SECRET_ID)
|
||||||
user = User{
|
user = User{
|
||||||
Name: "minio",
|
Name: "minio",
|
||||||
AccessKey: string(accesskey),
|
AccessKey: string(accesskey),
|
||||||
|
|
|
@ -16,9 +16,10 @@
|
||||||
|
|
||||||
package keys
|
package keys
|
||||||
|
|
||||||
|
// AccessID and SecretID length in bytes
|
||||||
const (
|
const (
|
||||||
MINIO_ACCESS_ID = 20
|
MinioAccessID = 20
|
||||||
MINIO_SECRET_ID = 40
|
MinioSecretID = 40
|
||||||
)
|
)
|
||||||
|
|
||||||
/// helpers
|
/// helpers
|
||||||
|
@ -28,8 +29,8 @@ func isalnum(c byte) bool {
|
||||||
return '0' <= c && c <= '9' || 'A' <= c && c <= 'Z' || 'a' <= c && c <= 'z'
|
return '0' <= c && c <= '9' || 'A' <= c && c <= 'Z' || 'a' <= c && c <= 'z'
|
||||||
}
|
}
|
||||||
|
|
||||||
// validate access key for only alphanumeric characters
|
// IsValidAccessKey - validate access key for only alphanumeric characters
|
||||||
func ValidateAccessKey(key []byte) bool {
|
func IsValidAccessKey(key []byte) bool {
|
||||||
for _, char := range key {
|
for _, char := range key {
|
||||||
if isalnum(char) {
|
if isalnum(char) {
|
||||||
continue
|
continue
|
||||||
|
|
|
@ -23,13 +23,10 @@ import (
|
||||||
|
|
||||||
// Static alphaNumeric table used for generating unique keys
|
// Static alphaNumeric table used for generating unique keys
|
||||||
var alphaNumericTable = []byte("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ")
|
var alphaNumericTable = []byte("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ")
|
||||||
var alphaNumericTableFull = []byte("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789")
|
|
||||||
|
|
||||||
/// helpers
|
// GenerateRandomAlphaNumeric - generate random alpha numeric value using only uppercase characters
|
||||||
|
|
||||||
// Generate random alpha numeric value using only uppercase characters
|
|
||||||
// takes input as size in integer
|
// takes input as size in integer
|
||||||
func GetRandomAlphaNumeric(size int) ([]byte, error) {
|
func GenerateRandomAlphaNumeric(size int) ([]byte, error) {
|
||||||
alpha := make([]byte, size)
|
alpha := make([]byte, size)
|
||||||
_, err := rand.Read(alpha)
|
_, err := rand.Read(alpha)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -42,22 +39,8 @@ func GetRandomAlphaNumeric(size int) ([]byte, error) {
|
||||||
return alpha, nil
|
return alpha, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generate random alpha numeric value using all alphanumeric characters
|
// GenerateRandomBase64 - generate random base64 numeric value from a random seed.
|
||||||
// takes input as size in integer
|
func GenerateRandomBase64(size int) ([]byte, error) {
|
||||||
func GetRandomAlphaNumericFull(size int) ([]byte, error) {
|
|
||||||
alphaFull := make([]byte, size)
|
|
||||||
_, err := rand.Read(alphaFull)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
for i := 0; i < size; i++ {
|
|
||||||
alphaFull[i] = alphaNumericTableFull[alphaFull[i]%byte(len(alphaNumericTableFull))]
|
|
||||||
}
|
|
||||||
return alphaFull, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Generate random base64 numeric value from a random seed.
|
|
||||||
func GetRandomBase64(size int) ([]byte, error) {
|
|
||||||
rb := make([]byte, size)
|
rb := make([]byte, size)
|
||||||
_, err := rand.Read(rb)
|
_, err := rand.Read(rb)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -29,10 +29,10 @@ type MySuite struct{}
|
||||||
var _ = Suite(&MySuite{})
|
var _ = Suite(&MySuite{})
|
||||||
|
|
||||||
func (s *MySuite) Testing(c *C) {
|
func (s *MySuite) Testing(c *C) {
|
||||||
value, err := GetRandomBase64(MINIO_SECRET_ID)
|
value, err := GenerateRandomBase64(MinioSecretID)
|
||||||
c.Assert(err, IsNil)
|
c.Assert(err, IsNil)
|
||||||
|
|
||||||
alphanum, err := GetRandomAlphaNumeric(MINIO_ACCESS_ID)
|
alphanum, err := GenerateRandomAlphaNumeric(MinioAccessID)
|
||||||
c.Assert(err, IsNil)
|
c.Assert(err, IsNil)
|
||||||
|
|
||||||
c.Log(string(value))
|
c.Log(string(value))
|
||||||
|
|
|
@ -32,7 +32,7 @@ import (
|
||||||
"github.com/minio-io/minio/pkg/utils/config"
|
"github.com/minio-io/minio/pkg/utils/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Sign a given http request using HMAC style signatures
|
// SignRequest - a given http request using HMAC style signatures
|
||||||
func SignRequest(user config.User, req *http.Request) {
|
func SignRequest(user config.User, req *http.Request) {
|
||||||
if date := req.Header.Get("Date"); date == "" {
|
if date := req.Header.Get("Date"); date == "" {
|
||||||
req.Header.Set("Date", time.Now().UTC().Format(http.TimeFormat))
|
req.Header.Set("Date", time.Now().UTC().Format(http.TimeFormat))
|
||||||
|
@ -49,7 +49,7 @@ func SignRequest(user config.User, req *http.Request) {
|
||||||
req.Header.Set("Authorization", authHeader.String())
|
req.Header.Set("Authorization", authHeader.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate an API request by validating its signature using HMAC signatures
|
// ValidateRequest - an API request by validating its signature using HMAC signatures
|
||||||
func ValidateRequest(user config.User, req *http.Request) (bool, error) {
|
func ValidateRequest(user config.User, req *http.Request) (bool, error) {
|
||||||
// Verify if date headers are set, if not reject the request
|
// Verify if date headers are set, if not reject the request
|
||||||
if req.Header.Get("x-amz-date") == "" {
|
if req.Header.Get("x-amz-date") == "" {
|
||||||
|
@ -117,7 +117,7 @@ func hasPrefixCaseInsensitive(s, pfx string) bool {
|
||||||
|
|
||||||
// Canonicalize amazon special headers, headers starting with 'x-amz-'
|
// Canonicalize amazon special headers, headers starting with 'x-amz-'
|
||||||
func writeCanonicalizedAmzHeaders(buf *bytes.Buffer, req *http.Request) {
|
func writeCanonicalizedAmzHeaders(buf *bytes.Buffer, req *http.Request) {
|
||||||
amzHeaders := make([]string, 0)
|
var amzHeaders []string
|
||||||
vals := make(map[string][]string)
|
vals := make(map[string][]string)
|
||||||
for k, vv := range req.Header {
|
for k, vv := range req.Header {
|
||||||
if hasPrefixCaseInsensitive(k, "x-amz-") {
|
if hasPrefixCaseInsensitive(k, "x-amz-") {
|
||||||
|
|
Loading…
Reference in New Issue