Skip to content

Commit

Permalink
Merge branch 'master' into turtle/ELT-16035
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthias Fuhrmeister authored Feb 29, 2024
2 parents 2c506be + 9c05343 commit 9383b14
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
6 changes: 5 additions & 1 deletion pkg/controller/postgresuser/postgresuser_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,11 @@ func (r *ReconcilePostgresUser) Reconcile(request reconcile.Request) (reconcile.

// Creation logic
var role, login string
password := utils.GetRandomString(15)
password, err := utils.GetSecureRandomString(15)

if err != nil {
return r.requeue(instance, err)
}

if instance.Status.PostgresRole == "" {
// We need to get the Postgres CR to get the group role name
Expand Down
16 changes: 16 additions & 0 deletions pkg/utils/random.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package utils

import cryptorand "crypto/rand"
import "math/rand"
import "math/big"

var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890")

Expand All @@ -11,3 +13,17 @@ func GetRandomString(length int) string {
}
return string(b)
}

// If the secure random number generator malfunctions it will return an error
func GetSecureRandomString(length int) (string, error) {
b := make([]rune, length)
for i := 0; i < length; i++ {
num, err := cryptorand.Int(cryptorand.Reader, big.NewInt(int64(len(letterRunes))))
if err != nil {
return "", err
}
b[i] = letterRunes[num.Int64()]
}

return string(b), nil
}

0 comments on commit 9383b14

Please sign in to comment.