浏览代码

Merge pull request #3605 from netbox-community/3445-webhooks-additional-headers

implemented #3445 - Add support for additional user defined headers t…
John Anderson 6 年之前
父节点
当前提交
64575fec42

+ 1 - 2
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)

+ 19 - 0
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),
+        ),
+    ]

+ 12 - 0
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

+ 3 - 0
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,