Merge branch 'feature/1816_Database-Service-(Network-and-User-Interaction)' into feature/1752-dns-server-and-client

This commit is contained in:
Czar Echavez
2023-09-11 09:50:16 +01:00
13 changed files with 246 additions and 10 deletions

View File

@@ -2,6 +2,7 @@ from __future__ import annotations
from enum import Enum
from random import choice
from typing import Any
class FileType(Enum):
@@ -95,7 +96,7 @@ class FileType(Enum):
"Generic DB file. Used by sqlite3."
@classmethod
def _missing_(cls, value):
def _missing_(cls, value: Any) -> FileType:
return cls.UNKNOWN
@classmethod
@@ -118,7 +119,7 @@ class FileType(Enum):
return size if size else 0
def get_file_type_from_extension(file_type_extension: str):
def get_file_type_from_extension(file_type_extension: str) -> FileType:
"""
Get a FileType from a file type extension.

View File

@@ -960,6 +960,7 @@ class Node(SimComponent):
return state
def show(self, markdown: bool = False, component: Literal["NIC", "OPEN_PORTS"] = "NIC"):
"""A multi-use .show function that accepts either NIC or OPEN_PORTS."""
if component == "NIC":
self._show_nic(markdown)
elif component == "OPEN_PORTS":

View File

@@ -7,7 +7,7 @@ from primaite.simulator.network.hardware.nodes.switch import Switch
from primaite.simulator.network.transmission.network_layer import IPProtocol
from primaite.simulator.network.transmission.transport_layer import Port
from primaite.simulator.system.applications.database_client import DatabaseClient
from primaite.simulator.system.services.database import DatabaseService
from primaite.simulator.system.services.database_service import DatabaseService
def client_server_routed() -> Network:

View File

@@ -54,13 +54,13 @@ class Application(IOSoftware):
return state
def run(self) -> None:
"""Open the Application"""
"""Open the Application."""
if self.operating_state == ApplicationOperatingState.CLOSED:
self.sys_log.info(f"Running Application {self.name}")
self.operating_state = ApplicationOperatingState.RUNNING
def close(self) -> None:
"""Close the Application"""
"""Close the Application."""
if self.operating_state == ApplicationOperatingState.RUNNING:
self.sys_log.info(f"Closed Application{self.name}")
self.operating_state = ApplicationOperatingState.CLOSED

View File

@@ -10,6 +10,15 @@ from primaite.simulator.system.core.software_manager import SoftwareManager
class DatabaseClient(Application):
"""
A DatabaseClient application.
Extends the Application class to provide functionality for connecting, querying, and disconnecting from a
Database Service. It mainly operates over TCP protocol.
:ivar server_ip_address: The IPv4 address of the Database Service server, defaults to None.
"""
server_ip_address: Optional[IPv4Address] = None
connected: bool = False
@@ -20,9 +29,21 @@ class DatabaseClient(Application):
super().__init__(**kwargs)
def describe_state(self) -> Dict:
"""
Describes the current state of the ACLRule.
:return: A dictionary representing the current state.
"""
pass
return super().describe_state()
def connect(self, server_ip_address: IPv4Address, password: Optional[str] = None) -> bool:
"""
Connect to a Database Service.
:param server_ip_address: The IPv4 Address of the Node the Database Service is running on.
:param password: The Database Service password. Is optional and has a default value of None.
"""
if not self.connected and self.operating_state.RUNNING:
return self._connect(server_ip_address, password)
@@ -44,6 +65,7 @@ class DatabaseClient(Application):
return self._connect(server_ip_address, password, True)
def disconnect(self):
"""Disconnect from the Database Service."""
if self.connected and self.operating_state.RUNNING:
software_manager: SoftwareManager = self.software_manager
software_manager.send_payload_to_session_manager(
@@ -54,6 +76,11 @@ class DatabaseClient(Application):
self.server_ip_address = None
def query(self, sql: str):
"""
Send a query to the Database Service.
:param sql: The SQL query.
"""
if self.connected and self.operating_state.RUNNING:
software_manager: SoftwareManager = self.software_manager
software_manager.send_payload_to_session_manager(
@@ -75,6 +102,13 @@ class DatabaseClient(Application):
print(table)
def receive(self, payload: Any, session_id: str, **kwargs) -> bool:
"""
Receive a payload from the Software Manager.
:param payload: A payload to receive.
:param session_id: The session id the payload relates to.
:return: True.
"""
if isinstance(payload, dict) and payload.get("type"):
if payload["type"] == "connect_response":
self.connected = payload["response"] == True

View File

@@ -9,7 +9,7 @@ from primaite.simulator.network.transmission.transport_layer import Port
from primaite.simulator.system.applications.application import Application, ApplicationOperatingState
from primaite.simulator.system.core.sys_log import SysLog
from primaite.simulator.system.services.service import Service, ServiceOperatingState
from primaite.simulator.system.software import IOSoftware, SoftwareType
from primaite.simulator.system.software import IOSoftware
if TYPE_CHECKING:
from primaite.simulator.system.core.session_manager import SessionManager
@@ -37,6 +37,11 @@ class SoftwareManager:
self.file_system: FileSystem = file_system
def get_open_ports(self) -> List[Port]:
"""
Get a list of open ports.
:return: A list of all open ports on the Node.
"""
open_ports = [Port.ARP]
for software in self.port_protocol_mapping.values():
if software.operating_state in {ApplicationOperatingState.RUNNING, ServiceOperatingState.RUNNING}:
@@ -45,6 +50,11 @@ class SoftwareManager:
return open_ports
def install(self, software_class: Type[IOSoftwareClass]):
"""
Install an Application or Service.
:param software_class: The software class.
"""
if software_class in self._software_class_to_name_map:
self.sys_log.info(f"Cannot install {software_class} as it is already installed")
return
@@ -59,6 +69,11 @@ class SoftwareManager:
software.operating_state = ApplicationOperatingState.CLOSED
def uninstall(self, software_name: str):
"""
Uninstall an Application or Service.
:param software_name: The software name.
"""
if software_name in self.software:
software = self.software.pop(software_name) # noqa
del software

View File

@@ -1,6 +1,5 @@
import sqlite3
from datetime import datetime
from ipaddress import IPv4Address
from sqlite3 import OperationalError
from typing import Any, Dict, List, Optional, Union
@@ -9,7 +8,6 @@ from prettytable import MARKDOWN, PrettyTable
from primaite.simulator.file_system.file_system import File
from primaite.simulator.network.transmission.network_layer import IPProtocol
from primaite.simulator.network.transmission.transport_layer import Port
from primaite.simulator.system.core.session_manager import Session
from primaite.simulator.system.core.software_manager import SoftwareManager
from primaite.simulator.system.services.service import Service, ServiceOperatingState
from primaite.simulator.system.software import SoftwareHealthState