Skip to content

Commit

Permalink
merge(#66): Merge
Browse files Browse the repository at this point in the history
  • Loading branch information
pro-akim committed Sep 6, 2023
2 parents fb5f2d0 + 5be2351 commit aca23ac
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/wazuh_qa_framework/global_variables/daemons.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
WAZUH_DB_DAEMON = 'wazuh-db'
AGENT_DAEMON = 'wazuh-agentd'

WAZUH_ANGENT_WINDOWS_SERVICE_NAME = 'WazuhSvc'
WAZUH_AGENT_WINDOWS_SERVICE_NAME = 'WazuhSvc'


WAZUH_AGENT_DAEMONS = [LOGCOLLECTOR_DAEMON, SYSCHECK_DAEMON, EXEC_DAEMON, MODULES_DAEMON, AGENT_DAEMON]
Expand Down
100 changes: 92 additions & 8 deletions src/wazuh_qa_framework/system/wazuh_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
from multiprocessing.pool import ThreadPool

from wazuh_qa_framework.generic_modules.logging.base_logger import BaseLogger
from wazuh_qa_framework.generic_modules.tools.configuration import conf_functions, xml_configuration_files
from wazuh_qa_framework.global_variables.daemons import WAZUH_ANGENT_WINDOWS_SERVICE_NAME
from wazuh_qa_framework.global_variables.daemons import WAZUH_AGENT_WINDOWS_SERVICE_NAME
from wazuh_qa_framework.system.host_manager import HostManager


Expand Down Expand Up @@ -627,8 +626,8 @@ def restart_agent(self, host):
Args:
host (str): Hostname
"""
self.logger.debug(f"Restarting agent {host}")
service_name = WAZUH_ANGENT_WINDOWS_SERVICE_NAME if self.is_windows(host) else 'wazuh-agent'
self.logger.debug(f'Restarting agent {host}')
service_name = WAZUH_AGENT_WINDOWS_SERVICE_NAME if self.is_windows(host) else 'wazuh-agent'
if self.is_agent(host):
self.control_service(host, service_name, 'restarted')
self.logger.debug(f"Agent {host} restarted successfully")
Expand Down Expand Up @@ -684,8 +683,8 @@ def stop_agent(self, host):
Args:
host (str): Hostname
"""
self.logger.debug(f"Stopping agent {host}")
service_name = WAZUH_ANGENT_WINDOWS_SERVICE_NAME if self.is_windows(host) else 'wazuh-agent'
self.logger.debug(f'Stopping agent {host}')
service_name = WAZUH_AGENT_WINDOWS_SERVICE_NAME if self.is_windows(host) else 'wazuh-agent'
if self.is_agent(host):
self.control_service(host, service_name, 'stopped')
self.logger.debug(f"Agent {host} stopped successfully")
Expand Down Expand Up @@ -741,8 +740,8 @@ def start_agent(self, host):
Args:
host (str): Hostname
"""
self.logger.debug(f"Starting agent {host}")
service_name = WAZUH_ANGENT_WINDOWS_SERVICE_NAME if self.is_windows(host) else 'wazuh-agent'
self.logger.debug(f'Starting agent {host}')
service_name = WAZUH_AGENT_WINDOWS_SERVICE_NAME if self.is_windows(host) else 'wazuh-agent'
if self.is_agent(host):
self.control_service(host, service_name, 'started')
self.logger.debug(f"Agent {host} started successfully")
Expand Down Expand Up @@ -979,3 +978,88 @@ def is_manager(self, host):
bool: True if host is manager
"""
return host in self.get_managers()

def create_rule(self, host, new_rules_filepath, rules_filename, overwrite=True):
"""Create new rules replaces an existing rule file or adds rules to an existing file from a file.
Args:
host (str): Host name.
new_rules_filepath (str): New rules filepath.
rules_filename (str): Host rules filename.
overwrite (bool): Replace the rules. Defaults to True.
"""
try:
with open(new_rules_filepath, 'r') as file:
new_rules = file.read()

files_information = self.find_files(host, os.path.dirname(rules_filename), '*')
file_exist = False
for file_information in files_information:
if rules_filename in file_information['path']:
file_exist = True
break

if file_exist:
if overwrite:
self.logger.info(message=f'Changing {rules_filename} to {new_rules_filepath}')
else:
current_rules = self.get_file_content(host, rules_filename)
index_rule = current_rules.rfind("</rule>")
if index_rule != -1:
new_rules = current_rules[:index_rule] + '</rule>\n' + new_rules + '\n</group>'
self.logger.info(message=f'Adding rule from {new_rules_filepath} to {rules_filename}')

self.modify_file_content(host, rules_filename, new_rules)
self.logger.info(message=f'Rules succefully updated')

else:
if overwrite:
self.logger.info(message=f'{rules_filename} does not exist to be overwritten')
else:
self.create_file(host, rules_filename, f"<group>\n{new_rules}\n</group>")
self.logger.info(message=f'Rules succefully added into a new rule file named {rules_filename}')

except FileNotFoundError:
self.logger.error(message=f'The file {new_rules_filepath} does not exist.')

def create_decoder(self, host, new_decoder_filepath, decoder_filename, overwrite=True):
"""Create new decoder replaces an existing decoder file or adds decoders to an existing file from a file.
Args:
host (str): Host name.
new_decoder_filepath (str): New decoder filepath.
decoder_filename (str): Host decoder filename.
overwrite (bool): Replace the decoder file. Defaults to True.
"""
try:
with open(new_decoder_filepath, 'r') as file:
new_decoders = file.read()

files_information = self.find_files(host, os.path.dirname(decoder_filename), '*')
file_exist = False
for file_information in files_information:
if decoder_filename in file_information['path']:
file_exist = True
break

if file_exist:
if overwrite:
self.logger.info(message=f'Changing {decoder_filename} to {new_decoder_filepath}')
else:
current_decoders = self.get_file_content(host, decoder_filename)
index_decoder = current_decoders.rfind("</decoder>")
if index_decoder != -1:
new_decoders = current_decoders[:index_decoder] + '</decoder>\n' + new_decoders
self.logger.info(message=f'Adding decoder from {new_decoder_filepath} to {decoder_filename}')
self.modify_file_content(host, decoder_filename, new_decoders)
self.logger.info(message=f'Decoders succefully updated')

else:
if overwrite:
self.logger.info(message=f'{decoder_filename} does not exist to be overwritten')
else:
self.create_file(host, decoder_filename, new_decoders)
self.logger.info(message=f'Decoders succefully added into a new file named {decoder_filename}')

except FileNotFoundError:
self.logger.error(message=f'The file {new_decoder_filepath} does not exist.')

0 comments on commit aca23ac

Please sign in to comment.