From db8f79dd3cccf6d2efa74c12a3b50f95f7d0a827 Mon Sep 17 00:00:00 2001 From: Derek Antrican Date: Mon, 14 Aug 2023 07:43:06 -0700 Subject: [PATCH] Added option to toggle "verify ssl certificate" for request endpoints (resolves #10) (#11) * Add setting to turn off SSL verification * Bumped version number for release --- octoprint_webhooks/__init__.py | 30 ++++++++++++++----- octoprint_webhooks/static/js/webhooks.js | 1 + .../templates/webhooks_settings.jinja2 | 6 ++++ setup.py | 2 +- 4 files changed, 30 insertions(+), 9 deletions(-) diff --git a/octoprint_webhooks/__init__.py b/octoprint_webhooks/__init__.py index 5dc8c72..9349861 100644 --- a/octoprint_webhooks/__init__.py +++ b/octoprint_webhooks/__init__.py @@ -197,7 +197,7 @@ def migrate_settings(self): "eventUserActionNeededMessage", "eventPrintProgressMessage", "eventErrorMessage", "headers", "data", "http_method", "content_type", "oauth", "oauth_url", "oauth_headers", "oauth_data", "oauth_http_method", "oauth_content_type", "test_event", "webhook_enabled", - "event_cooldown"] + "event_cooldown", "verify_ssl"] hooks = self._settings.get(["hooks"]) hook = dict() @@ -237,7 +237,18 @@ def migrate_settings(self): self._settings.set(["hooks"], hooks) self._settings.set(["settings_version"], 4) self._settings.save() + + if self._settings.get(["settings_version"]) == 4: + self._logger.info("Migrating settings from v4 to v5") + hooks = self._settings.get(["hooks"]) + for hook_index in range(0, len(hooks)): + hook = hooks[hook_index] + hook["verify_ssl"] = True + + self._settings.set(["hooks"], hooks) + self._settings.set(["settings_version"], 5) + self._settings.save() def get_settings_defaults(self): return dict( @@ -261,6 +272,7 @@ def get_settings_defaults(self): eventPrintProgressMessage = "Your print is @percentCompleteMilestone % complete.", eventErrorMessage = "There was an error.", customEvents = [], + verify_ssl = True, headers = '{\n "Content-Type":"application/json"\n}', data = '{\n "deviceIdentifier":"@deviceIdentifier",\n "apiSecret":"@apiSecret",\n "topic":"@topic",\n "message":"@message",\n "extra":"@extra",\n "state": "@state",\n "job": "@job",\n "progress": "@progress",\n "currentZ": "@currentZ",\n "offsets": "@offsets",\n "meta": "@meta",\n "currentTime": "@currentTime",\n "snapshot": "@snapshot"\n}', http_method = "POST", @@ -509,6 +521,7 @@ def on_event(self, event, payload): parsed_oauth_headers = 0 try: # 1.1) Get the request data and headers + verify_ssl = hook["verify_ssl"] oauth_url = hook["oauth_url"] oauth_headers = json.loads(hook["oauth_headers"]) parsed_oauth_headers = 1 @@ -522,20 +535,20 @@ def on_event(self, event, payload): response = "" if oauth_http_method == "GET": - response = requests.get(oauth_url, params=oauth_data, headers=oauth_headers) + response = requests.get(oauth_url, params=oauth_data, headers=oauth_headers, verify=verify_ssl) else: if oauth_content_type == "JSON": # Make sure the Content-Type header is set to application/json oauth_headers = check_for_header(oauth_headers, "content-type", "application/json") # self._logger.info("oauth headers: " + json.dumps(oauth_headers) + " - data: " + json.dumps(oauth_data)) # self._logger.info("oauth_http_method: " + oauth_http_method + " - oauth_content_type: " + oauth_content_type) - response = requests.request(oauth_http_method, oauth_url, json=oauth_data, headers=oauth_headers, timeout=30) + response = requests.request(oauth_http_method, oauth_url, json=oauth_data, headers=oauth_headers, timeout=30, verify=verify_ssl) else: # Make sure the Content-Type header is set to application/x-www-form-urlencoded oauth_headers = check_for_header(oauth_headers, "content-type", "application/x-www-form-urlencoded") # self._logger.info("oauth headers: " + json.dumps(oauth_headers) + " - data: " + json.dumps(oauth_data)) # self._logger.info("oauth_http_method: " + oauth_http_method + " - oauth_content_type: " + oauth_content_type) - response = requests.request(oauth_http_method, oauth_url, data=oauth_data, headers=oauth_headers, timeout=30) + response = requests.request(oauth_http_method, oauth_url, data=oauth_data, headers=oauth_headers, timeout=30, verify=verify_ssl) # 1.3) Check to make sure we got a valid response code. self._logger.info("OAuth Response: " + " - " + response.text) @@ -579,6 +592,7 @@ def on_event(self, event, payload): url = hook["url"] api_secret = hook["apiSecret"] device_identifier = hook["deviceIdentifier"] + verify_ssl = hook["verify_ssl"] headers = json.loads(hook["headers"]) parsed_headers = 1 data = json.loads(hook["data"]) @@ -650,7 +664,7 @@ def on_event(self, event, payload): response = "" if http_method == "GET": # Note: we can't upload a file with GET. - response = requests.get(url, params=data, headers=headers, timeout=10) + response = requests.get(url, params=data, headers=headers, timeout=10, verify=verify_ssl) else: if try_to_upload_file: # Delete the Content-Type header if provided so that requests can set it on its own @@ -673,7 +687,7 @@ def on_event(self, event, payload): } # No timeout when uploading file as this could take some time. - response = requests.request(http_method, url, files=files, data=data, headers=headers) + response = requests.request(http_method, url, files=files, data=data, headers=headers, verify=verify_ssl) elif content_type == "JSON": # Make sure the Content-Type header is set to application/json @@ -681,7 +695,7 @@ def on_event(self, event, payload): self._logger.info("headers: " + json.dumps(headers)) self._logger.info("data: " + json.dumps(data)) self._logger.info("http_method: " + http_method + " - content_type: " + content_type) - response = requests.request(http_method, url, json=data, headers=headers, timeout=30) + response = requests.request(http_method, url, json=data, headers=headers, timeout=30, verify=verify_ssl) else: # Make sure the Content-Type header is set to application/x-www-form-urlencoded @@ -691,7 +705,7 @@ def on_event(self, event, payload): self._logger.info("headers: " + json.dumps(headers)) self._logger.info("data: " + json.dumps(data)) self._logger.info("http_method: " + http_method + " - content_type: " + content_type) - response = requests.request(http_method, url, data=data, headers=headers, timeout=30) + response = requests.request(http_method, url, data=data, headers=headers, timeout=30, verify=verify_ssl) self._logger.info("Response: " + response.text) diff --git a/octoprint_webhooks/static/js/webhooks.js b/octoprint_webhooks/static/js/webhooks.js index d708ce1..864f6ad 100644 --- a/octoprint_webhooks/static/js/webhooks.js +++ b/octoprint_webhooks/static/js/webhooks.js @@ -213,6 +213,7 @@ $(function() { 'customEvents': ko.computed(() => []), + 'verify_ssl': ko.observable(true), 'headers': ko.observable('{\n "Content-Type": "application/json"\n}'), 'data': ko.observable('{\n "deviceIdentifier":"@deviceIdentifier",\n "apiSecret":"@apiSecret",\n "topic":"@topic",\n "message":"@message",\n "extra":"@extra",\n "state": "@state",\n "job": "@job",\n "progress": "@progress",\n "currentZ": "@currentZ",\n "offsets": "@offsets",\n "meta": "@meta",\n "currentTime": "@currentTime",\n "snapshot": "@snapshot"\n}'), 'http_method': ko.observable("POST"), diff --git a/octoprint_webhooks/templates/webhooks_settings.jinja2 b/octoprint_webhooks/templates/webhooks_settings.jinja2 index 23f6c43..945a36f 100644 --- a/octoprint_webhooks/templates/webhooks_settings.jinja2 +++ b/octoprint_webhooks/templates/webhooks_settings.jinja2 @@ -419,6 +419,12 @@ For a list of all possible parameters, see the README. +
+ + + Verify SSL (https) certificate + +
diff --git a/setup.py b/setup.py index f09c445..21849e1 100644 --- a/setup.py +++ b/setup.py @@ -14,7 +14,7 @@ plugin_name = "OctoPrint-Webhooks" # The plugin's version. Can be overwritten within OctoPrint's internal data via __plugin_version__ in the plugin module -plugin_version = "4.1.0" +plugin_version = "4.2.0" # The plugin's description. Can be overwritten within OctoPrint's internal data via __plugin_description__ in the plugin # module