Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Webhooks raising exception in v2.7.1 #3951

Closed
candlerb opened this issue Jan 17, 2020 · 1 comment
Closed

Webhooks raising exception in v2.7.1 #3951

candlerb opened this issue Jan 17, 2020 · 1 comment
Labels
status: accepted This issue has been accepted for implementation type: bug A confirmed report of unexpected behavior in the application

Comments

@candlerb
Copy link
Contributor

A previously-working webhook has stopped working in 2.7

Environment

  • Python version: 3.5.2
  • NetBox version: 2.7.1

Steps to Reproduce

  1. Running webhook - mine is to netbox-webhook-dnsupdate
  2. Change something which should trigger the webhook
  3. Check backend status at /admin/webhook-backend-status/
  4. Click on the "Failed Jobs" number

Expected Behavior

Webhook to work as before

Observed Behavior

Shown in web interface backend status for the job:

Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/rq/worker.py", line 812, in perform_job
    rv = job.perform()
  File "/usr/local/lib/python3.5/dist-packages/rq/job.py", line 588, in perform
    self._result = self._execute()
  File "/usr/local/lib/python3.5/dist-packages/rq/job.py", line 594, in _execute
    return self.func(*self.args, **self.kwargs)
  File "/opt/netbox/netbox/extras/webhooks_worker.py", line 49, in process_webhook
    msg=prepared_request.body.encode('utf8'),
AttributeError: 'NoneType' object has no attribute 'encode'
@candlerb
Copy link
Contributor Author

candlerb commented Jan 17, 2020

If you make this patch:

--- /opt/netbox/netbox/extras/webhooks_worker.py.orig	2020-01-17 09:06:49.290100942 +0000
+++ /opt/netbox/netbox/extras/webhooks_worker.py	2020-01-17 14:38:50.299963724 +0000
@@ -39,6 +39,8 @@
         params.update({'data': json.dumps(payload, cls=JSONEncoder)})
     elif webhook.http_content_type == WEBHOOK_CT_X_WWW_FORM_ENCODED:
         params.update({'data': payload})
+    else:
+        raise RuntimeError("Boom")

     prepared_request = requests.Request(**params).prepare()

Then the exception changes to "Boom".

In the database I have:

netbox=# select name,http_content_type from extras_webhook;
   name    | http_content_type
-----------+-------------------
 dnsupdate | application/json
(1 row)

(and the web interface displays it as "JSON" in the drop-down)

This seems to fix it:

--- /opt/netbox/netbox/extras/webhooks_worker.py.orig	2020-01-17 09:06:49.290100942 +0000
+++ /opt/netbox/netbox/extras/webhooks_worker.py	2020-01-17 14:48:00.065982061 +0000
@@ -6,8 +6,7 @@
 from django_rq import job
 from rest_framework.utils.encoders import JSONEncoder

-from .choices import ObjectChangeActionChoices
-from .constants import *
+from .choices import ObjectChangeActionChoices, WebhookContentTypeChoices


 @job('default')
@@ -35,10 +34,12 @@
         'headers': headers
     }

-    if webhook.http_content_type == WEBHOOK_CT_JSON:
+    if webhook.http_content_type == WebhookContentTypeChoices.CONTENTTYPE_JSON:
         params.update({'data': json.dumps(payload, cls=JSONEncoder)})
-    elif webhook.http_content_type == WEBHOOK_CT_X_WWW_FORM_ENCODED:
+    elif webhook.http_content_type == WebhookContentTypeChoices.CONTENTTYPE_FORMDATA:
         params.update({'data': payload})
+    else:
+        raise RuntimeError("Boom")

     prepared_request = requests.Request(**params).prepare()

I'm somewhat fazed as to why the non-existent constants WEBHOOK_CT_JSON and WEBHOOK_CT_X_WWW_FORM_ENCODED didn't raise an exception in the first place. I had restarted Netbox after the upgrade - ah, but I didn't restart rqworker. I am guessing that when I added raise RuntimeError("Boom") it re-imported the source module automatically, but kept the old constants.

@jeremystretch jeremystretch added status: accepted This issue has been accepted for implementation type: bug A confirmed report of unexpected behavior in the application labels Jan 17, 2020
@jeremystretch jeremystretch pinned this issue Jan 17, 2020
@jeremystretch jeremystretch changed the title Webhooks raising exception in 2.7 Webhooks raising exception in v2.7.1 Jan 17, 2020
@jeremystretch jeremystretch unpinned this issue Jan 21, 2020
@lock lock bot locked as resolved and limited conversation to collaborators Apr 17, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
status: accepted This issue has been accepted for implementation type: bug A confirmed report of unexpected behavior in the application
Projects
None yet
Development

No branches or pull requests

2 participants