|
27 | 27 | import java.util.Map; |
28 | 28 |
|
29 | 29 | import com.cloud.event.ActionEventUtils; |
| 30 | + |
| 31 | +import org.apache.cloudstack.acl.ControlledEntity; |
| 32 | +import org.apache.cloudstack.acl.Role; |
| 33 | +import org.apache.cloudstack.acl.RoleService; |
| 34 | +import org.apache.cloudstack.acl.RoleType; |
30 | 35 | import org.apache.cloudstack.acl.SecurityChecker.AccessType; |
31 | 36 | import org.apache.cloudstack.api.command.admin.account.UpdateAccountCmd; |
32 | 37 | import org.apache.cloudstack.api.command.admin.user.DeleteUserCmd; |
33 | | - |
34 | | -import org.apache.cloudstack.acl.ControlledEntity; |
35 | 38 | import org.apache.cloudstack.api.command.admin.user.GetUserKeysCmd; |
36 | 39 | import org.apache.cloudstack.api.command.admin.user.UpdateUserCmd; |
37 | 40 | import org.apache.cloudstack.api.response.UserTwoFactorAuthenticationSetupResponse; |
|
50 | 53 | import org.mockito.MockedStatic; |
51 | 54 | import org.mockito.Mockito; |
52 | 55 | import org.mockito.junit.MockitoJUnitRunner; |
| 56 | + |
53 | 57 | import org.springframework.beans.factory.NoSuchBeanDefinitionException; |
54 | 58 |
|
55 | 59 | import com.cloud.acl.DomainChecker; |
@@ -120,6 +124,8 @@ public class AccountManagerImplTest extends AccountManagetImplTestBase { |
120 | 124 |
|
121 | 125 | @Mock |
122 | 126 | ConfigKey<Boolean> enableUserTwoFactorAuthenticationMock; |
| 127 | + @Mock |
| 128 | + RoleService roleService; |
123 | 129 |
|
124 | 130 | @Before |
125 | 131 | public void setUp() throws Exception { |
@@ -1225,4 +1231,112 @@ public void testDeleteWebhooksForAccountNoBean() { |
1225 | 1231 | accountManagerImpl.deleteWebhooksForAccount(1L); |
1226 | 1232 | } |
1227 | 1233 | } |
| 1234 | + |
| 1235 | + @Test(expected = PermissionDeniedException.class) |
| 1236 | + public void testValidateRoleChangeUnknownCaller() { |
| 1237 | + Account account = Mockito.mock(Account.class); |
| 1238 | + Mockito.when(account.getRoleId()).thenReturn(1L); |
| 1239 | + Role role = Mockito.mock(Role.class); |
| 1240 | + Mockito.when(role.getRoleType()).thenReturn(RoleType.Unknown); |
| 1241 | + Account caller = Mockito.mock(Account.class); |
| 1242 | + Mockito.when(caller.getRoleId()).thenReturn(2L); |
| 1243 | + Mockito.when(roleService.findRole(2L)).thenReturn(role); |
| 1244 | + accountManagerImpl.validateRoleChange(account, Mockito.mock(Role.class), caller); |
| 1245 | + } |
| 1246 | + |
| 1247 | + @Test(expected = PermissionDeniedException.class) |
| 1248 | + public void testValidateRoleChangeUnknownNewRole() { |
| 1249 | + Account account = Mockito.mock(Account.class); |
| 1250 | + Mockito.when(account.getRoleId()).thenReturn(1L); |
| 1251 | + Role newRole = Mockito.mock(Role.class); |
| 1252 | + Mockito.when(newRole.getRoleType()).thenReturn(RoleType.Unknown); |
| 1253 | + Role callerRole = Mockito.mock(Role.class); |
| 1254 | + Mockito.when(callerRole.getRoleType()).thenReturn(RoleType.DomainAdmin); |
| 1255 | + Account caller = Mockito.mock(Account.class); |
| 1256 | + Mockito.when(caller.getRoleId()).thenReturn(2L); |
| 1257 | + Mockito.when(roleService.findRole(2L)).thenReturn(callerRole); |
| 1258 | + accountManagerImpl.validateRoleChange(account, newRole, caller); |
| 1259 | + } |
| 1260 | + |
| 1261 | + @Test |
| 1262 | + public void testValidateRoleNewRoleSameCaller() { |
| 1263 | + Account account = Mockito.mock(Account.class); |
| 1264 | + Mockito.when(account.getRoleId()).thenReturn(1L); |
| 1265 | + Role currentRole = Mockito.mock(Role.class); |
| 1266 | + Mockito.when(currentRole.getRoleType()).thenReturn(RoleType.User); |
| 1267 | + Mockito.when(roleService.findRole(1L)).thenReturn(currentRole); |
| 1268 | + Role newRole = Mockito.mock(Role.class); |
| 1269 | + Mockito.when(newRole.getRoleType()).thenReturn(RoleType.DomainAdmin); |
| 1270 | + Role callerRole = Mockito.mock(Role.class); |
| 1271 | + Mockito.when(callerRole.getRoleType()).thenReturn(RoleType.DomainAdmin); |
| 1272 | + Account caller = Mockito.mock(Account.class); |
| 1273 | + Mockito.when(caller.getRoleId()).thenReturn(2L); |
| 1274 | + Mockito.when(roleService.findRole(2L)).thenReturn(callerRole); |
| 1275 | + accountManagerImpl.validateRoleChange(account, newRole, caller); |
| 1276 | + } |
| 1277 | + |
| 1278 | + @Test |
| 1279 | + public void testValidateRoleCurrentRoleSameCaller() { |
| 1280 | + Account account = Mockito.mock(Account.class); |
| 1281 | + Mockito.when(account.getRoleId()).thenReturn(1L); |
| 1282 | + Role accountRole = Mockito.mock(Role.class); |
| 1283 | + Mockito.when(accountRole.getRoleType()).thenReturn(RoleType.DomainAdmin); |
| 1284 | + Role newRole = Mockito.mock(Role.class); |
| 1285 | + Mockito.when(newRole.getRoleType()).thenReturn(RoleType.User); |
| 1286 | + Role callerRole = Mockito.mock(Role.class); |
| 1287 | + Mockito.when(callerRole.getRoleType()).thenReturn(RoleType.DomainAdmin); |
| 1288 | + Account caller = Mockito.mock(Account.class); |
| 1289 | + Mockito.when(caller.getRoleId()).thenReturn(2L); |
| 1290 | + Mockito.when(roleService.findRole(1L)).thenReturn(accountRole); |
| 1291 | + Mockito.when(roleService.findRole(2L)).thenReturn(callerRole); |
| 1292 | + accountManagerImpl.validateRoleChange(account, newRole, caller); |
| 1293 | + } |
| 1294 | + |
| 1295 | + @Test(expected = PermissionDeniedException.class) |
| 1296 | + public void testValidateRoleNewRoleHigherCaller() { |
| 1297 | + Account account = Mockito.mock(Account.class); |
| 1298 | + Mockito.when(account.getRoleId()).thenReturn(1L); |
| 1299 | + Role newRole = Mockito.mock(Role.class); |
| 1300 | + Mockito.when(newRole.getRoleType()).thenReturn(RoleType.Admin); |
| 1301 | + Role callerRole = Mockito.mock(Role.class); |
| 1302 | + Mockito.when(callerRole.getRoleType()).thenReturn(RoleType.DomainAdmin); |
| 1303 | + Account caller = Mockito.mock(Account.class); |
| 1304 | + Mockito.when(caller.getRoleId()).thenReturn(2L); |
| 1305 | + Mockito.when(roleService.findRole(2L)).thenReturn(callerRole); |
| 1306 | + accountManagerImpl.validateRoleChange(account, newRole, caller); |
| 1307 | + } |
| 1308 | + |
| 1309 | + @Test |
| 1310 | + public void testValidateRoleNewRoleLowerCaller() { |
| 1311 | + Account account = Mockito.mock(Account.class); |
| 1312 | + Mockito.when(account.getRoleId()).thenReturn(1L); |
| 1313 | + Role newRole = Mockito.mock(Role.class); |
| 1314 | + Mockito.when(newRole.getRoleType()).thenReturn(RoleType.User); |
| 1315 | + Role accountRole = Mockito.mock(Role.class); |
| 1316 | + Mockito.when(accountRole.getRoleType()).thenReturn(RoleType.User); |
| 1317 | + Role callerRole = Mockito.mock(Role.class); |
| 1318 | + Mockito.when(callerRole.getRoleType()).thenReturn(RoleType.DomainAdmin); |
| 1319 | + Account caller = Mockito.mock(Account.class); |
| 1320 | + Mockito.when(caller.getRoleId()).thenReturn(2L); |
| 1321 | + Mockito.when(roleService.findRole(1L)).thenReturn(accountRole); |
| 1322 | + Mockito.when(roleService.findRole(2L)).thenReturn(callerRole); |
| 1323 | + accountManagerImpl.validateRoleChange(account, newRole, caller); |
| 1324 | + } |
| 1325 | + |
| 1326 | + @Test(expected = PermissionDeniedException.class) |
| 1327 | + public void testValidateRoleAdminCannotEscalateAdminFromNonRootDomain() { |
| 1328 | + Account account = Mockito.mock(Account.class); |
| 1329 | + Mockito.when(account.getRoleId()).thenReturn(1L); |
| 1330 | + Mockito.when(account.getDomainId()).thenReturn(2L); |
| 1331 | + Role newRole = Mockito.mock(Role.class); |
| 1332 | + Mockito.when(newRole.getRoleType()).thenReturn(RoleType.Admin); |
| 1333 | + Role accountRole = Mockito.mock(Role.class); |
| 1334 | + Role callerRole = Mockito.mock(Role.class); |
| 1335 | + Mockito.when(callerRole.getRoleType()).thenReturn(RoleType.Admin); |
| 1336 | + Account caller = Mockito.mock(Account.class); |
| 1337 | + Mockito.when(caller.getRoleId()).thenReturn(2L); |
| 1338 | + Mockito.when(roleService.findRole(1L)).thenReturn(accountRole); |
| 1339 | + Mockito.when(roleService.findRole(2L)).thenReturn(callerRole); |
| 1340 | + accountManagerImpl.validateRoleChange(account, newRole, caller); |
| 1341 | + } |
1228 | 1342 | } |
0 commit comments