Skip to content

Commit 1cb9bd5

Browse files
Harikrishna PatnalaAbhinandan Prateek
authored andcommitted
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>
1 parent 10b6c1c commit 1cb9bd5

4 files changed

Lines changed: 30 additions & 8 deletions

File tree

api/src/com/cloud/vm/UserVmService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ VirtualMachine migrateVirtualMachineWithVolume(Long vmId, Host destinationHost,
449449

450450
VirtualMachine vmStorageMigration(Long vmId, StoragePool destPool);
451451

452-
UserVm restoreVM(RestoreVMCmd cmd);
452+
UserVm restoreVM(RestoreVMCmd cmd) throws InsufficientCapacityException, ResourceUnavailableException;
453453

454454
UserVm upgradeVirtualMachine(ScaleVMCmd scaleVMCmd) throws ResourceUnavailableException, ConcurrentOperationException, ManagementServerException, VirtualMachineMigrationException;
455455

server/src/com/cloud/vm/UserVmManagerImpl.java

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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);

server/test/com/cloud/vm/MockUserVmManagerImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ public VirtualMachine vmStorageMigration(Long vmId, StoragePool destPool) {
401401
}
402402

403403
@Override
404-
public UserVm restoreVM(RestoreVMCmd cmd) {
404+
public UserVm restoreVM(RestoreVMCmd cmd) throws InsufficientCapacityException, ResourceUnavailableException{
405405
// TODO Auto-generated method stub
406406
return null;
407407
}

server/test/com/cloud/vm/UserVmManagerTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public void setup(){
121121

122122
// Test restoreVm when VM state not in running/stopped case
123123
@Test(expected=CloudRuntimeException.class)
124-
public void testRestoreVMF1() throws ResourceAllocationException {
124+
public void testRestoreVMF1() throws ResourceAllocationException, InsufficientCapacityException, ResourceUnavailableException {
125125

126126
when(_vmDao.findById(anyLong())).thenReturn(_vmMock);
127127
when(_templateDao.findById(anyLong())).thenReturn(_templateMock);

0 commit comments

Comments
 (0)