diff --git a/pkg/controller/postgresuser/postgresuser_controller.go b/pkg/controller/postgresuser/postgresuser_controller.go index ff88927c..85a9437f 100644 --- a/pkg/controller/postgresuser/postgresuser_controller.go +++ b/pkg/controller/postgresuser/postgresuser_controller.go @@ -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 diff --git a/pkg/utils/random.go b/pkg/utils/random.go index cd9d8e60..77aebbeb 100644 --- a/pkg/utils/random.go +++ b/pkg/utils/random.go @@ -1,6 +1,8 @@ package utils +import cryptorand "crypto/rand" import "math/rand" +import "math/big" var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890") @@ -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 +} \ No newline at end of file