diff --git a/backend/app/models/agents.py b/backend/app/models/agents.py index ce9562e0..142860ac 100644 --- a/backend/app/models/agents.py +++ b/backend/app/models/agents.py @@ -1,25 +1,49 @@ -# from datetime import datetime +from datetime import datetime -# from loguru import logger -# from sqlalchemy.dialects.postgresql import JSONB # Add this line +from sqlalchemy import Boolean +from sqlalchemy import Column +from sqlalchemy import DateTime +from sqlalchemy import Integer +from sqlalchemy import String from app import db from app import ma -# Class for agent metadata which stores the agent ID, IP address, hostname, OS, last seen timestamp, -# and boolean for critical assest. # Path: backend\app\models.py class AgentMetadata(db.Model): - id = db.Column(db.Integer, primary_key=True) - agent_id = db.Column(db.String(100)) - ip_address = db.Column(db.String(100)) - os = db.Column(db.String(100)) - hostname = db.Column(db.String(100)) - critical_asset = db.Column(db.Boolean, default=False) - last_seen = db.Column(db.DateTime) - - def __init__(self, agent_id, ip_address, os, hostname, critical_asset, last_seen): + """ + Class for agent metadata which stores the agent ID, IP address, hostname, OS, last seen timestamp, + and boolean for critical asset. This class inherits from SQLAlchemy's Model class. + """ + + id: Column[Integer] = db.Column(db.Integer, primary_key=True) + agent_id: Column[String] = db.Column(db.String(100)) + ip_address: Column[String] = db.Column(db.String(100)) + os: Column[String] = db.Column(db.String(100)) + hostname: Column[String] = db.Column(db.String(100)) + critical_asset: Column[Boolean] = db.Column(db.Boolean, default=False) + last_seen: Column[DateTime] = db.Column(db.DateTime) + + def __init__( + self, + agent_id: str, + ip_address: str, + os: str, + hostname: str, + critical_asset: bool, + last_seen: datetime, + ): + """ + Initialize a new instance of the AgentMetadata class. + + :param agent_id: Unique ID for the agent. + :param ip_address: IP address of the agent. + :param os: Operating system of the agent. + :param hostname: Hostname of the agent. + :param critical_asset: Boolean value indicating if the agent is a critical asset. + :param last_seen: Timestamp of when the agent was last seen. + """ self.agent_id = agent_id self.ip_address = ip_address self.os = os @@ -27,7 +51,12 @@ def __init__(self, agent_id, ip_address, os, hostname, critical_asset, last_seen self.critical_asset = critical_asset self.last_seen = last_seen - def __repr__(self): + def __repr__(self) -> str: + """ + Returns a string representation of the AgentMetadata instance. + + :return: A string representation of the agent ID. + """ return f"" def mark_as_critical(self): @@ -53,8 +82,16 @@ def commit_wazuh_agent_to_db(self): class AgentMetadataSchema(ma.Schema): + """ + Schema for serializing and deserializing instances of the AgentMetadata class. + """ + class Meta: - fields = ( + """ + Meta class defines the fields to be serialized/deserialized. + """ + + fields: tuple = ( "id", "agent_id", "ip_address", @@ -65,5 +102,5 @@ class Meta: ) -agent_metadata_schema = AgentMetadataSchema() -agent_metadatas_schema = AgentMetadataSchema(many=True) +agent_metadata_schema: AgentMetadataSchema = AgentMetadataSchema() +agent_metadatas_schema: AgentMetadataSchema = AgentMetadataSchema(many=True) diff --git a/backend/app/models/artifacts.py b/backend/app/models/artifacts.py index dec9b7cd..a2ed4010 100644 --- a/backend/app/models/artifacts.py +++ b/backend/app/models/artifacts.py @@ -1,29 +1,56 @@ -from datetime import datetime +from sqlalchemy import Column +from sqlalchemy import Integer +from sqlalchemy import String +from sqlalchemy.dialects.postgresql import TEXT # Add this line from app import db from app import ma -from sqlalchemy.dialects.postgresql import TEXT # Add this line -# Class for artifacts collected which stores the artifact name, artificat results (json), hostname + # Path: backend\app\models.py class Artifact(db.Model): - id = db.Column(db.Integer, primary_key=True) - artifact_name = db.Column(db.String(100)) - artifact_results = db.Column(TEXT) - hostname = db.Column(db.String(100)) - - def __init__(self, artifact_name, artifact_results, hostname): + """ + Class for artifacts collected which stores the artifact name, artifact results (json), and hostname. + This class inherits from SQLAlchemy's Model class. + """ + + id: Column[Integer] = db.Column(db.Integer, primary_key=True) + artifact_name: Column[String] = db.Column(db.String(100)) + artifact_results: Column[TEXT] = db.Column(TEXT) + hostname: Column[String] = db.Column(db.String(100)) + + def __init__(self, artifact_name: str, artifact_results: str, hostname: str): + """ + Initialize a new instance of the Artifact class. + + :param artifact_name: The name of the artifact. + :param artifact_results: The results of the artifact, stored as a JSON string. + :param hostname: The hostname where the artifact was collected. + """ self.artifact_name = artifact_name self.artifact_results = artifact_results self.hostname = hostname - def __repr__(self): + def __repr__(self) -> str: + """ + Returns a string representation of the Artifact instance. + + :return: A string representation of the artifact name. + """ return f"" class ArtifactSchema(ma.Schema): + """ + Schema for serializing and deserializing instances of the Artifact class. + """ + class Meta: - fields = ( + """ + Meta class defines the fields to be serialized/deserialized. + """ + + fields: tuple = ( "id", "artifact_name", "artifact_results", @@ -31,5 +58,5 @@ class Meta: ) -artifact_schema = ArtifactSchema() -artifacts_schema = ArtifactSchema(many=True) +artifact_schema: ArtifactSchema = ArtifactSchema() +artifacts_schema: ArtifactSchema = ArtifactSchema(many=True) diff --git a/backend/app/models/cases.py b/backend/app/models/cases.py index 45ba1b3a..53deb8dc 100644 --- a/backend/app/models/cases.py +++ b/backend/app/models/cases.py @@ -1,28 +1,55 @@ -from datetime import datetime +from sqlalchemy import Column +from sqlalchemy import Integer +from sqlalchemy import String from app import db from app import ma -# Class for cases which stores the case ID, case name, list of agents + # Path: backend\app\models.py class Case(db.Model): - id = db.Column(db.Integer, primary_key=True) - case_id = db.Column(db.Integer) - case_name = db.Column(db.String(100)) - agents = db.Column(db.String(1000)) - - def __init__(self, case_id, case_name, agents): + """ + Class for cases which stores the case ID, case name, and a list of agents. + This class inherits from SQLAlchemy's Model class. + """ + + id: Column[Integer] = db.Column(db.Integer, primary_key=True) + case_id: Column[Integer] = db.Column(db.Integer) + case_name: Column[String] = db.Column(db.String(100)) + agents: Column[String] = db.Column(db.String(1000)) + + def __init__(self, case_id: int, case_name: str, agents: str): + """ + Initialize a new instance of the Case class. + + :param case_id: The ID of the case. + :param case_name: The name of the case. + :param agents: A comma-separated string of agents associated with the case. + """ self.case_id = case_id self.case_name = case_name self.agents = agents - def __repr__(self): + def __repr__(self) -> str: + """ + Returns a string representation of the Case instance. + + :return: A string representation of the case ID. + """ return f"" class CaseSchema(ma.Schema): + """ + Schema for serializing and deserializing instances of the Case class. + """ + class Meta: - fields = ( + """ + Meta class defines the fields to be serialized/deserialized. + """ + + fields: tuple = ( "id", "case_id", "case_name", @@ -30,5 +57,5 @@ class Meta: ) -case_schema = CaseSchema() -cases_schema = CaseSchema(many=True) +case_schema: CaseSchema = CaseSchema() +cases_schema: CaseSchema = CaseSchema(many=True) diff --git a/backend/app/models/connectors.py b/backend/app/models/connectors.py index 870e777d..1b5047dd 100644 --- a/backend/app/models/connectors.py +++ b/backend/app/models/connectors.py @@ -3,6 +3,8 @@ from abc import ABC from abc import abstractmethod from dataclasses import dataclass +from typing import Any +from typing import Dict import grpc import pika @@ -17,10 +19,8 @@ from app.models.models import Connectors -# from werkzeug.utils import secure_filename - -def dynamic_import(module_name, class_name): +def dynamic_import(module_name: str, class_name: str) -> Any: """ This function dynamically imports a module and returns a specific class from it. @@ -43,10 +43,10 @@ class Connector(ABC): :param attributes: A dictionary of attributes necessary for the connector to connect to the service or system. """ - attributes: dict + attributes: Dict[str, Any] @abstractmethod - def verify_connection(self): + def verify_connection(self) -> Dict[str, Any]: """ This abstract method should be implemented by all subclasses of Connector. It is meant to verify the connection to the service or system the connector is designed to connect to. @@ -56,7 +56,7 @@ def verify_connection(self): pass @staticmethod - def get_connector_info_from_db(connector_name): + def get_connector_info_from_db(connector_name: str) -> Dict[str, Any]: """ This method retrieves connector information from the database. @@ -89,10 +89,10 @@ class WazuhIndexerConnector(Connector): :param connector_name: A string that specifies the name of the connector. """ - def __init__(self, connector_name): + def __init__(self, connector_name: str): super().__init__(attributes=self.get_connector_info_from_db(connector_name)) - def verify_connection(self): + def verify_connection(self) -> Dict[str, Any]: """ This method verifies the connection to the Wazuh indexer service. @@ -131,10 +131,10 @@ class GraylogConnector(Connector): :param connector_name: A string that specifies the name of the connector. """ - def __init__(self, connector_name): + def __init__(self, connector_name: str): super().__init__(attributes=self.get_connector_info_from_db(connector_name)) - def verify_connection(self): + def verify_connection(self) -> Dict[str, Any]: """ Verifies the connection to Graylog service. @@ -177,10 +177,10 @@ class WazuhManagerConnector(Connector): :param connector_name: A string that specifies the name of the connector. """ - def __init__(self, connector_name): + def __init__(self, connector_name: str): super().__init__(attributes=self.get_connector_info_from_db(connector_name)) - def verify_connection(self): + def verify_connection(self) -> Dict[str, Any]: """ Verifies the connection to Wazuh manager service. @@ -215,7 +215,7 @@ def verify_connection(self): ) return {"connectionSuccessful": False, "authToken": None} - def get_auth_token(self): + def get_auth_token(self) -> str: """ Returns the authentication token for the Wazuh manager service. @@ -232,10 +232,10 @@ class ShuffleConnector(Connector): :param connector_name: A string that specifies the name of the connector. """ - def __init__(self, connector_name): + def __init__(self, connector_name: str): super().__init__(attributes=self.get_connector_info_from_db(connector_name)) - def verify_connection(self): + def verify_connection(self) -> Dict[str, Any]: """ Verifies the connection to Shuffle service. @@ -278,10 +278,10 @@ class DfirIrisConnector(Connector): :param connector_name: A string that specifies the name of the connector. """ - def __init__(self, connector_name): + def __init__(self, connector_name: str): super().__init__(attributes=self.get_connector_info_from_db(connector_name)) - def verify_connection(self): + def verify_connection(self) -> Dict[str, Any]: """ Verifies the connection to DFIR IRIS service. @@ -326,10 +326,10 @@ class VelociraptorConnector(Connector): connector_name (str): The name of the connector. """ - def __init__(self, connector_name): + def __init__(self, connector_name: str): super().__init__(attributes=self.get_connector_info_from_db(connector_name)) - def verify_connection(self): + def verify_connection(self) -> Dict[str, Any]: """ Verifies the connection to Velociraptor service. @@ -391,10 +391,10 @@ class RabbitMQConnector(Connector): connector_name (str): The name of the connector. """ - def __init__(self, connector_name): + def __init__(self, connector_name: str): super().__init__(attributes=self.get_connector_info_from_db(connector_name)) - def verify_connection(self): + def verify_connection(self) -> Dict[str, Any]: """ Verifies the connection to RabbitMQ service. """ diff --git a/backend/app/models/graylog.py b/backend/app/models/graylog.py index df10cbea..d6b09996 100644 --- a/backend/app/models/graylog.py +++ b/backend/app/models/graylog.py @@ -1,32 +1,53 @@ from datetime import datetime +from sqlalchemy import Column +from sqlalchemy import DateTime +from sqlalchemy import Float +from sqlalchemy import Integer + from app import db from app import ma -# Class for Graylog allocation which stores throughput metrics -# Generate timestamp for each entry and invoke every 5 minutes. + # Path: backend\app\models.py class GraylogMetricsAllocation(db.Model): - id = db.Column(db.Integer, primary_key=True) - input_usage = db.Column(db.Float) - output_usage = db.Column(db.Float) - processor_usage = db.Column(db.Float) - input_1_sec_rate = db.Column(db.Float) - output_1_sec_rate = db.Column(db.Float) - total_input = db.Column(db.Float) - total_output = db.Column(db.Float) - timestamp = db.Column(db.DateTime, default=datetime.utcnow) + """ + Class for Graylog metrics allocation which stores throughput metrics. + The timestamp is generated for each entry and it is invoked every 5 minutes. + This class inherits from SQLAlchemy's Model class. + """ + + id: Column[Integer] = db.Column(db.Integer, primary_key=True) + input_usage: Column[Float] = db.Column(db.Float) + output_usage: Column[Float] = db.Column(db.Float) + processor_usage: Column[Float] = db.Column(db.Float) + input_1_sec_rate: Column[Float] = db.Column(db.Float) + output_1_sec_rate: Column[Float] = db.Column(db.Float) + total_input: Column[Float] = db.Column(db.Float) + total_output: Column[Float] = db.Column(db.Float) + timestamp: Column[DateTime] = db.Column(db.DateTime, default=datetime.utcnow) def __init__( self, - input_usage, - output_usage, - processor_usage, - input_1_sec_rate, - output_1_sec_rate, - total_input, - total_output, + input_usage: float, + output_usage: float, + processor_usage: float, + input_1_sec_rate: float, + output_1_sec_rate: float, + total_input: float, + total_output: float, ): + """ + Initialize a new instance of the GraylogMetricsAllocation class. + + :param input_usage: The input usage value. + :param output_usage: The output usage value. + :param processor_usage: The processor usage value. + :param input_1_sec_rate: The input per second rate. + :param output_1_sec_rate: The output per second rate. + :param total_input: The total input value. + :param total_output: The total output value. + """ self.input_usage = input_usage self.output_usage = output_usage self.processor_usage = processor_usage @@ -35,13 +56,26 @@ def __init__( self.total_input = total_input self.total_output = total_output - def __repr__(self): + def __repr__(self) -> str: + """ + Returns a string representation of the GraylogMetricsAllocation instance. + + :return: A string representation of the instance's id. + """ return f"" class GraylogMetricsAllocationSchema(ma.Schema): + """ + Schema for serializing and deserializing instances of the GraylogMetricsAllocation class. + """ + class Meta: - fields = ( + """ + Meta class defines the fields to be serialized/deserialized. + """ + + fields: tuple = ( "id", "input_usage", "output_usage", @@ -54,5 +88,9 @@ class Meta: ) -graylog_metrics_allocation_schema = GraylogMetricsAllocationSchema() -graylog_metrics_allocations_schema = GraylogMetricsAllocationSchema(many=True) +graylog_metrics_allocation_schema: GraylogMetricsAllocationSchema = ( + GraylogMetricsAllocationSchema() +) +graylog_metrics_allocations_schema: GraylogMetricsAllocationSchema = ( + GraylogMetricsAllocationSchema(many=True) +) diff --git a/backend/app/models/models.py b/backend/app/models/models.py index 2d2786fb..df04a88d 100644 --- a/backend/app/models/models.py +++ b/backend/app/models/models.py @@ -1,30 +1,66 @@ from datetime import datetime from loguru import logger +from sqlalchemy import Boolean +from sqlalchemy import Column +from sqlalchemy import DateTime +from sqlalchemy import Integer +from sqlalchemy import String from app import db from app import ma class ConnectorsAvailable(db.Model): - id = db.Column(db.Integer, primary_key=True) - connector_name = db.Column(db.String(100), unique=True) - connector_description = db.Column(db.String(100)) - connector_supports = db.Column(db.String(100)) - connector_configured = db.Column(db.Boolean, default=False) - connector_verified = db.Column(db.Boolean, default=False) - - def __init__(self, connector_name, connector_supports): + """ + Class representing the available connectors in the application. + This class inherits from SQLAlchemy's Model class. + + :ivar id: Unique integer ID of the connector. + :ivar connector_name: Name of the connector. + :ivar connector_description: Description of the connector. + :ivar connector_supports: Information on what the connector supports. + :ivar connector_configured: Boolean indicating whether the connector is configured or not. + :ivar connector_verified: Boolean indicating whether the connector is verified or not. + """ + + id: Column[Integer] = db.Column(db.Integer, primary_key=True) + connector_name: Column[String] = db.Column(db.String(100), unique=True) + connector_description: Column[String] = db.Column(db.String(100)) + connector_supports: Column[String] = db.Column(db.String(100)) + connector_configured: Column[Boolean] = db.Column(db.Boolean, default=False) + connector_verified: Column[Boolean] = db.Column(db.Boolean, default=False) + + def __init__(self, connector_name: str, connector_supports: str): + """ + Initialize a new instance of the ConnectorsAvailable class. + + :param connector_name: The name of the connector. + :param connector_supports: Information on what the connector supports. + """ self.connector_name = connector_name self.connector_supports = connector_supports - def __repr__(self): + def __repr__(self) -> str: + """ + Returns a string representation of the ConnectorsAvailable instance. + + :return: A string representation of the connector name. + """ return f"" class ConnectorsAvailableSchema(ma.Schema): + """ + Schema for serializing and deserializing instances of the ConnectorsAvailable class. + """ + class Meta: - fields = ( + """ + Meta class defines the fields to be serialized/deserialized. + """ + + fields: tuple = ( "id", "connector_name", "connector_description", @@ -34,38 +70,63 @@ class Meta: ) -connector_available_schema = ConnectorsAvailableSchema() -connectors_available_schema = ConnectorsAvailableSchema(many=True) +connector_available_schema: ConnectorsAvailableSchema = ConnectorsAvailableSchema() +connectors_available_schema: ConnectorsAvailableSchema = ConnectorsAvailableSchema( + many=True, +) -# Class for the connector which will store the endpoint url, connector name, connector type, connector last updated, -# username and password class Connectors(db.Model): - id = db.Column(db.Integer, primary_key=True) - connector_name = db.Column(db.String(100), unique=True) - connector_type = db.Column(db.String(100)) - connector_url = db.Column(db.String(100)) - connector_last_updated = db.Column(db.DateTime, default=datetime.utcnow) - connector_username = db.Column(db.String(100)) - connector_password = db.Column(db.String(100)) - connector_api_key = db.Column(db.String(100)) + """ + Class for the connector which will store the endpoint url, connector name, connector type, connector last updated, + username and password. + + :ivar id: Unique integer ID of the connector. + :ivar connector_name: Name of the connector. + :ivar connector_type: Type of the connector. + :ivar connector_url: URL of the connector. + :ivar connector_last_updated: Timestamp when the connector was last updated. + :ivar connector_username: Username for the connector. + :ivar connector_password: Password for the connector. + :ivar connector_api_key: API key for the connector. + """ + + id: Column[Integer] = db.Column(db.Integer, primary_key=True) + connector_name: Column[String] = db.Column(db.String(100), unique=True) + connector_type: Column[String] = db.Column(db.String(100)) + connector_url: Column[String] = db.Column(db.String(100)) + connector_last_updated: Column[DateTime] = db.Column( + db.DateTime, default=datetime.utcnow, + ) + connector_username: Column[String] = db.Column(db.String(100)) + connector_password: Column[String] = db.Column(db.String(100)) + connector_api_key: Column[String] = db.Column(db.String(100)) def __init__( self, - connector_name, - connector_type, - connector_url, - connector_username, - connector_password, - connector_api_key, + connector_name: str, + connector_type: str, + connector_url: str, + connector_username: str, + connector_password: str, + connector_api_key: str, ): + """ + Initialize a new instance of the Connectors class. + + :param connector_name: The name of the connector. + :param connector_type: The type of the connector. + :param connector_url: The URL of the connector. + :param connector_username: The username for the connector. + :param connector_password: The password for the connector. + :param connector_api_key: The API key for the connector. + """ self.connector_name = connector_name self.connector_type = connector_type self.connector_url = connector_url self.connector_username = connector_username self.connector_password = connector_password - # If the `connector_name` is `shuffle` or `dfir-irs` then set the `connector_api_key`. Otherwise set it to - # `None` + if ( connector_name.lower() == "shuffle" or connector_name.lower() == "dfir-irs" @@ -77,13 +138,26 @@ def __init__( logger.info(f"Not setting the API key for {connector_name}") self.connector_api_key = None - def __repr__(self): + def __repr__(self) -> str: + """ + Returns a string representation of the Connectors instance. + + :return: A string representation of the connector name. + """ return f"" class ConnectorsSchema(ma.Schema): + """ + Schema for serializing and deserializing instances of the Connectors class. + """ + class Meta: - fields = ( + """ + Meta class defines the fields to be serialized/deserialized. + """ + + fields: tuple = ( "id", "connector_name", "connector_type", @@ -95,8 +169,8 @@ class Meta: ) -connector_schema = ConnectorsSchema() -connectors_schema = ConnectorsSchema(many=True) +connector_schema: ConnectorsSchema = ConnectorsSchema() +connectors_schema: ConnectorsSchema = ConnectorsSchema(many=True) # Class for the disabled rule IDs which will store the rule ID, previous configuration, new configuration, reason for diff --git a/backend/app/models/rules.py b/backend/app/models/rules.py index 6a11db3b..b0fbdc30 100644 --- a/backend/app/models/rules.py +++ b/backend/app/models/rules.py @@ -1,45 +1,81 @@ from datetime import datetime +from sqlalchemy import Column +from sqlalchemy import DateTime +from sqlalchemy import Integer +from sqlalchemy import String + from app import db from app import ma -# from loguru import logger -# from sqlalchemy.dialects.postgresql import JSONB # Add this line - -# Class for the disabled rule IDs which will store the rule ID, previous configuration, new configuration, reason for -# disabling, date disabled, and the length of time the rule will be disabled for # Path: backend\app\rules.py class DisabledRules(db.Model): - id = db.Column(db.Integer, primary_key=True) - rule_id = db.Column(db.String(100)) - previous_level = db.Column(db.String(1000)) - new_level = db.Column(db.String(1000)) - reason_for_disabling = db.Column(db.String(100)) - date_disabled = db.Column(db.DateTime, default=datetime.utcnow) - length_of_time = db.Column(db.Integer) + """ + Class for disabled rules which stores the rule ID, previous configuration, new configuration, reason for + disabling, date disabled, and the length of time the rule will be disabled for. + This class inherits from SQLAlchemy's Model class. + + :ivar id: Unique integer ID of the rule. + :ivar rule_id: ID of the rule. + :ivar previous_level: Previous level configuration of the rule. + :ivar new_level: New level configuration of the rule. + :ivar reason_for_disabling: Reason for disabling the rule. + :ivar date_disabled: Date when the rule was disabled. + :ivar length_of_time: Length of time the rule will be disabled for. + """ + + id: Column[Integer] = db.Column(db.Integer, primary_key=True) + rule_id: Column[String] = db.Column(db.String(100)) + previous_level: Column[String] = db.Column(db.String(1000)) + new_level: Column[String] = db.Column(db.String(1000)) + reason_for_disabling: Column[String] = db.Column(db.String(100)) + date_disabled: Column[DateTime] = db.Column(db.DateTime, default=datetime.utcnow) + length_of_time: Column[Integer] = db.Column(db.Integer) def __init__( self, - rule_id, - previous_level, - new_level, - reason_for_disabling, - length_of_time, + rule_id: str, + previous_level: str, + new_level: str, + reason_for_disabling: str, + length_of_time: int, ): + """ + Initialize a new instance of the DisabledRules class. + + :param rule_id: The ID of the rule. + :param previous_level: The previous level configuration of the rule. + :param new_level: The new level configuration of the rule. + :param reason_for_disabling: The reason for disabling the rule. + :param length_of_time: The length of time the rule will be disabled for. + """ self.rule_id = rule_id self.previous_level = previous_level self.new_level = new_level self.reason_for_disabling = reason_for_disabling self.length_of_time = length_of_time - def __repr__(self): + def __repr__(self) -> str: + """ + Returns a string representation of the DisabledRules instance. + + :return: A string representation of the rule ID. + """ return f"" class DisabledRulesSchema(ma.Schema): + """ + Schema for serializing and deserializing instances of the DisabledRules class. + """ + class Meta: - fields = ( + """ + Meta class defines the fields to be serialized/deserialized. + """ + + fields: tuple = ( "id", "rule_id", "previous_level", @@ -50,5 +86,5 @@ class Meta: ) -disabled_rule_schema = DisabledRulesSchema() -disabled_rules_schema = DisabledRulesSchema(many=True) +disabled_rule_schema: DisabledRulesSchema = DisabledRulesSchema() +disabled_rules_schema: DisabledRulesSchema = DisabledRulesSchema(many=True) diff --git a/backend/app/models/wazuh_indexer.py b/backend/app/models/wazuh_indexer.py index 93e184fb..d74aa99e 100644 --- a/backend/app/models/wazuh_indexer.py +++ b/backend/app/models/wazuh_indexer.py @@ -1,41 +1,82 @@ from datetime import datetime +from sqlalchemy import Column +from sqlalchemy import DateTime +from sqlalchemy import Float +from sqlalchemy import Integer +from sqlalchemy import String + from app import db from app import ma -# Class for Wazuh Indexer allocation which stores disk stats and the host. -# Generate timestamp for each entry and invoke every 5 minutes. + # Path: backend\app\models.py class WazuhIndexerAllocation(db.Model): - id = db.Column(db.Integer, primary_key=True) - node = db.Column(db.String(100)) - disk_used = db.Column(db.Float) - disk_available = db.Column(db.Float) - disk_total = db.Column(db.Float) - disk_percent = db.Column(db.Float) - timestamp = db.Column(db.DateTime, default=datetime.utcnow) + """ + Class for Wazuh indexer allocation which stores disk stats and the host. + The timestamp is generated for each entry and it is invoked every 5 minutes. + This class inherits from SQLAlchemy's Model class. + + :ivar id: Unique integer ID for the allocation. + :ivar node: The node or host for the allocation. + :ivar disk_used: The amount of disk used. + :ivar disk_available: The amount of disk available. + :ivar disk_total: The total amount of disk. + :ivar disk_percent: The percent of disk used. + :ivar timestamp: The timestamp when the allocation was created. + """ + + id: Column[Integer] = db.Column(db.Integer, primary_key=True) + node: Column[String] = db.Column(db.String(100)) + disk_used: Column[Float] = db.Column(db.Float) + disk_available: Column[Float] = db.Column(db.Float) + disk_total: Column[Float] = db.Column(db.Float) + disk_percent: Column[Float] = db.Column(db.Float) + timestamp: Column[DateTime] = db.Column(db.DateTime, default=datetime.utcnow) def __init__( self, - node, - disk_used, - disk_available, - disk_total, - disk_percent, + node: str, + disk_used: float, + disk_available: float, + disk_total: float, + disk_percent: float, ): + """ + Initialize a new instance of the WazuhIndexerAllocation class. + + :param node: The node or host for the allocation. + :param disk_used: The amount of disk used. + :param disk_available: The amount of disk available. + :param disk_total: The total amount of disk. + :param disk_percent: The percent of disk used. + """ self.node = node self.disk_used = disk_used self.disk_available = disk_available self.disk_total = disk_total self.disk_percent = disk_percent - def __repr__(self): + def __repr__(self) -> str: + """ + Returns a string representation of the WazuhIndexerAllocation instance. + + :return: A string representation of the node. + """ return f"" class WazuhIndexerAllocationSchema(ma.Schema): + """ + Schema for serializing and deserializing instances of the WazuhIndexerAllocation class. + """ + class Meta: - fields = ( + """ + Meta class defines the fields to be serialized/deserialized. + """ + + fields: tuple = ( "id", "node", "disk_used", @@ -46,5 +87,9 @@ class Meta: ) -wazuh_indexer_allocation_schema = WazuhIndexerAllocationSchema() -wazuh_indexer_allocations_schema = WazuhIndexerAllocationSchema(many=True) +wazuh_indexer_allocation_schema: WazuhIndexerAllocationSchema = ( + WazuhIndexerAllocationSchema() +) +wazuh_indexer_allocations_schema: WazuhIndexerAllocationSchema = ( + WazuhIndexerAllocationSchema(many=True) +) diff --git a/backend/app/routes/agents.py b/backend/app/routes/agents.py index 03d223db..7d30dbb5 100644 --- a/backend/app/routes/agents.py +++ b/backend/app/routes/agents.py @@ -40,7 +40,7 @@ def get_agent(agent_id): json: A JSON response containing the details of the agent. """ service = AgentService() - agent = service.get_agent(agent_id) + agent = service.get_agent(agent_id=agent_id) return agent