-
Notifications
You must be signed in to change notification settings - Fork 460
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improvements for kubect-minio plugin
- Use secure GenerateCredentials() function for tenants created via the krew plugin - Use configuration secret instead of credsSecret for tenants created via the krew plugin Signed-off-by: Lenin Alevski <alevsk.8772@gmail.com>
- Loading branch information
Showing
4 changed files
with
200 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// Copyright (C) 2022, MinIO, Inc. | ||
// | ||
// This code is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Affero General Public License, version 3, | ||
// as published by the Free Software Foundation. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Affero General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Affero General Public License, version 3, | ||
// along with this program. If not, see <http://www.gnu.org/licenses/> | ||
|
||
package v2 | ||
|
||
import ( | ||
"crypto/rand" | ||
"encoding/base64" | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
const ( | ||
// Maximum length for MinIO access key. | ||
// There is no max length enforcement for access keys | ||
accessKeyMaxLen = 20 | ||
|
||
// Maximum secret key length for MinIO, this | ||
// is used when autogenerating new credentials. | ||
// There is no max length enforcement for secret keys | ||
secretKeyMaxLen = 40 | ||
|
||
// Alpha numeric table used for generating access keys. | ||
alphaNumericTable = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" | ||
|
||
// Total length of the alpha numeric table. | ||
alphaNumericTableLen = byte(len(alphaNumericTable)) | ||
) | ||
|
||
// 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 | ||
} | ||
|
||
// GenerateTenantConfigurationFile : | ||
func GenerateTenantConfigurationFile(configuration map[string]string) string { | ||
var rawConfiguration strings.Builder | ||
for key, val := range configuration { | ||
rawConfiguration.WriteString(fmt.Sprintf(`export %s="%s"`, key, val) + "\n") | ||
} | ||
return rawConfiguration.String() | ||
} |