@@ -509,18 +509,7 @@ public UserVm resetVMPassword(ResetVMPasswordCmd cmd, String password)
509509 // update the password in vm_details table too
510510 // Check if an SSH key pair was selected for the instance and if so
511511 // use it to encrypt & save the vm password
512- String sshPublicKey = userVm .getDetail ("SSH.PublicKey" );
513- if (sshPublicKey != null && !sshPublicKey .equals ("" )
514- && password != null && !password .equals ("saved_password" )) {
515- String encryptedPasswd = RSAHelper .encryptWithSSHPublicKey (
516- sshPublicKey , password );
517- if (encryptedPasswd == null ) {
518- throw new CloudRuntimeException ("Error encrypting password" );
519- }
520-
521- userVm .setDetail ("Encrypted.Password" , encryptedPasswd );
522- _vmDao .saveDetails (userVm );
523- }
512+ encryptAndStorePassword (userVm , password );
524513 } else {
525514 throw new CloudRuntimeException (
526515 "Failed to reset password for the virtual machine " );
@@ -643,13 +632,7 @@ public UserVm resetVMSSHKey(ResetVMSSHKeyCmd cmd)
643632 if (template != null && template .getEnablePassword ()) {
644633 userVm .setPassword (password );
645634 //update the encrypted password in vm_details table too
646- if (sshPublicKey != null && !sshPublicKey .equals ("" ) && password != null && !password .equals ("saved_password" )) {
647- String encryptedPasswd = RSAHelper .encryptWithSSHPublicKey (sshPublicKey , password );
648- if (encryptedPasswd == null ) {
649- throw new CloudRuntimeException ("Error encrypting password" );
650- }
651- userVm .setDetail ("Encrypted.Password" , encryptedPasswd );
652- }
635+ encryptAndStorePassword (userVm , password );
653636 }
654637 _vmDao .saveDetails (userVm );
655638 } else {
@@ -3304,18 +3287,7 @@ public Pair<UserVmVO, Map<VirtualMachineProfile.Param, Object>> startVirtualMach
33043287
33053288 // Check if an SSH key pair was selected for the instance and if so
33063289 // use it to encrypt & save the vm password
3307- String sshPublicKey = vm .getDetail ("SSH.PublicKey" );
3308- if (sshPublicKey != null && !sshPublicKey .equals ("" )
3309- && password != null && !password .equals ("saved_password" )) {
3310- String encryptedPasswd = RSAHelper .encryptWithSSHPublicKey (
3311- sshPublicKey , password );
3312- if (encryptedPasswd == null ) {
3313- throw new CloudRuntimeException ("Error encrypting password" );
3314- }
3315-
3316- vm .setDetail ("Encrypted.Password" , encryptedPasswd );
3317- _vmDao .saveDetails (vm );
3318- }
3290+ encryptAndStorePassword (vm , password );
33193291
33203292 params = new HashMap <VirtualMachineProfile .Param , Object >();
33213293 if (additionalParams != null ) {
@@ -4621,15 +4593,7 @@ public UserVm restoreVMInternal(Account caller, UserVmVO vm, Long newTemplateId)
46214593 // update the password in vm_details table too
46224594 // Check if an SSH key pair was selected for the instance and if so
46234595 // use it to encrypt & save the vm password
4624- String sshPublicKey = vm .getDetail ("SSH.PublicKey" );
4625- if (sshPublicKey != null && !sshPublicKey .equals ("" ) && password != null && !password .equals ("saved_password" )) {
4626- String encryptedPasswd = RSAHelper .encryptWithSSHPublicKey (sshPublicKey , password );
4627- if (encryptedPasswd == null ) {
4628- throw new CloudRuntimeException ("VM reset is completed but error occurred when encrypting newly created password" );
4629- }
4630- vm .setDetail ("Encrypted.Password" , encryptedPasswd );
4631- _vmDao .saveDetails (vm );
4632- }
4596+ encryptAndStorePassword (vm , password );
46334597 } else {
46344598 throw new CloudRuntimeException ("VM reset is completed but failed to reset password for the virtual machine " );
46354599 }
@@ -4717,5 +4681,24 @@ public void prepareStop(VirtualMachineProfile<UserVmVO> profile) {
47174681 if (vm .getState () == State .Running )
47184682 collectVmDiskStatistics (vm );
47194683 }
4684+
4685+ private void encryptAndStorePassword (UserVmVO vm , String password ) {
4686+ String sshPublicKey = vm .getDetail ("SSH.PublicKey" );
4687+ if (sshPublicKey != null && !sshPublicKey .equals ("" )
4688+ && password != null && !password .equals ("saved_password" )) {
4689+ if (!sshPublicKey .startsWith ("ssh-rsa" )) {
4690+ s_logger .warn ("Only RSA public keys can be used to encrypt a vm password." );
4691+ return ;
4692+ }
4693+ String encryptedPasswd = RSAHelper .encryptWithSSHPublicKey (
4694+ sshPublicKey , password );
4695+ if (encryptedPasswd == null ) {
4696+ throw new CloudRuntimeException ("Error encrypting password" );
4697+ }
4698+
4699+ vm .setDetail ("Encrypted.Password" , encryptedPasswd );
4700+ _vmDao .saveDetails (vm );
4701+ }
4702+ }
47204703
47214704}
0 commit comments