KafkaWildcardPrivilege implements Privilege { @Override public boolean implies(Privilege p) { if (!(p instanceof KafkaWildcardPrivilege)) { return false; } KafkaWildcardPrivilege wp = (KafkaWildcardPrivilege)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(KafkaActionConstant.actionName) && !(otherPart.getKey().equalsIgnoreCase(KafkaActionConstant.actionName))) { 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(KafkaActionConstant.ALL)) { return false; } } return true; } KafkaWildcardPrivilege(String permission); @Override boolean implies(Privilege p); @Override String toString();  }
@Test public void testSimpleAction() throws Exception { assertFalse(KAFKA_HOST1_WRITE.implies(KAFKA_HOST1_READ)); assertFalse(KAFKA_HOST1_READ.implies(KAFKA_HOST1_WRITE)); assertFalse(KAFKA_HOST1_GROUP1_WRITE.implies(KAFKA_HOST1_GROUP1_READ)); assertFalse(KAFKA_HOST1_GROUP1_READ.implies(KAFKA_HOST1_GROUP1_WRITE)); assertFalse(KAFKA_HOST1_TOPIC1_READ.implies(KAFKA_HOST1_TOPIC1_WRITE)); assertFalse(KAFKA_HOST1_TOPIC1_WRITE.implies(KAFKA_HOST1_TOPIC1_READ)); assertFalse(KAFKA_HOST1_CLUSTER1_READ.implies(KAFKA_HOST1_CLUSTER1_WRITE)); assertFalse(KAFKA_HOST1_CLUSTER1_WRITE.implies(KAFKA_HOST1_CLUSTER1_READ)); }
@Test public void testShorterThanRequest() throws Exception { assertTrue(KAFKA_HOST1_ALL.implies(KAFKA_HOST1_TOPIC1_ALL)); assertTrue(KAFKA_HOST1_ALL.implies(KAFKA_HOST1_TOPIC1_READ)); assertTrue(KAFKA_HOST1_ALL.implies(KAFKA_HOST1_TOPIC1_WRITE)); assertFalse(KAFKA_HOST1_WRITE.implies(KAFKA_HOST1_READ)); assertTrue(KAFKA_HOST1_READ.implies(KAFKA_HOST1_TOPIC1_READ)); assertTrue(KAFKA_HOST1_WRITE.implies(KAFKA_HOST1_TOPIC1_WRITE)); assertTrue(KAFKA_HOST1_ALL.implies(KAFKA_HOST1_CLUSTER1_ALL)); assertTrue(KAFKA_HOST1_ALL.implies(KAFKA_HOST1_CLUSTER1_READ)); assertTrue(KAFKA_HOST1_ALL.implies(KAFKA_HOST1_CLUSTER1_WRITE)); assertTrue(KAFKA_HOST1_READ.implies(KAFKA_HOST1_CLUSTER1_READ)); assertTrue(KAFKA_HOST1_WRITE.implies(KAFKA_HOST1_CLUSTER1_WRITE)); assertTrue(KAFKA_HOST1_ALL.implies(KAFKA_HOST1_GROUP1_ALL)); assertTrue(KAFKA_HOST1_ALL.implies(KAFKA_HOST1_GROUP1_READ)); assertTrue(KAFKA_HOST1_ALL.implies(KAFKA_HOST1_GROUP1_WRITE)); assertTrue(KAFKA_HOST1_READ.implies(KAFKA_HOST1_GROUP1_READ)); assertTrue(KAFKA_HOST1_WRITE.implies(KAFKA_HOST1_GROUP1_WRITE)); }
@Test public void testActionAll() throws Exception { assertTrue(KAFKA_HOST1_ALL.implies(KAFKA_HOST1_READ)); assertTrue(KAFKA_HOST1_ALL.implies(KAFKA_HOST1_WRITE)); assertTrue(KAFKA_HOST1_TOPIC1_ALL.implies(KAFKA_HOST1_TOPIC1_READ)); assertTrue(KAFKA_HOST1_TOPIC1_ALL.implies(KAFKA_HOST1_TOPIC1_WRITE)); assertTrue(KAFKA_HOST1_CLUSTER1_ALL.implies(KAFKA_HOST1_CLUSTER1_READ)); assertTrue(KAFKA_HOST1_CLUSTER1_ALL.implies(KAFKA_HOST1_CLUSTER1_WRITE)); assertTrue(KAFKA_HOST1_GROUP1_ALL.implies(KAFKA_HOST1_GROUP1_READ)); assertTrue(KAFKA_HOST1_GROUP1_ALL.implies(KAFKA_HOST1_GROUP1_WRITE)); }
@Test public void testUnexpected() throws Exception { Privilege p = new Privilege() { @Override public boolean implies(Privilege p) { return false; } }; Privilege topic1 = create(new KeyValue("HOST", "host"), new KeyValue("TOPIC", "topic1")); assertFalse(topic1.implies(null)); assertFalse(topic1.implies(p)); assertFalse(topic1.equals(null)); assertFalse(topic1.equals(p)); }