Skip to content

Commit

Permalink
Merge pull request #197 from Limmen/dev2
Browse files Browse the repository at this point in the history
emulation_defender_machine_observation_state.py
  • Loading branch information
Limmen authored Aug 16, 2023
2 parents 9d7db32 + a598165 commit 6ac927c
Showing 1 changed file with 31 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ def __init__(self, ips: List[str], kafka_config: KafkaConfig,
self.ossec_ids_alert_counters = ossec_ids_alert_counters
if self.ossec_ids_alert_counters is None:
self.ossec_ids_alert_counters = OSSECIdsAlertCounters()
self.host_metrics_consumer_thread = None
self.docker_stats_consumer_thread = None
self.snort_ids_log_consumer_thread = None
self.ossec_ids_log_consumer_thread = None
self.host_metrics_consumer_thread: Optional[HostMetricsConsumerThread] = None
self.docker_stats_consumer_thread: Optional[DockerHostStatsConsumerThread] = None
self.snort_ids_log_consumer_thread: Optional[SnortIdsLogConsumerThread] = None
self.ossec_ids_log_consumer_thread: Optional[OSSECIdsLogConsumerThread] = None

def start_monitor_threads(self) -> None:
"""
Expand Down Expand Up @@ -114,6 +114,8 @@ def from_dict(d: Dict[str, Any]) -> "EmulationDefenderMachineObservationState":
ossec_alert_counters = OSSECIdsAlertCounters.from_dict(d["ossec_ids_alert_counters"])
else:
ossec_alert_counters = OSSECIdsAlertCounters()
if kafka_config is None:
raise ValueError("KafkaConfig is None")
obj = EmulationDefenderMachineObservationState(
ips=d["ips"], kafka_config=kafka_config,
host_metrics=HostMetrics.from_dict(d["host_metrics"]),
Expand All @@ -131,15 +133,15 @@ def to_dict(self) -> Dict[str, Any]:
:return: a dict representation of the object
"""
d = {}
d: Dict[str, Any] = {}
d["ips"] = self.ips
d["os"] = self.os
d["ports"] = list(map(lambda x: x.to_dict(), self.ports))
d["ssh_connections"] = list(map(lambda x: x.to_dict(), self.ssh_connections))
d["host_metrics"] = self.host_metrics.to_dict()
d["docker_stats"] = self.docker_stats.to_dict()
d["ossec_ids_alert_counters"] = self.ossec_ids_alert_counters.to_dict()
d["snort_ids_ip_alert_counters"] = self.snort_ids_ip_alert_counters.to_dict()
d["host_metrics"] = self.host_metrics.to_dict() if self.host_metrics is not None else None
d["docker_stats"] = self.docker_stats.to_dict() if self.docker_stats is not None else None
d["ossec_ids_alert_counters"] = self.ossec_ids_alert_counters.to_dict() if self.ossec_ids_alert_counters is not None else None
d["snort_ids_ip_alert_counters"] = self.snort_ids_ip_alert_counters.to_dict() if self.snort_ids_ip_alert_counters is not None else None
if self.kafka_config is not None:
d["kafka_config"] = self.kafka_config.to_dict()
else:
Expand Down Expand Up @@ -183,7 +185,7 @@ def cleanup(self) -> None:
self.snort_ids_log_consumer_thread.running = False
self.snort_ids_log_consumer_thread.consumer.close()
if self.ossec_ids_log_consumer_thread is not None:
self.ossec_ids_alert_counters.running = False
self.ossec_ids_log_consumer_thread.running = False
self.ossec_ids_log_consumer_thread.consumer.close()
for c in self.ssh_connections:
c.cleanup()
Expand All @@ -193,15 +195,26 @@ def copy(self) -> "EmulationDefenderMachineObservationState":
:return: a copy of the object
"""
m_copy = EmulationDefenderMachineObservationState(
ips=self.ips, kafka_config=self.kafka_config, host_metrics=self.host_metrics.copy(),
docker_stats=self.docker_stats.copy())
ips=self.ips, kafka_config=self.kafka_config,
host_metrics=self.host_metrics.copy() if self.host_metrics is not None else self.host_metrics,
docker_stats=self.docker_stats.copy() if self.docker_stats is not None else self.docker_stats)
m_copy.os = self.os
m_copy.ports = list(map(lambda x: x.copy(), self.ports))
m_copy.ssh_connections = self.ssh_connections
m_copy.host_metrics = self.host_metrics.copy()
m_copy.docker_stats = self.docker_stats.copy()
m_copy.snort_ids_ip_alert_counters = self.snort_ids_ip_alert_counters.copy()
m_copy.ossec_ids_alert_counters = self.ossec_ids_alert_counters.copy()
if self.ports == []:
m_copy.ports = self.ports
else:
m_copy.ports = list(map(lambda x: x.copy(), self.ports))
if self.ssh_connections == []:
m_copy.ssh_connections = self.ssh_connections
else:
m_copy.ssh_connections = list(map(lambda x: x.copy(), self.ssh_connections))
if self.snort_ids_ip_alert_counters is None:
m_copy.snort_ids_ip_alert_counters = self.snort_ids_ip_alert_counters
else:
m_copy.snort_ids_ip_alert_counters = self.snort_ids_ip_alert_counters.copy()
if self.ossec_ids_alert_counters is None:
m_copy.ossec_ids_alert_counters = self.ossec_ids_alert_counters
else:
m_copy.ossec_ids_alert_counters = self.ossec_ids_alert_counters.copy()
return m_copy

@staticmethod
Expand Down

0 comments on commit 6ac927c

Please sign in to comment.