Minor, added authorizator and autheticator class variables in the config used in distribution

This commit is contained in:
andrea
2015-07-23 19:56:48 +02:00
parent 4a7c90e5bc
commit 14f14e20df
5 changed files with 74 additions and 25 deletions
+1
View File
@@ -1,4 +1,5 @@
Version 0.8: Version 0.8:
Introduced authenticator and authorizator implementations.
Version 0.7: Version 0.7:
Fix issue #38, added aligned to MQTT 3.1.1 Fix issue #38, added aligned to MQTT 3.1.1
@@ -196,17 +196,16 @@ public class SimpleMessaging implements IMessaging, EventHandler<ValueEvent> {
//subscriptions.init(storedSubscriptions); //subscriptions.init(storedSubscriptions);
subscriptions.init(m_sessionsStore); 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, ""); String authenticatorClassName = props.getProperty(Constants.AUTHENTICATOR_CLASS_NAME, "");
if(!authenticatorClassName.isEmpty()) { IAuthenticator authenticator = null;
authenticator = (IAuthenticator)loadClass(authenticatorClassName, IAuthenticator.class); if (!authenticatorClassName.isEmpty()) {
LOG.info("Loaded custom authenticator {}", authenticatorClassName); 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, ""); String passwdPath = props.getProperty(PASSWORD_FILE_PROPERTY_NAME, "");
if (passwdPath.isEmpty()) { if (passwdPath.isEmpty()) {
authenticator = new AcceptAllAuthenticator(); authenticator = new AcceptAllAuthenticator();
@@ -214,18 +213,15 @@ public class SimpleMessaging implements IMessaging, EventHandler<ValueEvent> {
authenticator = new FileAuthenticator(configPath, passwdPath); authenticator = new FileAuthenticator(configPath, passwdPath);
} }
} }
IAuthorizator authorizator = null;
IAuthorizator authorizator = null;
String authorizatorClassName = props.getProperty(Constants.AUTHORIZATOR_CLASS_NAME, ""); String authorizatorClassName = props.getProperty(Constants.AUTHORIZATOR_CLASS_NAME, "");
if(!authorizatorClassName.isEmpty()) { if (!authorizatorClassName.isEmpty()) {
authorizator = (IAuthorizator)loadClass(authorizatorClassName, IAuthorizator.class); authorizator = (IAuthorizator)loadClass(authorizatorClassName, IAuthorizator.class);
LOG.info("Loaded custom authorizator {}", authorizatorClassName); LOG.info("Loaded custom authorizator {}", authorizatorClassName);
} }
if(authorizator == null) {
if (authorizator == null) {
String aclFilePath = props.getProperty(ACL_FILE_PROPERTY_NAME, ""); String aclFilePath = props.getProperty(ACL_FILE_PROPERTY_NAME, "");
if (aclFilePath != null && !aclFilePath.isEmpty()) { if (aclFilePath != null && !aclFilePath.isEmpty()) {
authorizator = new DenyAllAuthorizator(); authorizator = new DenyAllAuthorizator();
@@ -242,17 +238,14 @@ public class SimpleMessaging implements IMessaging, EventHandler<ValueEvent> {
} }
} }
boolean allowAnonymous = Boolean.parseBoolean(props.getProperty(ALLOW_ANONYMOUS_PROPERTY_NAME, "true")); boolean allowAnonymous = Boolean.parseBoolean(props.getProperty(ALLOW_ANONYMOUS_PROPERTY_NAME, "true"));
m_processor.init(subscriptions, m_storageService, m_sessionsStore, authenticator, allowAnonymous, authorizator); m_processor.init(subscriptions, m_storageService, m_sessionsStore, authenticator, allowAnonymous, authorizator);
} }
private Object loadClass(String className, Class<?> cls) { private Object loadClass(String className, Class<?> cls) {
Object instance = null; Object instance = null;
try { try {
Class<?> clazz = Class.forName(className); Class<?> clazz = Class.forName(className);
// check if method getInstance exists // check if method getInstance exists
@@ -263,21 +256,17 @@ public class SimpleMessaging implements IMessaging, EventHandler<ValueEvent> {
LOG.error(null, ex); LOG.error(null, ex);
throw new RuntimeException("Cannot call method "+ className +".getInstance", ex); throw new RuntimeException("Cannot call method "+ className +".getInstance", ex);
} }
} }
catch (NoSuchMethodException noMethodEx) { catch (NoSuchMethodException nsmex) {
try { try {
instance = this.getClass().getClassLoader() instance = this.getClass().getClassLoader()
.loadClass(className) .loadClass(className)
.asSubclass(cls) .asSubclass(cls)
.newInstance(); .newInstance();
} catch (InstantiationException | IllegalAccessException | ClassNotFoundException ex) { } catch (InstantiationException | IllegalAccessException | ClassNotFoundException ex) {
LOG.error(null, ex); LOG.error(null, ex);
throw new RuntimeException("Cannot load custom authenticator class " + className, ex); throw new RuntimeException("Cannot load custom authenticator class " + className, ex);
} }
} catch (ClassNotFoundException ex) { } catch (ClassNotFoundException ex) {
LOG.error(null, ex); LOG.error(null, ex);
throw new RuntimeException("Class " + className + " not found", ex); throw new RuntimeException("Class " + className + " not found", ex);
@@ -59,3 +59,20 @@ allow_anonymous true
# defined by the system property moquette.path # defined by the system property moquette.path
#********************************************************************* #*********************************************************************
password_file config/password_file.conf 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>]]
+4
View File
@@ -34,6 +34,10 @@
</developers> </developers>
<contributors> <contributors>
<contributor>
<name>Steven Warwick</name>
<url>http://stevenwarwick.com</url>
</contributor>
<contributor> <contributor>
<name>Igor Yova</name> <name>Igor Yova</name>
<url>https://github.com/Fameing</url> <url>https://github.com/Fameing</url>
@@ -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()