-
Notifications
You must be signed in to change notification settings - Fork 264
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* webhook sink
- Loading branch information
Showing
9 changed files
with
126 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import textwrap | ||
|
||
import requests | ||
from typing import List | ||
|
||
from .webhook_sink_params import WebhookSinkConfigWrapper | ||
from ..transformer import Transformer | ||
from ...reporting import HeaderBlock, ListBlock, JsonBlock, KubernetesDiffBlock, MarkdownBlock | ||
from ...reporting.base import Finding, BaseBlock | ||
from ..sink_base import SinkBase | ||
|
||
|
||
class WebhookSink(SinkBase): | ||
def __init__(self, sink_config: WebhookSinkConfigWrapper, cluster_name: str): | ||
super().__init__(sink_config.webhook_sink) | ||
self.cluster_name = cluster_name | ||
self.url = sink_config.webhook_sink.url | ||
self.size_limit = sink_config.webhook_sink.size_limit | ||
|
||
def write_finding(self, finding: Finding, platform_enabled: bool): | ||
message_lines: List[str] = [finding.title] | ||
if platform_enabled: | ||
message_lines.append(f"Investigate: {finding.investigate_uri}") | ||
message_lines.append(f"Source: {self.cluster_name}") | ||
message_lines.append(finding.description) | ||
|
||
message = "" | ||
|
||
for enrichment in finding.enrichments: | ||
for block in enrichment.blocks: | ||
message_lines.extend(self.__to_unformatted_text(block)) | ||
|
||
for line in [line for line in message_lines if line]: | ||
wrapped = textwrap.dedent( | ||
f""" | ||
{line} | ||
""" | ||
) | ||
if len(message) + len(wrapped) >= self.size_limit: | ||
break | ||
message += wrapped | ||
|
||
requests.post(self.url, data=message) | ||
|
||
@classmethod | ||
def __to_clear_text(cls, markdown_text: str) -> str: | ||
# just create a readable links format | ||
links = Transformer.get_markdown_links(markdown_text) | ||
for link in links: | ||
# take only the data between the first '<' and last '>' | ||
splits = link[1:-1].split("|") | ||
if len(splits) == 2: # don't replace unexpected strings | ||
replacement = f"{splits[1]}: {splits[0]}" | ||
markdown_text = markdown_text.replace(link, replacement) | ||
|
||
return markdown_text | ||
|
||
def __to_unformatted_text(cls, block: BaseBlock) -> List[str]: | ||
lines = [] | ||
if isinstance(block, HeaderBlock): | ||
lines.append(block.text) | ||
elif isinstance(block, ListBlock): | ||
lines.extend([cls.__to_clear_text(item) for item in block.items]) | ||
elif isinstance(block, MarkdownBlock): | ||
lines.append(cls.__to_clear_text(block.text)) | ||
elif isinstance(block, JsonBlock): | ||
lines.append(block.json_str) | ||
elif isinstance(block, KubernetesDiffBlock): | ||
for diff in block.diffs: | ||
lines.append(f"*{'.'.join(diff.path)}*: {diff.other_value} ==> {diff.value}") | ||
return lines |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from ..sink_config import SinkConfigBase | ||
from ..sink_base_params import SinkBaseParams | ||
|
||
|
||
class WebhookSinkParams(SinkBaseParams): | ||
url: str | ||
size_limit: int = 4096 | ||
|
||
|
||
class WebhookSinkConfigWrapper(SinkConfigBase): | ||
webhook_sink: WebhookSinkParams | ||
|
||
def get_params(self) -> SinkBaseParams: | ||
return self.webhook_sink | ||
|