Refactored the collection of all ACL in a specialized collector class instead of using plain List
This commit is contained in:
@@ -206,7 +206,13 @@ public class SimpleMessaging implements IMessaging, EventHandler<ValueEvent> {
|
||||
String aclFilePath = props.getProperty(ACL_FILE_PROPERTY_NAME, "");
|
||||
IAuthorizator authorizator;
|
||||
if (aclFilePath != null && !aclFilePath.isEmpty()) {
|
||||
authorizator = new ACLAuthorizator(configPath, aclFilePath);
|
||||
authorizator = new DenyAllAuthorizator();
|
||||
File aclFile = new File(configPath, aclFilePath);
|
||||
try {
|
||||
authorizator = ACLFileParser.parse(aclFile);
|
||||
} catch (ParseException pex) {
|
||||
LOG.error(String.format("Format error in parsing acl file %s", aclFile), pex);
|
||||
}
|
||||
LOG.info("Using acl file defined at path {}", aclFilePath);
|
||||
} else {
|
||||
authorizator = new PermitAllAuthorizator();
|
||||
|
||||
@@ -1,70 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2012-2014 The original author or authors
|
||||
* ------------------------------------------------------
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* and Apache License v2.0 which accompanies this distribution.
|
||||
*
|
||||
* The Eclipse Public License is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
*
|
||||
* The Apache License v2.0 is available at
|
||||
* http://www.opensource.org/licenses/apache2.0.php
|
||||
*
|
||||
* You may elect to redistribute this code under either of these licenses.
|
||||
*/
|
||||
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;
|
||||
|
||||
import java.io.File;
|
||||
import java.text.ParseException;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author andrea
|
||||
*/
|
||||
public class ACLAuthorizator implements IAuthorizator {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(ACLAuthorizator.class);
|
||||
|
||||
List<Authorization> m_authorisations = Collections.emptyList();
|
||||
|
||||
public ACLAuthorizator(String parent, String filePath) {
|
||||
File aclFile = new File(parent, filePath);
|
||||
try {
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -39,21 +39,21 @@ public class ACLFileParser {
|
||||
/**
|
||||
* Parse the configuration from file.
|
||||
*/
|
||||
public static List<Authorization> parse(File file) throws ParseException {
|
||||
public static AuthorizationsCollector parse(File file) throws ParseException {
|
||||
if (file == null) {
|
||||
LOG.warn("parsing NULL file, so fallback on default configuration!");
|
||||
return Collections.emptyList();
|
||||
return AuthorizationsCollector.emptyImmutableCollector();
|
||||
}
|
||||
if (!file.exists()) {
|
||||
LOG.warn(String.format("parsing not existing file %s, so fallback on default configuration!", file.getAbsolutePath()));
|
||||
return Collections.emptyList();
|
||||
return AuthorizationsCollector.emptyImmutableCollector();
|
||||
}
|
||||
try {
|
||||
FileReader reader = new FileReader(file);
|
||||
return parse(reader);
|
||||
} catch (FileNotFoundException fex) {
|
||||
LOG.warn(String.format("parsing not existing file %s, so fallback on default configuration!", file.getAbsolutePath()), fex);
|
||||
return Collections.emptyList();
|
||||
return AuthorizationsCollector.emptyImmutableCollector();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -62,16 +62,17 @@ public class ACLFileParser {
|
||||
*
|
||||
* @throws java.text.ParseException if the format is not compliant.
|
||||
*/
|
||||
public static List<Authorization> parse(Reader reader) throws ParseException {
|
||||
public static AuthorizationsCollector parse(Reader reader) throws ParseException {
|
||||
if (reader == null) {
|
||||
//just log and return default properties
|
||||
LOG.warn("parsing NULL reader, so fallback on default configuration!");
|
||||
return Collections.emptyList();
|
||||
return AuthorizationsCollector.emptyImmutableCollector();
|
||||
}
|
||||
|
||||
BufferedReader br = new BufferedReader(reader);
|
||||
String line;
|
||||
List<Authorization> authorizations = new ArrayList();
|
||||
AuthorizationsCollector collector = new AuthorizationsCollector();
|
||||
|
||||
try {
|
||||
while ((line = br.readLine()) != null) {
|
||||
int commentMarker = line.indexOf('#');
|
||||
@@ -89,34 +90,12 @@ public class ACLFileParser {
|
||||
continue;
|
||||
}
|
||||
|
||||
authorizations.add(parseAuthLine(line));
|
||||
collector.parse(line);
|
||||
}
|
||||
}
|
||||
} catch (IOException ex) {
|
||||
throw new ParseException("Failed to read", 1);
|
||||
}
|
||||
return authorizations;
|
||||
}
|
||||
|
||||
protected static Authorization parseAuthLine(String line) throws ParseException {
|
||||
String[] tokens = line.split("\\s+");
|
||||
String keyword = tokens[0];
|
||||
if ("topic".equalsIgnoreCase(keyword)) {
|
||||
if (tokens.length > 2) {
|
||||
//if the tokenized lines has 3 token the second must be the permission
|
||||
try {
|
||||
Permission permission = 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);
|
||||
}
|
||||
return null;
|
||||
return collector;
|
||||
}
|
||||
}
|
||||
|
||||
+105
@@ -0,0 +1,105 @@
|
||||
/*
|
||||
* Copyright (c) 2012-2014 The original author or authors
|
||||
* ------------------------------------------------------
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* and Apache License v2.0 which accompanies this distribution.
|
||||
*
|
||||
* The Eclipse Public License is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
*
|
||||
* The Apache License v2.0 is available at
|
||||
* http://www.opensource.org/licenses/apache2.0.php
|
||||
*
|
||||
* You may elect to redistribute this code under either of these licenses.
|
||||
*/
|
||||
package org.eclipse.moquette.spi.impl.security;
|
||||
|
||||
import org.eclipse.moquette.spi.impl.subscriptions.SubscriptionsStore;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import static org.eclipse.moquette.spi.impl.security.Authorization.Permission.READ;
|
||||
import static org.eclipse.moquette.spi.impl.security.Authorization.Permission.READWRITE;
|
||||
import static org.eclipse.moquette.spi.impl.security.Authorization.Permission.WRITE;
|
||||
|
||||
/**
|
||||
* Used by the ACLFileParser to push all authorizations it finds.
|
||||
* ACLAuthorizator uses it in read mode to check it topics matches the ACLs.
|
||||
*
|
||||
* Not thread safe.
|
||||
*
|
||||
* @author andrea
|
||||
*/
|
||||
class AuthorizationsCollector implements IAuthorizator {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(AuthorizationsCollector.class);
|
||||
|
||||
private List<Authorization> m_authorizations = new ArrayList();
|
||||
|
||||
static final AuthorizationsCollector emptyImmutableCollector() {
|
||||
AuthorizationsCollector coll = new AuthorizationsCollector();
|
||||
coll.m_authorizations = Collections.emptyList();
|
||||
return coll;
|
||||
}
|
||||
|
||||
void parse(String line) {
|
||||
try {
|
||||
m_authorizations.add(parseAuthLine(line));
|
||||
} catch (ParseException pex) {
|
||||
LOG.warn(String.format("Error parsing ACL definition [%s]", line), pex);
|
||||
}
|
||||
}
|
||||
|
||||
protected static Authorization parseAuthLine(String line) throws ParseException {
|
||||
String[] tokens = line.split("\\s+");
|
||||
String keyword = tokens[0];
|
||||
if ("topic".equalsIgnoreCase(keyword)) {
|
||||
if (tokens.length > 2) {
|
||||
//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);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canWrite(String topic) {
|
||||
return canDoOperation(topic, WRITE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canRead(String topic) {
|
||||
return canDoOperation(topic, READ);
|
||||
}
|
||||
|
||||
private boolean canDoOperation(String topic, Authorization.Permission permission) {
|
||||
for (Authorization auth : m_authorizations) {
|
||||
if (auth.permission == permission || auth.permission == READWRITE) {
|
||||
if (SubscriptionsStore.matchTopics(topic, auth.topic)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean isEmpty() {
|
||||
return m_authorizations.isEmpty();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* Copyright (c) 2012-2014 The original author or authors
|
||||
* ------------------------------------------------------
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* and Apache License v2.0 which accompanies this distribution.
|
||||
*
|
||||
* The Eclipse Public License is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
*
|
||||
* The Apache License v2.0 is available at
|
||||
* http://www.opensource.org/licenses/apache2.0.php
|
||||
*
|
||||
* You may elect to redistribute this code under either of these licenses.
|
||||
*/
|
||||
package org.eclipse.moquette.spi.impl.security;
|
||||
|
||||
/**
|
||||
* @author andrea
|
||||
*/
|
||||
public class DenyAllAuthorizator implements IAuthorizator {
|
||||
@Override
|
||||
public boolean canWrite(String topic) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canRead(String topic) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user