Skip to content

Commit 69c73a8

Browse files
author
Ramgopal Nagaboina
committed
server: fix operator precedence in the updateResourceLimit own-domain guard
The domain branch guard parsed as (ownDomain && DOMAIN_ADMIN) || RESOURCE_DOMAIN_ADMIN because && binds tighter than ||, so a resource domain admin was denied updating the limits of every domain, including sub-domains it administers. Group the two admin types so only the caller's own domain is rejected, the same way the account branch of the method already does.
1 parent b93f603 commit 69c73a8

2 files changed

Lines changed: 49 additions & 1 deletion

File tree

‎server/src/main/java/com/cloud/resourcelimit/ResourceLimitManagerImpl.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -966,7 +966,7 @@ public ResourceLimitVO updateResourceLimit(Long accountId, Long domainId, Intege
966966
throw new PermissionDeniedException("Cannot update resource limit for ROOT domain " + domainId + ", permission denied");
967967
}
968968

969-
if ((caller.getDomainId() == domainId) && caller.getType() == Account.Type.DOMAIN_ADMIN || caller.getType() == Account.Type.RESOURCE_DOMAIN_ADMIN) {
969+
if ((caller.getDomainId() == domainId) && (caller.getType() == Account.Type.DOMAIN_ADMIN || caller.getType() == Account.Type.RESOURCE_DOMAIN_ADMIN)) {
970970
// if the admin is trying to update their own domain, disallow...
971971
throw new PermissionDeniedException("Unable to update resource limit for domain " + domainId + ", permission denied");
972972
}

‎server/src/test/java/com/cloud/resourcelimit/ResourceLimitManagerImplTest.java‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.apache.cloudstack.api.response.AccountResponse;
2727
import org.apache.cloudstack.api.response.DomainResponse;
2828
import org.apache.cloudstack.api.response.TaggedResourceLimitAndCountResponse;
29+
import org.apache.cloudstack.context.CallContext;
2930
import org.apache.cloudstack.framework.config.ConfigKey;
3031
import org.apache.cloudstack.reservation.dao.ReservationDao;
3132
import org.apache.cloudstack.resourcelimit.Reserver;
@@ -57,6 +58,7 @@
5758
import com.cloud.domain.Domain;
5859
import com.cloud.domain.DomainVO;
5960
import com.cloud.domain.dao.DomainDao;
61+
import com.cloud.exception.PermissionDeniedException;
6062
import com.cloud.exception.ResourceAllocationException;
6163
import com.cloud.offering.DiskOffering;
6264
import com.cloud.offering.ServiceOffering;
@@ -73,8 +75,10 @@
7375
import com.cloud.user.AccountManager;
7476
import com.cloud.user.AccountVO;
7577
import com.cloud.user.ResourceLimitService;
78+
import com.cloud.user.User;
7679
import com.cloud.user.dao.AccountDao;
7780
import com.cloud.utils.Pair;
81+
import com.cloud.utils.db.EntityManager;
7882
import com.cloud.vm.VirtualMachine;
7983
import com.cloud.vm.VirtualMachineManager;
8084
import com.cloud.vm.dao.UserVmDao;
@@ -123,6 +127,8 @@ public class ResourceLimitManagerImplTest extends TestCase {
123127
UserVmDao userVmDao;
124128
@Mock
125129
SnapshotDataStoreDao snapshotDataStoreDao;
130+
@Mock
131+
EntityManager entityManager;
126132

127133
private List<String> hostTags = List.of("htag1", "htag2", "htag3");
128134
private List<String> storageTags = List.of("stag1", "stag2");
@@ -1178,4 +1184,46 @@ public void testDecrementVmMemoryResourceCount() {
11781184
Mockito.verify(resourceLimitManager, Mockito.times(1))
11791185
.decrementResourceCountWithTag(accountId, Resource.ResourceType.memory, tag, Long.valueOf(memory));
11801186
}
1187+
1188+
private Account mockResourceDomainAdmin(long domainId) {
1189+
Account resourceDomainAdmin = Mockito.mock(Account.class);
1190+
Mockito.when(resourceDomainAdmin.getDomainId()).thenReturn(domainId);
1191+
Mockito.when(resourceDomainAdmin.getType()).thenReturn(Account.Type.RESOURCE_DOMAIN_ADMIN);
1192+
return resourceDomainAdmin;
1193+
}
1194+
1195+
@Test
1196+
public void updateResourceLimitAllowsAResourceDomainAdminToUpdateASubdomain() {
1197+
long ownDomainId = 5L;
1198+
long subdomainId = 7L;
1199+
Domain subdomain = Mockito.mock(Domain.class);
1200+
Mockito.when(subdomain.getParent()).thenReturn(ownDomainId);
1201+
Mockito.when(entityManager.findById(Domain.class, subdomainId)).thenReturn(subdomain);
1202+
DomainVO ownDomain = Mockito.mock(DomainVO.class);
1203+
Mockito.when(domainDao.findById(ownDomainId)).thenReturn(ownDomain);
1204+
Mockito.doReturn((long) Resource.RESOURCE_UNLIMITED).when(resourceLimitManager)
1205+
.findCorrectResourceLimitForDomain(ownDomain, Resource.ResourceType.user_vm, null);
1206+
1207+
CallContext.register(Mockito.mock(User.class), mockResourceDomainAdmin(ownDomainId));
1208+
try {
1209+
resourceLimitManager.updateResourceLimit(null, subdomainId, Resource.ResourceType.user_vm.getOrdinal(), 10L, null);
1210+
} finally {
1211+
CallContext.unregister();
1212+
}
1213+
Mockito.verify(resourceLimitDao).persist(Mockito.any(ResourceLimitVO.class));
1214+
}
1215+
1216+
@Test
1217+
public void updateResourceLimitDeniesAResourceDomainAdminOnTheirOwnDomain() {
1218+
long ownDomainId = 5L;
1219+
Mockito.when(entityManager.findById(Domain.class, ownDomainId)).thenReturn(Mockito.mock(Domain.class));
1220+
1221+
CallContext.register(Mockito.mock(User.class), mockResourceDomainAdmin(ownDomainId));
1222+
try {
1223+
Assert.assertThrows(PermissionDeniedException.class, () ->
1224+
resourceLimitManager.updateResourceLimit(null, ownDomainId, Resource.ResourceType.user_vm.getOrdinal(), 10L, null));
1225+
} finally {
1226+
CallContext.unregister();
1227+
}
1228+
}
11811229
}

0 commit comments

Comments
 (0)