MqttMonitor/MqttClient.py

branch
connection_profiles
changeset 24
b4e18aadc311
parent 22
545979c7dcd4
child 30
17ef10819773
--- a/MqttMonitor/MqttClient.py	Thu Sep 06 19:35:43 2018 +0200
+++ b/MqttMonitor/MqttClient.py	Fri Sep 07 18:10:31 2018 +0200
@@ -160,6 +160,20 @@
         self.__mqttClient.will_set(topic, payload=payload, qos=qos,
                                    retain=retain)
     
+    def setTLS(self, caCerts=None, certFile=None, keyFile=None):
+        """
+        Public method to enable secure connections and set the TLS parameters.
+        
+        @param caCerts path to the Certificate Authority certificates file
+        @type str
+        @param certFile PEM encoded client certificate file
+        @type str
+        @param keyFile PEM encoded private key file
+        @type str
+        """
+        self.__mqttClient.tls_set(ca_certs=caCerts, certfile=certFile,
+                                  keyfile=keyFile)
+    
     def startLoop(self):
         """
         Public method to start the MQTT client loop.
@@ -212,7 +226,8 @@
         @param options dictionary containing the connection options. This
             dictionary should contain the keys "ClientId", "Keepalive",
             "CleanSession", "Username", "Password", "WillTopic", "WillMessage",
-            "WillQos", "WillRetain"
+            "WillQos", "WillRetain", "TlsEnable", "TlsCaCert", "TlsClientCert",
+            "TlsClientKey"
         @type dict
         """
         if options:
@@ -246,7 +261,22 @@
                                  parametersDict["WillQos"],
                                  parametersDict["WillRetain"])
             
-            # step 4: connect to server
+            # step 4: set TLS parameters
+            if parametersDict["TlsEnable"]:
+                if parametersDict["TlsCaCert"] and \
+                        parametersDict["TlsClientCert"]:
+                    # use self signed client certificate
+                    self.setTLS(caCerts=parametersDict["TlsCaCert"],
+                                certFile=parametersDict["TlsClientCert"],
+                                keyFile=parametersDict["TlsClientKey"])
+                elif parametersDict["TlsCaCert"]:
+                    # use CA certificate file
+                    self.setTLS(caCerts=parametersDict["TlsCaCert"])
+                else:
+                    # use default TLS configuration
+                    self.setTLS()
+            
+            # step 5: connect to server
             self.connectToServer(host, port=port,
                                  keepalive=parametersDict["Keepalive"])
         else:
@@ -261,7 +291,8 @@
         
         @return dictionary containing the default connection options. It has
             the keys "ClientId", "Keepalive", "CleanSession", "Username",
-            "Password", "WillTopic", "WillMessage", "WillQos", "WillRetain"
+            "Password", "WillTopic", "WillMessage", "WillQos", "WillRetain",
+            "TlsEnable", "TlsCaCert", "TlsClientCert", "TlsClientKey".
         @rtype dict
         """
         return {
@@ -274,6 +305,10 @@
             "WillMessage": "",
             "WillQos": 0,
             "WillRetain": False,
+            "TlsEnable": False,
+            "TlsCaCert": "",
+            "TlsClientCert": "",
+            "TlsClientKey": "",
         }
     
     def reconnectToServer(self):

eric ide

mercurial