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;
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.LoggerFactory;
@@ -30,19 +33,38 @@ public class ACLAuthorizator implements IAuthorizator {
private static final Logger LOG = LoggerFactory.getLogger(ACLAuthorizator.class);
List<Authorization> authorisations = Collections.emptyList();
List<Authorization> m_authorisations = Collections.emptyList();
public ACLAuthorizator(String aclFilePath) {
File aclFile = new File(aclFilePath);
try {
authorisations = ACLFileParser.parse(aclFile);
m_authorisations = ACLFileParser.parse(aclFile);
} catch (ParseException pex) {
LOG.warn(String.format("Format error in parsing acl file %s", aclFile), pex);
}
}
public ACLAuthorizator(List<Authorization> authorisations) {
m_authorisations = authorisations;
}
@Override
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;
}
}
@@ -27,6 +27,10 @@ import java.util.List;
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
* */
public class ACLFileParser {
@@ -21,8 +21,8 @@ package org.eclipse.moquette.spi.impl.security;
* @author andrea
*/
public class Authorization {
private String topic;
private Permission permission;
protected final String topic;
protected final Permission permission;
/**
* Access rights
@@ -16,7 +16,11 @@
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
*/
@@ -26,4 +30,6 @@ public interface IAuthorizator {
* Ask the implementation of the authorizator if the topic can be used in a publish.
* */
boolean canWrite(String topic);
boolean canRead(String topic);
}
@@ -23,4 +23,9 @@ public class PermitAllAuthorizator implements IAuthorizator {
public boolean canWrite(String topic) {
return true;
}
@Override
public boolean canRead(String topic) {
return true;
}
}
@@ -15,7 +15,10 @@
*/
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 MULTI = new Token("#");