@@ -17,33 +17,21 @@ import (
17
17
"golang.org/x/crypto/bcrypt"
18
18
)
19
19
20
- // GenerateKubeAdminUserPassword creates and put updated kubeadmin password to ~/.crc/machine/crc/kubeadmin-password
21
- func GenerateKubeAdminUserPassword () error {
22
- logging .Infof ("Generating new password for the kubeadmin user" )
23
- kubeAdminPasswordFile := constants .GetKubeAdminPasswordPath ()
24
- kubeAdminPassword , err := GenerateRandomPasswordHash (23 )
20
+ // GenerateUserPassword creates and put updated password to ~/.crc/machine/crc/ directory
21
+ func GenerateUserPassword (passwordFile string , user string ) error {
22
+ logging .Infof ("Generating new password for the %s user" , user )
23
+ password , err := GenerateRandomPasswordHash (23 )
25
24
if err != nil {
26
- return fmt .Errorf ("Cannot generate the kubeadmin user password: %w" , err )
25
+ return fmt .Errorf ("cannot generate the %s user password: %w" , user , err )
27
26
}
28
- return os .WriteFile (kubeAdminPasswordFile , []byte (kubeAdminPassword ), 0600 )
27
+ return os .WriteFile (passwordFile , []byte (password ), 0600 )
29
28
}
30
29
31
- // UpdateKubeAdminUserPassword updates the htpasswd secret
32
- func UpdateKubeAdminUserPassword (ctx context.Context , ocConfig oc.Config , newPassword string ) error {
33
- if newPassword != "" {
34
- logging .Infof ("Overriding password for kubeadmin user" )
35
- if err := os .WriteFile (constants .GetKubeAdminPasswordPath (), []byte (strings .TrimSpace (newPassword )), 0600 ); err != nil {
36
- return err
37
- }
38
- }
39
-
40
- kubeAdminPassword , err := GetKubeadminPassword ()
30
+ // UpdateUserPasswords updates the htpasswd secret
31
+ func UpdateUserPasswords (ctx context.Context , ocConfig oc.Config , newKubeAdminPassword string , newDeveloperPassword string ) error {
32
+ credentials , err := resolveUserPasswords (newKubeAdminPassword , newDeveloperPassword , constants .GetKubeAdminPasswordPath (), constants .GetDeveloperPasswordPath ())
41
33
if err != nil {
42
- return fmt .Errorf ("Cannot read the kubeadmin user password from file: %w" , err )
43
- }
44
- credentials := map [string ]string {
45
- "developer" : "developer" ,
46
- "kubeadmin" : kubeAdminPassword ,
34
+ return err
47
35
}
48
36
49
37
if err := WaitForOpenshiftResource (ctx , ocConfig , "secret" ); err != nil {
@@ -62,7 +50,7 @@ func UpdateKubeAdminUserPassword(ctx context.Context, ocConfig oc.Config, newPas
62
50
return nil
63
51
}
64
52
65
- logging .Infof ("Changing the password for the kubeadmin user " )
53
+ logging .Infof ("Changing the password for the users " )
66
54
expected , err := getHtpasswd (credentials , externals )
67
55
if err != nil {
68
56
return err
@@ -72,14 +60,13 @@ func UpdateKubeAdminUserPassword(ctx context.Context, ocConfig oc.Config, newPas
72
60
"-n" , "openshift-config" , "--type" , "merge" }
73
61
_ , stderr , err = ocConfig .RunOcCommandPrivate (cmdArgs ... )
74
62
if err != nil {
75
- return fmt .Errorf ("Failed to update kubeadmin password %v: %s" , err , stderr )
63
+ return fmt .Errorf ("failed to update user passwords %v: %s" , err , stderr )
76
64
}
77
65
return nil
78
66
}
79
67
80
- func GetKubeadminPassword () (string , error ) {
81
- kubeAdminPasswordFile := constants .GetKubeAdminPasswordPath ()
82
- rawData , err := os .ReadFile (kubeAdminPasswordFile )
68
+ func GetUserPassword (passwordFile string ) (string , error ) {
69
+ rawData , err := os .ReadFile (passwordFile )
83
70
if err != nil {
84
71
return "" , err
85
72
}
@@ -192,3 +179,31 @@ func testBCryptPassword(password, hash string) (bool, error) {
192
179
}
193
180
return true , nil
194
181
}
182
+
183
+ func resolveUserPasswords (newKubeAdminPassword string , newDeveloperPassword string , kubeAdminPasswordPath string , developerPasswordPath string ) (map [string ]string , error ) {
184
+ if newKubeAdminPassword != "" {
185
+ logging .Infof ("Overriding password for kubeadmin user" )
186
+ if err := os .WriteFile (kubeAdminPasswordPath , []byte (strings .TrimSpace (newKubeAdminPassword )), 0600 ); err != nil {
187
+ return nil , err
188
+ }
189
+ }
190
+ if newDeveloperPassword != "" {
191
+ logging .Infof ("Overriding password for developer user" )
192
+ if err := os .WriteFile (developerPasswordPath , []byte (strings .TrimSpace (newDeveloperPassword )), 0600 ); err != nil {
193
+ return nil , err
194
+ }
195
+ }
196
+
197
+ kubeAdminPassword , err := GetUserPassword (kubeAdminPasswordPath )
198
+ if err != nil {
199
+ return nil , fmt .Errorf ("cannot read the kubeadmin user password from file: %w" , err )
200
+ }
201
+ developerPassword , err := GetUserPassword (developerPasswordPath )
202
+ if err != nil {
203
+ return nil , fmt .Errorf ("cannot read the developer user password from file: %w" , err )
204
+ }
205
+ return map [string ]string {
206
+ "developer" : developerPassword ,
207
+ "kubeadmin" : kubeAdminPassword ,
208
+ }, nil
209
+ }
0 commit comments