Added trivial implementation of ACL patterns
This commit is contained in:
@@ -299,7 +299,7 @@ class ProtocolProcessor implements EventHandler<ValueEvent> {
|
|||||||
boolean retain = msg.isRetainFlag();
|
boolean retain = msg.isRetainFlag();
|
||||||
//check if the topic can be wrote
|
//check if the topic can be wrote
|
||||||
String user = (String) session.getAttribute(NettyChannel.ATTR_KEY_USERNAME);
|
String user = (String) session.getAttribute(NettyChannel.ATTR_KEY_USERNAME);
|
||||||
if (m_authorizator.canWrite(topic, user)) {
|
if (m_authorizator.canWrite(topic, user, clientID)) {
|
||||||
processPublish(clientID, topic, qos, message, retain, msg.getMessageID());
|
processPublish(clientID, topic, qos, message, retain, msg.getMessageID());
|
||||||
} else {
|
} else {
|
||||||
LOG.debug("topic {} doesn't have write credentials", topic);
|
LOG.debug("topic {} doesn't have write credentials", topic);
|
||||||
@@ -439,7 +439,7 @@ class ProtocolProcessor implements EventHandler<ValueEvent> {
|
|||||||
LOG.debug("Session for clientId {} is {}", clientId, session);
|
LOG.debug("Session for clientId {} is {}", clientId, session);
|
||||||
|
|
||||||
String user = (String) session.getAttribute(NettyChannel.ATTR_KEY_USERNAME);
|
String user = (String) session.getAttribute(NettyChannel.ATTR_KEY_USERNAME);
|
||||||
if (!m_authorizator.canRead(topic, user)) {
|
if (!m_authorizator.canRead(topic, user, clientId)) {
|
||||||
LOG.debug("topic {} doesn't have read credentials", topic);
|
LOG.debug("topic {} doesn't have read credentials", topic);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ public class Authorization {
|
|||||||
* */
|
* */
|
||||||
enum Permission {
|
enum Permission {
|
||||||
READ, WRITE, READWRITE
|
READ, WRITE, READWRITE
|
||||||
};
|
}
|
||||||
|
|
||||||
Authorization(String topic) {
|
Authorization(String topic) {
|
||||||
this(topic, Permission.READWRITE);
|
this(topic, Permission.READWRITE);
|
||||||
|
|||||||
+43
-22
@@ -42,6 +42,7 @@ class AuthorizationsCollector implements IAuthorizator {
|
|||||||
private List<Authorization> m_patternAuthorizations = new ArrayList();
|
private List<Authorization> m_patternAuthorizations = new ArrayList();
|
||||||
private Map<String, List<Authorization>> m_userAuthorizations = new HashMap();
|
private Map<String, List<Authorization>> m_userAuthorizations = new HashMap();
|
||||||
private boolean m_parsingUsersSpecificSection = false;
|
private boolean m_parsingUsersSpecificSection = false;
|
||||||
|
private boolean m_parsingPatternSpecificSection = false;
|
||||||
private String m_currentUser = "";
|
private String m_currentUser = "";
|
||||||
|
|
||||||
static final AuthorizationsCollector emptyImmutableCollector() {
|
static final AuthorizationsCollector emptyImmutableCollector() {
|
||||||
@@ -65,6 +66,8 @@ class AuthorizationsCollector implements IAuthorizator {
|
|||||||
}
|
}
|
||||||
List<Authorization> userAuths = m_userAuthorizations.get(m_currentUser);
|
List<Authorization> userAuths = m_userAuthorizations.get(m_currentUser);
|
||||||
userAuths.add(acl);
|
userAuths.add(acl);
|
||||||
|
} else if (m_parsingPatternSpecificSection) {
|
||||||
|
m_patternAuthorizations.add(acl);
|
||||||
} else {
|
} else {
|
||||||
m_globalAuthorizations.add(acl);
|
m_globalAuthorizations.add(acl);
|
||||||
}
|
}
|
||||||
@@ -75,43 +78,50 @@ class AuthorizationsCollector implements IAuthorizator {
|
|||||||
String keyword = tokens[0].toLowerCase();
|
String keyword = tokens[0].toLowerCase();
|
||||||
switch (keyword) {
|
switch (keyword) {
|
||||||
case "topic":
|
case "topic":
|
||||||
if (tokens.length > 2) {
|
return createAuthorization(line, tokens);
|
||||||
//if the tokenized lines has 3 token the second must be the permission
|
|
||||||
try {
|
|
||||||
Authorization.Permission permission = Authorization.Permission.valueOf(tokens[1].toUpperCase());
|
|
||||||
//bring topic with all original spacing
|
|
||||||
String topic = line.substring(line.indexOf(tokens[2]));
|
|
||||||
|
|
||||||
return new Authorization(topic, permission);
|
|
||||||
} catch (IllegalArgumentException iaex) {
|
|
||||||
throw new ParseException("invalid permission token", 1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
String topic = tokens[1];
|
|
||||||
return new Authorization(topic);
|
|
||||||
case "user":
|
case "user":
|
||||||
m_parsingUsersSpecificSection = true;
|
m_parsingUsersSpecificSection = true;
|
||||||
m_currentUser = tokens[1];
|
m_currentUser = tokens[1];
|
||||||
|
m_parsingPatternSpecificSection = false;
|
||||||
return null;
|
return null;
|
||||||
case "pattern":
|
case "pattern":
|
||||||
//TODO implement the part for patter matching
|
m_parsingUsersSpecificSection = false;
|
||||||
return null;
|
m_currentUser = "";
|
||||||
|
m_parsingPatternSpecificSection = true;
|
||||||
|
return createAuthorization(line, tokens);
|
||||||
default:
|
default:
|
||||||
throw new ParseException(String.format("invalid line definition found %s", line), 1);
|
throw new ParseException(String.format("invalid line definition found %s", line), 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
private Authorization createAuthorization(String line, String[] tokens) throws ParseException {
|
||||||
public boolean canWrite(String topic, String user) {
|
if (tokens.length > 2) {
|
||||||
return canDoOperation(topic, WRITE, user);
|
//if the tokenized lines has 3 token the second must be the permission
|
||||||
|
try {
|
||||||
|
Authorization.Permission permission = Authorization.Permission.valueOf(tokens[1].toUpperCase());
|
||||||
|
//bring topic with all original spacing
|
||||||
|
String topic = line.substring(line.indexOf(tokens[2]));
|
||||||
|
|
||||||
|
return new Authorization(topic, permission);
|
||||||
|
} catch (IllegalArgumentException iaex) {
|
||||||
|
throw new ParseException("invalid permission token", 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
String topic = tokens[1];
|
||||||
|
return new Authorization(topic);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean canRead(String topic, String user) {
|
public boolean canWrite(String topic, String user, String client) {
|
||||||
return canDoOperation(topic, READ, user);
|
return canDoOperation(topic, WRITE, user, client);
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean canDoOperation(String topic, Authorization.Permission permission, String username) {
|
@Override
|
||||||
|
public boolean canRead(String topic, String user, String client) {
|
||||||
|
return canDoOperation(topic, READ, user, client);
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean canDoOperation(String topic, Authorization.Permission permission, String username, String client) {
|
||||||
for (Authorization auth : m_globalAuthorizations) {
|
for (Authorization auth : m_globalAuthorizations) {
|
||||||
if (auth.permission == permission || auth.permission == READWRITE) {
|
if (auth.permission == permission || auth.permission == READWRITE) {
|
||||||
if (SubscriptionsStore.matchTopics(topic, auth.topic)) {
|
if (SubscriptionsStore.matchTopics(topic, auth.topic)) {
|
||||||
@@ -120,6 +130,17 @@ class AuthorizationsCollector implements IAuthorizator {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (client != null && !client.isEmpty()) {
|
||||||
|
for (Authorization auth : m_patternAuthorizations) {
|
||||||
|
String substitutedTopic = auth.topic.replace("%c", client);
|
||||||
|
if (auth.permission == permission || auth.permission == READWRITE) {
|
||||||
|
if (SubscriptionsStore.matchTopics(topic, substitutedTopic)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (username != null && !username.isEmpty()) {
|
if (username != null && !username.isEmpty()) {
|
||||||
if (m_userAuthorizations.containsKey(username)) {
|
if (m_userAuthorizations.containsKey(username)) {
|
||||||
for (Authorization auth : m_userAuthorizations.get(username)) {
|
for (Authorization auth : m_userAuthorizations.get(username)) {
|
||||||
|
|||||||
+2
-2
@@ -20,12 +20,12 @@ package org.eclipse.moquette.spi.impl.security;
|
|||||||
*/
|
*/
|
||||||
public class DenyAllAuthorizator implements IAuthorizator {
|
public class DenyAllAuthorizator implements IAuthorizator {
|
||||||
@Override
|
@Override
|
||||||
public boolean canWrite(String topic, String user) {
|
public boolean canWrite(String topic, String user, String client) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean canRead(String topic, String user) {
|
public boolean canRead(String topic, String user, String client) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -29,7 +29,7 @@ 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, String user);
|
boolean canWrite(String topic, String user, String client);
|
||||||
|
|
||||||
boolean canRead(String topic, String user);
|
boolean canRead(String topic, String user, String client);
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-2
@@ -20,12 +20,12 @@ package org.eclipse.moquette.spi.impl.security;
|
|||||||
*/
|
*/
|
||||||
public class PermitAllAuthorizator implements IAuthorizator {
|
public class PermitAllAuthorizator implements IAuthorizator {
|
||||||
@Override
|
@Override
|
||||||
public boolean canWrite(String topic, String user) {
|
public boolean canWrite(String topic, String user, String client) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean canRead(String topic, String user) {
|
public boolean canRead(String topic, String user, String client) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ public class MapDBPersistentStoreTest {
|
|||||||
@Before
|
@Before
|
||||||
public void setUp() throws Exception {
|
public void setUp() throws Exception {
|
||||||
File dbFile = new File(DEFAULT_PERSISTENT_PATH);
|
File dbFile = new File(DEFAULT_PERSISTENT_PATH);
|
||||||
assertFalse(String.format("The DB storagefile %s already exists", DEFAULT_PERSISTENT_PATH), dbFile.exists());
|
assertFalse(String.format("The DB storage file %s already exists", DEFAULT_PERSISTENT_PATH), dbFile.exists());
|
||||||
|
|
||||||
m_storageService = new MapDBPersistentStore(DEFAULT_PERSISTENT_PATH);
|
m_storageService = new MapDBPersistentStore(DEFAULT_PERSISTENT_PATH);
|
||||||
m_storageService.initStore();
|
m_storageService.initStore();
|
||||||
|
|||||||
@@ -58,8 +58,8 @@ public class ACLFileParserTest {
|
|||||||
AuthorizationsCollector authorizations = ACLFileParser.parse(conf);
|
AuthorizationsCollector authorizations = ACLFileParser.parse(conf);
|
||||||
|
|
||||||
//Verify
|
//Verify
|
||||||
assertTrue(authorizations.canRead("/weather/italy/anemometer", ""));
|
assertTrue(authorizations.canRead("/weather/italy/anemometer", "", ""));
|
||||||
assertTrue(authorizations.canWrite("/weather/italy/anemometer", ""));
|
assertTrue(authorizations.canWrite("/weather/italy/anemometer", "", ""));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
+18
-8
@@ -18,6 +18,8 @@ package org.eclipse.moquette.spi.impl.security;
|
|||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.io.Reader;
|
||||||
|
import java.io.StringReader;
|
||||||
import java.text.ParseException;
|
import java.text.ParseException;
|
||||||
|
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
@@ -90,7 +92,7 @@ public class AuthorizationsCollectorTest {
|
|||||||
authorizator.parse("topic write /sensors");
|
authorizator.parse("topic write /sensors");
|
||||||
|
|
||||||
//verify
|
//verify
|
||||||
assertTrue(authorizator.canWrite("/sensors", ""));
|
assertTrue(authorizator.canWrite("/sensors", "", ""));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -98,7 +100,7 @@ public class AuthorizationsCollectorTest {
|
|||||||
authorizator.parse("topic read /sensors");
|
authorizator.parse("topic read /sensors");
|
||||||
|
|
||||||
//verify
|
//verify
|
||||||
assertTrue(authorizator.canRead("/sensors", ""));
|
assertTrue(authorizator.canRead("/sensors", "", ""));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -107,8 +109,8 @@ public class AuthorizationsCollectorTest {
|
|||||||
authorizator.parse("topic read /sensors/anemometer");
|
authorizator.parse("topic read /sensors/anemometer");
|
||||||
|
|
||||||
//verify
|
//verify
|
||||||
assertTrue(authorizator.canWrite("/sensors", ""));
|
assertTrue(authorizator.canWrite("/sensors", "", ""));
|
||||||
assertFalse(authorizator.canRead("/sensors", ""));
|
assertFalse(authorizator.canRead("/sensors", "", ""));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -116,7 +118,7 @@ public class AuthorizationsCollectorTest {
|
|||||||
authorizator.parse("topic write /sensors/#");
|
authorizator.parse("topic write /sensors/#");
|
||||||
|
|
||||||
//verify
|
//verify
|
||||||
assertTrue(authorizator.canWrite("/sensors/anemometer/wind", ""));
|
assertTrue(authorizator.canWrite("/sensors/anemometer/wind", "", ""));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -124,7 +126,7 @@ public class AuthorizationsCollectorTest {
|
|||||||
authorizator.parse("topic write /sensors/+");
|
authorizator.parse("topic write /sensors/+");
|
||||||
|
|
||||||
//verify
|
//verify
|
||||||
assertTrue(authorizator.canWrite("/sensors/anemometer", ""));
|
assertTrue(authorizator.canWrite("/sensors/anemometer", "", ""));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -133,7 +135,15 @@ public class AuthorizationsCollectorTest {
|
|||||||
authorizator.parse("topic write /sensors");
|
authorizator.parse("topic write /sensors");
|
||||||
|
|
||||||
//verify
|
//verify
|
||||||
assertTrue(authorizator.canWrite("/sensors", "john"));
|
assertTrue(authorizator.canWrite("/sensors", "john", ""));
|
||||||
assertFalse(authorizator.canWrite("/sensors", "jack"));
|
assertFalse(authorizator.canWrite("/sensors", "jack", ""));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testParsePatternClientLineACL() throws ParseException {
|
||||||
|
authorizator.parse("pattern read /weather/italy/%c");
|
||||||
|
|
||||||
|
//Verify
|
||||||
|
assertTrue(authorizator.canRead("/weather/italy/anemometer1", "", "anemometer1"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user