DBWildcardPrivilege implements Privilege { @Override public boolean implies(Privilege p) { if (!(p instanceof DBWildcardPrivilege)) { return false; } DBWildcardPrivilege wp = (DBWildcardPrivilege) p; List<KeyValue> otherParts = wp.parts; if(equals(wp)) { return true; } int index = 0; for (KeyValue otherPart : otherParts) { if (parts.size() - 1 < index) { return true; } else { KeyValue part = parts.get(index); if (part.getKey().equalsIgnoreCase("action") && !(otherPart.getKey().equalsIgnoreCase("action"))) { continue; } if(!part.getKey().equalsIgnoreCase(otherPart.getKey())) { return false; } if (!impliesKeyValue(part, otherPart)) { return false; } index++; } } for (; index < parts.size(); index++) { KeyValue part = parts.get(index); if (!part.getValue().equals(AccessConstants.ALL)) { return false; } } return true; } DBWildcardPrivilege(String wildcardString); @Override boolean implies(Privilege p); @Override String toString(); @Override boolean equals(Object o); @Override int hashCode();  }
@Test public void testOther() throws Exception { assertFalse(ROLE_SERVER_ALL_DB_ALL.implies(REQUEST_SERVER1_OTHER)); assertFalse(REQUEST_SERVER1_OTHER.implies(ROLE_SERVER_ALL_DB_ALL)); }
@Test public void testRoleShorterThanRequest() throws Exception { assertTrue(ROLE_SERVER_SERVER1.implies(REQUEST_SERVER1_DB1)); assertTrue(ROLE_SERVER_SERVER1.implies(REQUEST_SERVER1_DB2)); assertFalse(ROLE_SERVER_SERVER1.implies(REQUEST_SERVER2_DB1)); assertFalse(ROLE_SERVER_SERVER1.implies(REQUEST_SERVER2_DB2)); assertTrue(ROLE_SERVER_ALL_DB_ALL.implies(REQUEST_SERVER1)); assertFalse(ROLE_SERVER_ALL_DB_DB1.implies(REQUEST_SERVER1)); }
@Test public void testRolesAndRequests() throws Exception { assertTrue(ROLE_SERVER_SERVER1_DB_ALL.implies(REQUEST_SERVER1_DB1)); assertFalse(ROLE_SERVER_SERVER1_DB_ALL.implies(REQUEST_SERVER2_DB1)); assertTrue(ROLE_SERVER_SERVER1_DB_ALL.implies(REQUEST_SERVER1_DB2)); assertFalse(ROLE_SERVER_SERVER1_DB_ALL.implies(REQUEST_SERVER2_DB2)); assertTrue(REQUEST_SERVER1_DB1.implies(ROLE_SERVER_SERVER1_DB_ALL)); assertFalse(REQUEST_SERVER2_DB1.implies(ROLE_SERVER_SERVER1_DB_ALL)); assertTrue(REQUEST_SERVER1_DB2.implies(ROLE_SERVER_SERVER1_DB_ALL)); assertFalse(REQUEST_SERVER2_DB2.implies(ROLE_SERVER_SERVER1_DB_ALL)); assertTrue(ROLE_SERVER_SERVER1_DB_DB1.implies(REQUEST_SERVER1_DB1)); assertFalse(ROLE_SERVER_SERVER1_DB_DB1.implies(REQUEST_SERVER2_DB1)); assertFalse(ROLE_SERVER_SERVER1_DB_DB1.implies(REQUEST_SERVER1_DB2)); assertFalse(ROLE_SERVER_SERVER1_DB_DB1.implies(REQUEST_SERVER2_DB2)); assertTrue(REQUEST_SERVER1_DB1.implies(ROLE_SERVER_SERVER1_DB_DB1)); assertFalse(REQUEST_SERVER2_DB1.implies(ROLE_SERVER_SERVER1_DB_DB1)); assertFalse(REQUEST_SERVER1_DB2.implies(ROLE_SERVER_SERVER1_DB_DB1)); assertFalse(REQUEST_SERVER2_DB2.implies(ROLE_SERVER_SERVER1_DB_DB1)); assertFalse(ROLE_SERVER_SERVER2_DB_ALL.implies(REQUEST_SERVER1_DB1)); assertTrue(ROLE_SERVER_SERVER2_DB_ALL.implies(REQUEST_SERVER2_DB1)); assertFalse(ROLE_SERVER_SERVER2_DB_ALL.implies(REQUEST_SERVER1_DB2)); assertTrue(ROLE_SERVER_SERVER2_DB_ALL.implies(REQUEST_SERVER2_DB2)); assertFalse(REQUEST_SERVER1_DB1.implies(ROLE_SERVER_SERVER2_DB_ALL)); assertTrue(REQUEST_SERVER2_DB1.implies(ROLE_SERVER_SERVER2_DB_ALL)); assertFalse(REQUEST_SERVER1_DB2.implies(ROLE_SERVER_SERVER2_DB_ALL)); assertTrue(REQUEST_SERVER2_DB2.implies(ROLE_SERVER_SERVER2_DB_ALL)); assertFalse(ROLE_SERVER_SERVER2_DB_DB1.implies(REQUEST_SERVER1_DB1)); assertTrue(ROLE_SERVER_SERVER2_DB_DB1.implies(REQUEST_SERVER2_DB1)); assertFalse(ROLE_SERVER_SERVER2_DB_DB1.implies(REQUEST_SERVER1_DB2)); assertFalse(ROLE_SERVER_SERVER2_DB_DB1.implies(REQUEST_SERVER2_DB2)); assertFalse(REQUEST_SERVER1_DB1.implies(ROLE_SERVER_SERVER2_DB_DB1)); assertTrue(REQUEST_SERVER2_DB1.implies(ROLE_SERVER_SERVER2_DB_DB1)); assertFalse(REQUEST_SERVER1_DB2.implies(ROLE_SERVER_SERVER2_DB_DB1)); assertFalse(REQUEST_SERVER2_DB2.implies(ROLE_SERVER_SERVER2_DB_DB1)); assertTrue(ROLE_SERVER_ALL_DB_ALL.implies(REQUEST_SERVER1_DB1)); assertTrue(ROLE_SERVER_ALL_DB_ALL.implies(REQUEST_SERVER2_DB1)); assertTrue(ROLE_SERVER_ALL_DB_ALL.implies(REQUEST_SERVER1_DB2)); assertTrue(ROLE_SERVER_ALL_DB_ALL.implies(REQUEST_SERVER2_DB2)); assertTrue(REQUEST_SERVER1_DB1.implies(ROLE_SERVER_ALL_DB_ALL)); assertTrue(REQUEST_SERVER2_DB1.implies(ROLE_SERVER_ALL_DB_ALL)); assertTrue(REQUEST_SERVER1_DB2.implies(ROLE_SERVER_ALL_DB_ALL)); assertTrue(REQUEST_SERVER2_DB2.implies(ROLE_SERVER_ALL_DB_ALL)); assertTrue(ROLE_SERVER_ALL_DB_DB1.implies(REQUEST_SERVER1_DB1)); assertTrue(ROLE_SERVER_ALL_DB_DB1.implies(REQUEST_SERVER2_DB1)); assertFalse(ROLE_SERVER_ALL_DB_DB1.implies(REQUEST_SERVER1_DB2)); assertFalse(ROLE_SERVER_ALL_DB_DB1.implies(REQUEST_SERVER2_DB2)); assertTrue(REQUEST_SERVER1_DB1.implies(ROLE_SERVER_ALL_DB_DB1)); assertTrue(REQUEST_SERVER2_DB1.implies(ROLE_SERVER_ALL_DB_DB1)); assertFalse(REQUEST_SERVER1_DB2.implies(ROLE_SERVER_ALL_DB_DB1)); assertFalse(REQUEST_SERVER2_DB2.implies(ROLE_SERVER_ALL_DB_DB1)); assertTrue(ROLE_SERVER_SERVER1.implies(REQUEST_SERVER1_URI1)); assertTrue(ROLE_SERVER_SERVER1.implies(REQUEST_SERVER1_URI2)); assertTrue(ROLE_SERVER_SERVER1.implies(REQUEST_SERVER1_URI2)); assertTrue(ROLE_SERVER_SERVER1_URI_ALL.implies(REQUEST_SERVER1_URI1)); assertTrue(ROLE_SERVER_SERVER1_URI_ALL.implies(REQUEST_SERVER1_URI2)); assertTrue(ROLE_SERVER_SERVER1.implies(REQUEST_SERVER1_URI2)); assertTrue(ROLE_SERVER_SERVER1_URI_URI1.implies(REQUEST_SERVER1_URI1)); assertFalse(ROLE_SERVER_SERVER1_URI_URI1.implies(REQUEST_SERVER1_URI2)); assertTrue(ROLE_SERVER_SERVER1_URI_URI2.implies(REQUEST_SERVER1_URI2)); assertFalse(ROLE_SERVER_SERVER1_URI_URI2.implies(REQUEST_SERVER1_URI1)); assertFalse(REQUEST_SERVER2_DB2.implies(REQUEST_SERVER1_URI1)); assertFalse(ROLE_SERVER_ALL_DB_DB1.implies(REQUEST_SERVER1_URI1)); assertTrue(REQUEST_SERVER1_URI1.implies(ROLE_SERVER_SERVER1_URI_ALL)); assertTrue(REQUEST_SERVER1_URI2.implies(ROLE_SERVER_SERVER1_URI_ALL)); assertFalse(REQUEST_SERVER1_URI1.implies(ROLE_SERVER_SERVER1)); assertFalse(REQUEST_SERVER1_URI1.implies(ROLE_SERVER_SERVER1_URI_URI1)); assertFalse(REQUEST_SERVER1_URI2.implies(ROLE_SERVER_SERVER1_URI_URI1)); assertFalse(REQUEST_SERVER1_URI2.implies(ROLE_SERVER_SERVER1_URI_URI2)); assertFalse(REQUEST_SERVER1_URI1.implies(ROLE_SERVER_SERVER1_URI_URI2)); }
@Test public void testActionHierarchy() throws Exception { String dbName = "db1"; DBWildcardPrivilege dbAll = create(new KeyValue("server", "server1"), new KeyValue("db", dbName), new KeyValue("action", "ALL")); DBWildcardPrivilege dbSelect = create(new KeyValue("server", "server1"), new KeyValue("db", dbName), new KeyValue("action", "SELECT")); DBWildcardPrivilege dbInsert = create(new KeyValue("server", "server1"), new KeyValue("db", dbName), new KeyValue("action", "INSERT")); DBWildcardPrivilege dbAlter = create(new KeyValue("server", "server1"), new KeyValue("db", dbName), new KeyValue("action", "ALTER")); DBWildcardPrivilege dbCreate = create(new KeyValue("server", "server1"), new KeyValue("db", dbName), new KeyValue("action", "CREATE")); DBWildcardPrivilege dbDrop = create(new KeyValue("server", "server1"), new KeyValue("db", dbName), new KeyValue("action", "DROP")); DBWildcardPrivilege dbIndex = create(new KeyValue("server", "server1"), new KeyValue("db", dbName), new KeyValue("action", "INDEX")); DBWildcardPrivilege dbLock = create(new KeyValue("server", "server1"), new KeyValue("db", dbName), new KeyValue("action", "LOCK")); assertTrue(dbAll.implies(dbSelect)); assertTrue(dbAll.implies(dbInsert)); assertTrue(dbAll.implies(dbAlter)); assertTrue(dbAll.implies(dbCreate)); assertTrue(dbAll.implies(dbDrop)); assertTrue(dbAll.implies(dbIndex)); assertTrue(dbAll.implies(dbLock)); dbAll = create(new KeyValue("server", "server1"), new KeyValue("db", dbName), new KeyValue("action", "*")); assertTrue(dbAll.implies(dbSelect)); assertTrue(dbAll.implies(dbInsert)); assertTrue(dbAll.implies(dbAlter)); assertTrue(dbAll.implies(dbCreate)); assertTrue(dbAll.implies(dbDrop)); assertTrue(dbAll.implies(dbIndex)); assertTrue(dbAll.implies(dbLock)); dbAll = create(new KeyValue("server", "server1"), new KeyValue("db", dbName)); assertTrue(dbAll.implies(dbSelect)); assertTrue(dbAll.implies(dbInsert)); assertTrue(dbAll.implies(dbAlter)); assertTrue(dbAll.implies(dbCreate)); assertTrue(dbAll.implies(dbDrop)); assertTrue(dbAll.implies(dbIndex)); assertTrue(dbAll.implies(dbLock)); }