2016-12-26 13:21:23 -05:00
|
|
|
/*
|
2017-03-16 03:16:06 -04:00
|
|
|
* Minio Cloud Storage, (C) 2015, 2016, 2017 Minio, Inc.
|
2016-12-26 13:21:23 -05:00
|
|
|
*
|
|
|
|
* 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 (
|
|
|
|
"crypto/rand"
|
2017-08-23 18:59:37 -04:00
|
|
|
"crypto/subtle"
|
2016-12-26 13:21:23 -05:00
|
|
|
"encoding/base64"
|
2017-03-15 19:30:34 -04:00
|
|
|
"errors"
|
2016-12-26 13:21:23 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2017-06-05 18:18:03 -04:00
|
|
|
// Minimum length for Minio access key.
|
|
|
|
accessKeyMinLen = 5
|
|
|
|
|
|
|
|
// Maximum length for Minio access key.
|
2017-08-03 23:03:37 -04:00
|
|
|
// There is no max length enforcement for access keys
|
2017-06-05 18:18:03 -04:00
|
|
|
accessKeyMaxLen = 20
|
|
|
|
|
|
|
|
// Minimum length for Minio secret key for both server and gateway mode.
|
|
|
|
secretKeyMinLen = 8
|
|
|
|
|
|
|
|
// Maximum secret key length for Minio, this
|
|
|
|
// is used when autogenerating new credentials.
|
2017-08-03 23:03:37 -04:00
|
|
|
// There is no max length enforcement for secret keys
|
|
|
|
secretKeyMaxLen = 40
|
2017-06-05 18:18:03 -04:00
|
|
|
|
|
|
|
// Alpha numeric table used for generating access keys.
|
|
|
|
alphaNumericTable = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
|
|
|
|
|
|
// Total length of the alpha numeric table.
|
|
|
|
alphaNumericTableLen = byte(len(alphaNumericTable))
|
2016-12-26 13:21:23 -05:00
|
|
|
)
|
|
|
|
|
2017-06-05 18:18:03 -04:00
|
|
|
// Common errors generated for access and secret key validation.
|
2017-03-16 03:16:06 -04:00
|
|
|
var (
|
2017-08-03 23:03:37 -04:00
|
|
|
errInvalidAccessKeyLength = errors.New("Invalid access key, access key should be minimum 5 characters in length")
|
|
|
|
errInvalidSecretKeyLength = errors.New("Invalid secret key, secret key should be minimum 8 characters in length")
|
2017-03-16 03:16:06 -04:00
|
|
|
)
|
2017-03-16 15:21:58 -04:00
|
|
|
|
2016-12-26 13:21:23 -05:00
|
|
|
// isAccessKeyValid - validate access key for right length.
|
|
|
|
func isAccessKeyValid(accessKey string) bool {
|
2017-08-03 23:03:37 -04:00
|
|
|
return len(accessKey) >= accessKeyMinLen
|
2016-12-26 13:21:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// isSecretKeyValid - validate secret key for right length.
|
|
|
|
func isSecretKeyValid(secretKey string) bool {
|
2017-08-03 23:03:37 -04:00
|
|
|
return len(secretKey) >= secretKeyMinLen
|
2016-12-26 13:21:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// credential container for access and secret keys.
|
|
|
|
type credential struct {
|
2017-08-23 18:59:37 -04:00
|
|
|
AccessKey string `json:"accessKey,omitempty"`
|
|
|
|
SecretKey string `json:"secretKey,omitempty"`
|
2017-01-26 19:51:51 -05:00
|
|
|
}
|
|
|
|
|
2017-03-16 03:16:06 -04:00
|
|
|
// IsValid - returns whether credential is valid or not.
|
|
|
|
func (cred credential) IsValid() bool {
|
|
|
|
return isAccessKeyValid(cred.AccessKey) && isSecretKeyValid(cred.SecretKey)
|
2017-03-15 19:30:34 -04:00
|
|
|
}
|
|
|
|
|
2017-03-16 03:16:06 -04:00
|
|
|
// Equals - returns whether two credentials are equal or not.
|
|
|
|
func (cred credential) Equal(ccred credential) bool {
|
|
|
|
if !ccred.IsValid() {
|
|
|
|
return false
|
2017-01-26 19:51:51 -05:00
|
|
|
}
|
2017-08-23 18:59:37 -04:00
|
|
|
return cred.AccessKey == ccred.AccessKey && subtle.ConstantTimeCompare([]byte(cred.SecretKey), []byte(ccred.SecretKey)) == 1
|
2016-12-26 13:21:23 -05:00
|
|
|
}
|
2016-12-27 11:28:10 -05:00
|
|
|
|
2017-08-23 18:59:37 -04:00
|
|
|
// createCredential returns new credentials from the given access key and secret key.
|
|
|
|
// It returns an error if the access key or secret key are too long or short.
|
|
|
|
func createCredential(accessKey, secretKey string) (credential, error) {
|
2016-12-27 11:28:10 -05:00
|
|
|
if !isAccessKeyValid(accessKey) {
|
2017-08-23 18:59:37 -04:00
|
|
|
return credential{}, errInvalidAccessKeyLength
|
2016-12-27 11:28:10 -05:00
|
|
|
}
|
2017-08-23 18:59:37 -04:00
|
|
|
if !isSecretKeyValid(secretKey) {
|
|
|
|
return credential{}, errInvalidSecretKeyLength
|
|
|
|
}
|
|
|
|
return credential{
|
|
|
|
AccessKey: accessKey,
|
|
|
|
SecretKey: secretKey,
|
|
|
|
}, nil
|
2017-02-07 15:51:43 -05:00
|
|
|
}
|
|
|
|
|
2017-03-16 03:16:06 -04:00
|
|
|
// Initialize a new credential object
|
2017-08-03 23:03:37 -04:00
|
|
|
func getNewCredential(accessKeyLen, secretKeyLen int) (cred credential, err error) {
|
|
|
|
keyBytes := make([]byte, accessKeyLen)
|
|
|
|
_, err = rand.Read(keyBytes)
|
|
|
|
if err != nil {
|
|
|
|
return cred, err
|
|
|
|
}
|
|
|
|
for i := 0; i < accessKeyLen; i++ {
|
2017-03-16 03:16:06 -04:00
|
|
|
keyBytes[i] = alphaNumericTable[keyBytes[i]%alphaNumericTableLen]
|
|
|
|
}
|
|
|
|
accessKey := string(keyBytes)
|
2017-02-07 15:51:43 -05:00
|
|
|
|
2017-03-16 03:16:06 -04:00
|
|
|
// Generate secret key.
|
2017-08-03 23:03:37 -04:00
|
|
|
keyBytes = make([]byte, secretKeyLen)
|
2017-03-23 19:36:00 -04:00
|
|
|
_, err = rand.Read(keyBytes)
|
2017-08-03 23:03:37 -04:00
|
|
|
if err != nil {
|
|
|
|
return cred, err
|
|
|
|
}
|
|
|
|
secretKey := string([]byte(base64.StdEncoding.EncodeToString(keyBytes))[:secretKeyLen])
|
|
|
|
cred, err = createCredential(accessKey, secretKey)
|
2017-02-07 15:51:43 -05:00
|
|
|
|
2017-08-03 23:03:37 -04:00
|
|
|
return cred, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func mustGetNewCredential() credential {
|
|
|
|
// Generate Minio credentials with Minio key max lengths.
|
|
|
|
cred, err := getNewCredential(accessKeyMaxLen, secretKeyMaxLen)
|
|
|
|
fatalIf(err, "Unable to generate new credentials.")
|
2017-03-16 03:16:06 -04:00
|
|
|
return cred
|
2016-12-27 11:28:10 -05:00
|
|
|
}
|