CLOUDSTACK-1325: add password in response of RestoreVM

This commit is contained in:
Wei Zhou 2013-05-27 21:32:56 +02:00
parent 6fa9527353
commit a5723992a0
2 changed files with 34 additions and 27 deletions

View File

@ -416,5 +416,5 @@ public interface UserVmService {
VirtualMachine vmStorageMigration(Long vmId, StoragePool destPool);
UserVm restoreVM(RestoreVMCmd cmd);
UserVm restoreVM(RestoreVMCmd cmd) throws ResourceUnavailableException, InsufficientCapacityException;
}

View File

@ -409,22 +409,11 @@ public class UserVmManagerImpl implements UserVmManager, UserVmService, Manager
_accountMgr.checkAccess(caller, null, true, userVm);
boolean result = resetVMPasswordInternal(cmd, password);
boolean result = resetVMPasswordInternal(vmId, password);
if (result) {
userVm.setPassword(password);
//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 = userVm.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("Error encrypting password");
}
userVm.setDetail("Encrypted.Password", encryptedPasswd);
_vmDao.saveDetails(userVm);
}
savePasswordToDB(userVm, password);
} else {
throw new CloudRuntimeException("Failed to reset password for the virtual machine ");
}
@ -432,8 +421,22 @@ public class UserVmManagerImpl implements UserVmManager, UserVmService, Manager
return userVm;
}
private boolean resetVMPasswordInternal(ResetVMPasswordCmd cmd, String password) throws ResourceUnavailableException, InsufficientCapacityException {
Long vmId = cmd.getId();
private void savePasswordToDB(UserVmVO vm, String password) {
// 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("Error encrypting password");
}
vm.setDetail("Encrypted.Password", encryptedPasswd);
_vmDao.saveDetails(vm);
}
}
private boolean resetVMPasswordInternal(Long vmId, String password) throws ResourceUnavailableException, InsufficientCapacityException {
Long userId = UserContext.current().getCallerUserId();
VMInstanceVO vmInstance = _vmDao.findById(vmId);
@ -2908,16 +2911,7 @@ public class UserVmManagerImpl implements UserVmManager, UserVmService, Manager
}
// 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("Error encrypting password");
}
vm.setDetail("Encrypted.Password", encryptedPasswd);
_vmDao.saveDetails(vm);
}
savePasswordToDB(vm, password);
params = new HashMap<VirtualMachineProfile.Param, Object>();
if (additionalParams != null) {
@ -3709,7 +3703,7 @@ public class UserVmManagerImpl implements UserVmManager, UserVmService, Manager
}
@Override
public UserVm restoreVM(RestoreVMCmd cmd) {
public UserVm restoreVM(RestoreVMCmd cmd) throws ResourceUnavailableException, InsufficientCapacityException {
// Input validation
Account caller = UserContext.current().getCaller();
Long userId = UserContext.current().getCallerUserId();
@ -3781,6 +3775,19 @@ public class UserVmManagerImpl implements UserVmManager, UserVmService, Manager
s_logger.debug("Unable to delete old root volume " + root.getId() + ", user may manually delete it", e);
}
if (template.getEnablePassword()) {
String password = generateRandomPassword();
boolean result = resetVMPasswordInternal(vmId, password);
if (result) {
vm.setPassword(password);
_vmDao.loadDetails(vm);
savePasswordToDB(vm, password);
} else {
s_logger.debug("Failed to reset password when restore the virtual machine " + vmId + ", user need to reset the password later");
}
}
if (needRestart) {
try {
_itMgr.start(vm, null, user, caller);