Added basic initial implementation for ACL validation

This commit is contained in:
andrea
2015-05-19 22:07:57 +02:00
parent f3ed351c61
commit 8b8058dc47
7 changed files with 100 additions and 6 deletions
@@ -15,6 +15,9 @@
*/ */
package org.eclipse.moquette.spi.impl.security; package org.eclipse.moquette.spi.impl.security;
import static org.eclipse.moquette.spi.impl.security.Authorization.Permission.*;
import static org.eclipse.moquette.spi.impl.security.Authorization.Permission;
import org.eclipse.moquette.spi.impl.subscriptions.SubscriptionsStore;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@@ -30,19 +33,38 @@ public class ACLAuthorizator implements IAuthorizator {
private static final Logger LOG = LoggerFactory.getLogger(ACLAuthorizator.class); private static final Logger LOG = LoggerFactory.getLogger(ACLAuthorizator.class);
List<Authorization> authorisations = Collections.emptyList(); List<Authorization> m_authorisations = Collections.emptyList();
public ACLAuthorizator(String aclFilePath) { public ACLAuthorizator(String aclFilePath) {
File aclFile = new File(aclFilePath); File aclFile = new File(aclFilePath);
try { try {
authorisations = ACLFileParser.parse(aclFile); m_authorisations = ACLFileParser.parse(aclFile);
} catch (ParseException pex) { } catch (ParseException pex) {
LOG.warn(String.format("Format error in parsing acl file %s", aclFile), pex); LOG.warn(String.format("Format error in parsing acl file %s", aclFile), pex);
} }
} }
public ACLAuthorizator(List<Authorization> authorisations) {
m_authorisations = authorisations;
}
@Override @Override
public boolean canWrite(String topic) { public boolean canWrite(String topic) {
return canDoOperation(topic, WRITE);
}
public boolean canRead(String topic) {
return canDoOperation(topic, READ);
}
private boolean canDoOperation(String topic, Permission permission) {
for (Authorization auth : m_authorisations) {
if (auth.permission == permission) {
if (SubscriptionsStore.matchTopics(topic, auth.topic)) {
return true;
}
}
}
return false; return false;
} }
} }
@@ -27,6 +27,10 @@ import java.util.List;
import static org.eclipse.moquette.spi.impl.security.Authorization.Permission; import static org.eclipse.moquette.spi.impl.security.Authorization.Permission;
/** /**
* Parses the acl configuration file.
* If a line starts with # it's comment.
* Blank lines are skipped. The format is "topic [read|write|readwrite] <topic name>"
*
* @author andrea * @author andrea
* */ * */
public class ACLFileParser { public class ACLFileParser {
@@ -21,8 +21,8 @@ package org.eclipse.moquette.spi.impl.security;
* @author andrea * @author andrea
*/ */
public class Authorization { public class Authorization {
private String topic; protected final String topic;
private Permission permission; protected final Permission permission;
/** /**
* Access rights * Access rights
@@ -16,7 +16,11 @@
package org.eclipse.moquette.spi.impl.security; package org.eclipse.moquette.spi.impl.security;
/** /**
* ACL checker * ACL checker.
*
* Create an authorizator that matches topic names with same grammar of subscriptions.
* The # is always a terminator and its the multilevel matcher.
* The + sign is the single level matcher.
* *
* @author andrea * @author andrea
*/ */
@@ -26,4 +30,6 @@ public interface IAuthorizator {
* Ask the implementation of the authorizator if the topic can be used in a publish. * Ask the implementation of the authorizator if the topic can be used in a publish.
* */ * */
boolean canWrite(String topic); boolean canWrite(String topic);
boolean canRead(String topic);
} }
@@ -23,4 +23,9 @@ public class PermitAllAuthorizator implements IAuthorizator {
public boolean canWrite(String topic) { public boolean canWrite(String topic) {
return true; return true;
} }
@Override
public boolean canRead(String topic) {
return true;
}
} }
@@ -15,7 +15,10 @@
*/ */
package org.eclipse.moquette.spi.impl.subscriptions; package org.eclipse.moquette.spi.impl.subscriptions;
class Token { /**
* Internal use only class.
* */
public class Token {
static final Token EMPTY = new Token(""); static final Token EMPTY = new Token("");
static final Token MULTI = new Token("#"); static final Token MULTI = new Token("#");
@@ -0,0 +1,54 @@
package org.eclipse.moquette.spi.impl.security;
import org.junit.Test;
import static java.util.Arrays.*;
import static org.eclipse.moquette.spi.impl.security.Authorization.Permission.*;
import static org.junit.Assert.*;
public class ACLAuthorizatorTest {
@Test
public void testCanWriteSimpleTopic() {
ACLAuthorizator authorizator = new ACLAuthorizator(asList(new Authorization("/sensors", WRITE)));
//verify
assertTrue(authorizator.canWrite("/sensors"));
}
@Test
public void testCanReadSimpleTopic() {
ACLAuthorizator authorizator = new ACLAuthorizator(asList(new Authorization("/sensors", READ)));
//verify
assertTrue(authorizator.canRead("/sensors"));
}
@Test
public void testCanReadWriteMixedSimpleTopic() {
Authorization topicWriteACL = new Authorization("/sensors", WRITE);
Authorization topicReadACL = new Authorization("/sensors/anemometer", READ);
ACLAuthorizator authorizator = new ACLAuthorizator(asList(topicWriteACL, topicReadACL));
//verify
assertTrue(authorizator.canWrite("/sensors"));
assertFalse(authorizator.canRead("/sensors"));
}
@Test
public void testCanWriteMultiMatherTopic() {
ACLAuthorizator authorizator = new ACLAuthorizator(asList(new Authorization("/sensors/#", WRITE)));
//verify
assertTrue(authorizator.canWrite("/sensors/anemometer/wind"));
}
@Test
public void testCanWriteSingleMatherTopic() {
ACLAuthorizator authorizator = new ACLAuthorizator(asList(new Authorization("/sensors/+", WRITE)));
//verify
assertTrue(authorizator.canWrite("/sensors/anemometer"));
}
}