PageRenderTime 54ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/core-model-test/tests/src/test/java/org/jboss/as/core/model/test/access/RoleMappingTestCase.java

https://github.com/ehsavoie/wildfly-core
Java | 599 lines | 414 code | 101 blank | 84 comment | 7 complexity | 02888e777ce1f3775fadf63f5163857a MD5 | raw file
  1. /*
  2. * JBoss, Home of Professional Open Source.
  3. * Copyright 2013, Red Hat, Inc., and individual contributors
  4. * as indicated by the @author tags. See the copyright.txt file in the
  5. * distribution for a full listing of individual contributors.
  6. *
  7. * This is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU Lesser General Public License as
  9. * published by the Free Software Foundation; either version 2.1 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This software is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this software; if not, write to the Free
  19. * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  21. */
  22. package org.jboss.as.core.model.test.access;
  23. import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.ACCESS;
  24. import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.ADD;
  25. import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.AUTHORIZATION;
  26. import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.CORE_SERVICE;
  27. import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.FAILED;
  28. import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.INCLUDE_ALL;
  29. import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.MANAGEMENT;
  30. import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.NAME;
  31. import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.OP;
  32. import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.OP_ADDR;
  33. import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.OUTCOME;
  34. import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.REALM;
  35. import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.REMOVE;
  36. import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.RESULT;
  37. import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.ROLE_MAPPING;
  38. import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.SUCCESS;
  39. import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.TYPE;
  40. import static org.jboss.as.domain.management.ModelDescriptionConstants.IS_CALLER_IN_ROLE;
  41. import static org.junit.Assert.assertEquals;
  42. import static org.junit.Assert.assertTrue;
  43. import java.security.Principal;
  44. import java.security.PrivilegedAction;
  45. import java.util.Arrays;
  46. import java.util.Collections;
  47. import java.util.HashMap;
  48. import java.util.Map;
  49. import java.util.function.Function;
  50. import org.jboss.as.controller.AccessAuditContext;
  51. import org.jboss.as.controller.access.rbac.StandardRole;
  52. import org.jboss.as.controller.descriptions.ModelDescriptionConstants;
  53. import org.jboss.as.core.model.test.AbstractCoreModelTest;
  54. import org.jboss.as.core.model.test.KernelServices;
  55. import org.jboss.as.core.model.test.TestModelType;
  56. import org.jboss.as.core.security.AccountPrincipal;
  57. import org.jboss.as.core.security.GroupPrincipal;
  58. import org.jboss.as.core.security.RealmPrincipal;
  59. import org.jboss.as.core.security.RealmUser;
  60. import org.jboss.dmr.ModelNode;
  61. import org.junit.Before;
  62. import org.junit.Test;
  63. import org.wildfly.security.auth.permission.LoginPermission;
  64. import org.wildfly.security.auth.principal.NamePrincipal;
  65. import org.wildfly.security.auth.realm.SimpleMapBackedSecurityRealm;
  66. import org.wildfly.security.auth.realm.SimpleRealmEntry;
  67. import org.wildfly.security.auth.server.RealmIdentity;
  68. import org.wildfly.security.auth.server.RealmUnavailableException;
  69. import org.wildfly.security.auth.server.SecurityDomain;
  70. import org.wildfly.security.auth.server.SecurityIdentity;
  71. import org.wildfly.security.auth.server.ServerAuthenticationContext;
  72. import org.wildfly.security.authz.MapAttributes;
  73. import org.wildfly.security.authz.RoleDecoder;
  74. /**
  75. * Test case to test the role mapping behaviour (model and runtime mapping).
  76. *
  77. * @author <a href="mailto:darran.lofthouse@jboss.com">Darran Lofthouse</a>
  78. */
  79. public class RoleMappingTestCase extends AbstractCoreModelTest {
  80. private static final String TEST_REALM = "TestRealm";
  81. private static final String OTHER_REALM = "OtherRealm";
  82. private static final String OTHER_USER = "OtherUser";
  83. private KernelServices kernelServices;
  84. private int uniqueCount = 0;
  85. @Before
  86. public void setUp() throws Exception {
  87. kernelServices = createKernelServicesBuilder(TestModelType.STANDALONE)
  88. .setXmlResource("constraints.xml")
  89. .validateDescription()
  90. .build();
  91. }
  92. /**
  93. * Test that a user is assigned a role based on their username (not realm specific).
  94. *
  95. * Also verify that assignment of a group with the same name does not result in role assignment.
  96. */
  97. @Test
  98. public void testIncludeByUsername() {
  99. final String roleName = "Deployer";
  100. final String userName = "UserOne";
  101. addRole(roleName, false);
  102. addPrincipal(roleName, MappingType.INCLUDE, PrincipalType.USER, userName, null);
  103. assertIsCallerInRole(roleName, null, false);
  104. assertIsCallerInRole(roleName, true, userName, TEST_REALM, null);
  105. assertIsCallerInRole(roleName, false, OTHER_USER, TEST_REALM, null, userName);
  106. removeRole(roleName);
  107. }
  108. /**
  109. * Same as testIncludeByUsername but now verify that the users realm is taken into account.
  110. */
  111. @Test
  112. public void testIncludeByUsernameAndRealm() {
  113. final String roleName = "Deployer";
  114. final String userName = "UserTwo";
  115. addRole(roleName, false);
  116. addPrincipal(roleName, MappingType.INCLUDE, PrincipalType.USER, userName, TEST_REALM);
  117. assertIsCallerInRole(roleName, null, false);
  118. assertIsCallerInRole(roleName, true, userName, TEST_REALM, null);
  119. assertIsCallerInRole(roleName, false, userName, OTHER_REALM, null);
  120. assertIsCallerInRole(roleName, false, OTHER_USER, TEST_REALM, null, userName);
  121. removeRole(roleName);
  122. }
  123. /**
  124. * Test that a user is assigned a role based on their group membership (not realm specific).
  125. *
  126. * Also verify that a user account with the same name does not result in role assignment.
  127. */
  128. @Test
  129. public void testIncludeByGroup() {
  130. final String roleName = "Deployer";
  131. final String userName = "UserThree";
  132. final String groupName = "GroupThree";
  133. addRole(roleName, false);
  134. addPrincipal(roleName, MappingType.INCLUDE, PrincipalType.GROUP, groupName, null);
  135. assertIsCallerInRole(roleName, null, false);
  136. assertIsCallerInRole(roleName, true, userName, TEST_REALM, null, groupName);
  137. assertIsCallerInRole(roleName, true, userName, OTHER_REALM, null, groupName);
  138. assertIsCallerInRole(roleName, false, groupName, TEST_REALM, null, userName);
  139. removeRole(roleName);
  140. }
  141. /**
  142. * Same as testIncludeByGroup but now include the realm name in the match.
  143. */
  144. @Test
  145. public void testIncludeByGroupAndRealm() {
  146. final String roleName = "Deployer";
  147. final String userName = "UserFour";
  148. final String groupName = "GroupFour";
  149. addRole(roleName, false);
  150. addPrincipal(roleName, MappingType.INCLUDE, PrincipalType.GROUP, groupName, TEST_REALM);
  151. assertIsCallerInRole(roleName, null, false);
  152. assertIsCallerInRole(roleName, true, userName, TEST_REALM, null, groupName);
  153. assertIsCallerInRole(roleName, false, userName, OTHER_REALM, null, groupName);
  154. assertIsCallerInRole(roleName, false, groupName, TEST_REALM, null, userName);
  155. removeRole(roleName);
  156. }
  157. /**
  158. * Test that a user matched to a role by group is not assigned the role if their username is in the exclude list.
  159. */
  160. @Test
  161. public void testExcludeByUsername() {
  162. final String roleName = "Deployer";
  163. final String userName = "UserFive";
  164. final String groupName = "GroupFive";
  165. addRole(roleName, false);
  166. addPrincipal(roleName, MappingType.INCLUDE, PrincipalType.GROUP, groupName, null);
  167. addPrincipal(roleName, MappingType.EXCLUDE, PrincipalType.USER, userName, null);
  168. assertIsCallerInRole(roleName, null, false);
  169. assertIsCallerInRole(roleName, true, OTHER_USER, TEST_REALM, null, groupName);
  170. assertIsCallerInRole(roleName, false, userName, TEST_REALM, null, groupName);
  171. removeRole(roleName);
  172. }
  173. /**
  174. * Same as testExcludeByUsername except the exclusion is realm specific.
  175. */
  176. @Test
  177. public void testExcludeByUsernameAndRealm() {
  178. final String roleName = "Deployer";
  179. final String userName = "UserFive";
  180. final String groupName = "GroupFive";
  181. addRole(roleName, false);
  182. addPrincipal(roleName, MappingType.INCLUDE, PrincipalType.GROUP, groupName, null);
  183. addPrincipal(roleName, MappingType.EXCLUDE, PrincipalType.USER, userName, TEST_REALM);
  184. assertIsCallerInRole(roleName, null, false);
  185. assertIsCallerInRole(roleName, true, OTHER_USER, TEST_REALM, null, groupName);
  186. assertIsCallerInRole(roleName, false, userName, TEST_REALM, null, groupName);
  187. assertIsCallerInRole(roleName, true, userName, OTHER_REALM, null, groupName);
  188. removeRole(roleName);
  189. }
  190. /**
  191. * Test that a user assigned a role due to group membership is excluded based on the membership of another group.
  192. */
  193. @Test
  194. public void testExcludeByGroup() {
  195. final String roleName = "Deployer";
  196. final String userName = "UserSix";
  197. final String inGroupName = "GroupSix_In";
  198. final String outGroupName = "GroupSix_Out";
  199. addRole(roleName, false);
  200. addPrincipal(roleName, MappingType.INCLUDE, PrincipalType.GROUP, inGroupName, null);
  201. addPrincipal(roleName, MappingType.EXCLUDE, PrincipalType.GROUP, outGroupName, null);
  202. assertIsCallerInRole(roleName, null, false);
  203. assertIsCallerInRole(roleName, true, userName, TEST_REALM, null, inGroupName);
  204. assertIsCallerInRole(roleName, false, userName, TEST_REALM, null, inGroupName, outGroupName);
  205. removeRole(roleName);
  206. }
  207. /**
  208. * Same as testExcludeByGroup but the exclusion takes the realm into account.
  209. */
  210. @Test
  211. public void testExcludeByGroupAndRealm() {
  212. final String roleName = "Deployer";
  213. final String userName = "UserSeven";
  214. final String inGroupName = "GroupSeven_In";
  215. final String outGroupName = "GroupSeven_Out";
  216. addRole(roleName, false);
  217. addPrincipal(roleName, MappingType.INCLUDE, PrincipalType.GROUP, inGroupName, null);
  218. addPrincipal(roleName, MappingType.EXCLUDE, PrincipalType.GROUP, outGroupName, TEST_REALM);
  219. assertIsCallerInRole(roleName, null, false);
  220. assertIsCallerInRole(roleName, true, userName, TEST_REALM, null, inGroupName);
  221. assertIsCallerInRole(roleName, false, userName, TEST_REALM, null, inGroupName, outGroupName);
  222. assertIsCallerInRole(roleName, true, userName, OTHER_REALM, null, inGroupName, outGroupName);
  223. removeRole(roleName);
  224. }
  225. /**
  226. * Test that user assigned the SUPERUSER role can actually request a different role.
  227. *
  228. * On requesting the different role the user should not be assigned the SUPERUSER role anymore.
  229. */
  230. @Test
  231. public void testSuperUserAs() {
  232. final String roleName = "SuperUser";
  233. final String otherRole = "Deployer";
  234. final String userName = "UserThirteen";
  235. // TODO Elytron The SuperUser mapping was added to constraints.xml to allow the remainder of the tests to run.
  236. //addRole(roleName, false);
  237. ModelNode addedAddress = addPrincipal(roleName, MappingType.INCLUDE, PrincipalType.USER, userName, null);
  238. assertIsCallerInRole(roleName, true, userName, TEST_REALM, null);
  239. assertIsCallerInRole(otherRole, true, userName, TEST_REALM, otherRole);
  240. assertIsCallerInRole(roleName, false, userName, TEST_REALM, otherRole);
  241. removePrincipal(addedAddress);
  242. // TODO Elytron The SuperUser mapping was added to constraints.xml to allow the remainder of the tests to run.
  243. //removeRole(roleName);
  244. }
  245. /**
  246. * Test that user assigned the Deployer role can NOT request a different role.
  247. */
  248. @Test
  249. public void testDeployerAs() {
  250. final String roleName = "Deployer";
  251. final String otherRole = "MONITOR";
  252. final String userName = "UserFourteen";
  253. addRole(roleName, false);
  254. ModelNode addedAddress = addPrincipal(roleName, MappingType.INCLUDE, PrincipalType.USER, userName, null);
  255. assertIsCallerInRole(roleName, true, userName, TEST_REALM, null);
  256. assertIsCallerInRole(otherRole, false, userName, TEST_REALM, otherRole);
  257. assertIsCallerInRole(roleName, true, userName, TEST_REALM, otherRole);
  258. removePrincipal(addedAddress);
  259. removeRole(roleName);
  260. }
  261. /**
  262. * Test that an authenticated user is assigned a role where include-all = true.
  263. */
  264. @Test
  265. public void testIncludeAll() {
  266. final String roleName = "Deployer";
  267. final String userName = "UserEight";
  268. addRole(roleName, true);
  269. // TODO Elytron Hack - Default user 'anonymous' is picked up as being in the role.
  270. assertIsCallerInRole(roleName, null, true);
  271. assertIsCallerInRole(roleName, true, userName, TEST_REALM, null);
  272. removeRole(roleName);
  273. }
  274. /**
  275. * Test that a user matched to a role by include-all is not assigned the role if their username is in the exclude list.
  276. */
  277. @Test
  278. public void testIncludeAll_ExcludeByUsername() {
  279. final String roleName = "Deployer";
  280. final String userName = "UserNine";
  281. final String groupName = "GroupNine";
  282. addRole(roleName, true);
  283. addPrincipal(roleName, MappingType.EXCLUDE, PrincipalType.USER, userName, null);
  284. // TODO Elytron Hack to also exclude the default user 'anonymous'.
  285. addPrincipal(roleName, MappingType.EXCLUDE, PrincipalType.USER, "anonymous", null);
  286. assertIsCallerInRole(roleName, null, false);
  287. assertIsCallerInRole(roleName, true, OTHER_USER, TEST_REALM, null, groupName);
  288. assertIsCallerInRole(roleName, false, userName, TEST_REALM, null, groupName);
  289. removeRole(roleName);
  290. }
  291. /**
  292. * Test that a user matched to a role by include-all is not assigned the role if their group is in the exclude list.
  293. */
  294. @Test
  295. public void testIncludeAll_ExcludeByGroup() {
  296. final String roleName = "Deployer";
  297. final String userName = "UserTen";
  298. final String groupName = "GroupTen";
  299. addRole(roleName, true);
  300. addPrincipal(roleName, MappingType.EXCLUDE, PrincipalType.GROUP, groupName, null);
  301. // TODO Elytron Hack to also exclude the default user 'anonymous'.
  302. addPrincipal(roleName, MappingType.EXCLUDE, PrincipalType.USER, "anonymous", null);
  303. assertIsCallerInRole(roleName, null, false);
  304. assertIsCallerInRole(roleName, true, userName, TEST_REALM, null);
  305. assertIsCallerInRole(roleName, false, userName, TEST_REALM, null, groupName);
  306. removeRole(roleName);
  307. }
  308. /*
  309. * Duplicate Handling
  310. *
  311. * Tests to verify that the add operations successfully detect duplicate include/exclude definitions.
  312. */
  313. @Test
  314. public void testDuplicateUserComplete() {
  315. final String roleName = "Deployer";
  316. final String userName = "UserEleven";
  317. addRole(roleName, false);
  318. addPrincipal(roleName, MappingType.INCLUDE, PrincipalType.USER, userName, TEST_REALM);
  319. addPrincipal(roleName, MappingType.INCLUDE, PrincipalType.USER, userName, TEST_REALM, true);
  320. removeRole(roleName);
  321. }
  322. @Test
  323. public void testDuplicateUserRealmLess() {
  324. final String roleName = "Deployer";
  325. final String userName = "UserTwelve";
  326. addRole(roleName, false);
  327. addPrincipal(roleName, MappingType.INCLUDE, PrincipalType.USER, userName, null);
  328. addPrincipal(roleName, MappingType.INCLUDE, PrincipalType.USER, userName, TEST_REALM);
  329. addPrincipal(roleName, MappingType.INCLUDE, PrincipalType.USER, userName, null, true);
  330. addPrincipal(roleName, MappingType.EXCLUDE, PrincipalType.USER, userName, TEST_REALM);
  331. addPrincipal(roleName, MappingType.EXCLUDE, PrincipalType.USER, userName, null);
  332. addPrincipal(roleName, MappingType.EXCLUDE, PrincipalType.USER, userName, null, true);
  333. removeRole(roleName);
  334. }
  335. @Test
  336. public void testDuplicateGroupComplete() {
  337. final String roleName = "Deployer";
  338. final String groupName = "UserThirteen";
  339. addRole(roleName, false);
  340. addPrincipal(roleName, MappingType.EXCLUDE, PrincipalType.GROUP, groupName, TEST_REALM);
  341. addPrincipal(roleName, MappingType.EXCLUDE, PrincipalType.GROUP, groupName, TEST_REALM, true);
  342. removeRole(roleName);
  343. }
  344. @Test
  345. public void testDuplicateGroupRealmLess() {
  346. final String roleName = "Deployer";
  347. final String groupName = "UserFourteen";
  348. addRole(roleName, false);
  349. addPrincipal(roleName, MappingType.EXCLUDE, PrincipalType.GROUP, groupName, null);
  350. addPrincipal(roleName, MappingType.EXCLUDE, PrincipalType.GROUP, groupName, TEST_REALM);
  351. addPrincipal(roleName, MappingType.EXCLUDE, PrincipalType.GROUP, groupName, null, true);
  352. addPrincipal(roleName, MappingType.INCLUDE, PrincipalType.GROUP, groupName, TEST_REALM);
  353. addPrincipal(roleName, MappingType.INCLUDE, PrincipalType.GROUP, groupName, null);
  354. addPrincipal(roleName, MappingType.INCLUDE, PrincipalType.GROUP, groupName, null, true);
  355. removeRole(roleName);
  356. }
  357. private void addRole(final String roleName, boolean includeAll) {
  358. ModelNode operation = new ModelNode();
  359. operation.get(OP_ADDR).add(CORE_SERVICE, MANAGEMENT).add(ACCESS, AUTHORIZATION).add(ROLE_MAPPING, roleName);
  360. operation.get(OP).set(ADD);
  361. if (includeAll) {
  362. operation.get(INCLUDE_ALL).set(true);
  363. }
  364. ModelNode response = kernelServices.executeOperation(operation);
  365. assertEquals(SUCCESS, response.get(OUTCOME).asString());
  366. }
  367. private ModelNode addPrincipal(final String roleName, final MappingType mappingType, final PrincipalType principalType, final String name, final String realm) {
  368. return addPrincipal(roleName, mappingType, principalType, name, realm, false);
  369. }
  370. private ModelNode addPrincipal(final String roleName, final MappingType mappingType, final PrincipalType principalType, final String name, final String realm, boolean expectFailure) {
  371. ModelNode operation = new ModelNode();
  372. operation.get(OP_ADDR).add(CORE_SERVICE, MANAGEMENT).add(ACCESS, AUTHORIZATION).add(ROLE_MAPPING, roleName).add(mappingType.toString(), uniqueCount++);
  373. operation.get(OP).set(ADD);
  374. operation.get(TYPE).set(principalType.toString());
  375. operation.get(NAME).set(name);
  376. if (realm != null) {
  377. operation.get(REALM).set(realm);
  378. }
  379. ModelNode response = kernelServices.executeOperation(operation);
  380. if (expectFailure) {
  381. assertEquals(FAILED, response.get(OUTCOME).asString());
  382. } else {
  383. assertEquals(SUCCESS, response.get(OUTCOME).asString());
  384. }
  385. return operation.get(OP_ADDR);
  386. }
  387. private void removePrincipal(final ModelNode address) {
  388. ModelNode operation = new ModelNode();
  389. operation.get(OP_ADDR).set(address);
  390. operation.get(OP).set(REMOVE);
  391. ModelNode response = kernelServices.executeOperation(operation);
  392. assertEquals(SUCCESS, response.get(OUTCOME).asString());
  393. }
  394. private void removeRole(final String roleName) {
  395. ModelNode operation = new ModelNode();
  396. operation.get(OP_ADDR).add(CORE_SERVICE, MANAGEMENT).add(ACCESS, AUTHORIZATION).add(ROLE_MAPPING, roleName);
  397. operation.get(OP).set(REMOVE);
  398. ModelNode response = kernelServices.executeOperation(operation);
  399. assertEquals(SUCCESS, response.get(OUTCOME).asString());
  400. }
  401. private void assertIsCallerInRole(final String roleName, final String runAsRole, final boolean expectedOutcome) {
  402. ModelNode operation = new ModelNode();
  403. operation.get(OP_ADDR).add(CORE_SERVICE, MANAGEMENT).add(ACCESS, AUTHORIZATION).add(ROLE_MAPPING, roleName);
  404. operation.get(OP).set(IS_CALLER_IN_ROLE);
  405. if (runAsRole != null) {
  406. ModelNode headers = operation.get(ModelDescriptionConstants.OPERATION_HEADERS);
  407. headers.get("roles").set(runAsRole);
  408. }
  409. ModelNode response = kernelServices.executeOperation(operation);
  410. assertEquals(SUCCESS, response.get(OUTCOME).asString());
  411. assertEquals(expectedOutcome, response.get(RESULT).asBoolean());
  412. }
  413. private void assertIsCallerInRole(final String roleName, final boolean expectedOutcome, final String userName,
  414. final String realm, final String runAsRole, final String... groups) {
  415. MapAttributes testAttributes = new MapAttributes();
  416. testAttributes.addAll("groups", Arrays.asList(groups));
  417. Map<String, SimpleRealmEntry> entries = new HashMap<>(StandardRole.values().length);
  418. entries.put(userName, new SimpleRealmEntry(Collections.emptyList(), testAttributes));
  419. SimpleMapBackedSecurityRealm securityRealm = new SimpleMapBackedSecurityRealm() {
  420. @Override
  421. public RealmIdentity getRealmIdentity(Principal principal) {
  422. return super.getRealmIdentity(new NamePrincipal(principal.getName()));
  423. }
  424. };
  425. securityRealm.setPasswordMap(entries);
  426. SecurityDomain testDomain = SecurityDomain.builder()
  427. .setDefaultRealmName("Default")
  428. .setPreRealmRewriter((Function<Principal, Principal>) p -> new RealmUser(realm, p.getName()))
  429. .addRealm("Default", securityRealm)
  430. .setRoleDecoder(RoleDecoder.simple("groups"))
  431. .build()
  432. .setPermissionMapper((p,r) -> new LoginPermission())
  433. .build();
  434. SecurityIdentity securityIdentity;
  435. try {
  436. ServerAuthenticationContext authenticationContext = testDomain.createNewAuthenticationContext();
  437. authenticationContext.setAuthenticationName(userName);
  438. assertTrue("Authorized", authenticationContext.authorize());
  439. securityIdentity = authenticationContext.getAuthorizedIdentity();
  440. } catch (RealmUnavailableException e) {
  441. // Should not be possible
  442. throw new IllegalStateException(e);
  443. }
  444. AccessAuditContext.doAs(securityIdentity, null, new PrivilegedAction<Void>() {
  445. @Override
  446. public Void run() {
  447. assertIsCallerInRole(roleName, runAsRole, expectedOutcome);
  448. return null;
  449. }
  450. });
  451. }
  452. private enum PrincipalType {
  453. GROUP, USER;
  454. @Override
  455. public String toString() {
  456. return super.toString().toLowerCase();
  457. }
  458. }
  459. private enum MappingType {
  460. EXCLUDE, INCLUDE;
  461. @Override
  462. public String toString() {
  463. return super.toString().toLowerCase();
  464. }
  465. }
  466. private static class User implements RealmPrincipal, AccountPrincipal {
  467. private final String realm;
  468. private final String name;
  469. private User(final String name, final String realm) {
  470. this.name = name;
  471. this.realm = realm;
  472. }
  473. @Override
  474. public String getName() {
  475. return name;
  476. }
  477. @Override
  478. public String getRealm() {
  479. return realm;
  480. }
  481. }
  482. private static class Group implements RealmPrincipal, GroupPrincipal {
  483. private final String name;
  484. private final String realm;
  485. private Group(final String name, final String realm) {
  486. this.name = name;
  487. this.realm = realm;
  488. }
  489. @Override
  490. public String getName() {
  491. return name;
  492. }
  493. @Override
  494. public String getRealm() {
  495. return realm;
  496. }
  497. }
  498. }