@@ -17,33 +17,21 @@ import (
1717 "golang.org/x/crypto/bcrypt"
1818)
1919
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 ()
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 )
2423 kubeAdminPassword , err := GenerateRandomPasswordHash (23 )
2524 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 )
2726 }
28- return os .WriteFile (kubeAdminPasswordFile , []byte (kubeAdminPassword ), 0600 )
27+ return os .WriteFile (passwordFile , []byte (kubeAdminPassword ), 0600 )
2928}
3029
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+ // UpdateUserPassword updates the htpasswd secret
31+ func UpdateUserPassword (ctx context.Context , ocConfig oc.Config , newKubeAdminPassword string , newDeveloperPassword string ) error {
32+ credentials , err := resolveUserPassword (newKubeAdminPassword , newDeveloperPassword , constants .GetKubeAdminPasswordPath (), constants .GetDeveloperPasswordPath ())
4133 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
4735 }
4836
4937 if err := WaitForOpenshiftResource (ctx , ocConfig , "secret" ); err != nil {
@@ -77,9 +65,8 @@ func UpdateKubeAdminUserPassword(ctx context.Context, ocConfig oc.Config, newPas
7765 return nil
7866}
7967
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 )
8370 if err != nil {
8471 return "" , err
8572 }
@@ -192,3 +179,31 @@ func testBCryptPassword(password, hash string) (bool, error) {
192179 }
193180 return true , nil
194181}
182+
183+ func resolveUserPassword (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