Skip to content

Commit 9c05343

Browse files
authored
Merge pull request #149 from kanor1306/add-secure-string-generator
Add secure string generator for Password generator
2 parents 027cc24 + a75bc16 commit 9c05343

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

pkg/controller/postgresuser/postgresuser_controller.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,11 @@ func (r *ReconcilePostgresUser) Reconcile(request reconcile.Request) (reconcile.
162162

163163
// Creation logic
164164
var role, login string
165-
password := utils.GetRandomString(15)
165+
password, err := utils.GetSecureRandomString(15)
166+
167+
if err != nil {
168+
return r.requeue(instance, err)
169+
}
166170

167171
if instance.Status.PostgresRole == "" {
168172
// We need to get the Postgres CR to get the group role name

pkg/utils/random.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package utils
22

3+
import cryptorand "crypto/rand"
34
import "math/rand"
5+
import "math/big"
46

57
var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890")
68

@@ -11,3 +13,17 @@ func GetRandomString(length int) string {
1113
}
1214
return string(b)
1315
}
16+
17+
// If the secure random number generator malfunctions it will return an error
18+
func GetSecureRandomString(length int) (string, error) {
19+
b := make([]rune, length)
20+
for i := 0; i < length; i++ {
21+
num, err := cryptorand.Int(cryptorand.Reader, big.NewInt(int64(len(letterRunes))))
22+
if err != nil {
23+
return "", err
24+
}
25+
b[i] = letterRunes[num.Int64()]
26+
}
27+
28+
return string(b), nil
29+
}

0 commit comments

Comments
 (0)