Skip to content

Commit dfad178

Browse files
author
Likitha Shetty
committed
Bug CLOUDSTACK-1390: Allow Root/Domain admin to move a User VM to another user under a different domain
Add unit tests
1 parent e40aba3 commit dfad178

3 files changed

Lines changed: 88 additions & 11 deletions

File tree

‎client/tomcatconf/commands.properties.in‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ getVMPassword=15
6767
restoreVirtualMachine=15
6868
changeServiceForVirtualMachine=15
6969
scaleVirtualMachine=15
70-
assignVirtualMachine=1
70+
assignVirtualMachine=7
7171
migrateVirtualMachine=1
7272
migrateVirtualMachineWithVolume=1
7373
recoverVirtualMachine=7

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

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3725,19 +3725,14 @@ public UserVm moveVMToUser(AssignVMCmd cmd)
37253725
+ cmd.getAccountName() + " is disabled.");
37263726
}
37273727

3728-
// make sure the accounts are under same domain
3729-
if (oldAccount.getDomainId() != newAccount.getDomainId()) {
3730-
throw new InvalidParameterValueException(
3731-
"The account should be under same domain for moving VM between two accounts. Old owner domain ="
3732-
+ oldAccount.getDomainId()
3733-
+ " New owner domain="
3734-
+ newAccount.getDomainId());
3735-
}
3728+
//check caller has access to both the old and new account
3729+
_accountMgr.checkAccess(caller, null, true, oldAccount);
3730+
_accountMgr.checkAccess(caller, null, true, newAccount);
37363731

37373732
// make sure the accounts are not same
37383733
if (oldAccount.getAccountId() == newAccount.getAccountId()) {
37393734
throw new InvalidParameterValueException(
3740-
"The account should be same domain for moving VM between two accounts. Account id ="
3735+
"The new account is the same as the old account. Account id ="
37413736
+ oldAccount.getAccountId());
37423737
}
37433738

@@ -3829,6 +3824,7 @@ public UserVm moveVMToUser(AssignVMCmd cmd)
38293824
_resourceLimitMgr.decrementResourceCount(oldAccount.getAccountId(), ResourceType.primary_storage,
38303825
new Long(volume.getSize()));
38313826
volume.setAccountId(newAccount.getAccountId());
3827+
volume.setDomainId(newAccount.getDomainId());
38323828
_volsDao.persist(volume);
38333829
_resourceLimitMgr.incrementResourceCount(newAccount.getAccountId(), ResourceType.volume);
38343830
_resourceLimitMgr.incrementResourceCount(newAccount.getAccountId(), ResourceType.primary_storage,

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

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,26 @@
1717

1818
package com.cloud.vm;
1919

20+
import static org.mockito.Matchers.any;
2021
import static org.mockito.Matchers.anyBoolean;
2122
import static org.mockito.Matchers.anyFloat;
2223
import static org.mockito.Matchers.anyInt;
2324
import static org.mockito.Matchers.anyLong;
25+
import static org.mockito.Matchers.anyString;
2426
import static org.mockito.Matchers.eq;
2527
import static org.mockito.Mockito.doNothing;
2628
import static org.mockito.Mockito.doReturn;
29+
import static org.mockito.Mockito.doThrow;
2730
import static org.mockito.Mockito.when;
2831

2932
import java.lang.reflect.Field;
3033
import java.util.List;
34+
import java.util.UUID;
3135

36+
import org.apache.cloudstack.acl.ControlledEntity;
37+
import org.apache.cloudstack.acl.SecurityChecker.AccessType;
3238
import org.apache.cloudstack.api.ServerApiException;
39+
import org.apache.cloudstack.api.command.admin.vm.AssignVMCmd;
3340
import org.apache.cloudstack.api.command.user.vm.RestoreVMCmd;
3441
import org.apache.cloudstack.api.command.user.vm.ScaleVMCmd;
3542
import org.junit.Before;
@@ -44,9 +51,11 @@
4451
import com.cloud.exception.ConcurrentOperationException;
4552
import com.cloud.exception.InsufficientCapacityException;
4653
import com.cloud.exception.InvalidParameterValueException;
54+
import com.cloud.exception.PermissionDeniedException;
4755
import com.cloud.exception.ResourceAllocationException;
4856
import com.cloud.exception.ResourceUnavailableException;
4957
import com.cloud.hypervisor.Hypervisor;
58+
import com.cloud.hypervisor.Hypervisor.HypervisorType;
5059
import com.cloud.offering.ServiceOffering;
5160
import com.cloud.service.ServiceOfferingVO;
5261
import com.cloud.storage.VMTemplateVO;
@@ -57,6 +66,7 @@
5766
import com.cloud.storage.dao.VolumeDao;
5867
import com.cloud.user.Account;
5968
import com.cloud.user.AccountManager;
69+
import com.cloud.user.AccountService;
6070
import com.cloud.user.AccountVO;
6171
import com.cloud.user.UserContext;
6272
import com.cloud.user.UserVO;
@@ -73,6 +83,7 @@ public class UserVmManagerTest {
7383
@Mock VolumeManager _storageMgr;
7484
@Mock Account _account;
7585
@Mock AccountManager _accountMgr;
86+
@Mock AccountService _accountService;
7687
@Mock ConfigurationManager _configMgr;
7788
@Mock CapacityManager _capacityMgr;
7889
@Mock AccountDao _accountDao;
@@ -91,6 +102,7 @@ public class UserVmManagerTest {
91102
@Mock VMTemplateVO _templateMock;
92103
@Mock VolumeVO _volumeMock;
93104
@Mock List<VolumeVO> _rootVols;
105+
@Mock Account _accountMock2;
94106
@Before
95107
public void setup(){
96108
MockitoAnnotations.initMocks(this);
@@ -102,6 +114,7 @@ public void setup(){
102114
_userVmMgr._itMgr = _itMgr;
103115
_userVmMgr.volumeMgr = _storageMgr;
104116
_userVmMgr._accountDao = _accountDao;
117+
_userVmMgr._accountService = _accountService;
105118
_userVmMgr._userDao = _userDao;
106119
_userVmMgr._accountMgr = _accountMgr;
107120
_userVmMgr._configMgr = _configMgr;
@@ -370,6 +383,74 @@ private ServiceOfferingVO getSvcoffering(int ramSize){
370383
return serviceOffering;
371384
}
372385

373-
386+
// Test Move VM b/w accounts where caller is not ROOT/Domain admin
387+
@Test(expected=InvalidParameterValueException.class)
388+
public void testMoveVmToUser1() throws Exception {
389+
AssignVMCmd cmd = new AssignVMCmd();
390+
Class<?> _class = cmd.getClass();
391+
392+
Field virtualmachineIdField = _class.getDeclaredField("virtualMachineId");
393+
virtualmachineIdField.setAccessible(true);
394+
virtualmachineIdField.set(cmd, 1L);
395+
396+
Field accountNameField = _class.getDeclaredField("accountName");
397+
accountNameField.setAccessible(true);
398+
accountNameField.set(cmd, "account");
399+
400+
Field domainIdField = _class.getDeclaredField("domainId");
401+
domainIdField.setAccessible(true);
402+
domainIdField.set(cmd, 1L);
403+
404+
// caller is of type 0
405+
Account caller = (Account) new AccountVO("testaccount", 1, "networkdomain", (short) 0,
406+
UUID.randomUUID().toString());
407+
UserContext.registerContext(1, caller, null, true);
408+
409+
_userVmMgr.moveVMToUser(cmd);
410+
}
411+
412+
413+
// Test Move VM b/w accounts where caller doesn't have access to the old or new account
414+
@Test(expected=PermissionDeniedException.class)
415+
public void testMoveVmToUser2() throws Exception {
416+
AssignVMCmd cmd = new AssignVMCmd();
417+
Class<?> _class = cmd.getClass();
418+
419+
Field virtualmachineIdField = _class.getDeclaredField("virtualMachineId");
420+
virtualmachineIdField.setAccessible(true);
421+
virtualmachineIdField.set(cmd, 1L);
422+
423+
Field accountNameField = _class.getDeclaredField("accountName");
424+
accountNameField.setAccessible(true);
425+
accountNameField.set(cmd, "account");
426+
427+
Field domainIdField = _class.getDeclaredField("domainId");
428+
domainIdField.setAccessible(true);
429+
domainIdField.set(cmd, 1L);
430+
431+
// caller is of type 0
432+
Account caller = (Account) new AccountVO("testaccount", 1, "networkdomain", (short) 1,
433+
UUID.randomUUID().toString());
434+
UserContext.registerContext(1, caller, null, true);
435+
436+
Account oldAccount = (Account) new AccountVO("testaccount", 1, "networkdomain", (short) 0,
437+
UUID.randomUUID().toString());
438+
Account newAccount = (Account) new AccountVO("testaccount", 1, "networkdomain", (short) 1,
439+
UUID.randomUUID().toString());
440+
441+
UserVmVO vm = new UserVmVO(10L, "test", "test", 1L, HypervisorType.Any, 1L, false, false, 1L, 1L,
442+
5L, "test", "test", 1L);
443+
vm.setState(VirtualMachine.State.Stopped);
444+
when(_vmDao.findById(anyLong())).thenReturn(vm);
445+
446+
when(_accountService.getActiveAccountById(anyLong())).thenReturn(oldAccount);
447+
448+
when(_accountService.getActiveAccountByName(anyString(), anyLong())).thenReturn(newAccount);
449+
450+
doThrow(new PermissionDeniedException("Access check failed")).when(_accountMgr).checkAccess(any(Account.class), any(AccessType.class),
451+
any(Boolean.class), any(ControlledEntity.class));
452+
453+
_userVmMgr.moveVMToUser(cmd);
454+
}
374455

375456
}

0 commit comments

Comments
 (0)