|
16 | 16 | // under the License. |
17 | 17 | package com.cloud.user; |
18 | 18 |
|
| 19 | +import static org.mockito.ArgumentMatchers.nullable; |
| 20 | + |
19 | 21 | import java.net.InetAddress; |
20 | 22 | import java.net.UnknownHostException; |
21 | 23 | import java.util.ArrayList; |
22 | 24 | import java.util.Arrays; |
| 25 | +import java.util.HashMap; |
23 | 26 | import java.util.List; |
24 | 27 | import java.util.Map; |
25 | | -import java.util.HashMap; |
26 | 28 |
|
27 | 29 | import org.apache.cloudstack.acl.ControlledEntity; |
| 30 | +import org.apache.cloudstack.acl.Role; |
| 31 | +import org.apache.cloudstack.acl.RoleService; |
| 32 | +import org.apache.cloudstack.acl.RoleType; |
28 | 33 | import org.apache.cloudstack.acl.SecurityChecker.AccessType; |
29 | 34 | import org.apache.cloudstack.api.command.admin.user.GetUserKeysCmd; |
30 | 35 | import org.apache.cloudstack.api.command.admin.user.UpdateUserCmd; |
|
42 | 47 | import org.mockito.Mock; |
43 | 48 | import org.mockito.Mockito; |
44 | 49 | import org.mockito.junit.MockitoJUnitRunner; |
45 | | -import static org.mockito.ArgumentMatchers.nullable; |
46 | 50 |
|
47 | 51 | import com.cloud.acl.DomainChecker; |
48 | 52 | import com.cloud.api.auth.SetupUserTwoFactorAuthenticationCmd; |
@@ -102,6 +106,8 @@ public class AccountManagerImplTest extends AccountManagetImplTestBase { |
102 | 106 |
|
103 | 107 | @Mock |
104 | 108 | ConfigKey<Boolean> enableUserTwoFactorAuthenticationMock; |
| 109 | + @Mock |
| 110 | + RoleService roleService; |
105 | 111 |
|
106 | 112 | @Before |
107 | 113 | public void setUp() throws Exception { |
@@ -1086,4 +1092,112 @@ public void deleteAndCleanupUserTestRemovesUserFromProjects() { |
1086 | 1092 |
|
1087 | 1093 | Mockito.verify(_projectAccountDao).removeUserFromProjects(userId); |
1088 | 1094 | } |
| 1095 | + |
| 1096 | + @Test(expected = PermissionDeniedException.class) |
| 1097 | + public void testValidateRoleChangeUnknownCaller() { |
| 1098 | + Account account = Mockito.mock(Account.class); |
| 1099 | + Mockito.when(account.getRoleId()).thenReturn(1L); |
| 1100 | + Role role = Mockito.mock(Role.class); |
| 1101 | + Mockito.when(role.getRoleType()).thenReturn(RoleType.Unknown); |
| 1102 | + Account caller = Mockito.mock(Account.class); |
| 1103 | + Mockito.when(caller.getRoleId()).thenReturn(2L); |
| 1104 | + Mockito.when(roleService.findRole(2L)).thenReturn(role); |
| 1105 | + accountManagerImpl.validateRoleChange(account, Mockito.mock(Role.class), caller); |
| 1106 | + } |
| 1107 | + |
| 1108 | + @Test(expected = PermissionDeniedException.class) |
| 1109 | + public void testValidateRoleChangeUnknownNewRole() { |
| 1110 | + Account account = Mockito.mock(Account.class); |
| 1111 | + Mockito.when(account.getRoleId()).thenReturn(1L); |
| 1112 | + Role newRole = Mockito.mock(Role.class); |
| 1113 | + Mockito.when(newRole.getRoleType()).thenReturn(RoleType.Unknown); |
| 1114 | + Role callerRole = Mockito.mock(Role.class); |
| 1115 | + Mockito.when(callerRole.getRoleType()).thenReturn(RoleType.DomainAdmin); |
| 1116 | + Account caller = Mockito.mock(Account.class); |
| 1117 | + Mockito.when(caller.getRoleId()).thenReturn(2L); |
| 1118 | + Mockito.when(roleService.findRole(2L)).thenReturn(callerRole); |
| 1119 | + accountManagerImpl.validateRoleChange(account, newRole, caller); |
| 1120 | + } |
| 1121 | + |
| 1122 | + @Test |
| 1123 | + public void testValidateRoleNewRoleSameCaller() { |
| 1124 | + Account account = Mockito.mock(Account.class); |
| 1125 | + Mockito.when(account.getRoleId()).thenReturn(1L); |
| 1126 | + Role currentRole = Mockito.mock(Role.class); |
| 1127 | + Mockito.when(currentRole.getRoleType()).thenReturn(RoleType.User); |
| 1128 | + Mockito.when(roleService.findRole(1L)).thenReturn(currentRole); |
| 1129 | + Role newRole = Mockito.mock(Role.class); |
| 1130 | + Mockito.when(newRole.getRoleType()).thenReturn(RoleType.DomainAdmin); |
| 1131 | + Role callerRole = Mockito.mock(Role.class); |
| 1132 | + Mockito.when(callerRole.getRoleType()).thenReturn(RoleType.DomainAdmin); |
| 1133 | + Account caller = Mockito.mock(Account.class); |
| 1134 | + Mockito.when(caller.getRoleId()).thenReturn(2L); |
| 1135 | + Mockito.when(roleService.findRole(2L)).thenReturn(callerRole); |
| 1136 | + accountManagerImpl.validateRoleChange(account, newRole, caller); |
| 1137 | + } |
| 1138 | + |
| 1139 | + @Test |
| 1140 | + public void testValidateRoleCurrentRoleSameCaller() { |
| 1141 | + Account account = Mockito.mock(Account.class); |
| 1142 | + Mockito.when(account.getRoleId()).thenReturn(1L); |
| 1143 | + Role accountRole = Mockito.mock(Role.class); |
| 1144 | + Mockito.when(accountRole.getRoleType()).thenReturn(RoleType.DomainAdmin); |
| 1145 | + Role newRole = Mockito.mock(Role.class); |
| 1146 | + Mockito.when(newRole.getRoleType()).thenReturn(RoleType.User); |
| 1147 | + Role callerRole = Mockito.mock(Role.class); |
| 1148 | + Mockito.when(callerRole.getRoleType()).thenReturn(RoleType.DomainAdmin); |
| 1149 | + Account caller = Mockito.mock(Account.class); |
| 1150 | + Mockito.when(caller.getRoleId()).thenReturn(2L); |
| 1151 | + Mockito.when(roleService.findRole(1L)).thenReturn(accountRole); |
| 1152 | + Mockito.when(roleService.findRole(2L)).thenReturn(callerRole); |
| 1153 | + accountManagerImpl.validateRoleChange(account, newRole, caller); |
| 1154 | + } |
| 1155 | + |
| 1156 | + @Test(expected = PermissionDeniedException.class) |
| 1157 | + public void testValidateRoleNewRoleHigherCaller() { |
| 1158 | + Account account = Mockito.mock(Account.class); |
| 1159 | + Mockito.when(account.getRoleId()).thenReturn(1L); |
| 1160 | + Role newRole = Mockito.mock(Role.class); |
| 1161 | + Mockito.when(newRole.getRoleType()).thenReturn(RoleType.Admin); |
| 1162 | + Role callerRole = Mockito.mock(Role.class); |
| 1163 | + Mockito.when(callerRole.getRoleType()).thenReturn(RoleType.DomainAdmin); |
| 1164 | + Account caller = Mockito.mock(Account.class); |
| 1165 | + Mockito.when(caller.getRoleId()).thenReturn(2L); |
| 1166 | + Mockito.when(roleService.findRole(2L)).thenReturn(callerRole); |
| 1167 | + accountManagerImpl.validateRoleChange(account, newRole, caller); |
| 1168 | + } |
| 1169 | + |
| 1170 | + @Test |
| 1171 | + public void testValidateRoleNewRoleLowerCaller() { |
| 1172 | + Account account = Mockito.mock(Account.class); |
| 1173 | + Mockito.when(account.getRoleId()).thenReturn(1L); |
| 1174 | + Role newRole = Mockito.mock(Role.class); |
| 1175 | + Mockito.when(newRole.getRoleType()).thenReturn(RoleType.User); |
| 1176 | + Role accountRole = Mockito.mock(Role.class); |
| 1177 | + Mockito.when(accountRole.getRoleType()).thenReturn(RoleType.User); |
| 1178 | + Role callerRole = Mockito.mock(Role.class); |
| 1179 | + Mockito.when(callerRole.getRoleType()).thenReturn(RoleType.DomainAdmin); |
| 1180 | + Account caller = Mockito.mock(Account.class); |
| 1181 | + Mockito.when(caller.getRoleId()).thenReturn(2L); |
| 1182 | + Mockito.when(roleService.findRole(1L)).thenReturn(accountRole); |
| 1183 | + Mockito.when(roleService.findRole(2L)).thenReturn(callerRole); |
| 1184 | + accountManagerImpl.validateRoleChange(account, newRole, caller); |
| 1185 | + } |
| 1186 | + |
| 1187 | + @Test(expected = PermissionDeniedException.class) |
| 1188 | + public void testValidateRoleAdminCannotEscalateAdminFromNonRootDomain() { |
| 1189 | + Account account = Mockito.mock(Account.class); |
| 1190 | + Mockito.when(account.getRoleId()).thenReturn(1L); |
| 1191 | + Mockito.when(account.getDomainId()).thenReturn(2L); |
| 1192 | + Role newRole = Mockito.mock(Role.class); |
| 1193 | + Mockito.when(newRole.getRoleType()).thenReturn(RoleType.Admin); |
| 1194 | + Role accountRole = Mockito.mock(Role.class); |
| 1195 | + Role callerRole = Mockito.mock(Role.class); |
| 1196 | + Mockito.when(callerRole.getRoleType()).thenReturn(RoleType.Admin); |
| 1197 | + Account caller = Mockito.mock(Account.class); |
| 1198 | + Mockito.when(caller.getRoleId()).thenReturn(2L); |
| 1199 | + Mockito.when(roleService.findRole(1L)).thenReturn(accountRole); |
| 1200 | + Mockito.when(roleService.findRole(2L)).thenReturn(callerRole); |
| 1201 | + accountManagerImpl.validateRoleChange(account, newRole, caller); |
| 1202 | + } |
1089 | 1203 | } |
0 commit comments