mirror of
https://github.com/apache/cloudstack
synced 2026-08-02 13:35:41 +00:00
* CLOUDSTACK-10147 Disabled Xenserver Cluster can still deploy VM's. Added code to skip disabled clusters when selecting a host (#2442) (cherry picked from commitc3488a51db) Signed-off-by: Rohit Yadav <rohit.yadav@shapeblue.com> * CLOUDSTACK-10318: Bug on sorting ACL rules list in chrome (#2478) (cherry picked from commit4412563f19) Signed-off-by: Rohit Yadav <rohit.yadav@shapeblue.com> * CLOUDSTACK-10284:Creating a snapshot from VM Snapshot generates error if hypervisor is not KVM. Signed-off-by: Rohit Yadav <rohit.yadav@shapeblue.com> * CLOUDSTACK-10221: Allow IPv6 when creating a Basic Network (#2397) Since CloudStack 4.10 Basic Networking supports IPv6 and thus should be allowed to be specified when creating a network. Signed-off-by: Wido den Hollander <wido@widodh.nl> (cherry picked from commit9733a10ecd) Signed-off-by: Rohit Yadav <rohit.yadav@shapeblue.com> * CLOUDSTACK-10214: Unable to remove local primary storage (#2390) Allow admins to remove primary storage pool. Cherry-picked fromeba2e1d8a1Signed-off-by: Rohit Yadav <rohit.yadav@shapeblue.com> * dateutil: constistency of tzdate input and output (#2392) Signed-off-by: Yoan Blanc <yoan.blanc@exoscale.ch> Signed-off-by: Daan Hoogland <daan.hoogland@shapeblue.com> (cherry picked from commit2ad5202823) Signed-off-by: Rohit Yadav <rohit.yadav@shapeblue.com> * CLOUDSTACK-10054:Volume download times out in 3600 seconds (#2244) (cherry picked from commitbb607d07a9) Signed-off-by: Rohit Yadav <rohit.yadav@shapeblue.com> * When creating a new account (via domain admin) it is possible to select “root admin” as the role for the new user (#2606) * create account with domain admin showing 'root admin' role Domain admins should not be able to assign the role of root admin to new users. Therefore, the role ‘root admin’ (or any other of the same type) should not be visible to domain admins. * License and formatting * Break long sentence into multiple lines * Fix wording of method 'getCurrentAccount' * fix typo in variable name * [CLOUDSTACK-10259] Missing float part of secondary storage data in listAccounts * [CLOUDSTACK-9338] ACS not accounting resources of VMs with custom service offering ACS is accounting the resources properly when deploying VMs with custom service offerings. However, there are other methods (such as updateResourceCount) that do not execute the resource accounting properly, and these methods update the resource count for an account in the database. Therefore, if a user deploys VMs with custom service offerings, and later this user calls the “updateResourceCount” method, it (the method) will only account for VMs with normal service offerings, and update this as the number of resources used by the account. This will result in a smaller number of resources to be accounted for the given account than the real used value. The problem becomes worse because if the user starts to delete these VMs, it is possible to reach negative values of resources allocated (breaking all of the resource limiting for accounts). This is a very serious attack vector for public cloud providers! * [CLOUDSTACK-10230] User should not be able to use removed “Guest OS type” (#2404) * [CLOUDSTACK-10230] User is able to change to “Guest OS type” that has been removed Users are able to change the OS type of VMs to “Guest OS type” that has been removed. This becomes a security issue when we try to force users to use HVM VMs (Meltdown/Spectre thing). A removed “guest os type” should not be usable by any users in the cloud. * Remove trailing lines that are breaking build due to checkstyle compliance * Remove unused imports * fix classes that were in the wrong folder structure * Updates to capacity management
289 lines
11 KiB
Java
289 lines
11 KiB
Java
// Licensed to the Apache Software Foundation (ASF) under one
|
|
// or more contributor license agreements. See the NOTICE file
|
|
// distributed with this work for additional information
|
|
// regarding copyright ownership. The ASF licenses this file
|
|
// to you under the Apache License, Version 2.0 (the
|
|
// "License"); you may not use this file except in compliance
|
|
// with the License. You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing,
|
|
// software distributed under the License is distributed on an
|
|
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
// KIND, either express or implied. See the License for the
|
|
// specific language governing permissions and limitations
|
|
// under the License.
|
|
|
|
package org.apache.cloudstack.acl;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
import org.apache.cloudstack.acl.dao.RoleDao;
|
|
import org.apache.commons.collections.CollectionUtils;
|
|
import org.junit.Assert;
|
|
import org.junit.Before;
|
|
import org.junit.Test;
|
|
import org.junit.runner.RunWith;
|
|
import org.mockito.InjectMocks;
|
|
import org.mockito.Mock;
|
|
import org.mockito.Mockito;
|
|
import org.mockito.Spy;
|
|
import org.mockito.runners.MockitoJUnitRunner;
|
|
|
|
import com.cloud.user.Account;
|
|
import com.cloud.user.AccountManager;
|
|
|
|
@RunWith(MockitoJUnitRunner.class)
|
|
public class RoleManagerImplTest {
|
|
|
|
@Spy
|
|
@InjectMocks
|
|
private RoleManagerImpl roleManagerImpl;
|
|
@Mock
|
|
private AccountManager accountManagerMock;
|
|
@Mock
|
|
private RoleDao roleDaoMock;
|
|
|
|
@Mock
|
|
private Account accountMock;
|
|
private long accountMockId = 100l;
|
|
|
|
@Mock
|
|
private RoleVO roleVoMock;
|
|
private long roleMockId = 1l;
|
|
|
|
@Before
|
|
public void beforeTest() {
|
|
Mockito.doReturn(accountMockId).when(accountMock).getId();
|
|
Mockito.doReturn(accountMock).when(roleManagerImpl).getCurrentAccount();
|
|
|
|
Mockito.doReturn(roleMockId).when(roleVoMock).getId();
|
|
}
|
|
|
|
@Test
|
|
public void findRoleTestIdNull() {
|
|
Role returnedRole = roleManagerImpl.findRole(null);
|
|
Assert.assertNull(returnedRole);
|
|
}
|
|
|
|
@Test
|
|
public void findRoleTestIdZero() {
|
|
Role returnedRole = roleManagerImpl.findRole(0l);
|
|
Assert.assertNull(returnedRole);
|
|
}
|
|
|
|
@Test
|
|
public void findRoleTestIdNegative() {
|
|
Role returnedRole = roleManagerImpl.findRole(-1l);
|
|
Assert.assertNull(returnedRole);
|
|
}
|
|
|
|
@Test
|
|
public void findRoleTestRoleNotFound() {
|
|
Mockito.doReturn(null).when(roleDaoMock).findById(roleMockId);
|
|
Role returnedRole = roleManagerImpl.findRole(roleMockId);
|
|
Assert.assertNull(returnedRole);
|
|
}
|
|
|
|
@Test
|
|
public void findRoleTestNotRootAdminAndNotRoleAdminType() {
|
|
Mockito.doReturn(RoleType.DomainAdmin).when(roleVoMock).getRoleType();
|
|
Mockito.doReturn(roleVoMock).when(roleDaoMock).findById(roleMockId);
|
|
Mockito.doReturn(false).when(accountManagerMock).isRootAdmin(accountMockId);
|
|
|
|
Role returnedRole = roleManagerImpl.findRole(roleMockId);
|
|
|
|
Assert.assertEquals(roleMockId, returnedRole.getId());
|
|
Mockito.verify(accountManagerMock).isRootAdmin(accountMockId);
|
|
Mockito.verify(roleVoMock, Mockito.times(1)).getRoleType();
|
|
}
|
|
|
|
@Test
|
|
public void findRoleTestRootAdminAndNotRoleAdminType() {
|
|
Mockito.doReturn(RoleType.DomainAdmin).when(roleVoMock).getRoleType();
|
|
Mockito.doReturn(roleVoMock).when(roleDaoMock).findById(roleMockId);
|
|
Mockito.doReturn(true).when(accountManagerMock).isRootAdmin(accountMockId);
|
|
|
|
Role returnedRole = roleManagerImpl.findRole(roleMockId);
|
|
|
|
Assert.assertEquals(roleMockId, returnedRole.getId());
|
|
Mockito.verify(accountManagerMock).isRootAdmin(accountMockId);
|
|
Mockito.verify(roleVoMock, Mockito.times(0)).getRoleType();
|
|
}
|
|
|
|
@Test
|
|
public void findRoleTestRootAdminAndRoleAdminType() {
|
|
Mockito.doReturn(RoleType.Admin).when(roleVoMock).getRoleType();
|
|
Mockito.doReturn(roleVoMock).when(roleDaoMock).findById(roleMockId);
|
|
Mockito.doReturn(true).when(accountManagerMock).isRootAdmin(accountMockId);
|
|
|
|
Role returnedRole = roleManagerImpl.findRole(roleMockId);
|
|
|
|
Assert.assertEquals(roleMockId, returnedRole.getId());
|
|
Mockito.verify(accountManagerMock).isRootAdmin(accountMockId);
|
|
Mockito.verify(roleVoMock, Mockito.times(0)).getRoleType();
|
|
}
|
|
|
|
@Test
|
|
public void findRoleTestNotRootAdminAndRoleAdminType() {
|
|
Mockito.doReturn(RoleType.Admin).when(roleVoMock).getRoleType();
|
|
Mockito.doReturn(roleVoMock).when(roleDaoMock).findById(roleMockId);
|
|
Mockito.doReturn(false).when(accountManagerMock).isRootAdmin(accountMockId);
|
|
|
|
Role returnedRole = roleManagerImpl.findRole(roleMockId);
|
|
|
|
Assert.assertNull(returnedRole);
|
|
Mockito.verify(accountManagerMock).isRootAdmin(accountMockId);
|
|
Mockito.verify(roleVoMock, Mockito.times(1)).getRoleType();
|
|
}
|
|
|
|
@Test
|
|
public void findRolesByNameTestNullRoleName() {
|
|
List<Role> rolesFound = roleManagerImpl.findRolesByName(null);
|
|
|
|
Assert.assertTrue(CollectionUtils.isEmpty(rolesFound));
|
|
}
|
|
|
|
@Test
|
|
public void findRolesByNameTestEmptyRoleName() {
|
|
List<Role> rolesFound = roleManagerImpl.findRolesByName("");
|
|
|
|
Assert.assertTrue(CollectionUtils.isEmpty(rolesFound));
|
|
}
|
|
|
|
@Test
|
|
public void findRolesByNameTestBlankRoleName() {
|
|
List<Role> rolesFound = roleManagerImpl.findRolesByName(" ");
|
|
|
|
Assert.assertTrue(CollectionUtils.isEmpty(rolesFound));
|
|
}
|
|
|
|
@Test
|
|
public void findRolesByNameTest() {
|
|
String roleName = "roleName";
|
|
ArrayList<Role> toBeReturned = new ArrayList<>();
|
|
Mockito.doReturn(toBeReturned).when(roleDaoMock).findAllByName(roleName);
|
|
|
|
roleManagerImpl.findRolesByName(roleName);
|
|
|
|
Mockito.verify(roleManagerImpl).removeRootAdminRolesIfNeeded(toBeReturned);
|
|
}
|
|
|
|
@Test
|
|
public void removeRootAdminRolesIfNeededTestRootAdmin() {
|
|
Mockito.doReturn(accountMock).when(roleManagerImpl).getCurrentAccount();
|
|
Mockito.doReturn(true).when(accountManagerMock).isRootAdmin(accountMockId);
|
|
|
|
List<Role> roles = new ArrayList<>();
|
|
roleManagerImpl.removeRootAdminRolesIfNeeded(roles);
|
|
|
|
Mockito.verify(roleManagerImpl, Mockito.times(0)).removeRootAdminRoles(roles);
|
|
}
|
|
|
|
@Test
|
|
public void removeRootAdminRolesIfNeededTestNonRootAdminUser() {
|
|
Mockito.doReturn(accountMock).when(roleManagerImpl).getCurrentAccount();
|
|
Mockito.doReturn(false).when(accountManagerMock).isRootAdmin(accountMockId);
|
|
|
|
List<Role> roles = new ArrayList<>();
|
|
roleManagerImpl.removeRootAdminRolesIfNeeded(roles);
|
|
|
|
Mockito.verify(roleManagerImpl, Mockito.times(1)).removeRootAdminRoles(roles);
|
|
}
|
|
|
|
@Test
|
|
public void removeRootAdminRolesTest() {
|
|
List<Role> roles = new ArrayList<>();
|
|
Role roleRootAdmin = Mockito.mock(Role.class);
|
|
Mockito.doReturn(RoleType.Admin).when(roleRootAdmin).getRoleType();
|
|
|
|
Role roleDomainAdmin = Mockito.mock(Role.class);
|
|
Mockito.doReturn(RoleType.DomainAdmin).when(roleDomainAdmin).getRoleType();
|
|
|
|
Role roleResourceAdmin = Mockito.mock(Role.class);
|
|
Mockito.doReturn(RoleType.ResourceAdmin).when(roleResourceAdmin).getRoleType();
|
|
|
|
Role roleUser = Mockito.mock(Role.class);
|
|
Mockito.doReturn(RoleType.User).when(roleUser).getRoleType();
|
|
|
|
roles.add(roleRootAdmin);
|
|
roles.add(roleDomainAdmin);
|
|
roles.add(roleResourceAdmin);
|
|
roles.add(roleUser);
|
|
|
|
roleManagerImpl.removeRootAdminRoles(roles);
|
|
|
|
Assert.assertEquals(3, roles.size());
|
|
Assert.assertEquals(roleDomainAdmin, roles.get(0));
|
|
Assert.assertEquals(roleResourceAdmin, roles.get(1));
|
|
Assert.assertEquals(roleUser, roles.get(2));
|
|
}
|
|
|
|
@Test
|
|
public void findRolesByTypeTestNullRoleType() {
|
|
List<Role> returnedRoles = roleManagerImpl.findRolesByType(null);
|
|
|
|
Assert.assertEquals(0, returnedRoles.size());
|
|
Mockito.verify(accountManagerMock, Mockito.times(0)).isRootAdmin(Mockito.anyLong());
|
|
}
|
|
|
|
@Test
|
|
public void findRolesByTypeTestAdminRoleNonRootAdminUser() {
|
|
Mockito.doReturn(accountMock).when(roleManagerImpl).getCurrentAccount();
|
|
Mockito.doReturn(false).when(accountManagerMock).isRootAdmin(accountMockId);
|
|
|
|
List<Role> returnedRoles = roleManagerImpl.findRolesByType(RoleType.Admin);
|
|
|
|
Assert.assertEquals(0, returnedRoles.size());
|
|
Mockito.verify(accountManagerMock, Mockito.times(1)).isRootAdmin(Mockito.anyLong());
|
|
Mockito.verify(roleDaoMock, Mockito.times(0)).findAllByRoleType(Mockito.any(RoleType.class));
|
|
}
|
|
|
|
@Test
|
|
public void findRolesByTypeTestAdminRoleRootAdminUser() {
|
|
Mockito.doReturn(accountMock).when(roleManagerImpl).getCurrentAccount();
|
|
Mockito.doReturn(true).when(accountManagerMock).isRootAdmin(accountMockId);
|
|
|
|
List<Role> roles = new ArrayList<>();
|
|
roles.add(Mockito.mock(Role.class));
|
|
Mockito.doReturn(roles).when(roleDaoMock).findAllByRoleType(RoleType.Admin);
|
|
List<Role> returnedRoles = roleManagerImpl.findRolesByType(RoleType.Admin);
|
|
|
|
Assert.assertEquals(1, returnedRoles.size());
|
|
Mockito.verify(accountManagerMock, Mockito.times(1)).isRootAdmin(Mockito.anyLong());
|
|
Mockito.verify(roleDaoMock, Mockito.times(1)).findAllByRoleType(Mockito.any(RoleType.class));
|
|
}
|
|
|
|
@Test
|
|
public void findRolesByTypeTestNonAdminRoleRootAdminUser() {
|
|
Mockito.doReturn(accountMock).when(roleManagerImpl).getCurrentAccount();
|
|
Mockito.doReturn(true).when(accountManagerMock).isRootAdmin(accountMockId);
|
|
|
|
List<Role> roles = new ArrayList<>();
|
|
roles.add(Mockito.mock(Role.class));
|
|
Mockito.doReturn(roles).when(roleDaoMock).findAllByRoleType(RoleType.User);
|
|
List<Role> returnedRoles = roleManagerImpl.findRolesByType(RoleType.User);
|
|
|
|
Assert.assertEquals(1, returnedRoles.size());
|
|
Mockito.verify(accountManagerMock, Mockito.times(0)).isRootAdmin(Mockito.anyLong());
|
|
Mockito.verify(roleDaoMock, Mockito.times(1)).findAllByRoleType(Mockito.any(RoleType.class));
|
|
}
|
|
|
|
@Test
|
|
public void listRolesTest() {
|
|
List<Role> roles = new ArrayList<>();
|
|
roles.add(Mockito.mock(Role.class));
|
|
|
|
Mockito.doReturn(roles).when(roleDaoMock).listAll();
|
|
Mockito.doNothing().when(roleManagerImpl).removeRootAdminRolesIfNeeded(roles);
|
|
|
|
List<Role> returnedRoles = roleManagerImpl.listRoles();
|
|
|
|
Assert.assertEquals(roles.size(), returnedRoles.size());
|
|
Mockito.verify(roleDaoMock).listAll();
|
|
Mockito.verify(roleManagerImpl).removeRootAdminRolesIfNeeded(roles);
|
|
}
|
|
}
|