Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 2308174

Browse files
committedFeb 21, 2025·
webhook: support adding request headers to the message
Signed-off-by: László Várady <laszlo.varady@anno.io>
1 parent be31729 commit 2308174

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed
 

‎modules/python-modules/syslogng/modules/webhook/scl/webhook.conf

+2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ block source webhook(
3333
tls_ca_file("")
3434
tls_ca_dir("")
3535
behind_proxy(no)
36+
include_request_headers(no)
3637
...
3738
)
3839
{
@@ -50,6 +51,7 @@ block source webhook(
5051
"tls_ca_file" => "`tls_ca_file`"
5152
"tls_ca_dir" => "`tls_ca_dir`"
5253
"behind_proxy" => `behind_proxy`
54+
"include_request_headers" => `include_request_headers`
5355
)
5456
`__VARARGS__`
5557
);

‎modules/python-modules/syslogng/modules/webhook/source.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,14 @@
2828
import tornado
2929
import ssl
3030
import signal
31+
import json
3132
from typing import Any
3233

3334
signal.signal(signal.SIGINT, signal.SIG_IGN)
3435
signal.signal(signal.SIGTERM, signal.SIG_IGN)
35-
WEBHOOK_QUERY_NV_PREFIX = "webhook.query."
36+
WEBHOOK_NV_PREFIX = "webhook."
37+
WEBHOOK_QUERY_NV_PREFIX = WEBHOOK_NV_PREFIX + "query."
38+
WEBHOOK_HEADERS_KEY = WEBHOOK_NV_PREFIX + "headers"
3639

3740
WEBHOOK_PROXY_SOURCEIP_HEADERS = [
3841
"x-forwarded-for",
@@ -45,6 +48,7 @@
4548
"x-forwarded",
4649
]
4750

51+
4852
class Handler(tornado.web.RequestHandler):
4953
def initialize(self, source) -> None:
5054
self.source = source
@@ -69,7 +73,7 @@ def _set_proxied_ip(self, msg: LogMessage) -> None:
6973
if header and len(header) > 0:
7074
# the closest/last IP (the behind_proxy flag implies that the last one can be trusted)
7175
msg.set_source_ipaddress(header[-1])
72-
msg["webhook.proxy_ip"] = self.request.remote_ip
76+
msg[WEBHOOK_NV_PREFIX + "proxy_ip"] = self.request.remote_ip
7377
return
7478

7579
msg.set_source_ipaddress(self.request.remote_ip)
@@ -85,6 +89,9 @@ def _construct_msg(self, request, path_arguments) -> LogMessage:
8589
for key, value in path_arguments.items():
8690
msg[key] = value
8791

92+
if self.source.include_request_headers:
93+
msg[WEBHOOK_HEADERS_KEY] = json.dumps(list(self.request.headers.get_all()))
94+
8895
if self.source.behind_proxy:
8996
self._set_proxied_ip(msg)
9097
else:
@@ -211,6 +218,7 @@ def init_options(self, options: dict[str, Any]) -> bool:
211218
self.tls_ca_dir = options.get("tls_ca_dir")
212219

213220
self.behind_proxy = bool(options.get("behind_proxy", False))
221+
self.include_request_headers = bool(options.get("include_request_headers", False))
214222
return True
215223
except KeyError as e:
216224
self.logger.error(f"Missing option '{e.args[0]}'")

0 commit comments

Comments
 (0)
Please sign in to comment.