Skip to content

Commit 2d58940

Browse files
committed
added check for holmes_slackbot enabled - pre-test
1 parent dad0c24 commit 2d58940

3 files changed

Lines changed: 47 additions & 4 deletions

File tree

src/robusta/core/sinks/robusta/robusta_sink.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,14 @@
4242
from robusta.integrations.receiver import ActionRequestReceiver
4343
from robusta.runner.web_api import WebApi
4444
from robusta.utils.stack_tracer import StackTracer
45+
from robusta.core.model.env_vars import ROBUSTA_API_ENDPOINT
46+
from cachetools import TTLCache
4547

48+
RUNNER_GET_HOLMES_SLACKBOT_INFO = f"{ROBUSTA_API_ENDPOINT}/api/holmes/integrations/slack/runner"
49+
HOLMES_SLACKBOT_CACHE_TTL = int(os.getenv("HOLMES_SLACKBOT_CACHE_TTL", 15 * 60))
50+
51+
# Define the cache with a single slot and the configured TTL
52+
_holmes_slackbot_cache = TTLCache(maxsize=1, ttl=HOLMES_SLACKBOT_CACHE_TTL)
4653

4754
class RobustaSink(SinkBase, EventHandler):
4855
services_publish_lock = threading.Lock()
@@ -695,3 +702,28 @@ def __update_job(self, new_job: Job, operation: K8sOperationType):
695702
self.__safe_delete_job(job_key)
696703
self.__discovery_metrics.on_jobs_updated(1)
697704
return
705+
706+
def is_holmes_slackbot_connected(self) -> bool:
707+
if 'status' in _holmes_slackbot_cache:
708+
return _holmes_slackbot_cache['status']
709+
710+
session_token = self.dal.get_session_token()
711+
try:
712+
message_json = {
713+
"session_token": session_token,
714+
"account_id": self.account_id,
715+
}
716+
response = requests.post(
717+
RUNNER_GET_HOLMES_SLACKBOT_INFO,
718+
data=message_json,
719+
headers={"Content-Type": "application/json"}
720+
)
721+
response.raise_for_status()
722+
result = response.json()
723+
logging.warning(f"Holmes slackbot result {result}")
724+
_holmes_slackbot_cache['status'] = True
725+
return True
726+
except Exception:
727+
logging.info("Failed to get holmes slackbot info")
728+
_holmes_slackbot_cache['status'] = False
729+
return False

src/robusta/core/sinks/slack/slack_sink.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def __init__(self, sink_config: SlackSinkConfigWrapper, registry, is_preview=Fal
1616
self.slack_channel = slack_sink_params.slack_channel
1717
self.api_key = slack_sink_params.api_key
1818
self.slack_sender = slack_module.SlackSender(
19-
self.api_key, self.account_id, self.cluster_name, self.signing_key, self.slack_channel, is_preview
19+
self.api_key, self.account_id, self.cluster_name, self.signing_key, self.slack_channel, registry, is_preview
2020
)
2121
self.registry.subscribe("replace_callback_with_string", self)
2222

src/robusta/integrations/slack/sender.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
from robusta.core.sinks.sink_base import KeyT
4949
from robusta.core.sinks.slack.slack_sink_params import SlackSinkParams
5050
from robusta.core.sinks.slack.preview.slack_sink_preview_params import SlackSinkPreviewParams
51-
51+
from robusta.model.config import Registry
5252
from robusta.core.sinks.transformer import Transformer
5353

5454
ACTION_TRIGGER_PLAYBOOK = "trigger_playbook"
@@ -62,7 +62,7 @@ class SlackSender:
6262
verified_api_tokens: Set[str] = set()
6363
channel_name_to_id = {}
6464

65-
def __init__(self, slack_token: str, account_id: str, cluster_name: str, signing_key: str, slack_channel: str, is_preview: bool = False):
65+
def __init__(self, slack_token: str, account_id: str, cluster_name: str, signing_key: str, slack_channel: str, registry: Registry, is_preview: bool = False):
6666
"""
6767
Connect to Slack and verify that the Slack token is valid.
6868
Return True on success, False on failure
@@ -80,6 +80,7 @@ def __init__(self, slack_token: str, account_id: str, cluster_name: str, signing
8080
timeout=SLACK_REQUEST_TIMEOUT,
8181
retry_handlers=all_builtin_retry_handlers(),
8282
)
83+
self.registry = registry
8384
self.signing_key = signing_key
8485
self.account_id = account_id
8586
self.cluster_name = cluster_name
@@ -672,6 +673,15 @@ def send_finding_to_slack(
672673
thread_ts=thread_ts
673674
)
674675

676+
def __is_holmes_slackbot_enabled(self) -> bool:
677+
robusta_sinks = self.registry.get_sinks().get_robusta_sinks()
678+
if not robusta_sinks:
679+
logging.debug("No robusta sinks found, holmes not connected to slackbot")
680+
return False
681+
682+
robusta_sink = robusta_sinks[0]
683+
return robusta_sink.is_holmes_slackbot_connected()
684+
675685
def __send_finding_to_slack(
676686
self,
677687
finding: Finding,
@@ -731,7 +741,8 @@ def __send_finding_to_slack(
731741

732742
blocks.append(DividerBlock())
733743

734-
holmes_block = self.get_holmes_block(platform_enabled, HOLMES_ENABLED)
744+
is_holmes_slackbot_enabled = self.__is_holmes_slackbot_enabled()
745+
holmes_block = self.get_holmes_block(platform_enabled, is_holmes_slackbot_enabled)
735746
if holmes_block:
736747
blocks.append(holmes_block)
737748

0 commit comments

Comments
 (0)