File tree Expand file tree Collapse file tree 2 files changed +21
-1
lines changed Expand file tree Collapse file tree 2 files changed +21
-1
lines changed Original file line number Diff line number Diff line change @@ -162,7 +162,11 @@ func (r *ReconcilePostgresUser) Reconcile(request reconcile.Request) (reconcile.
162
162
163
163
// Creation logic
164
164
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
+ }
166
170
167
171
if instance .Status .PostgresRole == "" {
168
172
// We need to get the Postgres CR to get the group role name
@@ -172,6 +176,7 @@ func (r *ReconcilePostgresUser) Reconcile(request reconcile.Request) (reconcile.
172
176
}
173
177
// Create user role
174
178
suffix := utils .GetRandomString (6 )
179
+
175
180
role = fmt .Sprintf ("%s-%s" , instance .Spec .Role , suffix )
176
181
login , err = r .pg .CreateUserRole (role , password )
177
182
if err != nil {
Original file line number Diff line number Diff line change 1
1
package utils
2
2
3
+ import cryptorand "crypto/rand"
3
4
import "math/rand"
4
5
5
6
var letterRunes = []rune ("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890" )
@@ -11,3 +12,17 @@ func GetRandomString(length int) string {
11
12
}
12
13
return string (b )
13
14
}
15
+
16
+ // If the secure random number generator malfunctions it will return an error
17
+ func GetSecureRandomString (length int ) (string , error ) {
18
+ b := make ([]rune , length )
19
+ for i := 0 ; i < length ; i ++ {
20
+ num , err := cryptorand .Int (cryptorand .Reader , big .NewInt (int64 (len (letterRunes ))))
21
+ if err != nil {
22
+ return "" , err
23
+ }
24
+ b [i ] = letterRunes [num .Int64 ()]
25
+ }
26
+
27
+ return string (b ), nil
28
+ }
You can’t perform that action at this time.
0 commit comments