Merged PR 455: Add folder and file request permissions validators
## Summary Implemented the last of the request validators from https://dev.azure.com/ma-dev-uk/PrimAITE/_wiki/wikis/PrimAITE.wiki/703/Action-Masking files and folders validators ## Test process integration tests for files and folders added unit tests for previous validators as well ## Checklist - [X] PR is linked to a **work item** - [X] **acceptance criteria** of linked ticket are met - [X] performed **self-review** of the code - [X] written **tests** for any new functionality added with this PR - [ ] updated the **documentation** if this PR changes or adds functionality - [ ] written/updated **design docs** if this PR implements new functionality - [ ] updated the **change log** - [X] ran **pre-commit** checks for code style - [ ] attended to any **TO-DOs** left in the code Related work items: #2740
This commit is contained in:
@@ -0,0 +1,159 @@
|
||||
# © Crown-owned copyright 2024, Defence Science and Technology Laboratory UK
|
||||
import uuid
|
||||
from typing import Tuple
|
||||
|
||||
import pytest
|
||||
|
||||
from primaite.game.agent.interface import ProxyAgent
|
||||
from primaite.game.game import PrimaiteGame
|
||||
from primaite.simulator.file_system.file_system_item_abc import FileSystemItemHealthStatus
|
||||
from primaite.simulator.network.hardware.nodes.host.computer import Computer
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def game_and_agent_fixture(game_and_agent):
|
||||
"""Create a game with a simple agent that can be controlled by the tests."""
|
||||
game, agent = game_and_agent
|
||||
|
||||
client_1: Computer = game.simulation.network.get_node_by_hostname("client_1")
|
||||
client_1.start_up_duration = 3
|
||||
|
||||
return (game, agent)
|
||||
|
||||
|
||||
def test_create_file(game_and_agent_fixture: Tuple[PrimaiteGame, ProxyAgent]):
|
||||
"""Test that the validator allows a files to be created."""
|
||||
game, agent = game_and_agent_fixture
|
||||
|
||||
client_1 = game.simulation.network.get_node_by_hostname("client_1")
|
||||
|
||||
random_folder = str(uuid.uuid4())
|
||||
random_file = str(uuid.uuid4())
|
||||
|
||||
assert client_1.file_system.get_file(folder_name=random_folder, file_name=random_file) is None
|
||||
|
||||
action = (
|
||||
"NODE_FILE_CREATE",
|
||||
{"node_id": 0, "folder_name": random_folder, "file_name": random_file},
|
||||
)
|
||||
agent.store_action(action)
|
||||
game.step()
|
||||
|
||||
assert client_1.file_system.get_file(folder_name=random_folder, file_name=random_file) is not None
|
||||
|
||||
|
||||
def test_file_delete_action(game_and_agent_fixture: Tuple[PrimaiteGame, ProxyAgent]):
|
||||
"""Test that the validator allows a file to be deleted."""
|
||||
game, agent = game_and_agent_fixture
|
||||
|
||||
client_1 = game.simulation.network.get_node_by_hostname("client_1")
|
||||
file = client_1.file_system.get_file(folder_name="downloads", file_name="cat.png")
|
||||
assert file.deleted is False
|
||||
|
||||
action = (
|
||||
"NODE_FILE_DELETE",
|
||||
{"node_id": 0, "folder_id": 0, "file_id": 0},
|
||||
)
|
||||
agent.store_action(action)
|
||||
game.step()
|
||||
|
||||
assert file.deleted
|
||||
|
||||
|
||||
def test_file_scan_action(game_and_agent_fixture: Tuple[PrimaiteGame, ProxyAgent]):
|
||||
"""Test that the validator allows a file to be scanned."""
|
||||
game, agent = game_and_agent_fixture
|
||||
|
||||
client_1 = game.simulation.network.get_node_by_hostname("client_1")
|
||||
file = client_1.file_system.get_file(folder_name="downloads", file_name="cat.png")
|
||||
|
||||
file.corrupt()
|
||||
assert file.health_status == FileSystemItemHealthStatus.CORRUPT
|
||||
assert file.visible_health_status == FileSystemItemHealthStatus.GOOD
|
||||
|
||||
action = (
|
||||
"NODE_FILE_SCAN",
|
||||
{"node_id": 0, "folder_id": 0, "file_id": 0},
|
||||
)
|
||||
agent.store_action(action)
|
||||
game.step()
|
||||
|
||||
assert file.health_status == FileSystemItemHealthStatus.CORRUPT
|
||||
assert file.visible_health_status == FileSystemItemHealthStatus.CORRUPT
|
||||
|
||||
|
||||
def test_file_repair_action(game_and_agent_fixture: Tuple[PrimaiteGame, ProxyAgent]):
|
||||
"""Test that the validator allows a folder to be created."""
|
||||
game, agent = game_and_agent_fixture
|
||||
|
||||
client_1 = game.simulation.network.get_node_by_hostname("client_1")
|
||||
file = client_1.file_system.get_file(folder_name="downloads", file_name="cat.png")
|
||||
|
||||
file.corrupt()
|
||||
assert file.health_status == FileSystemItemHealthStatus.CORRUPT
|
||||
|
||||
action = (
|
||||
"NODE_FILE_REPAIR",
|
||||
{"node_id": 0, "folder_id": 0, "file_id": 0},
|
||||
)
|
||||
agent.store_action(action)
|
||||
game.step()
|
||||
|
||||
assert file.health_status == FileSystemItemHealthStatus.GOOD
|
||||
|
||||
|
||||
def test_file_restore_action(game_and_agent_fixture: Tuple[PrimaiteGame, ProxyAgent]):
|
||||
"""Test that the validator allows a file to be restored."""
|
||||
game, agent = game_and_agent_fixture
|
||||
|
||||
client_1 = game.simulation.network.get_node_by_hostname("client_1")
|
||||
file = client_1.file_system.get_file(folder_name="downloads", file_name="cat.png")
|
||||
|
||||
file.corrupt()
|
||||
assert file.health_status == FileSystemItemHealthStatus.CORRUPT
|
||||
|
||||
action = (
|
||||
"NODE_FILE_RESTORE",
|
||||
{"node_id": 0, "folder_id": 0, "file_id": 0},
|
||||
)
|
||||
agent.store_action(action)
|
||||
game.step()
|
||||
|
||||
assert file.health_status == FileSystemItemHealthStatus.GOOD
|
||||
|
||||
|
||||
def test_file_corrupt_action(game_and_agent_fixture: Tuple[PrimaiteGame, ProxyAgent]):
|
||||
"""Test that the validator allows a file to be corrupted."""
|
||||
game, agent = game_and_agent_fixture
|
||||
|
||||
client_1 = game.simulation.network.get_node_by_hostname("client_1")
|
||||
file = client_1.file_system.get_file(folder_name="downloads", file_name="cat.png")
|
||||
|
||||
assert file.health_status == FileSystemItemHealthStatus.GOOD
|
||||
|
||||
action = (
|
||||
"NODE_FILE_CORRUPT",
|
||||
{"node_id": 0, "folder_id": 0, "file_id": 0},
|
||||
)
|
||||
agent.store_action(action)
|
||||
game.step()
|
||||
|
||||
assert file.health_status == FileSystemItemHealthStatus.CORRUPT
|
||||
|
||||
|
||||
def test_file_access_action(game_and_agent_fixture: Tuple[PrimaiteGame, ProxyAgent]):
|
||||
"""Test that the validator allows a file to be accessed."""
|
||||
game, agent = game_and_agent_fixture
|
||||
|
||||
client_1 = game.simulation.network.get_node_by_hostname("client_1")
|
||||
file = client_1.file_system.get_file(folder_name="downloads", file_name="cat.png")
|
||||
assert file.num_access == 0
|
||||
|
||||
action = (
|
||||
"NODE_FILE_ACCESS",
|
||||
{"node_id": 0, "folder_name": file.folder_name, "file_name": file.name},
|
||||
)
|
||||
agent.store_action(action)
|
||||
game.step()
|
||||
|
||||
assert file.num_access == 1
|
||||
@@ -0,0 +1,123 @@
|
||||
# © Crown-owned copyright 2024, Defence Science and Technology Laboratory UK
|
||||
import uuid
|
||||
from typing import Tuple
|
||||
|
||||
import pytest
|
||||
|
||||
from primaite.game.agent.interface import ProxyAgent
|
||||
from primaite.game.game import PrimaiteGame
|
||||
from primaite.simulator.file_system.file_system_item_abc import FileSystemItemHealthStatus
|
||||
from primaite.simulator.network.hardware.nodes.host.computer import Computer
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def game_and_agent_fixture(game_and_agent):
|
||||
"""Create a game with a simple agent that can be controlled by the tests."""
|
||||
game, agent = game_and_agent
|
||||
|
||||
client_1: Computer = game.simulation.network.get_node_by_hostname("client_1")
|
||||
client_1.start_up_duration = 3
|
||||
|
||||
return (game, agent)
|
||||
|
||||
|
||||
def test_create_folder(game_and_agent_fixture: Tuple[PrimaiteGame, ProxyAgent]):
|
||||
"""Test that the validator allows a folder to be created."""
|
||||
game, agent = game_and_agent_fixture
|
||||
|
||||
client_1 = game.simulation.network.get_node_by_hostname("client_1")
|
||||
|
||||
random_folder = str(uuid.uuid4())
|
||||
|
||||
assert client_1.file_system.get_folder(folder_name=random_folder) is None
|
||||
|
||||
action = (
|
||||
"NODE_FOLDER_CREATE",
|
||||
{
|
||||
"node_id": 0,
|
||||
"folder_name": random_folder,
|
||||
},
|
||||
)
|
||||
agent.store_action(action)
|
||||
game.step()
|
||||
|
||||
assert client_1.file_system.get_folder(folder_name=random_folder) is not None
|
||||
|
||||
|
||||
def test_folder_scan_action(game_and_agent_fixture: Tuple[PrimaiteGame, ProxyAgent]):
|
||||
"""Test to make sure that the validator checks if the folder exists before scanning."""
|
||||
game, agent = game_and_agent_fixture
|
||||
|
||||
client_1 = game.simulation.network.get_node_by_hostname("client_1")
|
||||
|
||||
folder = client_1.file_system.get_folder(folder_name="downloads")
|
||||
assert folder.health_status == FileSystemItemHealthStatus.GOOD
|
||||
assert folder.visible_health_status == FileSystemItemHealthStatus.GOOD
|
||||
|
||||
folder.corrupt()
|
||||
|
||||
assert folder.health_status == FileSystemItemHealthStatus.CORRUPT
|
||||
assert folder.visible_health_status == FileSystemItemHealthStatus.GOOD
|
||||
|
||||
action = (
|
||||
"NODE_FOLDER_SCAN",
|
||||
{
|
||||
"node_id": 0, # client_1,
|
||||
"folder_id": 0, # downloads
|
||||
},
|
||||
)
|
||||
agent.store_action(action)
|
||||
game.step()
|
||||
|
||||
for i in range(folder.scan_duration + 1):
|
||||
game.step()
|
||||
|
||||
assert folder.health_status == FileSystemItemHealthStatus.CORRUPT
|
||||
assert folder.visible_health_status == FileSystemItemHealthStatus.CORRUPT
|
||||
|
||||
|
||||
def test_folder_repair_action(game_and_agent_fixture: Tuple[PrimaiteGame, ProxyAgent]):
|
||||
"""Test to make sure that the validator checks if the folder exists before repairing."""
|
||||
game, agent = game_and_agent_fixture
|
||||
|
||||
client_1 = game.simulation.network.get_node_by_hostname("client_1")
|
||||
|
||||
folder = client_1.file_system.get_folder(folder_name="downloads")
|
||||
folder.corrupt()
|
||||
assert folder.health_status == FileSystemItemHealthStatus.CORRUPT
|
||||
|
||||
action = (
|
||||
"NODE_FOLDER_REPAIR",
|
||||
{
|
||||
"node_id": 0, # client_1,
|
||||
"folder_id": 0, # downloads
|
||||
},
|
||||
)
|
||||
agent.store_action(action)
|
||||
game.step()
|
||||
|
||||
assert folder.health_status == FileSystemItemHealthStatus.GOOD
|
||||
|
||||
|
||||
def test_folder_restore_action(game_and_agent_fixture: Tuple[PrimaiteGame, ProxyAgent]):
|
||||
"""Test to make sure that the validator checks if the folder exists before restoring."""
|
||||
game, agent = game_and_agent_fixture
|
||||
|
||||
client_1 = game.simulation.network.get_node_by_hostname("client_1")
|
||||
|
||||
folder = client_1.file_system.get_folder(folder_name="downloads")
|
||||
folder.corrupt()
|
||||
|
||||
assert folder.health_status == FileSystemItemHealthStatus.CORRUPT
|
||||
|
||||
action = (
|
||||
"NODE_FOLDER_RESTORE",
|
||||
{
|
||||
"node_id": 0, # client_1,
|
||||
"folder_id": 0, # downloads
|
||||
},
|
||||
)
|
||||
agent.store_action(action)
|
||||
game.step()
|
||||
|
||||
assert folder.health_status == FileSystemItemHealthStatus.RESTORING
|
||||
@@ -6,8 +6,6 @@ import pytest
|
||||
from primaite.game.agent.interface import ProxyAgent
|
||||
from primaite.game.game import PrimaiteGame
|
||||
from primaite.simulator.network.hardware.nodes.host.computer import Computer
|
||||
from primaite.simulator.network.hardware.nodes.host.server import Server
|
||||
from primaite.simulator.system.services.service import ServiceOperatingState
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
|
||||
@@ -155,7 +155,7 @@ def test_nic_monitored_traffic(simulation):
|
||||
assert traffic_obs["icmp"]["outbound"] == 0
|
||||
|
||||
# send a ping
|
||||
pc.ping(target_ip_address=pc2.network_interface[1].ip_address)
|
||||
assert pc.ping(target_ip_address=pc2.network_interface[1].ip_address)
|
||||
traffic_obs = nic_obs.observe(simulation.describe_state()).get("TRAFFIC")
|
||||
|
||||
assert traffic_obs["icmp"]["inbound"] == 1
|
||||
@@ -178,7 +178,7 @@ def test_nic_monitored_traffic(simulation):
|
||||
traffic_obs = nic_obs.observe(simulation.describe_state()).get("TRAFFIC")
|
||||
assert traffic_obs["icmp"]["inbound"] == 0
|
||||
assert traffic_obs["icmp"]["outbound"] == 0
|
||||
assert traffic_obs["tcp"][53]["inbound"] == 0
|
||||
assert traffic_obs["tcp"][53]["inbound"] == 1
|
||||
assert traffic_obs["tcp"][53]["outbound"] == 1 # getting a webpage sent a dns request out
|
||||
|
||||
simulation.pre_timestep(2) # apply timestep to whole sim
|
||||
|
||||
@@ -101,6 +101,7 @@ def test_port_scan_full_subnet_all_ports_and_protocols(example_network):
|
||||
|
||||
actual_result = client_1_nmap.port_scan(
|
||||
target_ip_address=IPv4Network("192.168.10.0/24"),
|
||||
target_port=[Port.ARP, Port.HTTP, Port.FTP, Port.DNS, Port.NTP],
|
||||
)
|
||||
|
||||
expected_result = {
|
||||
|
||||
Reference in New Issue
Block a user