Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(json-webhook): Add json option to webhook sink #1185

Merged
merged 2 commits into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions src/robusta/core/sinks/webhook/webhook_sink.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
import logging
import textwrap
from typing import List
Expand All @@ -16,6 +17,7 @@ def __init__(self, sink_config: WebhookSinkConfigWrapper, registry):
super().__init__(sink_config.webhook_sink, registry)

self.url = sink_config.webhook_sink.url
self.format = sink_config.webhook_sink.format
self.headers = (
{"Authorization": sink_config.webhook_sink.authorization.get_secret_value()}
if sink_config.webhook_sink.authorization
Expand All @@ -24,6 +26,14 @@ def __init__(self, sink_config: WebhookSinkConfigWrapper, registry):
self.size_limit = sink_config.webhook_sink.size_limit

def write_finding(self, finding: Finding, platform_enabled: bool):
if self.format == "text":
self.__write_text(finding, platform_enabled)
elif self.format == "json":
self.__write_json(finding, platform_enabled)
else:
logging.exception(f"Webhook format {self.format} is not supported")

def __write_text(self, finding: Finding, platform_enabled: bool):
message_lines: List[str] = [finding.title]
if platform_enabled:
message_lines.append(f"Investigate: {finding.get_investigate_uri(self.account_id, self.cluster_name)}")
Expand Down Expand Up @@ -61,6 +71,33 @@ def write_finding(self, finding: Finding, platform_enabled: bool):
except Exception:
logging.exception(f"Webhook request error\n headers: \n{self.headers}")

def __write_json(self, finding: Finding, platform_enabled: bool):
finding_dict = json.loads(json.dumps(finding, default=lambda o: getattr(o, '__dict__', str(o))))

if platform_enabled:
finding_dict["investigate"] = finding.get_investigate_uri(self.account_id, self.cluster_name)

if finding.add_silence_url:
finding_dict["silence"] = finding.get_prometheus_silence_url(self.account_id, self.cluster_name)

message = {}
message_length = 0

for key, value in finding_dict.items():
pair_length = len(json.dumps({key: value}).encode('utf-8'))

if message_length + pair_length <= self.size_limit:
message[key] = value
message_length += pair_length
else:
break

try:
r = requests.post(self.url, json=message, headers=self.headers)
r.raise_for_status()
except Exception:
logging.exception(f"Webhook request error\n headers: \n{self.headers}")

@classmethod
def __to_clear_text(cls, markdown_text: str) -> str:
# just create a readable links format
Expand Down
1 change: 1 addition & 0 deletions src/robusta/core/sinks/webhook/webhook_sink_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class WebhookSinkParams(SinkBaseParams):
url: str
size_limit: int = 4096
authorization: SecretStr = None
format: str = "text"


class WebhookSinkConfigWrapper(SinkConfigBase):
Expand Down
Loading