mirror of
https://github.com/psviderski/uncloud.git
synced 2026-10-06 13:18:58 +00:00
refactor machine name generation using secrets package
This commit is contained in:
1 parent
22bca16226
commit
2c62e1f6df
1 file changed
+3
-10
@@ -1,9 +1,7 @@
|
||||
package cluster
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"fmt"
|
||||
"math/big"
|
||||
"uncloud/internal/secret"
|
||||
)
|
||||
|
||||
@@ -14,14 +12,9 @@ func NewMachineID() (string, error) {
|
||||
|
||||
// NewRandomMachineName generates a random machine name in the format "machine-xxxx".
|
||||
func NewRandomMachineName() (string, error) {
|
||||
const charset = "abcdefghijklmnopqrstuvwxyz0123456789"
|
||||
suffix := make([]byte, 4)
|
||||
for i := range suffix {
|
||||
randIdx, err := rand.Int(rand.Reader, big.NewInt(int64(len(charset))))
|
||||
suffix, err := secret.RandomAlphaNumeric(4)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("get random number: %w", err)
|
||||
return "", fmt.Errorf("generate random suffix: %w", err)
|
||||
}
|
||||
suffix[i] = charset[randIdx.Int64()]
|
||||
}
|
||||
return "machine-" + string(suffix), nil
|
||||
return "machine-" + suffix, nil
|
||||
}
|
||||
Reference in new issue
Block a user