-
Notifications
You must be signed in to change notification settings - Fork 3
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
adding kdr slack and teams tests #436
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d874c60
chore: Update default paths for notifications and kdr configurations
kooomix 9198030
chore: Add kdr incidents alerts for Slack and Teams
kooomix 05dfbcc
chore: Add runtime incidents rulesets and alert notifications
kooomix cb07654
chore: Add logging for runtime incident messages in assert_runtime_in…
kooomix 9d82b2e
chore: Update teams alert notification webhook key name
kooomix 0bd666f
chore: Remove unused DEFAULT_KDR_PATHS import
kooomix File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
|
@@ -1502,6 +1502,32 @@ | |
"skip_on_environment": "", | ||
"owner": "" | ||
}, | ||
"kdr_slack_alerts": { | ||
"target": [ | ||
"Backend" | ||
], | ||
"target_repositories": [ | ||
"cadashboardbe", | ||
"event-ingester-service", | ||
"config-service" | ||
], | ||
"description": "Test kdr incidents is being sent to slack", | ||
"skip_on_environment": "", | ||
"owner": "[email protected]" | ||
}, | ||
"kdr_teams_alerts": { | ||
"target": [ | ||
"Backend" | ||
], | ||
"target_repositories": [ | ||
"cadashboardbe", | ||
"event-ingester-service", | ||
"config-service" | ||
], | ||
"description": "Test kdr incidents is being sent to teams", | ||
"skip_on_environment": "", | ||
"owner": "[email protected]" | ||
}, | ||
"sr_ac_scan_status": { | ||
"target": [ | ||
"In cluster", | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
|
||
|
||
import json | ||
from random import random | ||
import time | ||
from configurations.system.tests_cases.structures import TestConfiguration | ||
from infrastructure.kubectl_wrapper import KubectlWrapper | ||
from systest_utils.systests_utilities import TestUtil | ||
from systest_utils.tests_logger import Logger | ||
from tests_scripts.base_test import BaseTest | ||
from tests_scripts.runtime.policies import POLICY_CREATED_RESPONSE, RuntimePoliciesConfigurations | ||
from tests_scripts.users_notifications.alert_notifications import TEST_NAMESPACE, AlertNotifications, get_env | ||
|
||
|
||
class IncidentsAlerts(AlertNotifications, RuntimePoliciesConfigurations): | ||
def __init__(self, test_obj: TestConfiguration = None, backend=None, test_driver=None): | ||
super(IncidentsAlerts, self).__init__(test_obj=test_obj, backend=backend, test_driver=test_driver) | ||
|
||
self.helm_kwargs = { | ||
"capabilities.configurationScan": "disable", | ||
"capabilities.continuousScan": "disable", | ||
"capabilities.nodeScan": "disable", | ||
"capabilities.vulnerabilityScan": "disable", | ||
"grypeOfflineDB.enabled": "false", | ||
# not clear why | ||
"capabilities.relevancy": "enable", | ||
# enable application profile, malware and runtime detection | ||
"capabilities.runtimeObservability": "enable", | ||
"capabilities.malwareDetection": "enable", | ||
"capabilities.runtimeDetection": "enable", | ||
"capabilities.nodeProfileService": "enable", | ||
"alertCRD.installDefault": True, | ||
"alertCRD.scopeClustered": True, | ||
# short learning period | ||
"nodeAgent.config.maxLearningPeriod": "60s", | ||
"nodeAgent.config.learningPeriod": "50s", | ||
"nodeAgent.config.updatePeriod": "30s", | ||
"nodeAgent.config.nodeProfileInterval": "1m", | ||
# "nodeAgent.image.repository": "docker.io/amitschendel/node-agent", | ||
# "nodeAgent.image.tag": "v0.0.5", | ||
} | ||
test_helm_kwargs = self.test_obj.get_arg("helm_kwargs") | ||
if test_helm_kwargs: | ||
self.helm_kwargs.update(test_helm_kwargs) | ||
|
||
self.fw_name = None | ||
self.cluster = None | ||
self.wait_for_agg_to_end = False | ||
self.test_policy_guids = [] | ||
|
||
def start(self): | ||
""" | ||
agenda: | ||
1. get runtime incidents rulesets | ||
2. enrich the new runtime policy with alert notifications | ||
3. create new runtime policy | ||
4. Install kubescape | ||
5. apply the deployment that will generate the incident | ||
6. wait for the runtime incidents to be generated | ||
7. verify messages were sent | ||
""" | ||
assert self.backend is not None, f'the test {self.test_driver.test_name} must run with backend' | ||
|
||
|
||
self.cluster, namespace = self.setup(apply_services=False) | ||
|
||
before_test_message_ts = time.time() | ||
|
||
|
||
Logger.logger.info("1. get runtime incidents rulesets") | ||
res = self.backend.get_runtime_incidents_rulesets() | ||
incident_rulesets = json.loads(res.text) | ||
|
||
incident_rulesets_guids = [rule["guid"] for rule in incident_rulesets["response"] if rule["name"] == "Anomaly"] | ||
|
||
|
||
# Update the name field | ||
new_runtime_policy_body = { | ||
"name": f"Malware-new-systest-" + self.cluster, | ||
"description": "Default Malware RuleSet System Test", | ||
"enabled": True, | ||
"scope": {}, | ||
"ruleSetType": "Managed", | ||
"managedRuleSetIDs": incident_rulesets_guids, | ||
"notifications": [], | ||
"actions": [] | ||
} | ||
|
||
|
||
Logger.logger.info("2. enrich the new runtime policy with alert notifications") | ||
self.test_obj["enrichAlertChannelFunc"](new_runtime_policy_body) | ||
|
||
|
||
Logger.logger.info("3. create new runtime policy") | ||
new_policy_guid = self.validate_new_policy(new_runtime_policy_body) | ||
self.test_policy_guids.append(new_policy_guid) | ||
|
||
|
||
Logger.logger.info('4. Install kubescape') | ||
self.install_kubescape(helm_kwargs=self.helm_kwargs) | ||
|
||
Logger.logger.info('5. apply the deployment that will generate the incident') | ||
workload_objs: list = self.apply_directory(path=self.test_obj["deployments"], namespace=namespace) | ||
self.verify_all_pods_are_running(namespace=namespace, workload=workload_objs, timeout=240) | ||
|
||
wlids = self.get_wlid(workload=workload_objs, namespace=namespace, cluster=self.cluster) | ||
if isinstance(wlids, str): | ||
wlids = [wlids] | ||
|
||
Logger.logger.info('6. wait for the runtime incidents to be generated') | ||
self.wait_for_report(self.verify_running_pods, sleep_interval=5, timeout=180, namespace=namespace) | ||
|
||
Logger.logger.info( | ||
f'workloads are running, waiting for application profile finalizing before exec into pod {wlids}') | ||
self.wait_for_report(self.verify_application_profiles, wlids=wlids, namespace=namespace) | ||
time.sleep(6) | ||
self.exec_pod(wlid=wlids[0], command="ls -l /tmp") | ||
|
||
|
||
Logger.logger.info('7. verify messages were sent') | ||
res = self.wait_for_report(self.assert_all_messages_sent, begin_time=before_test_message_ts, cluster=self.cluster) | ||
return self.cleanup() | ||
|
||
def cleanup(self): | ||
for policy_guid in self.test_policy_guids: | ||
body = { | ||
"innerFilters": [ | ||
{ | ||
"guid": policy_guid, | ||
} | ||
] | ||
} | ||
self.backend.delete_runtime_policies(body) | ||
return super().cleanup() | ||
|
||
|
||
def assert_all_messages_sent(self, begin_time, cluster): | ||
messages = self.test_obj["getMessagesFunc"](begin_time) | ||
found = str(messages).count(cluster) | ||
assert found > 0, f"expected to have at least 1 message, found {found}" | ||
assert_runtime_incident_message_sent(messages, cluster) | ||
|
||
|
||
|
||
def assert_runtime_incident_message_sent(messages, cluster): | ||
found = 0 | ||
Logger.logger.info(f"total messages found: {len(messages)}, looking for runtime incident messages") | ||
if len(messages) > 0: | ||
Logger.logger.info(f"first message: {messages[0]}") | ||
|
||
for message in messages: | ||
message_string = str(message) | ||
if "New threat found" in message_string and cluster in message_string and "redis" in message_string: | ||
found += 1 | ||
assert found > 0, "expected to have at least one runtime incident message" | ||
|
||
|
||
def enrich_teams_alert_notifications(data): | ||
data["notifications"] =[ | ||
{ | ||
"provider": "teams", | ||
"teamsWebhookURL" : get_env("CHANNEL_WEBHOOK") | ||
} | ||
] | ||
|
||
|
||
def enrich_slack_alert_notifications(data): | ||
data["notifications"] = [ | ||
{ | ||
"provider": "slack", | ||
"slackChannel": { | ||
"channelID": get_env("SLACK_CHANNEL_ID"), | ||
"channelName": "dev-system-tests" | ||
} | ||
} | ||
] |
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
R U sure you want them to run on PROD?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For now yes