#2888 - Software: align identifiers, tidy up schemas
This commit is contained in:
@@ -3,6 +3,7 @@ from typing import Any, Dict, Tuple
|
||||
|
||||
import pytest
|
||||
import yaml
|
||||
from pydantic import Field
|
||||
from ray import init as rayinit
|
||||
|
||||
from primaite import getLogger, PRIMAITE_PATHS
|
||||
@@ -40,12 +41,12 @@ _LOGGER = getLogger(__name__)
|
||||
class DummyService(Service, identifier="DummyService"):
|
||||
"""Test Service class"""
|
||||
|
||||
config: "DummyService.ConfigSchema" = None
|
||||
|
||||
class ConfigSchema(Service.ConfigSchema):
|
||||
"""ConfigSchema for DummyService."""
|
||||
|
||||
type: str = "DUMMY_SERVICE"
|
||||
type: str = "DummyService"
|
||||
|
||||
config: "DummyService.ConfigSchema" = Field(default_factory=lambda: DummyService.ConfigSchema())
|
||||
|
||||
def describe_state(self) -> Dict:
|
||||
return super().describe_state()
|
||||
@@ -63,12 +64,12 @@ class DummyService(Service, identifier="DummyService"):
|
||||
class DummyApplication(Application, identifier="DummyApplication"):
|
||||
"""Test Application class"""
|
||||
|
||||
config: "DummyApplication.ConfigSchema" = None
|
||||
|
||||
class ConfigSchema(Application.ConfigSchema):
|
||||
"""ConfigSchema for DummyApplication."""
|
||||
|
||||
type: str = "DUMMY_APPLICATION"
|
||||
type: str = "DummyApplication"
|
||||
|
||||
config: "DummyApplication.ConfigSchema" = Field(default_factory=lambda: DummyApplication.ConfigSchema())
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
kwargs["name"] = "DummyApplication"
|
||||
|
||||
@@ -4,7 +4,7 @@ from ipaddress import IPv4Address
|
||||
from typing import Dict, List, Optional
|
||||
from urllib.parse import urlparse
|
||||
|
||||
from pydantic import BaseModel, ConfigDict
|
||||
from pydantic import BaseModel, ConfigDict, Field
|
||||
|
||||
from primaite import getLogger
|
||||
from primaite.interface.request import RequestResponse
|
||||
@@ -31,7 +31,12 @@ class ExtendedApplication(Application, identifier="ExtendedApplication"):
|
||||
The application requests and loads web pages using its domain name and requesting IP addresses using DNS.
|
||||
"""
|
||||
|
||||
config: "ExtendedApplication.ConfigSchema" = None
|
||||
class ConfigSchema(Application.ConfigSchema):
|
||||
"""ConfigSchema for ExtendedApplication."""
|
||||
|
||||
type: str = "ExtendedApplication"
|
||||
|
||||
config: "ExtendedApplication.ConfigSchema" = Field(default_factory=lambda: ExtendedApplication.ConfigSchema())
|
||||
|
||||
target_url: Optional[str] = None
|
||||
|
||||
@@ -44,11 +49,6 @@ class ExtendedApplication(Application, identifier="ExtendedApplication"):
|
||||
history: List["BrowserHistoryItem"] = []
|
||||
"""Keep a log of visited websites and information about the visit, such as response code."""
|
||||
|
||||
class ConfigSchema(Application.ConfigSchema):
|
||||
"""ConfigSchema for ExtendedApplication."""
|
||||
|
||||
type: str = "EXTENDED_APPLICATION"
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
kwargs["name"] = "ExtendedApplication"
|
||||
kwargs["protocol"] = PROTOCOL_LOOKUP["TCP"]
|
||||
|
||||
@@ -3,6 +3,8 @@ from ipaddress import IPv4Address
|
||||
from typing import Any, Dict, List, Literal, Optional, Union
|
||||
from uuid import uuid4
|
||||
|
||||
from pydantic import Field
|
||||
|
||||
from primaite import getLogger
|
||||
from primaite.simulator.file_system.file_system import File
|
||||
from primaite.simulator.file_system.file_system_item_abc import FileSystemItemHealthStatus
|
||||
@@ -24,7 +26,12 @@ class ExtendedService(Service, identifier="ExtendedService"):
|
||||
This class inherits from the `Service` class and provides methods to simulate a SQL database.
|
||||
"""
|
||||
|
||||
config: "ExtendedService.ConfigSchema" = None
|
||||
class ConfigSchema(Service.ConfigSchema):
|
||||
"""ConfigSchema for ExtendedService."""
|
||||
|
||||
type: str = "ExtendedService"
|
||||
|
||||
config: "ExtendedService.ConfigSchema" = Field(default_factory=lambda: ExtendedService.ConfigSchema())
|
||||
|
||||
password: Optional[str] = None
|
||||
"""Password that needs to be provided by clients if they want to connect to the DatabaseService."""
|
||||
@@ -38,11 +45,6 @@ class ExtendedService(Service, identifier="ExtendedService"):
|
||||
latest_backup_file_name: str = None
|
||||
"""File name of latest backup."""
|
||||
|
||||
class ConfigSchema(Service.ConfigSchema):
|
||||
"""ConfigSchema for ExtendedService."""
|
||||
|
||||
type: str = "EXTENDED_SERVICE"
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
kwargs["name"] = "ExtendedService"
|
||||
kwargs["port"] = PORT_LOOKUP["POSTGRES_SERVER"]
|
||||
|
||||
@@ -3,6 +3,7 @@ from ipaddress import IPv4Address, IPv4Network
|
||||
from typing import Any, Dict, List, Tuple
|
||||
|
||||
import pytest
|
||||
from pydantic import Field
|
||||
|
||||
from primaite.simulator.network.container import Network
|
||||
from primaite.simulator.network.hardware.nodes.host.computer import Computer
|
||||
@@ -17,12 +18,12 @@ from primaite.utils.validation.port import PORT_LOOKUP
|
||||
class BroadcastTestService(Service, identifier="BroadcastTestService"):
|
||||
"""A service for sending broadcast and unicast messages over a network."""
|
||||
|
||||
config: "BroadcastTestService.ConfigSchema" = None
|
||||
|
||||
class ConfigSchema(Service.ConfigSchema):
|
||||
"""ConfigSchema for BroadcastTestService."""
|
||||
|
||||
type: str = "BROADCAST_TEST_SERVICE"
|
||||
type: str = "BroadcastTestService"
|
||||
|
||||
config: "BroadcastTestService.ConfigSchema" = Field(default_factory=lambda: BroadcastTestService.ConfigSchema())
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
# Set default service properties for broadcasting
|
||||
@@ -53,6 +54,13 @@ class BroadcastTestService(Service, identifier="BroadcastTestService"):
|
||||
class BroadcastTestClient(Application, identifier="BroadcastTestClient"):
|
||||
"""A client application to receive broadcast and unicast messages."""
|
||||
|
||||
class ConfigSchema(Service.ConfigSchema):
|
||||
"""ConfigSchema for BroadcastTestClient."""
|
||||
|
||||
type: str = "BroadcastTestClient"
|
||||
|
||||
config: ConfigSchema = Field(default_factory=lambda: BroadcastTestClient.ConfigSchema())
|
||||
|
||||
payloads_received: List = []
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
|
||||
@@ -15,18 +15,18 @@ from tests import TEST_ASSETS_ROOT
|
||||
|
||||
|
||||
class _DatabaseListener(Service, identifier="_DatabaseListener"):
|
||||
config: "_DatabaseListener.ConfigSchema" = None
|
||||
class ConfigSchema(Service.ConfigSchema):
|
||||
"""ConfigSchema for _DatabaseListener."""
|
||||
|
||||
type: str = "_DatabaseListener"
|
||||
|
||||
config: "_DatabaseListener.ConfigSchema" = Field(default_factory=lambda: _DatabaseListener.ConfigSchema())
|
||||
name: str = "DatabaseListener"
|
||||
protocol: str = PROTOCOL_LOOKUP["TCP"]
|
||||
port: int = PORT_LOOKUP["NONE"]
|
||||
listen_on_ports: Set[int] = {PORT_LOOKUP["POSTGRES_SERVER"]}
|
||||
payloads_received: List[Any] = Field(default_factory=list)
|
||||
|
||||
class ConfigSchema(Service.ConfigSchema):
|
||||
"""ConfigSchema for _DatabaseListener."""
|
||||
|
||||
type: str = "_DATABASE_LISTENER"
|
||||
|
||||
def receive(self, payload: Any, session_id: str, **kwargs) -> bool:
|
||||
self.payloads_received.append(payload)
|
||||
self.sys_log.info(f"{self.name}: received payload {payload}")
|
||||
|
||||
@@ -128,13 +128,13 @@ def test_c2_handle_switching_port(basic_c2_network):
|
||||
assert c2_server.c2_connection_active is True
|
||||
|
||||
# Assert to confirm that both the C2 server and the C2 beacon are configured correctly.
|
||||
assert c2_beacon.c2_config.keep_alive_frequency is 2
|
||||
assert c2_beacon.c2_config.masquerade_port is PORT_LOOKUP["HTTP"]
|
||||
assert c2_beacon.c2_config.masquerade_protocol is PROTOCOL_LOOKUP["TCP"]
|
||||
assert c2_beacon.config.keep_alive_frequency is 2
|
||||
assert c2_beacon.config.masquerade_port is PORT_LOOKUP["HTTP"]
|
||||
assert c2_beacon.config.masquerade_protocol is PROTOCOL_LOOKUP["TCP"]
|
||||
|
||||
assert c2_server.c2_config.keep_alive_frequency is 2
|
||||
assert c2_server.c2_config.masquerade_port is PORT_LOOKUP["HTTP"]
|
||||
assert c2_server.c2_config.masquerade_protocol is PROTOCOL_LOOKUP["TCP"]
|
||||
assert c2_server.config.keep_alive_frequency is 2
|
||||
assert c2_server.config.masquerade_port is PORT_LOOKUP["HTTP"]
|
||||
assert c2_server.config.masquerade_protocol is PROTOCOL_LOOKUP["TCP"]
|
||||
|
||||
# Configuring the C2 Beacon.
|
||||
c2_beacon.configure(
|
||||
@@ -150,11 +150,11 @@ def test_c2_handle_switching_port(basic_c2_network):
|
||||
|
||||
# Assert to confirm that both the C2 server and the C2 beacon
|
||||
# Have reconfigured their C2 settings.
|
||||
assert c2_beacon.c2_config.masquerade_port is PORT_LOOKUP["FTP"]
|
||||
assert c2_beacon.c2_config.masquerade_protocol is PROTOCOL_LOOKUP["TCP"]
|
||||
assert c2_beacon.config.masquerade_port is PORT_LOOKUP["FTP"]
|
||||
assert c2_beacon.config.masquerade_protocol is PROTOCOL_LOOKUP["TCP"]
|
||||
|
||||
assert c2_server.c2_config.masquerade_port is PORT_LOOKUP["FTP"]
|
||||
assert c2_server.c2_config.masquerade_protocol is PROTOCOL_LOOKUP["TCP"]
|
||||
assert c2_server.config.masquerade_port is PORT_LOOKUP["FTP"]
|
||||
assert c2_server.config.masquerade_protocol is PROTOCOL_LOOKUP["TCP"]
|
||||
|
||||
|
||||
def test_c2_handle_switching_frequency(basic_c2_network):
|
||||
@@ -174,8 +174,8 @@ def test_c2_handle_switching_frequency(basic_c2_network):
|
||||
assert c2_server.c2_connection_active is True
|
||||
|
||||
# Assert to confirm that both the C2 server and the C2 beacon are configured correctly.
|
||||
assert c2_beacon.c2_config.keep_alive_frequency is 2
|
||||
assert c2_server.c2_config.keep_alive_frequency is 2
|
||||
assert c2_beacon.config.keep_alive_frequency is 2
|
||||
assert c2_server.config.keep_alive_frequency is 2
|
||||
|
||||
# Configuring the C2 Beacon.
|
||||
c2_beacon.configure(c2_server_ip_address="192.168.0.1", keep_alive_frequency=10)
|
||||
@@ -186,8 +186,8 @@ def test_c2_handle_switching_frequency(basic_c2_network):
|
||||
|
||||
# Assert to confirm that both the C2 server and the C2 beacon
|
||||
# Have reconfigured their C2 settings.
|
||||
assert c2_beacon.c2_config.keep_alive_frequency is 10
|
||||
assert c2_server.c2_config.keep_alive_frequency is 10
|
||||
assert c2_beacon.config.keep_alive_frequency is 10
|
||||
assert c2_server.config.keep_alive_frequency is 10
|
||||
|
||||
# Now skipping 9 time steps to confirm keep alive inactivity
|
||||
for i in range(9):
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
from typing import Dict
|
||||
|
||||
import pytest
|
||||
from pydantic import Field
|
||||
|
||||
from primaite.simulator.system.core.sys_log import SysLog
|
||||
from primaite.simulator.system.services.service import Service
|
||||
@@ -11,13 +12,12 @@ from primaite.utils.validation.port import PORT_LOOKUP
|
||||
|
||||
|
||||
class TestSoftware(Service, identifier="TestSoftware"):
|
||||
|
||||
config: "TestSoftware.ConfigSchema" = None
|
||||
|
||||
class ConfigSchema(Service.ConfigSchema):
|
||||
"""ConfigSChema for TestSoftware."""
|
||||
|
||||
type: str = "TEST_SOFTWARE"
|
||||
type: str = "TestSoftware"
|
||||
|
||||
config: "TestSoftware.ConfigSchema" = Field(default_factory=lambda: TestSoftware.ConfigSchema())
|
||||
|
||||
def describe_state(self) -> Dict:
|
||||
pass
|
||||
|
||||
Reference in New Issue
Block a user