From d80eb15fa28fafc9bd32155ffb63559bfc5b5d2c Mon Sep 17 00:00:00 2001 From: Nick Todd Date: Wed, 8 May 2024 14:56:44 +0100 Subject: [PATCH 1/5] #2560: Fix typos and punctuation. --- .../network_simulator_demo.ipynb | 26 ++++++++++--------- 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/src/primaite/simulator/_package_data/network_simulator_demo.ipynb b/src/primaite/simulator/_package_data/network_simulator_demo.ipynb index 7f4cf3b1..a90c4751 100644 --- a/src/primaite/simulator/_package_data/network_simulator_demo.ipynb +++ b/src/primaite/simulator/_package_data/network_simulator_demo.ipynb @@ -54,7 +54,7 @@ "## The Network\n", "First let's create our network. The network comes 'pre-packaged' with PrimAITE in the `primaite.simulator.network.networks` module.\n", "\n", - "> ℹ️ You'll see a bunch of logs associated with parts of the Network that aern't an 'electronic' device on the Network and thus don't have a stsrem to log to. Soon these logs are going to be pushed to a Network Logger so we're not clogging up the PrimAITE application logs." + "> ℹ️ You'll see a bunch of logs associated with parts of the Network that aren't an 'electronic' device on the Network and thus don't have a stream to log to. Soon these logs are going to be pushed to a Network Logger so we're not clogging up the PrimAITE application logs." ] }, { @@ -208,7 +208,9 @@ "id": "17", "metadata": {}, "source": [ - "Calling `router.sys_log.show()` displays the Router system log. By default, only the last 10 log entries are displayed, this can be changed by passing `last_n=`." + "Calling `router.sys_log.show()` displays the Router system log. By default, only the last 10 log entries are displayed, this can be changed by passing `last_n=`.\n", + "\n", + "NB: For `sys_log.show()` to work correctly log files need to be created with a sys_log level of INFO or below." ] }, { @@ -230,7 +232,7 @@ "source": [ "### Switch Nodes\n", "\n", - "Next we'll inspect the Switch node and some of it's core services." + "Next we'll inspect the Switch node and some of its core services." ] }, { @@ -258,7 +260,7 @@ "id": "22", "metadata": {}, "source": [ - "Calling `switch.sys_log.show()` displays the Switch system log. By default, only the last 10 log entries are displayed, this can be changed by passing `last_n=`." + "Calling `switch.sys_log.show()` displays the Switch system log. By default; only the last 10 log entries are displayed, this can be changed by passing `last_n=`." ] }, { @@ -330,7 +332,7 @@ "id": "29", "metadata": {}, "source": [ - "Calling `computer.sys_log.show()` displays the Computer/Server system log. By default, only the last 10 log entries are displayed, this can be changed by passing `last_n=`." + "Calling `computer.sys_log.show()` displays the Computer/Server system log. By default, only the last 10 log entries are displayed; this can be changed by passing `last_n=`." ] }, { @@ -474,7 +476,7 @@ "id": "43", "metadata": {}, "source": [ - "Let's attempt to prevent client_2 from being able to ping the web server. First, we'll confirm that it can ping the server first..." + "Let's attempt to prevent client_2 from being able to ping the web server. First, we'll confirm that it can ping the server..." ] }, { @@ -494,7 +496,7 @@ "id": "45", "metadata": {}, "source": [ - "If we look at the client_2 sys log we can see that the four ICMP echo requests were sent and four ICMP each replies were received:" + "If we look at the client_2 sys log we can see that the four ICMP echo requests were sent and four ICMP each replies were received." ] }, { @@ -514,7 +516,7 @@ "id": "47", "metadata": {}, "source": [ - "Now we'll add an ACL to block ICMP from 192.168.10.22" + "Now we'll add an ACL to block ICMP from 192.168.10.22." ] }, { @@ -554,7 +556,7 @@ "id": "50", "metadata": {}, "source": [ - "Now we attempt (and fail) to ping the web server" + "Now we attempt (and fail) to ping the web server." ] }, { @@ -574,7 +576,7 @@ "id": "52", "metadata": {}, "source": [ - "We can check that the ping was actually sent by client_2 by viewing the sys log" + "We can check that the ping was actually sent by client_2 by viewing the sys log." ] }, { @@ -594,7 +596,7 @@ "id": "54", "metadata": {}, "source": [ - "We can check the router sys log to see why the traffic was blocked" + "We can check the router sys log to see why the traffic was blocked." ] }, { @@ -658,7 +660,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.11" + "version": "3.10.12" } }, "nbformat": 4, From dc8105e8d6fd48e88568d5c3295cefd8a3c1123f Mon Sep 17 00:00:00 2001 From: Charlie Crane Date: Fri, 17 May 2024 16:55:41 +0100 Subject: [PATCH 2/5] 2560 - Some minor tweaks to network_simulator_demo.ipynb and networks.py after looking through some bugs. Sys_log should print --- .../_package_data/network_simulator_demo.ipynb | 14 +++++++------- src/primaite/simulator/network/networks.py | 12 +++++++++++- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/src/primaite/simulator/_package_data/network_simulator_demo.ipynb b/src/primaite/simulator/_package_data/network_simulator_demo.ipynb index a90c4751..4b041a31 100644 --- a/src/primaite/simulator/_package_data/network_simulator_demo.ipynb +++ b/src/primaite/simulator/_package_data/network_simulator_demo.ipynb @@ -66,7 +66,7 @@ }, "outputs": [], "source": [ - "from primaite.simulator.network.networks import arcd_uc2_network" + "from primaite.simulator.network.networks import network_simulator_demo_example" ] }, { @@ -78,7 +78,7 @@ }, "outputs": [], "source": [ - "network = arcd_uc2_network()" + "network = network_simulator_demo_example()" ] }, { @@ -188,7 +188,7 @@ "id": "15", "metadata": {}, "source": [ - "Calling `router.router_table.show()` displays the static routes the Router provides." + "Calling `router.router_table.show()` displays the static routes the Router provides. " ] }, { @@ -222,7 +222,7 @@ }, "outputs": [], "source": [ - "network.get_node_by_hostname(\"router_1\").sys_log.show(last_n=10)" + "network.get_node_by_hostname(\"router_1\").sys_log.show()" ] }, { @@ -344,7 +344,7 @@ }, "outputs": [], "source": [ - "network.get_node_by_hostname(\"security_suite\").sys_log.show()" + "network.get_node_by_hostname(\"security_suite\").sys_log.show(last_n=25)" ] }, { @@ -398,7 +398,7 @@ }, "outputs": [], "source": [ - "network.get_node_by_hostname(\"client_1\").sys_log.show(15)" + "network.get_node_by_hostname(\"client_1\").sys_log.show(last_n=15)" ] }, { @@ -660,7 +660,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.12" + "version": "3.10.11" } }, "nbformat": 4, diff --git a/src/primaite/simulator/network/networks.py b/src/primaite/simulator/network/networks.py index bd06e615..1e176561 100644 --- a/src/primaite/simulator/network/networks.py +++ b/src/primaite/simulator/network/networks.py @@ -287,6 +287,16 @@ def arcd_uc2_network() -> Network: return network +def network_simulator_demo_example() -> Network: + """Returns a lightly modified version of the ARCD UC2 Network.""" + network = arcd_uc2_network() + network.get_node_by_hostname("router_1").route_table.add_route( + address="192.168.10.0", subnet_mask="255.255.255.0", next_hop_ip_address="192.168.1.2" + ) + + return network + + def _get_example_network(path: str) -> Network: try: with open(path, "r") as file: @@ -308,7 +318,7 @@ def client_server_p2p_network_example() -> Network: def basic_lan_network_example() -> Network: """Get the basic LAN example network.""" - path = PRIMAITE_PATHS.user_config_path / "example_config" / "basic_network_network_example.yaml" + path = PRIMAITE_PATHS.user_config_path / "example_config" / "basic_lan_network_example.yaml" return _get_example_network(path) From 103c7f8d39e02cdc57e38722076b59985f1c8635 Mon Sep 17 00:00:00 2001 From: Charlie Crane Date: Mon, 20 May 2024 12:28:02 +0100 Subject: [PATCH 3/5] #2560 - Addressing the sys_log bug that could occur when scripts run across two time intervals. Additional updates to network_simulator notebook so that sys_log works in production mode --- src/primaite/simulator/__init__.py | 14 ++++++-------- .../_package_data/create-simulation_demo.ipynb | 9 +++++++++ .../_package_data/network_simulator_demo.ipynb | 2 +- src/primaite/simulator/network/networks.py | 5 +++++ src/primaite/simulator/system/core/sys_log.py | 3 ++- 5 files changed, 23 insertions(+), 10 deletions(-) diff --git a/src/primaite/simulator/__init__.py b/src/primaite/simulator/__init__.py index bbcf9af4..d2993b34 100644 --- a/src/primaite/simulator/__init__.py +++ b/src/primaite/simulator/__init__.py @@ -27,10 +27,10 @@ class LogLevel(IntEnum): class _SimOutput: def __init__(self): - date_str = datetime.now().strftime("%Y-%m-%d") - time_str = datetime.now().strftime("%H-%M-%S") + self.date_str = datetime.now().strftime("%Y-%m-%d") + self.time_str = datetime.now().strftime("%H-%M-%S") - path = PRIMAITE_PATHS.user_sessions_path / date_str / time_str + path = PRIMAITE_PATHS.user_sessions_path / self.date_str / self.time_str self._path = path self._save_pcap_logs: bool = False @@ -41,17 +41,15 @@ class _SimOutput: @property def path(self) -> Path: if is_dev_mode(): - date_str = datetime.now().strftime("%Y-%m-%d") - time_str = datetime.now().strftime("%H-%M-%S") # if dev mode is enabled, if output dir is not set, print to primaite repo root - path: Path = _PRIMAITE_ROOT.parent.parent / "sessions" / date_str / time_str / "simulation_output" + path: Path = _PRIMAITE_ROOT.parent.parent / "sessions" / self.date_str / self.time_str / "simulation_output" # otherwise print to output dir if PRIMAITE_CONFIG["developer_mode"]["output_dir"]: path: Path = ( Path(PRIMAITE_CONFIG["developer_mode"]["output_dir"]) / "sessions" - / date_str - / time_str + / self.date_str + / self.time_str / "simulation_output" ) self._path = path diff --git a/src/primaite/simulator/_package_data/create-simulation_demo.ipynb b/src/primaite/simulator/_package_data/create-simulation_demo.ipynb index 06ecd4be..0ab0c1f4 100644 --- a/src/primaite/simulator/_package_data/create-simulation_demo.ipynb +++ b/src/primaite/simulator/_package_data/create-simulation_demo.ipynb @@ -148,6 +148,15 @@ "my_server.file_system.create_file(\"favicon.ico\", file_type=FileType.PNG)" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "my_server.sys_log.show()" + ] + }, { "cell_type": "markdown", "metadata": {}, diff --git a/src/primaite/simulator/_package_data/network_simulator_demo.ipynb b/src/primaite/simulator/_package_data/network_simulator_demo.ipynb index 4b041a31..9ea478f0 100644 --- a/src/primaite/simulator/_package_data/network_simulator_demo.ipynb +++ b/src/primaite/simulator/_package_data/network_simulator_demo.ipynb @@ -7,7 +7,7 @@ "source": [ "# PrimAITE Router Simulation Demo\n", "\n", - "This demo uses the ARCD Use Case 2 Network (seen below) to demonstrate the capabilities of the Network simulator in PrimAITE." + "This demo uses a modified version ARCD Use Case 2 Network (seen below) to demonstrate the capabilities of the Network simulator in PrimAITE." ] }, { diff --git a/src/primaite/simulator/network/networks.py b/src/primaite/simulator/network/networks.py index 1e176561..f3d43e5b 100644 --- a/src/primaite/simulator/network/networks.py +++ b/src/primaite/simulator/network/networks.py @@ -4,6 +4,7 @@ import yaml from primaite import getLogger, PRIMAITE_PATHS from primaite.game.game import PrimaiteGame +from primaite.simulator import LogLevel, SIM_OUTPUT from primaite.simulator.network.container import Network from primaite.simulator.network.hardware.nodes.host.computer import Computer from primaite.simulator.network.hardware.nodes.host.host_node import NIC @@ -289,6 +290,10 @@ def arcd_uc2_network() -> Network: def network_simulator_demo_example() -> Network: """Returns a lightly modified version of the ARCD UC2 Network.""" + # Ensure that sys_log will be viewable for demo + SIM_OUTPUT.sys_log_level = LogLevel.DEBUG + SIM_OUTPUT.save_sys_logs = True + network = arcd_uc2_network() network.get_node_by_hostname("router_1").route_table.add_route( address="192.168.10.0", subnet_mask="255.255.255.0", next_hop_ip_address="192.168.1.2" diff --git a/src/primaite/simulator/system/core/sys_log.py b/src/primaite/simulator/system/core/sys_log.py index d3afce94..cf68b674 100644 --- a/src/primaite/simulator/system/core/sys_log.py +++ b/src/primaite/simulator/system/core/sys_log.py @@ -52,7 +52,8 @@ class SysLog: file_handler.setFormatter(logging.Formatter(log_format)) self.logger = logging.getLogger(f"{self.hostname}_sys_log") - self.logger.handlers.clear() # clear handlers + for handler in self.logger.handlers: + self.logger.removeHandler(handler) self.logger.setLevel(logging.DEBUG) self.logger.addHandler(file_handler) From e9f3cef805c6f33877a36546b0ed6c1d337149f0 Mon Sep 17 00:00:00 2001 From: Charlie Crane Date: Mon, 20 May 2024 12:32:03 +0100 Subject: [PATCH 4/5] #2560 - Removing a sys_log.show() call in create-simulation_demo notebook that was used for debugging. --- .../simulator/_package_data/create-simulation_demo.ipynb | 9 --------- 1 file changed, 9 deletions(-) diff --git a/src/primaite/simulator/_package_data/create-simulation_demo.ipynb b/src/primaite/simulator/_package_data/create-simulation_demo.ipynb index 0ab0c1f4..06ecd4be 100644 --- a/src/primaite/simulator/_package_data/create-simulation_demo.ipynb +++ b/src/primaite/simulator/_package_data/create-simulation_demo.ipynb @@ -148,15 +148,6 @@ "my_server.file_system.create_file(\"favicon.ico\", file_type=FileType.PNG)" ] }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "my_server.sys_log.show()" - ] - }, { "cell_type": "markdown", "metadata": {}, From 7584271ad03bd9b4ca56171c968359d691f8adfa Mon Sep 17 00:00:00 2001 From: Charlie Crane Date: Mon, 20 May 2024 15:24:21 +0100 Subject: [PATCH 5/5] #2560 - Addressing review comments --- .../simulator/_package_data/network_simulator_demo.ipynb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/primaite/simulator/_package_data/network_simulator_demo.ipynb b/src/primaite/simulator/_package_data/network_simulator_demo.ipynb index 9ea478f0..9265b5f2 100644 --- a/src/primaite/simulator/_package_data/network_simulator_demo.ipynb +++ b/src/primaite/simulator/_package_data/network_simulator_demo.ipynb @@ -7,7 +7,7 @@ "source": [ "# PrimAITE Router Simulation Demo\n", "\n", - "This demo uses a modified version ARCD Use Case 2 Network (seen below) to demonstrate the capabilities of the Network simulator in PrimAITE." + "This demo uses a modified version of the ARCD Use Case 2 Network (seen below) to demonstrate the capabilities of the Network simulator in PrimAITE." ] }, { @@ -260,7 +260,7 @@ "id": "22", "metadata": {}, "source": [ - "Calling `switch.sys_log.show()` displays the Switch system log. By default; only the last 10 log entries are displayed, this can be changed by passing `last_n=`." + "Calling `switch.sys_log.show()` displays the Switch system log. By default, only the last 10 log entries are displayed, this can be changed by passing `last_n=`." ] }, {