#1355 - Carried out full renaming in node.py, active_node.py, passive_node.py, and service_node.py to make params and variable names explicit.
- Made the same renaming in the yaml laydown config files. - Added Type hints wherever I've been. - Added a custom NodeType in custom_typing.py to encompass the Union of ActiveNode, PassiveNode, ServiceNode.
This commit is contained in:
@@ -9,15 +9,15 @@
|
||||
serviceList:
|
||||
- name: ftp
|
||||
- itemType: NODE
|
||||
id: '1'
|
||||
node_id: '1'
|
||||
name: node
|
||||
baseType: SERVICE
|
||||
nodeType: COMPUTER
|
||||
node_class: SERVICE
|
||||
node_type: COMPUTER
|
||||
priority: P1
|
||||
hardwareState: 'ON'
|
||||
ipAddress: 192.168.0.1
|
||||
softwareState: GOOD
|
||||
fileSystemState: GOOD
|
||||
hardware_state: 'ON'
|
||||
ip_address: 192.168.0.1
|
||||
software_state: GOOD
|
||||
file_system_state: GOOD
|
||||
services:
|
||||
- name: ftp
|
||||
port: '21'
|
||||
|
||||
@@ -26,7 +26,7 @@ observationSpaceHighValue: 1000000000
|
||||
# Reward values
|
||||
# Generic
|
||||
allOk: 0
|
||||
# Node Operating State
|
||||
# Node Hardware State
|
||||
offShouldBeOn: -10
|
||||
offShouldBeResetting: -5
|
||||
onShouldBeOff: -2
|
||||
@@ -34,7 +34,7 @@ onShouldBeResetting: -5
|
||||
resettingShouldBeOn: -5
|
||||
resettingShouldBeOff: -2
|
||||
resetting: -3
|
||||
# Node O/S or Service State
|
||||
# Node Software or Service State
|
||||
goodShouldBePatching: 2
|
||||
goodShouldBeCompromised: 5
|
||||
goodShouldBeOverwhelmed: 5
|
||||
|
||||
@@ -5,7 +5,7 @@ from typing import Union
|
||||
|
||||
import yaml
|
||||
|
||||
from primaite.common.config_values_main import config_values_main
|
||||
from primaite.common.config_values_main import ConfigValuesMain
|
||||
from primaite.environment.primaite_env import Primaite
|
||||
|
||||
ACTION_SPACE_NODE_VALUES = 1
|
||||
@@ -32,7 +32,7 @@ def _get_primaite_env_from_config(
|
||||
# Reward values
|
||||
# Generic
|
||||
config_values.all_ok = int(config_data["allOk"])
|
||||
# Node Operating State
|
||||
# Node Hardware State
|
||||
config_values.off_should_be_on = int(config_data["offShouldBeOn"])
|
||||
config_values.off_should_be_resetting = int(config_data["offShouldBeResetting"])
|
||||
config_values.on_should_be_off = int(config_data["onShouldBeOff"])
|
||||
@@ -40,7 +40,7 @@ def _get_primaite_env_from_config(
|
||||
config_values.resetting_should_be_on = int(config_data["resettingShouldBeOn"])
|
||||
config_values.resetting_should_be_off = int(config_data["resettingShouldBeOff"])
|
||||
config_values.resetting = int(config_data["resetting"])
|
||||
# Node O/S or Service State
|
||||
# Node Software or Service State
|
||||
config_values.good_should_be_patching = int(config_data["goodShouldBePatching"])
|
||||
config_values.good_should_be_compromised = int(
|
||||
config_data["goodShouldBeCompromised"]
|
||||
@@ -160,7 +160,7 @@ def _get_primaite_env_from_config(
|
||||
config_file_main = open(main_config_path, "r")
|
||||
config_data = yaml.safe_load(config_file_main)
|
||||
# Create a config class
|
||||
config_values = config_values_main()
|
||||
config_values = ConfigValuesMain()
|
||||
# Load in config data
|
||||
load_config_values()
|
||||
env = Primaite(config_values, [])
|
||||
|
||||
@@ -1,22 +1,22 @@
|
||||
"""Used to test Active Node functions."""
|
||||
import pytest
|
||||
|
||||
from primaite.common.enums import FILE_SYSTEM_STATE, HARDWARE_STATE, SOFTWARE_STATE
|
||||
from primaite.common.enums import FileSystemState, HardwareState, SoftwareState
|
||||
from primaite.nodes.active_node import ActiveNode
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"operating_state, expected_state",
|
||||
[
|
||||
(HARDWARE_STATE.OFF, SOFTWARE_STATE.GOOD),
|
||||
(HARDWARE_STATE.ON, SOFTWARE_STATE.OVERWHELMED),
|
||||
(HardwareState.OFF, SoftwareState.GOOD),
|
||||
(HardwareState.ON, SoftwareState.OVERWHELMED),
|
||||
],
|
||||
)
|
||||
def test_os_state_change(operating_state, expected_state):
|
||||
"""
|
||||
Test that a node cannot change its operating system state.
|
||||
Test that a node cannot change its Software State.
|
||||
|
||||
When its operating state is OFF.
|
||||
When its hardware state is OFF.
|
||||
"""
|
||||
active_node = ActiveNode(
|
||||
0,
|
||||
@@ -25,28 +25,28 @@ def test_os_state_change(operating_state, expected_state):
|
||||
"1",
|
||||
operating_state,
|
||||
"192.168.0.1",
|
||||
SOFTWARE_STATE.GOOD,
|
||||
SoftwareState.GOOD,
|
||||
"GOOD",
|
||||
1,
|
||||
)
|
||||
|
||||
active_node.set_os_state(SOFTWARE_STATE.OVERWHELMED)
|
||||
active_node.software_state = SoftwareState.OVERWHELMED
|
||||
|
||||
assert active_node.get_os_state() == expected_state
|
||||
assert active_node.software_state == expected_state
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"operating_state, expected_state",
|
||||
[
|
||||
(HARDWARE_STATE.OFF, SOFTWARE_STATE.GOOD),
|
||||
(HARDWARE_STATE.ON, SOFTWARE_STATE.OVERWHELMED),
|
||||
(HardwareState.OFF, SoftwareState.GOOD),
|
||||
(HardwareState.ON, SoftwareState.OVERWHELMED),
|
||||
],
|
||||
)
|
||||
def test_os_state_change_if_not_compromised(operating_state, expected_state):
|
||||
"""
|
||||
Test that a node cannot change its operating system state.
|
||||
Test that a node cannot change its Software State.
|
||||
|
||||
If not compromised) when its operating state is OFF.
|
||||
If not compromised) when its hardware state is OFF.
|
||||
"""
|
||||
active_node = ActiveNode(
|
||||
0,
|
||||
@@ -55,25 +55,25 @@ def test_os_state_change_if_not_compromised(operating_state, expected_state):
|
||||
"1",
|
||||
operating_state,
|
||||
"192.168.0.1",
|
||||
SOFTWARE_STATE.GOOD,
|
||||
SoftwareState.GOOD,
|
||||
"GOOD",
|
||||
1,
|
||||
)
|
||||
|
||||
active_node.set_os_state_if_not_compromised(SOFTWARE_STATE.OVERWHELMED)
|
||||
active_node.set_software_state_if_not_compromised(SoftwareState.OVERWHELMED)
|
||||
|
||||
assert active_node.get_os_state() == expected_state
|
||||
assert active_node.software_state == expected_state
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"operating_state, expected_state",
|
||||
[
|
||||
(HARDWARE_STATE.OFF, FILE_SYSTEM_STATE.GOOD),
|
||||
(HARDWARE_STATE.ON, FILE_SYSTEM_STATE.CORRUPT),
|
||||
(HardwareState.OFF, FileSystemState.GOOD),
|
||||
(HardwareState.ON, FileSystemState.CORRUPT),
|
||||
],
|
||||
)
|
||||
def test_file_system_change(operating_state, expected_state):
|
||||
"""Test that a node cannot change its file system state when its operating state is ON."""
|
||||
"""Test that a node cannot change its file system state when its hardware state is ON."""
|
||||
active_node = ActiveNode(
|
||||
0,
|
||||
"node",
|
||||
@@ -82,27 +82,27 @@ def test_file_system_change(operating_state, expected_state):
|
||||
operating_state,
|
||||
"192.168.0.1",
|
||||
"COMPROMISED",
|
||||
FILE_SYSTEM_STATE.GOOD,
|
||||
FileSystemState.GOOD,
|
||||
1,
|
||||
)
|
||||
|
||||
active_node.set_file_system_state(FILE_SYSTEM_STATE.CORRUPT)
|
||||
active_node.set_file_system_state(FileSystemState.CORRUPT)
|
||||
|
||||
assert active_node.get_file_system_state_actual() == expected_state
|
||||
assert active_node.file_system_state_actual == expected_state
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"operating_state, expected_state",
|
||||
[
|
||||
(HARDWARE_STATE.OFF, FILE_SYSTEM_STATE.GOOD),
|
||||
(HARDWARE_STATE.ON, FILE_SYSTEM_STATE.CORRUPT),
|
||||
(HardwareState.OFF, FileSystemState.GOOD),
|
||||
(HardwareState.ON, FileSystemState.CORRUPT),
|
||||
],
|
||||
)
|
||||
def test_file_system_change_if_not_compromised(operating_state, expected_state):
|
||||
"""
|
||||
Test that a node cannot change its file system state.
|
||||
|
||||
If not compromised) when its operating state is OFF.
|
||||
If not compromised) when its hardware state is OFF.
|
||||
"""
|
||||
active_node = ActiveNode(
|
||||
0,
|
||||
@@ -112,10 +112,10 @@ def test_file_system_change_if_not_compromised(operating_state, expected_state):
|
||||
operating_state,
|
||||
"192.168.0.1",
|
||||
"GOOD",
|
||||
FILE_SYSTEM_STATE.GOOD,
|
||||
FileSystemState.GOOD,
|
||||
1,
|
||||
)
|
||||
|
||||
active_node.set_file_system_state_if_not_compromised(FILE_SYSTEM_STATE.CORRUPT)
|
||||
active_node.set_file_system_state_if_not_compromised(FileSystemState.CORRUPT)
|
||||
|
||||
assert active_node.get_file_system_state_actual() == expected_state
|
||||
assert active_node.file_system_state_actual == expected_state
|
||||
|
||||
@@ -19,7 +19,7 @@ def test_rewards_are_being_penalised_at_each_step_function():
|
||||
File System State: goodShouldBeCorrupt = 5 (between Steps 1 & 3)
|
||||
Hardware State: onShouldBeOff = -2 (between Steps 4 & 6)
|
||||
Service State: goodShouldBeCompromised = 5 (between Steps 7 & 9)
|
||||
Operating System State (Software State): goodShouldBeCompromised = 5 (between Steps 10 & 12)
|
||||
Software State (Software State): goodShouldBeCompromised = 5 (between Steps 10 & 12)
|
||||
|
||||
Total Reward: -2 - 2 + 5 + 5 + 5 + 5 + 5 + 5 = 26
|
||||
Step Count: 13
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
"""Used to test Service Node functions."""
|
||||
import pytest
|
||||
|
||||
from primaite.common.enums import HARDWARE_STATE, SOFTWARE_STATE
|
||||
from primaite.common.enums import HardwareState, SoftwareState
|
||||
from primaite.common.service import Service
|
||||
from primaite.nodes.service_node import ServiceNode
|
||||
|
||||
@@ -9,15 +9,15 @@ from primaite.nodes.service_node import ServiceNode
|
||||
@pytest.mark.parametrize(
|
||||
"operating_state, expected_state",
|
||||
[
|
||||
(HARDWARE_STATE.OFF, SOFTWARE_STATE.GOOD),
|
||||
(HARDWARE_STATE.ON, SOFTWARE_STATE.OVERWHELMED),
|
||||
(HardwareState.OFF, SoftwareState.GOOD),
|
||||
(HardwareState.ON, SoftwareState.OVERWHELMED),
|
||||
],
|
||||
)
|
||||
def test_service_state_change(operating_state, expected_state):
|
||||
"""
|
||||
Test that a node cannot change the state of a running service.
|
||||
|
||||
When its operating state is OFF.
|
||||
When its hardware state is OFF.
|
||||
"""
|
||||
service_node = ServiceNode(
|
||||
0,
|
||||
@@ -30,10 +30,10 @@ def test_service_state_change(operating_state, expected_state):
|
||||
"RESTORING",
|
||||
1,
|
||||
)
|
||||
service = Service("TCP", 80, SOFTWARE_STATE.GOOD)
|
||||
service = Service("TCP", 80, SoftwareState.GOOD)
|
||||
service_node.add_service(service)
|
||||
|
||||
service_node.set_service_state("TCP", SOFTWARE_STATE.OVERWHELMED)
|
||||
service_node.set_service_state("TCP", SoftwareState.OVERWHELMED)
|
||||
|
||||
assert service_node.get_service_state("TCP") == expected_state
|
||||
|
||||
@@ -41,15 +41,15 @@ def test_service_state_change(operating_state, expected_state):
|
||||
@pytest.mark.parametrize(
|
||||
"operating_state, expected_state",
|
||||
[
|
||||
(HARDWARE_STATE.OFF, SOFTWARE_STATE.GOOD),
|
||||
(HARDWARE_STATE.ON, SOFTWARE_STATE.OVERWHELMED),
|
||||
(HardwareState.OFF, SoftwareState.GOOD),
|
||||
(HardwareState.ON, SoftwareState.OVERWHELMED),
|
||||
],
|
||||
)
|
||||
def test_service_state_change_if_not_comprised(operating_state, expected_state):
|
||||
"""
|
||||
Test that a node cannot change the state of a running service.
|
||||
|
||||
If not compromised when its operating state is ON.
|
||||
If not compromised when its hardware state is ON.
|
||||
"""
|
||||
service_node = ServiceNode(
|
||||
0,
|
||||
@@ -62,9 +62,9 @@ def test_service_state_change_if_not_comprised(operating_state, expected_state):
|
||||
"RESTORING",
|
||||
1,
|
||||
)
|
||||
service = Service("TCP", 80, SOFTWARE_STATE.GOOD)
|
||||
service = Service("TCP", 80, SoftwareState.GOOD)
|
||||
service_node.add_service(service)
|
||||
|
||||
service_node.set_service_state_if_not_compromised("TCP", SOFTWARE_STATE.OVERWHELMED)
|
||||
service_node.set_service_state_if_not_compromised("TCP", SoftwareState.OVERWHELMED)
|
||||
|
||||
assert service_node.get_service_state("TCP") == expected_state
|
||||
|
||||
Reference in New Issue
Block a user