SimpleFilteredPrivilegeCache implements FilteredPrivilegeCache { @Override public Set<Privilege> listPrivilegeObjects(Set<String> groups, Set<String> users, ActiveRoleSet roleSet, Authorizable... authorizationHierarchy) { Set<String> privilegeStrings = listPrivileges(groups, users, roleSet, authorizationHierarchy); return privilegeStrings.stream() .filter(priString -> priString != null) .map(priString -> getPrivilegeObject(priString)) .collect(Collectors.toSet()); } SimpleFilteredPrivilegeCache(Set<String> cachedPrivileges, PrivilegeFactory inPrivilegeFactory); @Override Set<String> listPrivileges(Set<String> groups, ActiveRoleSet roleSet); @Override void close(); @Override Set<String> listPrivileges(Set<String> groups, Set<String> users, ActiveRoleSet roleSet); @Override Set<String> listPrivileges(Set<String> groups, Set<String> users, ActiveRoleSet roleSet,
    Authorizable... authorizationHierarchy); @Override Set<Privilege> listPrivilegeObjects(Set<String> groups, Set<String> users, ActiveRoleSet roleSet,
    Authorizable... authorizationHierarchy);  }
@Test public void testListPrivilegeObjectsWildCard() { CommonPrivilege t1D1Select = create(new KeyValue("Server", "server1"), new KeyValue("db", "db1"), new KeyValue("table", "t1"), new KeyValue("action", "SELECT")); CommonPrivilege t1D2Select = create(new KeyValue("Server", "server1"), new KeyValue("db", "db2"), new KeyValue("table", "t1"), new KeyValue("action", "SELECT")); CommonPrivilege t2Select = create(new KeyValue("Server", "server1"), new KeyValue("db", "db1"), new KeyValue("table", "t2"), new KeyValue("action", "SELECT")); CommonPrivilege wildCardTable = create(new KeyValue("Server", "server1"), new KeyValue("db", "db1"), new KeyValue("table", "*"), new KeyValue("action", "SELECT")); CommonPrivilege allTable = create(new KeyValue("Server", "server1"), new KeyValue("db", "db1"), new KeyValue("table", "ALL"), new KeyValue("action", "SELECT")); CommonPrivilege allDatabase = create(new KeyValue("Server", "server1"), new KeyValue("db", "*")); CommonPrivilege colSelect = create(new KeyValue("Server", "server1"), new KeyValue("db", "db1"), new KeyValue("table", "t1"), new KeyValue("column", "c1"), new KeyValue("action", "SELECT")); SimpleFilteredPrivilegeCache cache = new SimpleFilteredPrivilegeCache(Sets.newHashSet(t1D1Select.toString(), t1D2Select.toString(), t2Select.toString(), wildCardTable.toString(), allTable.toString(), allDatabase.toString(), colSelect.toString()), null); assertEquals(0, cache.listPrivilegeObjects(null, null, null, new Server("server1")).size()); assertEquals(1, cache.listPrivilegeObjects(null, null, null, new Server("server1"), new Database("db1")).size()); assertEquals(1, cache.listPrivilegeObjects(null, null, null, new Server("server1"), new Database("db2")).size()); assertEquals(4, cache.listPrivilegeObjects(null, null, null, new Server("server1"), new Database("db1"), new Table("t1")).size()); assertEquals(5, cache.listPrivilegeObjects(null, null, null, new Server("server1"), new Database("db1"), new Table("*")).size()); assertEquals(5, cache.listPrivilegeObjects(null, null, null, new Server("server1"), new Database("db1"), new Table("t1"), new Column("*")).size()); assertEquals(6, cache.listPrivilegeObjects(null, null, null, new Server("server1"), new Database("db1"), new Table("*"), new Column("*")).size()); assertEquals(2, cache.listPrivilegeObjects(null, null, null, new Server("server1"), new Database("db2"), new Table("t1"), new Column("*")).size()); }
@Test @Ignore("This test should be run manually.") public void testListPrivilegeObjectsPerf() { Set<String> privileges = generatePrivilegeStrings(1000, 10); SimpleFilteredPrivilegeCache cache = new SimpleFilteredPrivilegeCache(privileges, null); List<Authorizable[]> authorizables = generateAuthoriables(12000, 10); long start = System.currentTimeMillis(); for (Authorizable[] authorizableHierarchy : authorizables) { Set<Privilege> privilegeSet = cache.listPrivilegeObjects(null, null, null, authorizableHierarchy); for (Privilege currPri : privilegeSet) { currPri.getParts(); } } long end = System.currentTimeMillis(); System.out.println("SimpleFilteredPrivilegeCache - total time on list obj: " + (end - start) + " ms"); }