Skip to content

Commit 4d2cf61

Browse files
committed
Generate complex password
1 parent a04efa9 commit 4d2cf61

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

internal/controller/mysqluser_controller.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,11 @@ func (r *MySQLUserReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
190190
if err != nil {
191191
if errors.IsNotFound(err) { // Secret doesn't exists -> generate password
192192
log.Info("[password] Generate new password for Secret", "secretName", secretName)
193-
password = utils.GenerateRandomString(16)
193+
password, genErr := utils.GenerateComplexRandomString(16)
194+
if genErr != nil {
195+
log.Error(genErr, "[password] Failed to generate password")
196+
return ctrl.Result{}, genErr // Handle error
197+
}
194198
} else {
195199
log.Error(err, "[password] Failed to get Secret", "secretName", secretName)
196200
return ctrl.Result{}, err // requeue

internal/utils/utils_oci.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package utils
2+
3+
import (
4+
"crypto/rand"
5+
"math/big"
6+
)
7+
8+
const (
9+
letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
10+
numberBytes = "0123456789"
11+
specialBytes = "!@#$%^&*()-_=+[]{}|;:,.<>?/~`"
12+
allBytes = letterBytes + numberBytes + specialBytes
13+
)
14+
15+
// GenerateComplexRandomString generates a random string of specified length with a mix of letters, numbers, and special characters.
16+
func GenerateComplexRandomString(length int) (string, error) {
17+
result := make([]byte, length)
18+
19+
for i := 0; i < length; i++ {
20+
index, err := rand.Int(rand.Reader, big.NewInt(int64(len(allBytes))))
21+
if err != nil {
22+
return "", err
23+
}
24+
result[i] = allBytes[index.Int64()]
25+
}
26+
return string(result), nil
27+
}
28+

0 commit comments

Comments
 (0)