mirror of
				https://github.com/apache/cloudstack.git
				synced 2025-11-04 00:02:37 +01:00 
			
		
		
		
	CLOUDSTACK-2180: restoreVirtualMachine returns no password if the template is password enabled
New password is generated as part of restore vm(passwd enabled template) and send new password on VR Signed-off-by: Abhinandan Prateek <aprateek@apache.org>
This commit is contained in:
		
							parent
							
								
									10b6c1c6c8
								
							
						
					
					
						commit
						1cb9bd531f
					
				@ -449,7 +449,7 @@ public interface UserVmService {
 | 
			
		||||
 | 
			
		||||
    VirtualMachine vmStorageMigration(Long vmId, StoragePool destPool);
 | 
			
		||||
 | 
			
		||||
    UserVm restoreVM(RestoreVMCmd cmd);
 | 
			
		||||
    UserVm restoreVM(RestoreVMCmd cmd) throws InsufficientCapacityException, ResourceUnavailableException;
 | 
			
		||||
 | 
			
		||||
    UserVm upgradeVirtualMachine(ScaleVMCmd scaleVMCmd) throws ResourceUnavailableException, ConcurrentOperationException, ManagementServerException, VirtualMachineMigrationException;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -485,7 +485,7 @@ public class UserVmManagerImpl extends ManagerBase implements UserVmManager, Use
 | 
			
		||||
 | 
			
		||||
        _accountMgr.checkAccess(caller, null, true, userVm);
 | 
			
		||||
 | 
			
		||||
        boolean result = resetVMPasswordInternal(cmd, password);
 | 
			
		||||
        boolean result = resetVMPasswordInternal(vmId, password);
 | 
			
		||||
 | 
			
		||||
        if (result) {
 | 
			
		||||
            userVm.setPassword(password);
 | 
			
		||||
@ -512,10 +512,9 @@ public class UserVmManagerImpl extends ManagerBase implements UserVmManager, Use
 | 
			
		||||
        return userVm;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private boolean resetVMPasswordInternal(ResetVMPasswordCmd cmd,
 | 
			
		||||
    private boolean resetVMPasswordInternal(Long vmId,
 | 
			
		||||
            String password) throws ResourceUnavailableException,
 | 
			
		||||
            InsufficientCapacityException {
 | 
			
		||||
        Long vmId = cmd.getId();
 | 
			
		||||
        Long userId = UserContext.current().getCallerUserId();
 | 
			
		||||
        VMInstanceVO vmInstance = _vmDao.findById(vmId);
 | 
			
		||||
 | 
			
		||||
@ -4078,7 +4077,7 @@ public class UserVmManagerImpl extends ManagerBase implements UserVmManager, Use
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public UserVm restoreVM(RestoreVMCmd cmd) {
 | 
			
		||||
    public UserVm restoreVM(RestoreVMCmd cmd) throws InsufficientCapacityException, ResourceUnavailableException {
 | 
			
		||||
        // Input validation
 | 
			
		||||
        Account caller = UserContext.current().getCaller();
 | 
			
		||||
 | 
			
		||||
@ -4096,7 +4095,7 @@ public class UserVmManagerImpl extends ManagerBase implements UserVmManager, Use
 | 
			
		||||
        return restoreVMInternal(caller, vm, newTemplateId);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public UserVm restoreVMInternal(Account caller, UserVmVO vm, Long newTemplateId){
 | 
			
		||||
    public UserVm restoreVMInternal(Account caller, UserVmVO vm, Long newTemplateId) throws InsufficientCapacityException, ResourceUnavailableException {
 | 
			
		||||
 | 
			
		||||
        Long userId = caller.getId();
 | 
			
		||||
        Account owner = _accountDao.findById(vm.getAccountId());
 | 
			
		||||
@ -4190,6 +4189,29 @@ public class UserVmManagerImpl extends ManagerBase implements UserVmManager, Use
 | 
			
		||||
        _volsDao.detachVolume(root.getId());
 | 
			
		||||
        this.volumeMgr.destroyVolume(root);
 | 
			
		||||
 | 
			
		||||
        if (template.getEnablePassword()) {
 | 
			
		||||
            String password = generateRandomPassword();
 | 
			
		||||
            boolean result = resetVMPasswordInternal(vmId, password);
 | 
			
		||||
            if (result) {
 | 
			
		||||
                vm.setPassword(password);
 | 
			
		||||
                _vmDao.loadDetails(vm);
 | 
			
		||||
                // update the password in vm_details table too
 | 
			
		||||
                // Check if an SSH key pair was selected for the instance and if so
 | 
			
		||||
                // use it to encrypt & save the vm password
 | 
			
		||||
                String sshPublicKey = vm.getDetail("SSH.PublicKey");
 | 
			
		||||
                if (sshPublicKey != null && !sshPublicKey.equals("") && password != null && !password.equals("saved_password")) {
 | 
			
		||||
                    String encryptedPasswd = RSAHelper.encryptWithSSHPublicKey(sshPublicKey, password);
 | 
			
		||||
                    if (encryptedPasswd == null) {
 | 
			
		||||
                        throw new CloudRuntimeException("VM reset is completed but error occurred when encrypting newly created password");
 | 
			
		||||
                    }
 | 
			
		||||
                    vm.setDetail("Encrypted.Password", encryptedPasswd);
 | 
			
		||||
                    _vmDao.saveDetails(vm);
 | 
			
		||||
                }
 | 
			
		||||
            } else {
 | 
			
		||||
                throw new CloudRuntimeException("VM reset is completed but failed to reset password for the virtual machine ");
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (needRestart) {
 | 
			
		||||
            try {
 | 
			
		||||
                _itMgr.start(vm, null, user, caller);
 | 
			
		||||
 | 
			
		||||
@ -401,7 +401,7 @@ public class MockUserVmManagerImpl extends ManagerBase implements UserVmManager,
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public UserVm restoreVM(RestoreVMCmd cmd) {
 | 
			
		||||
    public UserVm restoreVM(RestoreVMCmd cmd) throws InsufficientCapacityException, ResourceUnavailableException{
 | 
			
		||||
        // TODO Auto-generated method stub
 | 
			
		||||
        return null;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@ -121,7 +121,7 @@ public class UserVmManagerTest {
 | 
			
		||||
 | 
			
		||||
    // Test restoreVm when VM state not in running/stopped case
 | 
			
		||||
    @Test(expected=CloudRuntimeException.class)
 | 
			
		||||
    public void testRestoreVMF1() throws ResourceAllocationException {
 | 
			
		||||
    public void testRestoreVMF1() throws ResourceAllocationException, InsufficientCapacityException, ResourceUnavailableException {
 | 
			
		||||
 | 
			
		||||
        when(_vmDao.findById(anyLong())).thenReturn(_vmMock);
 | 
			
		||||
        when(_templateDao.findById(anyLong())).thenReturn(_templateMock);
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user