Skip to content

Commit

Permalink
Generate complex password
Browse files Browse the repository at this point in the history
  • Loading branch information
sprasad09 committed Sep 24, 2024
1 parent a04efa9 commit 4d2cf61
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
6 changes: 5 additions & 1 deletion internal/controller/mysqluser_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,11 @@ func (r *MySQLUserReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
if err != nil {
if errors.IsNotFound(err) { // Secret doesn't exists -> generate password
log.Info("[password] Generate new password for Secret", "secretName", secretName)
password = utils.GenerateRandomString(16)
password, genErr := utils.GenerateComplexRandomString(16)
if genErr != nil {
log.Error(genErr, "[password] Failed to generate password")
return ctrl.Result{}, genErr // Handle error
}
} else {
log.Error(err, "[password] Failed to get Secret", "secretName", secretName)
return ctrl.Result{}, err // requeue
Expand Down
28 changes: 28 additions & 0 deletions internal/utils/utils_oci.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package utils

import (
"crypto/rand"
"math/big"
)

const (
letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
numberBytes = "0123456789"
specialBytes = "!@#$%^&*()-_=+[]{}|;:,.<>?/~`"
allBytes = letterBytes + numberBytes + specialBytes
)

// GenerateComplexRandomString generates a random string of specified length with a mix of letters, numbers, and special characters.
func GenerateComplexRandomString(length int) (string, error) {
result := make([]byte, length)

for i := 0; i < length; i++ {
index, err := rand.Int(rand.Reader, big.NewInt(int64(len(allBytes))))
if err != nil {
return "", err
}
result[i] = allBytes[index.Int64()]
}
return string(result), nil
}

0 comments on commit 4d2cf61

Please sign in to comment.