1
0
mirror of https://github.com/netbox-community/netbox.git synced 2024-05-10 07:54:54 +00:00

Extend templatization ability to additional_headers field

This commit is contained in:
Jeremy Stretch
2020-02-24 17:47:17 -05:00
parent 1fbd3a2c26
commit 9a532b1eb2
3 changed files with 52 additions and 37 deletions

View File

@ -1,14 +1,10 @@
import json
import logging
import requests
from django_rq import job
from jinja2.exceptions import TemplateError
from rest_framework.utils.encoders import JSONEncoder
from utilities.utils import render_jinja2
from .choices import ObjectChangeActionChoices
from .constants import HTTP_CONTENT_TYPE_JSON
from .webhooks import generate_signature
logger = logging.getLogger('netbox.webhooks_worker')
@ -28,55 +24,56 @@ def process_webhook(webhook, data, model_name, event, timestamp, username, reque
'data': data
}
# Build HTTP headers
# Build the headers for the HTTP request
headers = {
'Content-Type': webhook.http_content_type,
}
if webhook.additional_headers:
headers.update(webhook.additional_headers)
try:
headers.update(webhook.render_headers(context))
except (TemplateError, ValueError) as e:
logger.error("Error parsing HTTP headers for webhook {}: {}".format(webhook, e))
raise e
# Render the request body
try:
body = webhook.render_body(context)
except TemplateError as e:
logger.error("Error rendering request body for webhook {}: {}".format(webhook, e))
raise e
# Prepare the HTTP request
params = {
'method': 'POST',
'url': webhook.payload_url,
'headers': headers
'headers': headers,
'data': body,
}
logger.info(
"Sending webhook to {}: {} {}".format(params['url'], context['model'], context['event'])
)
logger.debug(params)
try:
prepared_request = requests.Request(**params).prepare()
except requests.exceptions.RequestException as e:
logger.error("Error forming HTTP request: {}".format(e))
raise e
# Construct the request body. If a template has been defined, use it. Otherwise, dump the context as either JSON
# or form data.
if webhook.body_template:
try:
params['data'] = render_jinja2(webhook.body_template, context)
except TemplateError as e:
logger.error("Error rendering request body: {}".format(e))
return
elif webhook.http_content_type == HTTP_CONTENT_TYPE_JSON:
params['data'] = json.dumps(context, cls=JSONEncoder)
else:
params['data'] = context
prepared_request = requests.Request(**params).prepare()
# If a secret key is defined, sign the request with a hash of the key and its content
if webhook.secret != '':
# Sign the request with a hash of the secret key and its content.
prepared_request.headers['X-Hook-Signature'] = generate_signature(prepared_request.body, webhook.secret)
# Send the request
with requests.Session() as session:
session.verify = webhook.ssl_verification
if webhook.ca_file_path:
session.verify = webhook.ca_file_path
response = session.send(prepared_request)
logger.debug(params)
if 200 <= response.status_code <= 299:
logger.info("Request succeeded; response status {}".format(response.status_code))
return 'Status {} returned, webhook successfully processed.'.format(response.status_code)
else:
logger.error("Request failed; response status {}: {}".format(response.status_code, response.content))
logger.warning("Request failed; response status {}: {}".format(response.status_code, response.content))
raise requests.exceptions.RequestException(
"Status {} returned with content '{}', webhook FAILED to process.".format(
response.status_code, response.content