2015-02-08 11:54:21 +01:00
|
|
|
/*
|
2015-07-25 02:51:40 +02:00
|
|
|
* Minio Cloud Storage, (C) 2015 Minio, Inc.
|
2015-02-08 11:54:21 +01: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.
|
|
|
|
*/
|
|
|
|
|
2015-10-05 01:31:07 +02:00
|
|
|
package main
|
2015-01-28 20:48:26 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
2015-01-29 00:27:59 +01:00
|
|
|
"encoding/base64"
|
2015-10-16 20:26:01 +02:00
|
|
|
"regexp"
|
2015-08-04 01:17:21 +02:00
|
|
|
|
2015-10-16 20:26:01 +02:00
|
|
|
"github.com/minio/minio-xl/pkg/probe"
|
2015-01-28 20:48:26 +01:00
|
|
|
)
|
|
|
|
|
2015-10-16 20:26:01 +02:00
|
|
|
const (
|
|
|
|
minioAccessID = 20
|
|
|
|
minioSecretID = 40
|
|
|
|
)
|
|
|
|
|
|
|
|
// isValidAccessKey - validate access key
|
|
|
|
func isValidAccessKey(accessKeyID string) bool {
|
|
|
|
if accessKeyID == "" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
regex := regexp.MustCompile("^[A-Z0-9\\-\\.\\_\\~]{20}$")
|
|
|
|
return regex.MatchString(accessKeyID)
|
|
|
|
}
|
|
|
|
|
2015-10-05 08:11:18 +02:00
|
|
|
// generateAccessKeyID - generate random alpha numeric value using only uppercase characters
|
2015-02-24 02:44:55 +01:00
|
|
|
// takes input as size in integer
|
2015-10-05 08:11:18 +02:00
|
|
|
func generateAccessKeyID() ([]byte, *probe.Error) {
|
2015-10-16 20:26:01 +02:00
|
|
|
alpha := make([]byte, minioAccessID)
|
2015-01-28 20:48:26 +01:00
|
|
|
_, err := rand.Read(alpha)
|
|
|
|
if err != nil {
|
2015-08-08 08:47:22 +02:00
|
|
|
return nil, probe.NewError(err)
|
2015-01-28 20:48:26 +01:00
|
|
|
}
|
2015-10-16 20:26:01 +02:00
|
|
|
for i := 0; i < minioAccessID; i++ {
|
2015-01-28 20:48:26 +01:00
|
|
|
alpha[i] = alphaNumericTable[alpha[i]%byte(len(alphaNumericTable))]
|
|
|
|
}
|
|
|
|
return alpha, nil
|
|
|
|
}
|
|
|
|
|
2015-10-05 08:11:18 +02:00
|
|
|
// generateSecretAccessKey - generate random base64 numeric value from a random seed.
|
|
|
|
func generateSecretAccessKey() ([]byte, *probe.Error) {
|
2015-10-16 20:26:01 +02:00
|
|
|
rb := make([]byte, minioSecretID)
|
2015-01-29 00:27:59 +01:00
|
|
|
_, err := rand.Read(rb)
|
2015-01-28 20:48:26 +01:00
|
|
|
if err != nil {
|
2015-08-08 08:47:22 +02:00
|
|
|
return nil, probe.NewError(err)
|
2015-01-28 20:48:26 +01:00
|
|
|
}
|
2015-10-16 20:26:01 +02:00
|
|
|
return []byte(base64.StdEncoding.EncodeToString(rb))[:minioSecretID], nil
|
2015-01-28 20:48:26 +01:00
|
|
|
}
|
2015-10-05 08:11:18 +02:00
|
|
|
|
|
|
|
// mustGenerateAccessKeyID - must generate random alpha numeric value using only uppercase characters
|
|
|
|
// takes input as size in integer
|
|
|
|
func mustGenerateAccessKeyID() []byte {
|
2015-10-16 20:26:01 +02:00
|
|
|
alpha, err := generateAccessKeyID()
|
|
|
|
fatalIf(err.Trace(), "Unable to generate accessKeyID.", nil)
|
2015-10-05 08:11:18 +02:00
|
|
|
return alpha
|
|
|
|
}
|
|
|
|
|
|
|
|
// mustGenerateSecretAccessKey - generate random base64 numeric value from a random seed.
|
|
|
|
func mustGenerateSecretAccessKey() []byte {
|
2015-10-16 20:26:01 +02:00
|
|
|
secretKey, err := generateSecretAccessKey()
|
|
|
|
fatalIf(err.Trace(), "Unable to generate secretAccessKey.", nil)
|
|
|
|
return secretKey
|
2015-10-05 08:11:18 +02:00
|
|
|
}
|