/
utils.go
103 lines (88 loc) · 3.09 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// 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 (
"bytes"
"crypto/rand"
"encoding/json"
"fmt"
"io"
"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 auto generating new credentials.
// There is no max length enforcement for secret keys
secretKeyMaxLen = 40
// Alpha numeric table used for generating access keys.
alphaNumericTable = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
// Alpha numeric table used for generating secret keys.
alphaNumericTableFull = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
// Total length of the alphanumeric table.
alphaNumericTableLen = byte(len(alphaNumericTable))
// Total length of the full alphanumeric table.
alphaNumericTableFullLen = byte(len(alphaNumericTableFull))
)
// 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 = io.ReadFull(rand.Reader, 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
}
for i := 0; i < secretKeyMaxLen; i++ {
keyBytes[i] = alphaNumericTableFull[keyBytes[i]%alphaNumericTableFullLen]
}
secretKey = string(keyBytes)
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()
}
// CompactJSONString removes white spaces, tabs and line return
func CompactJSONString(jsonObject string) (string, error) {
objectByte := []byte(jsonObject)
buffer := new(bytes.Buffer)
if err := json.Compact(buffer, objectByte); err != nil {
return jsonObject, err
}
return buffer.String(), nil
}