RoleMapper implements ConfigurationResource { public Map<String, MappedRole> mapRoles(Set<String> roles) { Map<String, MappedRole> mappedRoles = new HashMap<>(); if (roles != null) { String originalRole; Matcher matcher; for (String role : roles) { originalRole = role; if (roleMappers != null && !roleMappers.isEmpty()) { for (RoleMapperHolder mapper : roleMappers) { matcher = mapper.getPattern().matcher(role); if (matcher.matches()) { String matchedRole = matcher.replaceAll(mapper.getRole()); mappedRoles.put(originalRole, new MappedRole(originalRole, mapper.getKeepOriginalRole())); if (mapper.getContinueMapping()) { originalRole = matchedRole; } else { originalRole = null; mappedRoles.put(matchedRole, new MappedRole(matchedRole, mapper.getKeepResultingRole())); break; } } } if (originalRole != null) { mappedRoles.put(originalRole, new MappedRole(originalRole, true)); } } else { mappedRoles.put(originalRole, new MappedRole(originalRole, true)); } } } return mappedRoles; } RoleMapper(ResourceStorage storage, String securityRoot, Map<String, Object> properties); @Override void checkConfigResource(Buffer buffer); @Override void configUpdate(); Map<String, MappedRole> mapRoles(Set<String> roles); static final Logger log; static final String ROLEMAPPER; }
@Test public void checkNoMapping(TestContext context) { Set<String> roles = new HashSet<>(); roles.add("domain-user"); Map<String, RoleMapper.MappedRole> mappedRoles = roleMapper.mapRoles(roles); context.assertNotNull(mappedRoles); context.assertTrue(mappedRoles.size() == 1); context.assertTrue(mappedRoles.containsKey("domain-user")); }
@Test public void checkMappingWithoutKeep(TestContext context) { Set<String> roles = new HashSet<>(); roles.add("domain1-user"); Map<String, RoleMapper.MappedRole> mappedRoles = roleMapper.mapRoles(roles); context.assertNotNull(mappedRoles); context.assertTrue(mappedRoles.size() == 2); context.assertTrue(mappedRoles.get("domain1").forward == true); context.assertTrue(mappedRoles.get("domain1-user").forward == false); }
@Test public void checkMappingWithKeep(TestContext context) { Set<String> roles = new HashSet<>(); roles.add("domain2-user"); Map<String, RoleMapper.MappedRole> mappedRoles = roleMapper.mapRoles(roles); context.assertNotNull(mappedRoles); context.assertTrue(mappedRoles.size() == 2); context.assertTrue(mappedRoles.containsKey("domain2")); context.assertTrue(mappedRoles.containsKey("domain2-user")); }
@Test public void checkStageMappingWithoutFurtherHit(TestContext context) { Set<String> roles = new HashSet<>(); roles.add("domain-user-stage-prod"); Map<String, RoleMapper.MappedRole> mappedRoles = roleMapper.mapRoles(roles); context.assertNotNull(mappedRoles); context.assertTrue(mappedRoles.size() == 2); context.assertTrue(mappedRoles.get("domain-user-stage-prod").forward == false); context.assertTrue(mappedRoles.get("domain-user").forward == true); }
@Test public void checkStageMappingWithFurtherHit(TestContext context) { Set<String> roles = new HashSet<>(); roles.add("domain-admin-stage-prod"); Map<String, RoleMapper.MappedRole> mappedRoles = roleMapper.mapRoles(roles); context.assertNotNull(mappedRoles); context.assertTrue(mappedRoles.size() == 3); context.assertTrue(mappedRoles.get("domain-admin-stage-prod").forward == false); context.assertTrue(mappedRoles.get("domain-admin").forward == true); context.assertTrue(mappedRoles.get("domain").forward == false); }
@Test public void checkStageMappingWithoutAnyKeepOriginal(TestContext context) { Set<String> roles = new HashSet<>(); roles.add("domain1-user-stage-prod"); Map<String, RoleMapper.MappedRole> mappedRoles = roleMapper.mapRoles(roles); context.assertNotNull(mappedRoles); context.assertTrue(mappedRoles.size() == 3); context.assertTrue(mappedRoles.get("domain1-user-stage-prod").forward == false); context.assertTrue(mappedRoles.get("domain1-user").forward == false); context.assertTrue(mappedRoles.get("domain1").forward == true); }
@Test public void checkStageMappingWithKeepLastOriginal(TestContext context) { Set<String> roles = new HashSet<>(); roles.add("domain2-user-stage-prod"); Map<String, RoleMapper.MappedRole> mappedRoles = roleMapper.mapRoles(roles); context.assertNotNull(mappedRoles); context.assertTrue(mappedRoles.size() == 3); context.assertTrue(mappedRoles.get("domain2-user-stage-prod").forward == false); context.assertTrue(mappedRoles.get("domain2-user").forward == true); context.assertTrue(mappedRoles.get("domain2").forward == true); }
@Test public void checkStageMappingWithKeepFirstOriginalAndEnvironementProperty(TestContext context) { Set<String> roles = new HashSet<>(); roles.add("domain1-user-stage-int"); Map<String, RoleMapper.MappedRole> mappedRoles = roleMapper.mapRoles(roles); context.assertNotNull(mappedRoles); context.assertTrue(mappedRoles.size() == 3); context.assertTrue(mappedRoles.get("domain1-user-stage-int").forward == true); context.assertTrue(mappedRoles.get("domain1-user-int").forward == false); context.assertTrue(mappedRoles.get("domain1").forward == true); }
@Test public void checkStageMappingWithKeepAllOriginalAndEnvironementProperty(TestContext context) { Set<String> roles = new HashSet<>(); roles.add("domain2-user-stage-testint"); Map<String, RoleMapper.MappedRole> mappedRoles = roleMapper.mapRoles(roles); context.assertNotNull(mappedRoles); context.assertTrue(mappedRoles.size() == 3); context.assertTrue(mappedRoles.containsKey("domain2-user-stage-testint")); context.assertTrue(mappedRoles.containsKey("domain2-user-int")); context.assertTrue(mappedRoles.containsKey("domain2")); }
@Test public void checkLocalMappingPerformance(TestContext context) { Set<String> roles = new HashSet<>(); roles.add("domain1-user-stage-prod"); Map<String, RoleMapper.MappedRole> mappedRoles; long startTime = System.currentTimeMillis(); System.out.println("Start: " + startTime); for (int i = 0; i < 10000; i++) { mappedRoles = roleMapper.mapRoles(roles); if (i == 0) { context.assertNotNull(mappedRoles); context.assertTrue(mappedRoles.size() == 3); context.assertTrue(mappedRoles.get("domain1-user-stage-prod").forward == false); context.assertTrue(mappedRoles.get("domain1-user").forward == false); context.assertTrue(mappedRoles.get("domain1").forward == true); } } long endTime = System.currentTimeMillis(); long duration = endTime - startTime; System.out.println("End: " + startTime); System.out.println("Duration: " + duration); }