Skip to content

Commit

Permalink
webhook: support adding request headers to the message
Browse files Browse the repository at this point in the history
Signed-off-by: László Várady <[email protected]>
  • Loading branch information
MrAnno committed Feb 28, 2025
1 parent 29fc942 commit 9295855
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ block source webhook(
tls_ca_file("")
tls_ca_dir("")
proxy_header("")
include_request_headers(no)
...
)
{
Expand All @@ -50,6 +51,7 @@ block source webhook(
"tls_ca_file" => "`tls_ca_file`"
"tls_ca_dir" => "`tls_ca_dir`"
"proxy_header" => "`proxy_header`"
"include_request_headers" => `include_request_headers`
)
`__VARARGS__`
);
Expand Down
21 changes: 20 additions & 1 deletion modules/python-modules/syslogng/modules/webhook/source.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,23 @@
#############################################################################

from syslogng import LogSource, LogMessage
from collections import defaultdict

import logging
import asyncio
import threading
import tornado
import ssl
import signal
import json
from typing import Any

signal.signal(signal.SIGINT, signal.SIG_IGN)
signal.signal(signal.SIGTERM, signal.SIG_IGN)
WEBHOOK_QUERY_NV_PREFIX = "webhook.query."
WEBHOOK_NV_PREFIX = "webhook."
WEBHOOK_QUERY_NV_PREFIX = WEBHOOK_NV_PREFIX + "query."
WEBHOOK_HEADERS_KEY = WEBHOOK_NV_PREFIX + "headers"



class Handler(tornado.web.RequestHandler):
Expand Down Expand Up @@ -63,6 +68,15 @@ def _set_proxied_ip(self, msg: LogMessage) -> None:

msg.set_source_ipaddress(self.request.remote_ip)

def _set_request_headers(self, msg: LogMessage) -> None:
headers = defaultdict(list)
for h in self.request.headers.get_all():
name = h[0]
if name:
headers[name].append(h[1])

msg[WEBHOOK_HEADERS_KEY] = json.dumps(headers)

def _construct_msg(self, request, path_arguments) -> LogMessage:
msg = LogMessage(self.request.body)
msg.set_recvd_rawmsg_size(len(self.request.body))
Expand All @@ -74,6 +88,9 @@ def _construct_msg(self, request, path_arguments) -> LogMessage:
for key, value in path_arguments.items():
msg[key] = value

if self.source.include_request_headers:
self._set_request_headers(msg)

if self.source.proxy_header:
self._set_proxied_ip(msg)
else:
Expand Down Expand Up @@ -203,6 +220,8 @@ def init_options(self, options: dict[str, Any]) -> bool:
if self.proxy_header == "yes":
self.proxy_header = "x-forwarded-for"

self.include_request_headers = bool(options.get("include_request_headers", False))

return True
except KeyError as e:
self.logger.error(f"Missing option '{e.args[0]}'")
Expand Down

0 comments on commit 9295855

Please sign in to comment.