randomly generate AK and SK on mc client (#4565)

This commit is contained in:
dorman
2023-05-09 09:14:34 -07:00
committed by GitHub
parent 6050568e66
commit ada4096d0b
+73
View File
@@ -19,6 +19,8 @@ package cmd
import (
"bytes"
"crypto/rand"
"encoding/base64"
"fmt"
"os"
"strings"
@@ -75,6 +77,12 @@ FLAGS:
EXAMPLES:
1. Add a new service account for user 'foobar' to MinIO server.
{{.Prompt}} {{.HelpName}} myminio foobar
2. Add a new service account to MinIO server with specified access key and secret key for user'foobar'.
{{.Prompt}} {{.HelpName}} myminio foobar --access-key "myaccesskey" --secret-key "mysecretkey"
3. Add a new service account to MinIO server with specified access key and random secret key for user'foobar'.
{{.Prompt}} {{.HelpName}} myminio foobar --access-key "myaccesskey"
4. Add a new service account to MinIO server with specified secret key and random access key for user'foobar'.
{{.Prompt}} {{.HelpName}} myminio foobar --secret-key "mysecretkey"
`,
}
@@ -116,6 +124,21 @@ const (
svcAccOpSet
stsAccOpInfo
// Maximum length for MinIO access key.
// There is no max length enforcement for access keys
accessKeyMaxLen = 20
// Alpha numeric table used for generating access keys.
alphaNumericTable = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
// Total length of the alpha numeric table.
alphaNumericTableLen = byte(len(alphaNumericTable))
// Maximum secret key length for MinIO, this
// is used when autogenerating new credentials.
// There is no max length enforcement for secret keys
secretKeyMaxLen = 40
)
func (u acctMessage) String() string {
@@ -161,6 +184,42 @@ func (u acctMessage) String() string {
return ""
}
// generateCredentials - creates randomly generated credentials of maximum
// allowed length.
func generateCredentials() (accessKey, secretKey string, err error) {
readBytes := func(size int) (data []byte, err error) {
data = make([]byte, size)
var n int
if n, err = rand.Read(data); err != nil {
return nil, err
} else if n != size {
return nil, fmt.Errorf("Not enough data. Expected to read: %v bytes, got: %v bytes", size, n)
}
return data, nil
}
// Generate access key.
keyBytes, err := readBytes(accessKeyMaxLen)
if err != nil {
return "", "", err
}
for i := 0; i < accessKeyMaxLen; i++ {
keyBytes[i] = alphaNumericTable[keyBytes[i]%alphaNumericTableLen]
}
accessKey = string(keyBytes)
// Generate secret key.
keyBytes, err = readBytes(secretKeyMaxLen)
if err != nil {
return "", "", err
}
secretKey = strings.ReplaceAll(string([]byte(base64.StdEncoding.EncodeToString(keyBytes))[:secretKeyMaxLen]),
"/", "+")
return accessKey, secretKey, nil
}
func (u acctMessage) JSON() string {
u.Status = "success"
jsonMessageBytes, e := json.MarshalIndent(u, "", " ")
@@ -185,6 +244,20 @@ func mainAdminUserSvcAcctAdd(ctx *cli.Context) error {
policyPath := ctx.String("policy")
comment := ctx.String("comment")
// generate access key and secret key
if len(accessKey) <= 0 || len(secretKey) <= 0 {
randomAccessKey, randomSecretKey, err := generateCredentials()
if err != nil {
fatalIf(probe.NewError(err), "Unable to add a new service account")
}
if len(accessKey) <= 0 {
accessKey = randomAccessKey
}
if len(secretKey) <= 0 {
secretKey = randomSecretKey
}
}
// Create a new MinIO Admin Client
client, err := newAdminClient(aliasedURL)
fatalIf(err, "Unable to initialize admin connection.")