#2676: Put global variables in dataclass

This commit is contained in:
Nick Todd
2024-06-26 16:51:30 +01:00
parent bf5f443604
commit 4796cee2dc

View File

@@ -1,48 +1,55 @@
# © Crown-owned copyright 2024, Defence Science and Technology Laboratory UK # © Crown-owned copyright 2024, Defence Science and Technology Laboratory UK
from typing import Dict, Final, List from dataclasses import dataclass, field
from typing import Dict, List
CAPTURE_NMNE: bool = True
"""Indicates whether Malicious Network Events (MNEs) should be captured. Default is True."""
NMNE_CAPTURE_KEYWORDS: List[str] = [] @dataclass
class nmne_data:
"""Store all the information to perform NMNE operations."""
capture_nmne: bool = True
"""Indicates whether Malicious Network Events (MNEs) should be captured."""
nmne_capture_keywords: List[str] = field(default_factory=list)
"""List of keywords to identify malicious network events.""" """List of keywords to identify malicious network events."""
capture_by_direction: bool = True
# TODO: Remove final and make configurable after example layout when the NICObservation creates nmne structure dynamically """Captures should be organized by traffic direction (inbound/outbound)."""
CAPTURE_BY_DIRECTION: Final[bool] = True capture_by_ip_address: bool = False
"""Flag to determine if captures should be organized by traffic direction (inbound/outbound).""" """Captures should be organized by source or destination IP address."""
CAPTURE_BY_IP_ADDRESS: Final[bool] = False capture_by_protocol: bool = False
"""Flag to determine if captures should be organized by source or destination IP address.""" """Captures should be organized by network protocol (e.g., TCP, UDP)."""
CAPTURE_BY_PROTOCOL: Final[bool] = False capture_by_port: bool = False
"""Flag to determine if captures should be organized by network protocol (e.g., TCP, UDP).""" """Captures should be organized by source or destination port."""
CAPTURE_BY_PORT: Final[bool] = False capture_by_keyword: bool = False
"""Flag to determine if captures should be organized by source or destination port.""" """Captures should be filtered and categorised based on specific keywords."""
CAPTURE_BY_KEYWORD: Final[bool] = False
"""Flag to determine if captures should be filtered and categorised based on specific keywords."""
def set_nmne_config(nmne_config: Dict): def set_nmne_config(nmne_config: Dict) -> nmne_data:
""" """
Sets the configuration for capturing Malicious Network Events (MNEs) based on a provided dictionary. Sets the configuration for capturing Malicious Network Events (MNEs) based on a provided
dictionary.
This function updates global settings related to NMNE capture, including whether to capture NMNEs and what This function updates global settings related to NMNE capture, including whether to capture
keywords to use for identifying NMNEs. NMNEs and what keywords to use for identifying NMNEs.
The function ensures that the settings are updated only if they are provided in the `nmne_config` dictionary, The function ensures that the settings are updated only if they are provided in the
and maintains type integrity by checking the types of the provided values. `nmne_config` dictionary, and maintains type integrity by checking the types of the provided
values.
:param nmne_config: A dictionary containing the NMNE configuration settings. Possible keys include: :param nmne_config: A dictionary containing the NMNE configuration settings. Possible keys
"capture_nmne" (bool) to indicate whether NMNEs should be captured, "nmne_capture_keywords" (list of strings) include:
to specify keywords for NMNE identification. "capture_nmne" (bool) to indicate whether NMNEs should be captured;
"nmne_capture_keywords" (list of strings) to specify keywords for NMNE identification.
:rvar dataclass with data read from config file.
""" """
global NMNE_CAPTURE_KEYWORDS nmne_capture_keywords = []
global CAPTURE_NMNE
# Update the NMNE capture flag, defaulting to False if not specified or if the type is incorrect # Update the NMNE capture flag, defaulting to False if not specified or if the type is incorrect
CAPTURE_NMNE = nmne_config.get("capture_nmne", False) capture_nmne = nmne_config.get("capture_nmne", False)
if not isinstance(CAPTURE_NMNE, bool): if not isinstance(capture_nmne, bool):
CAPTURE_NMNE = True # Revert to default True if the provided value is not a boolean capture_nmne = True # Revert to default True if the provided value is not a boolean
# Update the NMNE capture keywords, appending new keywords if provided # Update the NMNE capture keywords, appending new keywords if provided
NMNE_CAPTURE_KEYWORDS += nmne_config.get("nmne_capture_keywords", []) nmne_capture_keywords += nmne_config.get("nmne_capture_keywords", [])
if not isinstance(NMNE_CAPTURE_KEYWORDS, list): if not isinstance(nmne_capture_keywords, list):
NMNE_CAPTURE_KEYWORDS = [] # Reset to empty list if the provided value is not a list nmne_capture_keywords = [] # Reset to empty list if the provided value is not a list
return nmne_data(capture_nmne=capture_nmne, nmne_capture_keywords=nmne_capture_keywords)