MqttConnectionOptionsDialog: added support for TLS. connection_profiles

Sat, 08 Sep 2018 15:28:48 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sat, 08 Sep 2018 15:28:48 +0200
branch
connection_profiles
changeset 25
01d44a4decf5
parent 24
b4e18aadc311
child 26
ad232a5129cc

MqttConnectionOptionsDialog: added support for TLS.

MqttMonitor/MqttConnectionOptionsDialog.py file | annotate | diff | comparison | revisions
MqttMonitor/MqttConnectionOptionsDialog.ui file | annotate | diff | comparison | revisions
MqttMonitor/MqttMonitorWidget.py file | annotate | diff | comparison | revisions
--- a/MqttMonitor/MqttConnectionOptionsDialog.py	Fri Sep 07 18:10:31 2018 +0200
+++ b/MqttMonitor/MqttConnectionOptionsDialog.py	Sat Sep 08 15:28:48 2018 +0200
@@ -13,6 +13,7 @@
 from PyQt5.QtWidgets import QDialog, QDialogButtonBox, QAbstractButton
 
 from E5Gui import E5MessageBox
+from E5Gui.E5PathPicker import E5PathPickerModes
 
 from .Ui_MqttConnectionOptionsDialog import Ui_MqttConnectionOptionsDialog
 
@@ -32,7 +33,7 @@
         @param options dictionary containing the connection options to
             populate the dialog with. It must have the keys "ClientId",
             "Keepalive", "CleanSession", "Username", "Password", "WillTopic",
-            "WillMessage", "WillQos", "WillRetain".
+            "WillMessage", "WillQos", "WillRetain", "TlsEnable", "TlsCaCert".
         @type dict
         @param parent reference to the parent widget
         @type QWidget
@@ -42,6 +43,10 @@
         
         self.__client = client
         
+        self.tlsCertsFilePicker.setMode(E5PathPickerModes.OpenFileMode)
+        self.tlsCertsFilePicker.setFilters(
+            self.tr("Certificate Files (*.crt *.pem);;All Files (*)"))
+        
         self.__populateDefaults(options=options)
         
         self.__updateOkButton()
@@ -91,7 +96,7 @@
         @param options dictionary containing the connection options to populate
             the dialog with. It must have the keys "ClientId", "Keepalive",
             "CleanSession", "Username", "Password", "WillTopic", "WillMessage",
-            "WillQos", "WillRetain".
+            "WillQos", "WillRetain", "TlsEnable", "TlsCaCert".
         @type dict
         """
         if options is None:
@@ -111,6 +116,10 @@
         self.willRetainCheckBox.setChecked(options["WillRetain"])
         self.willTopicEdit.setText(options["WillTopic"])
         self.willMessageEdit.setPlainText(options["WillMessage"])
+        
+        # TLS parameters
+        self.tlsEnableCheckBox.setChecked(options["TlsEnable"])
+        self.tlsCertsFilePicker.setText(options["TlsCaCert"])
     
     def getConnectionOptions(self):
         """
@@ -118,7 +127,8 @@
         
         @return dictionary containing the connection options. It has the keys
             "ClientId", "Keepalive", "CleanSession", "Username", "Password",
-            "WillTopic", "WillMessage", "WillQos", "WillRetain".
+            "WillTopic", "WillMessage", "WillQos", "WillRetain", "TlsEnable",
+            "TlsCaCert".
         @rtype tuple of (int, dict)
         """
         return {
@@ -131,6 +141,8 @@
             "WillMessage": self.willMessageEdit.toPlainText(),
             "WillQos": self.willQosSpinBox.value(),
             "WillRetain": self.willRetainCheckBox.isChecked(),
+            "TlsEnable": self.tlsEnableCheckBox.isChecked(),
+            "TlsCaCert": self.tlsCertsFilePicker.text()
         }
     
     @pyqtSlot(str)
--- a/MqttMonitor/MqttConnectionOptionsDialog.ui	Fri Sep 07 18:10:31 2018 +0200
+++ b/MqttMonitor/MqttConnectionOptionsDialog.ui	Sat Sep 08 15:28:48 2018 +0200
@@ -16,7 +16,7 @@
   <property name="sizeGripEnabled">
    <bool>true</bool>
   </property>
-  <layout class="QVBoxLayout" name="verticalLayout_2">
+  <layout class="QVBoxLayout" name="verticalLayout">
    <item>
     <widget class="QGroupBox" name="groupBox">
      <property name="title">
@@ -206,6 +206,51 @@
     </widget>
    </item>
    <item>
+    <widget class="QGroupBox" name="groupBox_4">
+     <property name="title">
+      <string>SSL/TLS</string>
+     </property>
+     <layout class="QGridLayout" name="gridLayout_4">
+      <item row="0" column="0" colspan="2">
+       <widget class="QCheckBox" name="tlsEnableCheckBox">
+        <property name="toolTip">
+         <string>Select to enable SSL/TLS connections</string>
+        </property>
+        <property name="text">
+         <string>SSL/TLS Enabled</string>
+        </property>
+       </widget>
+      </item>
+      <item row="1" column="0">
+       <widget class="QLabel" name="label_8">
+        <property name="text">
+         <string>CA File:</string>
+        </property>
+       </widget>
+      </item>
+      <item row="1" column="1">
+       <widget class="E5PathPicker" name="tlsCertsFilePicker" native="true">
+        <property name="enabled">
+         <bool>false</bool>
+        </property>
+        <property name="sizePolicy">
+         <sizepolicy hsizetype="Expanding" vsizetype="Preferred">
+          <horstretch>0</horstretch>
+          <verstretch>0</verstretch>
+         </sizepolicy>
+        </property>
+        <property name="focusPolicy">
+         <enum>Qt::StrongFocus</enum>
+        </property>
+        <property name="toolTip">
+         <string>Enter the full path to the CA certificate file; leave empty to use platform default</string>
+        </property>
+       </widget>
+      </item>
+     </layout>
+    </widget>
+   </item>
+   <item>
     <widget class="QDialogButtonBox" name="buttonBox">
      <property name="orientation">
       <enum>Qt::Horizontal</enum>
@@ -223,6 +268,12 @@
    <extends>QLineEdit</extends>
    <header>E5Gui/E5LineEdit.h</header>
   </customwidget>
+  <customwidget>
+   <class>E5PathPicker</class>
+   <extends>QWidget</extends>
+   <header>E5Gui/E5PathPicker.h</header>
+   <container>1</container>
+  </customwidget>
  </customwidgets>
  <tabstops>
   <tabstop>clientIdEdit</tabstop>
@@ -245,8 +296,8 @@
    <slot>accept()</slot>
    <hints>
     <hint type="sourcelabel">
-     <x>248</x>
-     <y>254</y>
+     <x>257</x>
+     <y>590</y>
     </hint>
     <hint type="destinationlabel">
      <x>157</x>
@@ -261,8 +312,8 @@
    <slot>reject()</slot>
    <hints>
     <hint type="sourcelabel">
-     <x>316</x>
-     <y>260</y>
+     <x>325</x>
+     <y>590</y>
     </hint>
     <hint type="destinationlabel">
      <x>286</x>
@@ -270,5 +321,21 @@
     </hint>
    </hints>
   </connection>
+  <connection>
+   <sender>tlsEnableCheckBox</sender>
+   <signal>toggled(bool)</signal>
+   <receiver>tlsCertsFilePicker</receiver>
+   <slot>setEnabled(bool)</slot>
+   <hints>
+    <hint type="sourcelabel">
+     <x>82</x>
+     <y>523</y>
+    </hint>
+    <hint type="destinationlabel">
+     <x>92</x>
+     <y>544</y>
+    </hint>
+   </hints>
+  </connection>
  </connections>
 </ui>
--- a/MqttMonitor/MqttMonitorWidget.py	Fri Sep 07 18:10:31 2018 +0200
+++ b/MqttMonitor/MqttMonitorWidget.py	Sat Sep 08 15:28:48 2018 +0200
@@ -329,6 +329,16 @@
                 self.__client, self.__connectionOptions, parent=self)
             if dlg.exec_() == QDialog.Accepted:
                 self.__connectionOptions = dlg.getConnectionOptions()
+                if self.__connectionOptions["TlsEnable"]:
+                    port = self.brokerPortComboBox.currentText().strip()
+                    if port == "1883":
+                        # it is default non-encrypted port => set to TLS port
+                        self.brokerPortComboBox.setEditText("8883")
+                else:
+                    port = self.brokerPortComboBox.currentText().strip()
+                    if port == "8883":
+                        # it is default TLS port => set to non-encrypted port
+                        self.brokerPortComboBox.setEditText("1883")
     
     @pyqtSlot()
     def on_connectButton_clicked(self):

eric ide

mercurial