diff --git a/docs/release-notes/version-2.6.md b/docs/release-notes/version-2.6.md index 4badcfedf..fe72331a7 100644 --- a/docs/release-notes/version-2.6.md +++ b/docs/release-notes/version-2.6.md @@ -2,12 +2,11 @@ ## Enhancements +* [#3445](https://github.com/netbox-community/netbox/issues/3445) - Add support for additional user defined headers to be added to webhook requests * [#3499](https://github.com/netbox-community/netbox/issues/3499) - Add `ca_file_path` to Webhook model to support user supplied CA certificate verification of webhook requests ## Bug Fixes - - --- # v2.6.6 (2019-10-10) diff --git a/netbox/extras/migrations/0027_webhook_additional_headers.py b/netbox/extras/migrations/0027_webhook_additional_headers.py new file mode 100644 index 000000000..8b1f04f19 --- /dev/null +++ b/netbox/extras/migrations/0027_webhook_additional_headers.py @@ -0,0 +1,19 @@ +# Generated by Django 2.2 on 2019-10-13 07:06 + +import django.contrib.postgres.fields.jsonb +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('extras', '0026_webhook_ca_file_path'), + ] + + operations = [ + migrations.AddField( + model_name='webhook', + name='additional_headers', + field=django.contrib.postgres.fields.jsonb.JSONField(blank=True, null=True), + ), + ] diff --git a/netbox/extras/models.py b/netbox/extras/models.py index 4e8a56b34..ea71cf95e 100644 --- a/netbox/extras/models.py +++ b/netbox/extras/models.py @@ -70,6 +70,12 @@ class Webhook(models.Model): default=WEBHOOK_CT_JSON, verbose_name='HTTP content type' ) + additional_headers = JSONField( + null=True, + blank=True, + help_text="User supplied headers which should be added to the request in addition to the HTTP content type. " + "Headers are supplied as key/value pairs in a JSON object." + ) secret = models.CharField( max_length=255, blank=True, @@ -115,6 +121,12 @@ class Webhook(models.Model): 'ca_file_path': 'Do not specify a CA certificate file if SSL verification is dissabled.' }) + # Verify that JSON data is provided as an object + if self.additional_headers and type(self.additional_headers) is not dict: + raise ValidationError({ + 'additional_headers': 'Header JSON data must be in object form. Example: {"X-API-KEY": "abc123"}' + }) + # # Custom fields diff --git a/netbox/extras/webhooks_worker.py b/netbox/extras/webhooks_worker.py index c50a0a368..9a637e852 100644 --- a/netbox/extras/webhooks_worker.py +++ b/netbox/extras/webhooks_worker.py @@ -25,6 +25,9 @@ def process_webhook(webhook, data, model_name, event, timestamp, username, reque headers = { 'Content-Type': webhook.get_http_content_type_display(), } + if webhook.additional_headers: + headers.update(webhook.additional_headers) + params = { 'method': 'POST', 'url': webhook.payload_url,