{"action":"created","comment":{"url":"https://api.github.com/repos/diwanc/liota/pulls/comments/76181706","id":76181706,"diff_hunk":"@@ -44,29 +46,112 @@ def on_subscribe(self, client, userdata, mid, granted_qos):\n \n     def on_message(self, client, userdata, msg):\n         log.debug(\"On Message {0} {1} {2}\".format(msg.topic, str(msg.qos), str(msg.payload)))\n+        self.client.disconnect()\n \n     def on_publish(self, client, userdata, mid):\n         log.debug(\"mid: {0}\".format(str(mid)))\n \n-    def __init__(self, url, port):\n+    def on_connect(self, client, userdata, flags, rc):\n+        if rc != 0:\n+            raise ValueError(\"Error: \" + self.mqttresults.resultcodes.get(rc))\n+        else:\n+            log.debug(\"Connected with result code \" + str(rc) + \" : \" + self.mqttresults.resultcodes.get(rc))\n+\n+    def __init__(self, gw_identity, TLSConf, QoSConf, url, port, keepalive, tls_support=True, enable_authentication = True):\n+        self.gw_identity = gw_identity\n+        self.TLSConf = TLSConf\n         self.url = url\n         self.port = port\n+        self.keepalive = keepalive\n+        self.QoSConf = QoSConf\n         self.client = paho.Client()\n         self.client.on_message = self.on_message\n         self.client.on_publish = self.on_publish\n         self.client.on_subscribe = self.on_subscribe\n+        self.tls_support = tls_support\n+        self.enable_authentication = enable_authentication\n+        self.mqttresults = mqtt_results()\n         self.connect_soc()\n         TransportLayer.__init__(self)\n \n     def connect_soc(self):\n-        self.client.connect_soc(host=self.url, port=self.port, keepalive=60)\n-        log.info(\"Connection Successful\")\n+        # Set up TLS support\n+        if self.tls_support:\n+\n+            # Validate CA certificate path\n+            if self.gw_identity.cacert:\n+                if not(os.path.exists(self.gw_identity.cacert)):\n+                    raise ValueError(\"Error : Wrong CA certificate path.\")\n+            else:\n+                raise ValueError(\"Error : CA certificate path is missing\")\n+\n+            # Validate client certificate path\n+            if self.gw_identity.certfile:\n+                if os.path.exists(self.gw_identity.certfile):\n+                    client_cert_available = True\n+                else:\n+                    raise ValueError(\"Error : Wrong client certificate path.\")\n+            else:\n+                client_cert_available = False\n+\n+            # Validate client key file path\n+            if self.gw_identity.keyfile:\n+                if os.path.exists(self.gw_identity.keyfile):\n+                    client_key_available = True\n+                else:\n+                    raise ValueError(\"Error : Wrong client key path.\")\n+            else:\n+                client_key_available = False\n+\n+            # Multiple conditions for certificate validations\n+                # 1. Both Client certificate and key file should be present\n+                # 2. If both are not there proceed without client certificate and key\n+                # 3. If client certificate is not there throw an error\n+                # 4. If client key is not there throw an error\n+            if client_cert_available and client_key_available:\n+                log.debug(\"Certificates : \", self.gw_identity.cacert, self.gw_identity.certfile, self.gw_identity.keyfile)\n+\n+                self.client.tls_set(self.gw_identity.cacert, self.gw_identity.certfile, self.gw_identity.keyfile,\n+                                    cert_reqs=getattr(ssl, self.TLSConf.cert_required),\n+                                    tls_version=getattr(ssl, self.TLSConf.tls_version), ciphers=self.TLSConf.cipher)\n+            elif not client_cert_available and not client_key_available:\n+                self.client.tls_set(self.gw_identity.cacert,\n+                                    cert_reqs=getattr(ssl, self.TLSConf.cert_required),\n+                                    tls_version=getattr(ssl, self.TLSConf.tls_version), ciphers=self.TLSConf.cipher)\n+            elif not client_cert_available and client_key_available:\n+                raise ValueError(\"Error : Client key found, but client certificate not found\")\n+            else:\n+                raise ValueError(\"Error : Client certificate found, but client key not found\")\n+\n+            log.info(\"TLS support is set up.\")\n+\n+        # Set up username-password\n+        if self.enable_authentication:\n+            if not self.gw_identity.username:\n+                raise ValueError(\"Username not found\")\n+            elif not self.gw_identity.password:\n+                raise ValueError(\"Password not found\")\n+            else:\n+                self.client.username_pw_set(self.gw_identity.username, self.gw_identity.password)\n+\n+        # Set QoS parameters\n+        if self.QoSConf.qoslevel:","path":"liota/transports/mqtt.py","position":109,"original_position":109,"commit_id":"26fe6a326d43736d0afea701d4d8b4e50544724a","original_commit_id":"26fe6a326d43736d0afea701d4d8b4e50544724a","user":{"login":"diwanc","id":4242007,"avatar_url":"https://avatars.githubusercontent.com/u/4242007?v=3","gravatar_id":"","url":"https://api.github.com/users/diwanc","html_url":"https://github.com/diwanc","followers_url":"https://api.github.com/users/diwanc/followers","following_url":"https://api.github.com/users/diwanc/following{/other_user}","gists_url":"https://api.github.com/users/diwanc/gists{/gist_id}","starred_url":"https://api.github.com/users/diwanc/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/diwanc/subscriptions","organizations_url":"https://api.github.com/users/diwanc/orgs","repos_url":"https://api.github.com/users/diwanc/repos","events_url":"https://api.github.com/users/diwanc/events{/privacy}","received_events_url":"https://api.github.com/users/diwanc/received_events","type":"User","site_admin":false},"body":"What if the qoslevel is not present?","created_at":"2016-08-25T05:22:35Z","updated_at":"2016-08-25T05:22:35Z","html_url":"https://github.com/diwanc/liota/pull/3#discussion_r76181706","pull_request_url":"https://api.github.com/repos/diwanc/liota/pulls/3","_links":{"self":{"href":"https://api.github.com/repos/diwanc/liota/pulls/comments/76181706"},"html":{"href":"https://github.com/diwanc/liota/pull/3#discussion_r76181706"},"pull_request":{"href":"https://api.github.com/repos/diwanc/liota/pulls/3"}}},"pull_request":{"url":"https://api.github.com/repos/diwanc/liota/pulls/3","id":80607314,"html_url":"https://github.com/diwanc/liota/pull/3","diff_url":"https://github.com/diwanc/liota/pull/3.diff","patch_url":"https://github.com/diwanc/liota/pull/3.patch","issue_url":"https://api.github.com/repos/diwanc/liota/issues/3","number":3,"state":"open","locked":false,"title":"Authentication and QoS feature","user":{"login":"ashish-vmware","id":20726257,"avatar_url":"https://avatars.githubusercontent.com/u/20726257?v=3","gravatar_id":"","url":"https://api.github.com/users/ashish-vmware","html_url":"https://github.com/ashish-vmware","followers_url":"https://api.github.com/users/ashish-vmware/followers","following_url":"https://api.github.com/users/ashish-vmware/following{/other_user}","gists_url":"https://api.github.com/users/ashish-vmware/gists{/gist_id}","starred_url":"https://api.github.com/users/ashish-vmware/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ashish-vmware/subscriptions","organizations_url":"https://api.github.com/users/ashish-vmware/orgs","repos_url":"https://api.github.com/users/ashish-vmware/repos","events_url":"https://api.github.com/users/ashish-vmware/events{/privacy}","received_events_url":"https://api.github.com/users/ashish-vmware/received_events","type":"User","site_admin":false},"body":"- Added username password setup methods\r\n- Added QoS configuration validations and setup methods\r\n- Added QoS conf class which does the abstraction for QoS related configurations\r\n- Added protocol utility class which will encapsulate result codes for various protocols","created_at":"2016-08-09T11:56:13Z","updated_at":"2016-08-25T05:22:36Z","closed_at":null,"merged_at":null,"merge_commit_sha":null,"assignee":null,"assignees":[],"milestone":null,"commits_url":"https://api.github.com/repos/diwanc/liota/pulls/3/commits","review_comments_url":"https://api.github.com/repos/diwanc/liota/pulls/3/comments","review_comment_url":"https://api.github.com/repos/diwanc/liota/pulls/comments{/number}","comments_url":"https://api.github.com/repos/diwanc/liota/issues/3/comments","statuses_url":"https://api.github.com/repos/diwanc/liota/statuses/26fe6a326d43736d0afea701d4d8b4e50544724a","head":{"label":"ashish-vmware:Authentication_QoS_feature","ref":"Authentication_QoS_feature","sha":"26fe6a326d43736d0afea701d4d8b4e50544724a","user":{"login":"ashish-vmware","id":20726257,"avatar_url":"https://avatars.githubusercontent.com/u/20726257?v=3","gravatar_id":"","url":"https://api.github.com/users/ashish-vmware","html_url":"https://github.com/ashish-vmware","followers_url":"https://api.github.com/users/ashish-vmware/followers","following_url":"https://api.github.com/users/ashish-vmware/following{/other_user}","gists_url":"https://api.github.com/users/ashish-vmware/gists{/gist_id}","starred_url":"https://api.github.com/users/ashish-vmware/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ashish-vmware/subscriptions","organizations_url":"https://api.github.com/users/ashish-vmware/orgs","repos_url":"https://api.github.com/users/ashish-vmware/repos","events_url":"https://api.github.com/users/ashish-vmware/events{/privacy}","received_events_url":"https://api.github.com/users/ashish-vmware/received_events","type":"User","site_admin":false},"repo":{"id":64544861,"name":"liota","full_name":"ashish-vmware/liota","owner":{"login":"ashish-vmware","id":20726257,"avatar_url":"https://avatars.githubusercontent.com/u/20726257?v=3","gravatar_id":"","url":"https://api.github.com/users/ashish-vmware","html_url":"https://github.com/ashish-vmware","followers_url":"https://api.github.com/users/ashish-vmware/followers","following_url":"https://api.github.com/users/ashish-vmware/following{/other_user}","gists_url":"https://api.github.com/users/ashish-vmware/gists{/gist_id}","starred_url":"https://api.github.com/users/ashish-vmware/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ashish-vmware/subscriptions","organizations_url":"https://api.github.com/users/ashish-vmware/orgs","repos_url":"https://api.github.com/users/ashish-vmware/repos","events_url":"https://api.github.com/users/ashish-vmware/events{/privacy}","received_events_url":"https://api.github.com/users/ashish-vmware/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/ashish-vmware/liota","description":null,"fork":true,"url":"https://api.github.com/repos/ashish-vmware/liota","forks_url":"https://api.github.com/repos/ashish-vmware/liota/forks","keys_url":"https://api.github.com/repos/ashish-vmware/liota/keys{/key_id}","collaborators_url":"https://api.github.com/repos/ashish-vmware/liota/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/ashish-vmware/liota/teams","hooks_url":"https://api.github.com/repos/ashish-vmware/liota/hooks","issue_events_url":"https://api.github.com/repos/ashish-vmware/liota/issues/events{/number}","events_url":"https://api.github.com/repos/ashish-vmware/liota/events","assignees_url":"https://api.github.com/repos/ashish-vmware/liota/assignees{/user}","branches_url":"https://api.github.com/repos/ashish-vmware/liota/branches{/branch}","tags_url":"https://api.github.com/repos/ashish-vmware/liota/tags","blobs_url":"https://api.github.com/repos/ashish-vmware/liota/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/ashish-vmware/liota/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/ashish-vmware/liota/git/refs{/sha}","trees_url":"https://api.github.com/repos/ashish-vmware/liota/git/trees{/sha}","statuses_url":"https://api.github.com/repos/ashish-vmware/liota/statuses/{sha}","languages_url":"https://api.github.com/repos/ashish-vmware/liota/languages","stargazers_url":"https://api.github.com/repos/ashish-vmware/liota/stargazers","contributors_url":"https://api.github.com/repos/ashish-vmware/liota/contributors","subscribers_url":"https://api.github.com/repos/ashish-vmware/liota/subscribers","subscription_url":"https://api.github.com/repos/ashish-vmware/liota/subscription","commits_url":"https://api.github.com/repos/ashish-vmware/liota/commits{/sha}","git_commits_url":"https://api.github.com/repos/ashish-vmware/liota/git/commits{/sha}","comments_url":"https://api.github.com/repos/ashish-vmware/liota/comments{/number}","issue_comment_url":"https://api.github.com/repos/ashish-vmware/liota/issues/comments{/number}","contents_url":"https://api.github.com/repos/ashish-vmware/liota/contents/{+path}","compare_url":"https://api.github.com/repos/ashish-vmware/liota/compare/{base}...{head}","merges_url":"https://api.github.com/repos/ashish-vmware/liota/merges","archive_url":"https://api.github.com/repos/ashish-vmware/liota/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/ashish-vmware/liota/downloads","issues_url":"https://api.github.com/repos/ashish-vmware/liota/issues{/number}","pulls_url":"https://api.github.com/repos/ashish-vmware/liota/pulls{/number}","milestones_url":"https://api.github.com/repos/ashish-vmware/liota/milestones{/number}","notifications_url":"https://api.github.com/repos/ashish-vmware/liota/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/ashish-vmware/liota/labels{/name}","releases_url":"https://api.github.com/repos/ashish-vmware/liota/releases{/id}","deployments_url":"https://api.github.com/repos/ashish-vmware/liota/deployments","created_at":"2016-07-30T13:14:07Z","updated_at":"2016-07-30T13:14:08Z","pushed_at":"2016-08-22T14:40:54Z","git_url":"git://github.com/ashish-vmware/liota.git","ssh_url":"git@github.com:ashish-vmware/liota.git","clone_url":"https://github.com/ashish-vmware/liota.git","svn_url":"https://github.com/ashish-vmware/liota","homepage":null,"size":133,"stargazers_count":0,"watchers_count":0,"language":"Python","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"diwanc:master","ref":"master","sha":"c155f6d961bc46ba1185fea27871ae66b202bbcb","user":{"login":"diwanc","id":4242007,"avatar_url":"https://avatars.githubusercontent.com/u/4242007?v=3","gravatar_id":"","url":"https://api.github.com/users/diwanc","html_url":"https://github.com/diwanc","followers_url":"https://api.github.com/users/diwanc/followers","following_url":"https://api.github.com/users/diwanc/following{/other_user}","gists_url":"https://api.github.com/users/diwanc/gists{/gist_id}","starred_url":"https://api.github.com/users/diwanc/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/diwanc/subscriptions","organizations_url":"https://api.github.com/users/diwanc/orgs","repos_url":"https://api.github.com/users/diwanc/repos","events_url":"https://api.github.com/users/diwanc/events{/privacy}","received_events_url":"https://api.github.com/users/diwanc/received_events","type":"User","site_admin":false},"repo":{"id":64391850,"name":"liota","full_name":"diwanc/liota","owner":{"login":"diwanc","id":4242007,"avatar_url":"https://avatars.githubusercontent.com/u/4242007?v=3","gravatar_id":"","url":"https://api.github.com/users/diwanc","html_url":"https://github.com/diwanc","followers_url":"https://api.github.com/users/diwanc/followers","following_url":"https://api.github.com/users/diwanc/following{/other_user}","gists_url":"https://api.github.com/users/diwanc/gists{/gist_id}","starred_url":"https://api.github.com/users/diwanc/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/diwanc/subscriptions","organizations_url":"https://api.github.com/users/diwanc/orgs","repos_url":"https://api.github.com/users/diwanc/repos","events_url":"https://api.github.com/users/diwanc/events{/privacy}","received_events_url":"https://api.github.com/users/diwanc/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/diwanc/liota","description":null,"fork":true,"url":"https://api.github.com/repos/diwanc/liota","forks_url":"https://api.github.com/repos/diwanc/liota/forks","keys_url":"https://api.github.com/repos/diwanc/liota/keys{/key_id}","collaborators_url":"https://api.github.com/repos/diwanc/liota/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/diwanc/liota/teams","hooks_url":"https://api.github.com/repos/diwanc/liota/hooks","issue_events_url":"https://api.github.com/repos/diwanc/liota/issues/events{/number}","events_url":"https://api.github.com/repos/diwanc/liota/events","assignees_url":"https://api.github.com/repos/diwanc/liota/assignees{/user}","branches_url":"https://api.github.com/repos/diwanc/liota/branches{/branch}","tags_url":"https://api.github.com/repos/diwanc/liota/tags","blobs_url":"https://api.github.com/repos/diwanc/liota/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/diwanc/liota/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/diwanc/liota/git/refs{/sha}","trees_url":"https://api.github.com/repos/diwanc/liota/git/trees{/sha}","statuses_url":"https://api.github.com/repos/diwanc/liota/statuses/{sha}","languages_url":"https://api.github.com/repos/diwanc/liota/languages","stargazers_url":"https://api.github.com/repos/diwanc/liota/stargazers","contributors_url":"https://api.github.com/repos/diwanc/liota/contributors","subscribers_url":"https://api.github.com/repos/diwanc/liota/subscribers","subscription_url":"https://api.github.com/repos/diwanc/liota/subscription","commits_url":"https://api.github.com/repos/diwanc/liota/commits{/sha}","git_commits_url":"https://api.github.com/repos/diwanc/liota/git/commits{/sha}","comments_url":"https://api.github.com/repos/diwanc/liota/comments{/number}","issue_comment_url":"https://api.github.com/repos/diwanc/liota/issues/comments{/number}","contents_url":"https://api.github.com/repos/diwanc/liota/contents/{+path}","compare_url":"https://api.github.com/repos/diwanc/liota/compare/{base}...{head}","merges_url":"https://api.github.com/repos/diwanc/liota/merges","archive_url":"https://api.github.com/repos/diwanc/liota/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/diwanc/liota/downloads","issues_url":"https://api.github.com/repos/diwanc/liota/issues{/number}","pulls_url":"https://api.github.com/repos/diwanc/liota/pulls{/number}","milestones_url":"https://api.github.com/repos/diwanc/liota/milestones{/number}","notifications_url":"https://api.github.com/repos/diwanc/liota/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/diwanc/liota/labels{/name}","releases_url":"https://api.github.com/repos/diwanc/liota/releases{/id}","deployments_url":"https://api.github.com/repos/diwanc/liota/deployments","created_at":"2016-07-28T11:58:52Z","updated_at":"2016-07-28T11:58:53Z","pushed_at":"2016-08-23T15:28:16Z","git_url":"git://github.com/diwanc/liota.git","ssh_url":"git@github.com:diwanc/liota.git","clone_url":"https://github.com/diwanc/liota.git","svn_url":"https://github.com/diwanc/liota","homepage":null,"size":153,"stargazers_count":0,"watchers_count":0,"language":"Python","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":3,"mirror_url":null,"open_issues_count":4,"forks":3,"open_issues":4,"watchers":0,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/diwanc/liota/pulls/3"},"html":{"href":"https://github.com/diwanc/liota/pull/3"},"issue":{"href":"https://api.github.com/repos/diwanc/liota/issues/3"},"comments":{"href":"https://api.github.com/repos/diwanc/liota/issues/3/comments"},"review_comments":{"href":"https://api.github.com/repos/diwanc/liota/pulls/3/comments"},"review_comment":{"href":"https://api.github.com/repos/diwanc/liota/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/diwanc/liota/pulls/3/commits"},"statuses":{"href":"https://api.github.com/repos/diwanc/liota/statuses/26fe6a326d43736d0afea701d4d8b4e50544724a"}}}}
{"actor":{"display_login":"diwanc"}}