CommonPrivilege implements Privilege { @Override public boolean implies(Privilege privilege, Model model) { if (!(privilege instanceof CommonPrivilege)) { return false; } CommonPrivilege requiredPrivilege = (CommonPrivilege) privilege; if ((requiredPrivilege.grantOption == true) && (this.grantOption == false)) { return false; } List<KeyValue> otherParts = requiredPrivilege.getParts(); if(parts.equals(otherParts)) { return true; } int index = 0; for (KeyValue otherPart : otherParts) { if (parts.size() - 1 < index) { return true; } else { KeyValue part = parts.get(index); String policyKey = part.getKey(); if(!policyKey.equalsIgnoreCase(otherPart.getKey())) { if (SentryConstants.PRIVILEGE_NAME.equalsIgnoreCase(policyKey)) { continue; } return false; } if (SentryConstants.PRIVILEGE_NAME.equalsIgnoreCase(policyKey)) { if (!impliesAction(part.getValue(), otherPart.getValue(), model.getBitFieldActionFactory())) { return false; } } else { if (!impliesResource(model.getImplyMethodMap().get(policyKey.toLowerCase()), part.getValue(), otherPart.getValue())) { return false; } } index++; } } for (; index < parts.size(); index++) { KeyValue part = parts.get(index); if (!isPrivilegeActionAll(part, model.getBitFieldActionFactory())) { return false; } } return true; } CommonPrivilege(String privilegeStr); @Override boolean implies(Privilege privilege, Model model); @Override List<KeyValue> getAuthorizable(); @Override String toString(); @Override List<KeyValue> getParts(); @Override boolean equals(Object o); @Override int hashCode();  }
@Test public void testImplyCommonPrivilegeWithoutAction() throws Exception { CommonPrivilege requestPrivilege = new CommonPrivilege("server=server1->db=db1->table=table1"); CommonPrivilege privilegForTest1 = new CommonPrivilege("server=server1->db=db1->table=table1"); CommonPrivilege privilegForTest2 = new CommonPrivilege("server=server1->db=db1"); CommonPrivilege privilegForTest3 = new CommonPrivilege("server=server1->db=db1->table=table2"); CommonPrivilege privilegForTest4 = new CommonPrivilege("server=server1->db=db1->table=table1->column=col1"); CommonPrivilege privilegForTest5 = new CommonPrivilege("server=server1->db=db1->table=table1->column=*"); assertTrue(privilegForTest1.implies(requestPrivilege, testModel)); assertTrue(privilegForTest2.implies(requestPrivilege, testModel)); assertFalse(privilegForTest3.implies(requestPrivilege, testModel)); assertFalse(privilegForTest4.implies(requestPrivilege, testModel)); assertTrue(privilegForTest5.implies(requestPrivilege, testModel)); }
@Test public void testImplyCommonPrivilegeWithUrl() throws Exception { CommonPrivilege requestPrivilege = new CommonPrivilege("server=server1->uri=hdfs: CommonPrivilege privilegForTest1 = new CommonPrivilege("server=server1->uri=hdfs: CommonPrivilege privilegForTest2 = new CommonPrivilege("server=server1->uri=hdfs: CommonPrivilege privilegForTest3 = new CommonPrivilege("server=server1->uri=hdfs: assertTrue(privilegForTest1.implies(requestPrivilege, testModel)); assertTrue(privilegForTest2.implies(requestPrivilege, testModel)); assertFalse(privilegForTest3.implies(requestPrivilege, testModel)); }
@Test public void testImplyCommonPrivilegeForAction() throws Exception { CommonPrivilege privilegForSelect = new CommonPrivilege("server=server1->db=db1->table=table1->action=select"); CommonPrivilege privilegForInsert = new CommonPrivilege("server=server1->db=db1->table=table1->action=insert"); CommonPrivilege privilegForAll = new CommonPrivilege("server=server1->db=db1->table=table1->action=all"); assertTrue(privilegForSelect.implies(privilegForSelect, testModel)); assertTrue(privilegForInsert.implies(privilegForInsert, testModel)); assertTrue(privilegForAll.implies(privilegForAll, testModel)); assertFalse(privilegForInsert.implies(privilegForSelect, testModel)); assertTrue(privilegForAll.implies(privilegForSelect, testModel)); assertFalse(privilegForSelect.implies(privilegForInsert, testModel)); assertTrue(privilegForAll.implies(privilegForInsert, testModel)); assertFalse(privilegForSelect.implies(privilegForAll, testModel)); assertFalse(privilegForInsert.implies(privilegForAll, testModel)); }
@Test public void testImplyStringCaseSensitive() throws Exception { CommonPrivilege privileg1 = new CommonPrivilege("server=server1->db=db1->table=table1->column=col1->action=select"); CommonPrivilege privileg2 = new CommonPrivilege("server=server1->db=db1->table=table1->column=CoL1->action=select"); CommonPrivilege privileg3 = new CommonPrivilege("server=SERver1->db=Db1->table=TAbLe1->column=col1->action=select"); CommonPrivilege privileg4 = new CommonPrivilege("SERVER=server1->DB=db1->TABLE=table1->COLUMN=col1->ACTION=select"); assertFalse(privileg1.implies(privileg2, testModel)); assertTrue(privileg1.implies(privileg3, testModel)); assertTrue(privileg1.implies(privileg4, testModel)); }