Skip to content

Commit 85ab7ad

Browse files
committed
Closes #14395: Move & rename process_webhook()
1 parent 4fc0a99 commit 85ab7ad

File tree

4 files changed

+97
-89
lines changed

4 files changed

+97
-89
lines changed

netbox/extras/events.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ def process_event_rules(event_rules, model_name, event, data, username, snapshot
108108

109109
# Enqueue the task
110110
rq_queue.enqueue(
111-
"extras.webhooks_worker.process_webhook",
111+
"extras.webhooks.send_webhook",
112112
**params
113113
)
114114

netbox/extras/tests/test_event_rules.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111
from extras.choices import EventRuleActionChoices, ObjectChangeActionChoices
1212
from extras.events import enqueue_object, flush_events, serialize_for_event
1313
from extras.models import EventRule, Tag, Webhook
14-
from extras.webhooks import generate_signature
15-
from extras.webhooks_worker import process_webhook
14+
from extras.webhooks import generate_signature, send_webhook
1615
from requests import Session
1716
from rest_framework import status
1817
from utilities.testing import APITestCase
@@ -331,7 +330,7 @@ def test_bulk_delete_process_eventrule(self):
331330
self.assertEqual(job.kwargs['snapshots']['prechange']['name'], sites[i].name)
332331
self.assertEqual(job.kwargs['snapshots']['prechange']['tags'], ['Bar', 'Foo'])
333332

334-
def test_webhooks_worker(self):
333+
def test_send_webhook(self):
335334
request_id = uuid.uuid4()
336335

337336
def dummy_send(_, request, **kwargs):
@@ -376,4 +375,4 @@ def dummy_send(_, request, **kwargs):
376375

377376
# Patch the Session object with our dummy_send() method, then process the webhook for sending
378377
with patch.object(Session, 'send', dummy_send) as mock_send:
379-
process_webhook(**job.kwargs)
378+
send_webhook(**job.kwargs)

netbox/extras/webhooks.py

+86
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
import hashlib
22
import hmac
3+
import logging
4+
5+
import requests
6+
from django.conf import settings
7+
from django_rq import job
8+
from jinja2.exceptions import TemplateError
9+
10+
from .constants import WEBHOOK_EVENT_TYPES
11+
12+
logger = logging.getLogger('netbox.webhooks')
313

414

515
def generate_signature(request_body, secret):
@@ -12,3 +22,79 @@ def generate_signature(request_body, secret):
1222
digestmod=hashlib.sha512
1323
)
1424
return hmac_prep.hexdigest()
25+
26+
27+
@job('default')
28+
def send_webhook(event_rule, model_name, event, data, timestamp, username, request_id=None, snapshots=None):
29+
"""
30+
Make a POST request to the defined Webhook
31+
"""
32+
webhook = event_rule.action_object
33+
34+
# Prepare context data for headers & body templates
35+
context = {
36+
'event': WEBHOOK_EVENT_TYPES[event],
37+
'timestamp': timestamp,
38+
'model': model_name,
39+
'username': username,
40+
'request_id': request_id,
41+
'data': data,
42+
}
43+
if snapshots:
44+
context.update({
45+
'snapshots': snapshots
46+
})
47+
48+
# Build the headers for the HTTP request
49+
headers = {
50+
'Content-Type': webhook.http_content_type,
51+
}
52+
try:
53+
headers.update(webhook.render_headers(context))
54+
except (TemplateError, ValueError) as e:
55+
logger.error(f"Error parsing HTTP headers for webhook {webhook}: {e}")
56+
raise e
57+
58+
# Render the request body
59+
try:
60+
body = webhook.render_body(context)
61+
except TemplateError as e:
62+
logger.error(f"Error rendering request body for webhook {webhook}: {e}")
63+
raise e
64+
65+
# Prepare the HTTP request
66+
params = {
67+
'method': webhook.http_method,
68+
'url': webhook.render_payload_url(context),
69+
'headers': headers,
70+
'data': body.encode('utf8'),
71+
}
72+
logger.info(
73+
f"Sending {params['method']} request to {params['url']} ({context['model']} {context['event']})"
74+
)
75+
logger.debug(params)
76+
try:
77+
prepared_request = requests.Request(**params).prepare()
78+
except requests.exceptions.RequestException as e:
79+
logger.error(f"Error forming HTTP request: {e}")
80+
raise e
81+
82+
# If a secret key is defined, sign the request with a hash of the key and its content
83+
if webhook.secret != '':
84+
prepared_request.headers['X-Hook-Signature'] = generate_signature(prepared_request.body, webhook.secret)
85+
86+
# Send the request
87+
with requests.Session() as session:
88+
session.verify = webhook.ssl_verification
89+
if webhook.ca_file_path:
90+
session.verify = webhook.ca_file_path
91+
response = session.send(prepared_request, proxies=settings.HTTP_PROXIES)
92+
93+
if 200 <= response.status_code <= 299:
94+
logger.info(f"Request succeeded; response status {response.status_code}")
95+
return f"Status {response.status_code} returned, webhook successfully processed."
96+
else:
97+
logger.warning(f"Request failed; response status {response.status_code}: {response.content}")
98+
raise requests.exceptions.RequestException(
99+
f"Status {response.status_code} returned with content '{response.content}', webhook FAILED to process."
100+
)

netbox/extras/webhooks_worker.py

+7-84
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,10 @@
1-
import logging
1+
import warnings
22

3-
import requests
4-
from django.conf import settings
5-
from django_rq import job
6-
from jinja2.exceptions import TemplateError
3+
from .webhooks import send_webhook as process_webhook
74

8-
from .constants import WEBHOOK_EVENT_TYPES
9-
from .webhooks import generate_signature
105

11-
logger = logging.getLogger('netbox.webhooks_worker')
12-
13-
14-
@job('default')
15-
def process_webhook(event_rule, model_name, event, data, timestamp, username, request_id=None, snapshots=None):
16-
"""
17-
Make a POST request to the defined Webhook
18-
"""
19-
webhook = event_rule.action_object
20-
21-
# Prepare context data for headers & body templates
22-
context = {
23-
'event': WEBHOOK_EVENT_TYPES[event],
24-
'timestamp': timestamp,
25-
'model': model_name,
26-
'username': username,
27-
'request_id': request_id,
28-
'data': data,
29-
}
30-
if snapshots:
31-
context.update({
32-
'snapshots': snapshots
33-
})
34-
35-
# Build the headers for the HTTP request
36-
headers = {
37-
'Content-Type': webhook.http_content_type,
38-
}
39-
try:
40-
headers.update(webhook.render_headers(context))
41-
except (TemplateError, ValueError) as e:
42-
logger.error(f"Error parsing HTTP headers for webhook {webhook}: {e}")
43-
raise e
44-
45-
# Render the request body
46-
try:
47-
body = webhook.render_body(context)
48-
except TemplateError as e:
49-
logger.error(f"Error rendering request body for webhook {webhook}: {e}")
50-
raise e
51-
52-
# Prepare the HTTP request
53-
params = {
54-
'method': webhook.http_method,
55-
'url': webhook.render_payload_url(context),
56-
'headers': headers,
57-
'data': body.encode('utf8'),
58-
}
59-
logger.info(
60-
f"Sending {params['method']} request to {params['url']} ({context['model']} {context['event']})"
61-
)
62-
logger.debug(params)
63-
try:
64-
prepared_request = requests.Request(**params).prepare()
65-
except requests.exceptions.RequestException as e:
66-
logger.error(f"Error forming HTTP request: {e}")
67-
raise e
68-
69-
# If a secret key is defined, sign the request with a hash of the key and its content
70-
if webhook.secret != '':
71-
prepared_request.headers['X-Hook-Signature'] = generate_signature(prepared_request.body, webhook.secret)
72-
73-
# Send the request
74-
with requests.Session() as session:
75-
session.verify = webhook.ssl_verification
76-
if webhook.ca_file_path:
77-
session.verify = webhook.ca_file_path
78-
response = session.send(prepared_request, proxies=settings.HTTP_PROXIES)
79-
80-
if 200 <= response.status_code <= 299:
81-
logger.info(f"Request succeeded; response status {response.status_code}")
82-
return f"Status {response.status_code} returned, webhook successfully processed."
83-
else:
84-
logger.warning(f"Request failed; response status {response.status_code}: {response.content}")
85-
raise requests.exceptions.RequestException(
86-
f"Status {response.status_code} returned with content '{response.content}', webhook FAILED to process."
87-
)
6+
# TODO: Remove in v4.0
7+
warnings.warn(
8+
f"webhooks_worker.process_webhook has been moved to webhooks.send_webhook.",
9+
DeprecationWarning
10+
)

0 commit comments

Comments
 (0)