@@ -485,7 +485,7 @@ public UserVm resetVMPassword(ResetVMPasswordCmd cmd, String password)
485485
486486 _accountMgr .checkAccess (caller , null , true , userVm );
487487
488- boolean result = resetVMPasswordInternal (cmd , password );
488+ boolean result = resetVMPasswordInternal (vmId , password );
489489
490490 if (result ) {
491491 userVm .setPassword (password );
@@ -512,10 +512,9 @@ public UserVm resetVMPassword(ResetVMPasswordCmd cmd, String password)
512512 return userVm ;
513513 }
514514
515- private boolean resetVMPasswordInternal (ResetVMPasswordCmd cmd ,
515+ private boolean resetVMPasswordInternal (Long vmId ,
516516 String password ) throws ResourceUnavailableException ,
517517 InsufficientCapacityException {
518- Long vmId = cmd .getId ();
519518 Long userId = UserContext .current ().getCallerUserId ();
520519 VMInstanceVO vmInstance = _vmDao .findById (vmId );
521520
@@ -4078,7 +4077,7 @@ public UserVm moveVMToUser(AssignVMCmd cmd)
40784077 }
40794078
40804079 @ Override
4081- public UserVm restoreVM (RestoreVMCmd cmd ) {
4080+ public UserVm restoreVM (RestoreVMCmd cmd ) throws InsufficientCapacityException , ResourceUnavailableException {
40824081 // Input validation
40834082 Account caller = UserContext .current ().getCaller ();
40844083
@@ -4096,7 +4095,7 @@ public UserVm restoreVM(RestoreVMCmd cmd) {
40964095 return restoreVMInternal (caller , vm , newTemplateId );
40974096 }
40984097
4099- public UserVm restoreVMInternal (Account caller , UserVmVO vm , Long newTemplateId ){
4098+ public UserVm restoreVMInternal (Account caller , UserVmVO vm , Long newTemplateId ) throws InsufficientCapacityException , ResourceUnavailableException {
41004099
41014100 Long userId = caller .getId ();
41024101 Account owner = _accountDao .findById (vm .getAccountId ());
@@ -4190,6 +4189,29 @@ public UserVm restoreVMInternal(Account caller, UserVmVO vm, Long newTemplateId)
41904189 _volsDao .detachVolume (root .getId ());
41914190 this .volumeMgr .destroyVolume (root );
41924191
4192+ if (template .getEnablePassword ()) {
4193+ String password = generateRandomPassword ();
4194+ boolean result = resetVMPasswordInternal (vmId , password );
4195+ if (result ) {
4196+ vm .setPassword (password );
4197+ _vmDao .loadDetails (vm );
4198+ // update the password in vm_details table too
4199+ // Check if an SSH key pair was selected for the instance and if so
4200+ // use it to encrypt & save the vm password
4201+ String sshPublicKey = vm .getDetail ("SSH.PublicKey" );
4202+ if (sshPublicKey != null && !sshPublicKey .equals ("" ) && password != null && !password .equals ("saved_password" )) {
4203+ String encryptedPasswd = RSAHelper .encryptWithSSHPublicKey (sshPublicKey , password );
4204+ if (encryptedPasswd == null ) {
4205+ throw new CloudRuntimeException ("VM reset is completed but error occurred when encrypting newly created password" );
4206+ }
4207+ vm .setDetail ("Encrypted.Password" , encryptedPasswd );
4208+ _vmDao .saveDetails (vm );
4209+ }
4210+ } else {
4211+ throw new CloudRuntimeException ("VM reset is completed but failed to reset password for the virtual machine " );
4212+ }
4213+ }
4214+
41934215 if (needRestart ) {
41944216 try {
41954217 _itMgr .start (vm , null , user , caller );
0 commit comments