From 14f14e20dfe05bbba65470cec587667fdc0a7d79 Mon Sep 17 00:00:00 2001 From: andrea Date: Thu, 23 Jul 2015 19:56:48 +0200 Subject: [PATCH] Minor, added authorizator and autheticator class variables in the config used in distribution --- ChangeLog.txt | 1 + .../moquette/spi/impl/SimpleMessaging.java | 39 +++++++------------ distribution/src/main/resources/moquette.conf | 17 ++++++++ pom.xml | 4 ++ .../integration/issue10_acl_file_test.groovy | 38 ++++++++++++++++++ 5 files changed, 74 insertions(+), 25 deletions(-) create mode 100644 tools_scripts/integration/issue10_acl_file_test.groovy diff --git a/ChangeLog.txt b/ChangeLog.txt index 89255129..0a5fed94 100644 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -1,4 +1,5 @@ Version 0.8: + Introduced authenticator and authorizator implementations. Version 0.7: Fix issue #38, added aligned to MQTT 3.1.1 diff --git a/broker/src/main/java/org/eclipse/moquette/spi/impl/SimpleMessaging.java b/broker/src/main/java/org/eclipse/moquette/spi/impl/SimpleMessaging.java index 238bbd4e..46f90b06 100644 --- a/broker/src/main/java/org/eclipse/moquette/spi/impl/SimpleMessaging.java +++ b/broker/src/main/java/org/eclipse/moquette/spi/impl/SimpleMessaging.java @@ -196,17 +196,16 @@ public class SimpleMessaging implements IMessaging, EventHandler { //subscriptions.init(storedSubscriptions); subscriptions.init(m_sessionsStore); - IAuthenticator authenticator = null; - - String configPath = System.getProperty("moquette.path", null); + String configPath = System.getProperty("moquette.path", null); String authenticatorClassName = props.getProperty(Constants.AUTHENTICATOR_CLASS_NAME, ""); - - if(!authenticatorClassName.isEmpty()) { - authenticator = (IAuthenticator)loadClass(authenticatorClassName, IAuthenticator.class); - LOG.info("Loaded custom authenticator {}", authenticatorClassName); + + IAuthenticator authenticator = null; + if (!authenticatorClassName.isEmpty()) { + authenticator = (IAuthenticator)loadClass(authenticatorClassName, IAuthenticator.class); + LOG.info("Loaded custom authenticator {}", authenticatorClassName); } - - if(authenticator == null) { + + if (authenticator == null) { String passwdPath = props.getProperty(PASSWORD_FILE_PROPERTY_NAME, ""); if (passwdPath.isEmpty()) { authenticator = new AcceptAllAuthenticator(); @@ -214,18 +213,15 @@ public class SimpleMessaging implements IMessaging, EventHandler { authenticator = new FileAuthenticator(configPath, passwdPath); } } - - - IAuthorizator authorizator = null; + IAuthorizator authorizator = null; String authorizatorClassName = props.getProperty(Constants.AUTHORIZATOR_CLASS_NAME, ""); - if(!authorizatorClassName.isEmpty()) { + if (!authorizatorClassName.isEmpty()) { authorizator = (IAuthorizator)loadClass(authorizatorClassName, IAuthorizator.class); LOG.info("Loaded custom authorizator {}", authorizatorClassName); - } - - if(authorizator == null) { + } + if (authorizator == null) { String aclFilePath = props.getProperty(ACL_FILE_PROPERTY_NAME, ""); if (aclFilePath != null && !aclFilePath.isEmpty()) { authorizator = new DenyAllAuthorizator(); @@ -242,17 +238,14 @@ public class SimpleMessaging implements IMessaging, EventHandler { } } - + boolean allowAnonymous = Boolean.parseBoolean(props.getProperty(ALLOW_ANONYMOUS_PROPERTY_NAME, "true")); m_processor.init(subscriptions, m_storageService, m_sessionsStore, authenticator, allowAnonymous, authorizator); } private Object loadClass(String className, Class cls) { - Object instance = null; - try { - Class clazz = Class.forName(className); // check if method getInstance exists @@ -263,21 +256,17 @@ public class SimpleMessaging implements IMessaging, EventHandler { LOG.error(null, ex); throw new RuntimeException("Cannot call method "+ className +".getInstance", ex); } - } - catch (NoSuchMethodException noMethodEx) { - + catch (NoSuchMethodException nsmex) { try { instance = this.getClass().getClassLoader() .loadClass(className) .asSubclass(cls) .newInstance(); - } catch (InstantiationException | IllegalAccessException | ClassNotFoundException ex) { LOG.error(null, ex); throw new RuntimeException("Cannot load custom authenticator class " + className, ex); } - } catch (ClassNotFoundException ex) { LOG.error(null, ex); throw new RuntimeException("Class " + className + " not found", ex); diff --git a/distribution/src/main/resources/moquette.conf b/distribution/src/main/resources/moquette.conf index ab7a1c23..9ec95fdf 100644 --- a/distribution/src/main/resources/moquette.conf +++ b/distribution/src/main/resources/moquette.conf @@ -59,3 +59,20 @@ allow_anonymous true # defined by the system property moquette.path #********************************************************************* password_file config/password_file.conf + + +#********************************************************************* +# Optional +# authorizator_class: +# class name of the authorizator, by default uses the +# password_file.conf. +# If not specified uses the class: org.eclipse.moquette.spi.impl.security.AuthorizationsCollector +# +# Optional +# authenticator_class: +# class name of the authenticator, default implementation uses +# definitions in the acl.conf. +# If not specified uses org.eclipse.moquette.spi.impl.security.FileAuthenticator +#********************************************************************* +# authenticator_class [[path to your class>]] +# authorizator_class [[path to your class>]] \ No newline at end of file diff --git a/pom.xml b/pom.xml index e2616bcd..d7c9f5f8 100644 --- a/pom.xml +++ b/pom.xml @@ -34,6 +34,10 @@ + + Steven Warwick + http://stevenwarwick.com + Igor Yova https://github.com/Fameing diff --git a/tools_scripts/integration/issue10_acl_file_test.groovy b/tools_scripts/integration/issue10_acl_file_test.groovy new file mode 100644 index 00000000..4f3ce03c --- /dev/null +++ b/tools_scripts/integration/issue10_acl_file_test.groovy @@ -0,0 +1,38 @@ +@Grab(group='org.fusesource.mqtt-client', module='mqtt-client', version='1.10') + +import org.fusesource.mqtt.client.BlockingConnection +import org.fusesource.mqtt.client.MQTT +import org.fusesource.mqtt.client.Message +import org.fusesource.mqtt.client.QoS +import org.fusesource.mqtt.client.Topic + + +String host = args[0] +//start a publisher +MQTT mqtt2 = new MQTT() +mqtt2.setHost(host, 1883) +mqtt2.setUserName("gravity") +mqtt2.setPassword("passwd") +BlockingConnection client = mqtt2.blockingConnection() +client.connect() +String topicOut = "out" +String topicIn = "in" +/*println "try to publish (write) on topic ${topicOut}" + +client.publish(topicOut, 'publish test!!'.bytes, QoS.AT_MOST_ONCE, false) +println "published" + +println "try to subscribe (read) on topic ${topicIn}" +client.subscribe([new Topic(topicIn, QoS.AT_LEAST_ONCE)] as Topic[]) +println "subscribed"*/ +println "try to subscribe (read) on topic that should fail, ${topicOut}" +client.subscribe([new Topic(topicOut, QoS.AT_LEAST_ONCE)] as Topic[]) +println "if subscribed is an error!!" + +/*String topicUntouch = "untouchable" +println "try to subscribe (read) on untouchable topic for this user, ${topicUntouch}" +client.subscribe([new Topic(topicUntouch, QoS.AT_LEAST_ONCE)] as Topic[]) +println "subscribed to untouchable, if subscribed is an error!!"*/ + +println "shutdown client" +client.disconnect() \ No newline at end of file