Skip to content

Commit

Permalink
Merge pull request #149 from kanor1306/add-secure-string-generator
Browse files Browse the repository at this point in the history
Add secure string generator for Password generator
  • Loading branch information
hitman99 authored Feb 28, 2024
2 parents 027cc24 + a75bc16 commit 9c05343
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 9c05343

Please sign in to comment.