From 5d7d96f3fc36e095b4cb4253443bdcd626d8321f Mon Sep 17 00:00:00 2001 From: Yuhu-kth Date: Thu, 1 Aug 2024 12:25:48 +0200 Subject: [PATCH 1/2] add aggregatedHostMetricsThread test --- .../aggregated_host_metrics_thread.py | 2 +- .../src/csle_common/util/connection_util.py | 3 + .../src/csle_common/util/export_util.py | 2 +- .../test_aggregated_host_metrics_thread.py | 116 ++++++++++++++++++ 4 files changed, 121 insertions(+), 2 deletions(-) create mode 100644 simulation-system/libs/csle-common/tests/test_aggregated_host_metrics_thread.py diff --git a/simulation-system/libs/csle-common/src/csle_common/consumer_threads/aggregated_host_metrics_thread.py b/simulation-system/libs/csle-common/src/csle_common/consumer_threads/aggregated_host_metrics_thread.py index f27c2d768..009b63715 100644 --- a/simulation-system/libs/csle-common/src/csle_common/consumer_threads/aggregated_host_metrics_thread.py +++ b/simulation-system/libs/csle-common/src/csle_common/consumer_threads/aggregated_host_metrics_thread.py @@ -17,7 +17,7 @@ def __init__(self, host_metrics: HostMetrics, Initializes the thread :param host_metrics: the host metrics to update - :param machines: the list of machiens to update the host metrics with + :param machines: the list of machines to update the host metrics with """ threading.Thread.__init__(self) self.machines = machines diff --git a/simulation-system/libs/csle-common/src/csle_common/util/connection_util.py b/simulation-system/libs/csle-common/src/csle_common/util/connection_util.py index 8cc616ffa..e6a3b92e9 100644 --- a/simulation-system/libs/csle-common/src/csle_common/util/connection_util.py +++ b/simulation-system/libs/csle-common/src/csle_common/util/connection_util.py @@ -272,6 +272,7 @@ def _ssh_setup_connection(a: EmulationAttackerAction, if s.attacker_obs_state is None or s.attacker_obs_state.agent_reachable is None: raise ValueError("EmulationAttackerObservationState is None") if not a.ips_match(list(s.attacker_obs_state.agent_reachable)): + print("2") continue for cr in credentials: for ip in a.ips: @@ -282,6 +283,7 @@ def _ssh_setup_connection(a: EmulationAttackerAction, agent_transport = proxy_conn.conn.get_transport() relay_channel = agent_transport.open_channel(constants.SSH.DIRECT_CHANNEL, target_addr, agent_addr) + print("here") target_conn = paramiko.SSHClient() target_conn.set_missing_host_key_policy(paramiko.AutoAddPolicy()) if target_conn is None: @@ -466,6 +468,7 @@ def _telnet_finalize_connection(target_machine: EmulationAttackerMachineObservat """ start = time.time() root = False + print(connection_setup_dto.target_connections) for i in range(constants.ENV_CONSTANTS.ATTACKER_RETRY_CHECK_ROOT): connection_setup_dto.target_connections[i].write("sudo -l\n".encode()) response = connection_setup_dto.target_connections[i].read_until(constants.TELNET.PROMPT, timeout=3) diff --git a/simulation-system/libs/csle-common/src/csle_common/util/export_util.py b/simulation-system/libs/csle-common/src/csle_common/util/export_util.py index f522a335f..f1bcce6d4 100644 --- a/simulation-system/libs/csle-common/src/csle_common/util/export_util.py +++ b/simulation-system/libs/csle-common/src/csle_common/util/export_util.py @@ -35,7 +35,7 @@ def zipdir(dir_path: str, file_path: str) -> None: @staticmethod def get_dir_size_gb(dir_path: str = '.') -> float: """ - Utility method to calculate the zie of a file directory in gb + Utility method to calculate the size of a file directory in gb :param dir_path: the path to the directory :return: the size of the directory in GB diff --git a/simulation-system/libs/csle-common/tests/test_aggregated_host_metrics_thread.py b/simulation-system/libs/csle-common/tests/test_aggregated_host_metrics_thread.py new file mode 100644 index 000000000..82ebee643 --- /dev/null +++ b/simulation-system/libs/csle-common/tests/test_aggregated_host_metrics_thread.py @@ -0,0 +1,116 @@ +from typing import List +from unittest.mock import MagicMock, patch +from csle_common.consumer_threads.aggregated_host_metrics_thread import AggregatedHostMetricsThread +from csle_collector.host_manager.dao.host_metrics import HostMetrics +from csle_common.dao.emulation_observation.defender.emulation_defender_machine_observation_state import ( + EmulationDefenderMachineObservationState, +) + + +class TestSuiteAggregatedHostMetricsThread: + """ + Test suite for AggregatedHostMetricsThread + """ + def test_initialization(self) -> None: + """ + Test the initialization function + + :return: None + """ + host_metrics = HostMetrics + mock_machine = MagicMock(spec='EmulationDefenderMachineObservationState') + mock_machines = [mock_machine, mock_machine] + sleep_time = 3 + thread = AggregatedHostMetricsThread(host_metrics=host_metrics, machines=mock_machines, sleep_time=sleep_time) + assert thread.host_metrics == host_metrics + assert thread.machines == mock_machines + assert thread.sleep_time == sleep_time + + @patch("time.sleep", return_value=None) + def test_run(self, mock_sleep) -> None: + """ + Test the method that runs the thread + + :param mock_sleep: mock_sleep + + :return: None + """ + mock_host_metrics_1 = MagicMock(spec="HostMetrics") + mock_host_metrics_1.num_logged_in_users = 5 + mock_host_metrics_1.num_failed_login_attempts = 2 + mock_host_metrics_1.num_open_connections = 10 + mock_host_metrics_1.num_login_events = 7 + mock_host_metrics_1.num_processes = 50 + mock_host_metrics_1.num_users = 3 + + mock_host_metrics_2 = MagicMock(spec="HostMetrics") + mock_host_metrics_2.num_logged_in_users = 3 + mock_host_metrics_2.num_failed_login_attempts = 1 + mock_host_metrics_2.num_open_connections = 5 + mock_host_metrics_2.num_login_events = 4 + mock_host_metrics_2.num_processes = 30 + mock_host_metrics_2.num_users = 2 + + mock_machine_1 = MagicMock(spec="EmulationDefenderMachineObservationState") + mock_machine_1.host_metrics = mock_host_metrics_1 + + mock_machine_2 = MagicMock(spec="EmulationDefenderMachineObservationState") + mock_machine_2.host_metrics = mock_host_metrics_2 + + mock_machines = [mock_machine_1, mock_machine_2] + + mock_aggregated_host_metrics = MagicMock(spec="HostMetrics") + mock_aggregated_host_metrics.copy = MagicMock(spec="HostMetrics") + mock_aggregated_host_metrics.copy.return_value = MagicMock(spec="HostMetrics") + + sleep_time = 1 + + thread = AggregatedHostMetricsThread( + host_metrics=mock_aggregated_host_metrics, machines=mock_machines, sleep_time=sleep_time + ) + + def stop_running(*args, **kwargs): + thread.running = False + + mock_sleep.side_effect = stop_running + thread.run() + assert thread.host_metrics.num_logged_in_users == 8 + assert thread.host_metrics.num_failed_login_attempts == 3 + assert thread.host_metrics.num_open_connections == 15 + assert thread.host_metrics.num_login_events == 11 + assert thread.host_metrics.num_processes == 80 + assert thread.host_metrics.num_users == 5 + + def test_get_average_aggregated_host_metrics(self) -> None: + """ + Test the method that returns the average of the list of aggregated host metrics + + :return: None + """ + mock_host_metrics_1 = MagicMock(spec="HostMetrics") + mock_host_metrics_1.num_logged_in_users = 10 + mock_host_metrics_1.num_failed_login_attempts = 5 + mock_host_metrics_1.num_open_connections = 15 + mock_host_metrics_1.num_login_events = 20 + mock_host_metrics_1.num_processes = 25 + mock_host_metrics_1.num_users = 30 + + mock_host_metrics_2 = MagicMock(spec="HostMetrics") + mock_host_metrics_2.num_logged_in_users = 20 + mock_host_metrics_2.num_failed_login_attempts = 10 + mock_host_metrics_2.num_open_connections = 25 + mock_host_metrics_2.num_login_events = 30 + mock_host_metrics_2.num_processes = 35 + mock_host_metrics_2.num_users = 40 + + thread = thread = AggregatedHostMetricsThread( + host_metrics=MagicMock(spec="HostMetrics"), machines=[], sleep_time=1 + ) + thread.host_metrics_list = [mock_host_metrics_1, mock_host_metrics_2] + avg_metrics = thread.get_average_aggregated_host_metrics() + assert avg_metrics.num_logged_in_users == 15 + assert avg_metrics.num_failed_login_attempts == 8 + assert avg_metrics.num_open_connections == 20 + assert avg_metrics.num_login_events == 25 + assert avg_metrics.num_processes == 30 + assert avg_metrics.num_users == 35 From ed2ca4bc0f36623218964d0b04c18b1f23299fc4 Mon Sep 17 00:00:00 2001 From: Yuhu-kth Date: Thu, 1 Aug 2024 12:29:05 +0200 Subject: [PATCH 2/2] deleted prints --- .../libs/csle-common/src/csle_common/util/connection_util.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/simulation-system/libs/csle-common/src/csle_common/util/connection_util.py b/simulation-system/libs/csle-common/src/csle_common/util/connection_util.py index e6a3b92e9..8cc616ffa 100644 --- a/simulation-system/libs/csle-common/src/csle_common/util/connection_util.py +++ b/simulation-system/libs/csle-common/src/csle_common/util/connection_util.py @@ -272,7 +272,6 @@ def _ssh_setup_connection(a: EmulationAttackerAction, if s.attacker_obs_state is None or s.attacker_obs_state.agent_reachable is None: raise ValueError("EmulationAttackerObservationState is None") if not a.ips_match(list(s.attacker_obs_state.agent_reachable)): - print("2") continue for cr in credentials: for ip in a.ips: @@ -283,7 +282,6 @@ def _ssh_setup_connection(a: EmulationAttackerAction, agent_transport = proxy_conn.conn.get_transport() relay_channel = agent_transport.open_channel(constants.SSH.DIRECT_CHANNEL, target_addr, agent_addr) - print("here") target_conn = paramiko.SSHClient() target_conn.set_missing_host_key_policy(paramiko.AutoAddPolicy()) if target_conn is None: @@ -468,7 +466,6 @@ def _telnet_finalize_connection(target_machine: EmulationAttackerMachineObservat """ start = time.time() root = False - print(connection_setup_dto.target_connections) for i in range(constants.ENV_CONSTANTS.ATTACKER_RETRY_CHECK_ROOT): connection_setup_dto.target_connections[i].write("sudo -l\n".encode()) response = connection_setup_dto.target_connections[i].read_until(constants.TELNET.PROMPT, timeout=3)