Skip to content

Commit

Permalink
Added option to toggle "verify ssl certificate" for request endpoints (
Browse files Browse the repository at this point in the history
…resolves #10) (#11)

* Add setting to turn off SSL verification

* Bumped version number for release
  • Loading branch information
derekantrican authored Aug 14, 2023
1 parent d47d163 commit db8f79d
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 9 deletions.
30 changes: 22 additions & 8 deletions octoprint_webhooks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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(
Expand All @@ -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",
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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"])
Expand Down Expand Up @@ -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
Expand All @@ -673,15 +687,15 @@ 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
headers = check_for_header(headers, "content-type", "application/json")
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
Expand All @@ -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)

Expand Down
1 change: 1 addition & 0 deletions octoprint_webhooks/static/js/webhooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
6 changes: 6 additions & 0 deletions octoprint_webhooks/templates/webhooks_settings.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,12 @@
For a list of all possible parameters, see the
<a target="_blank" href="https://github.com/derekantrican/OctoPrint-Webhooks#advanced-configuration">README</a>.
</div>
<div class="controls" style="margin-bottom: 20px">
<input type="checkbox" data-bind="checked: verify_ssl">
<span class="control-description2">
Verify SSL (https) certificate
</span>
</div>
<label class="control-label">{{ _('HEADERS') }}</label>
<div class="controls">
<button type="button" class="input-button" data-bind="click: $root.resetHeadersToDefaults">Reset to Defaults</button>
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit db8f79d

Please sign in to comment.