From b60486689e1a48a6d85e94cf7198fff8a499b5bf Mon Sep 17 00:00:00 2001 From: Antonio Date: Mon, 29 Apr 2024 12:09:32 +0200 Subject: [PATCH] fix(#5229): Linux os fixed --- .../modules/testing/tests/helpers/agent.py | 67 +++++++++++-------- .../modules/testing/tests/helpers/generic.py | 19 ++++-- .../modules/testing/tests/helpers/utils.py | 2 + .../testing/tests/test_agent/test_install.py | 1 - 4 files changed, 52 insertions(+), 37 deletions(-) diff --git a/deployability/modules/testing/tests/helpers/agent.py b/deployability/modules/testing/tests/helpers/agent.py index f0dd5133ea..18ef2aa10b 100644 --- a/deployability/modules/testing/tests/helpers/agent.py +++ b/deployability/modules/testing/tests/helpers/agent.py @@ -25,7 +25,7 @@ def install_agent(inventory_path, agent_name, wazuh_version, wazuh_revision, liv os_type = HostInformation.get_os_type(inventory_path) commands = [] - if 'linux' in os_type: + if os_type == 'linux': distribution = HostInformation.get_linux_distribution(inventory_path) architecture = HostInformation.get_architecture(inventory_path) @@ -53,7 +53,7 @@ def install_agent(inventory_path, agent_name, wazuh_version, wazuh_revision, liv ] commands.extend(system_commands) - elif 'windows' in os_type : + elif os_type == 'windows' : commands.extend([ f"Invoke-WebRequest -Uri https://packages.wazuh.com/{release}/windows/wazuh-agent-{wazuh_version}-1.msi " "-OutFile $env:TEMP\wazuh-agent.msi" @@ -65,7 +65,7 @@ def install_agent(inventory_path, agent_name, wazuh_version, wazuh_revision, liv f"WAZUH_REGISTRATION_SERVER='MANAGER_IP' " ]) commands.extend(["NET START WazuhSvc"]) - elif 'macos' in os_type: + elif os_type == 'macos': if 'amd64' in architecture: commands.extend([ f'curl -so wazuh-agent.pkg https://{s3_url}.wazuh.com/{release}/macos/wazuh-agent-{wazuh_version}-1.intel64.pkg && echo "WAZUH_MANAGER=\'MANAGER_IP\' && WAZUH_AGENT_NAME=\'{agent_name}\'" > /tmp/wazuh_envs && sudo installer -pkg ./wazuh-agent.pkg -target /' @@ -104,29 +104,38 @@ def register_agent(inventory_path, manager_path): os_type = HostInformation.get_os_type(inventory_path) logger.info(f'Registering agent in {HostInformation.get_os_name_and_version_from_inventory(inventory_path)}') - os_type = HostInformation.get_os_type(inventory_path) if os_type == 'linux': - host_ip = HostInformation.get_internal_ip_from_aws_dns(manager_host) if 'amazonaws' in manager_host else manager_host - commands = [ - f"sed -i 's/
MANAGER_IP<\/address>/
{host_ip}<\/address>/g' {WAZUH_CONF}", - "systemctl restart wazuh-agent" - ] - ConnectionManager.execute_commands(inventory_path, commands) - assert host_ip in ConnectionManager.execute_commands(inventory_path, f'cat {WAZUH_CONF}'), logger.error(f'Error configuring the Manager IP ({host_ip}) in: {HostInformation.get_os_name_and_version_from_inventory(inventory_path)} agent') + try: + host_ip = HostInformation.get_internal_ip_from_aws_dns(manager_host) if 'amazonaws' in manager_host else manager_host + commands = [ + f"sed -i 's/
MANAGER_IP<\/address>/
{host_ip}<\/address>/g' {WAZUH_CONF}", + "systemctl restart wazuh-agent" + ] + ConnectionManager.execute_commands(inventory_path, commands) + except Exception as e: + raise Exception(f'Error registering agent. Error executing: {commands} with error: {e}') + + result = ConnectionManager.execute_commands(inventory_path, f'cat {WAZUH_CONF}') + assert host_ip in result.get('output'), logger.error(f'Error configuring the Manager IP ({host_ip}) in: {HostInformation.get_os_name_and_version_from_inventory(inventory_path)} agent') elif os_type == 'macos': - if 'amazonaws' in manager_host and 'amazonaws' in agent_host: - host_ip = HostInformation.get_internal_ip_from_aws_dns(manager_host) - else: - host_ip = HostInformation.get_public_ip_from_aws_dns(manager_host) - commands = [ - f"sed -i '.bak' 's/
MANAGER_IP<\/address>/
{host_ip}<\/address>/g' /Library/Ossec/etc/ossec.conf", - "/Library/Ossec/bin/wazuh-control restart" - ] - ConnectionManager.execute_commands(inventory_path, commands) - assert host_ip in ConnectionManager.execute_commands(inventory_path, f'cat /Library/Ossec/etc/ossec.conf'), logger.error(f'Error configuring the Manager IP ({host_ip}) in: {HostInformation.get_os_name_and_version_from_inventory(inventory_path)} agent') + try: + if 'amazonaws' in manager_host and 'amazonaws' in agent_host: + host_ip = HostInformation.get_internal_ip_from_aws_dns(manager_host) + else: + host_ip = HostInformation.get_public_ip_from_aws_dns(manager_host) + commands = [ + f"sed -i '.bak' 's/
MANAGER_IP<\/address>/
{host_ip}<\/address>/g' /Library/Ossec/etc/ossec.conf", + "/Library/Ossec/bin/wazuh-control restart" + ] + ConnectionManager.execute_commands(inventory_path, commands) + except Exception as e: + raise Exception(f'Error registering agent. Error executing: {commands} with error: {e}') - elif 'windows' in os_type : + result = ConnectionManager.execute_commands(inventory_path, f'cat {WAZUH_CONF}') + assert host_ip in result.get('output'), logger.error(f'Error configuring the Manager IP ({host_ip}) in: {HostInformation.get_os_name_and_version_from_inventory(inventory_path)} agent') + + elif os_type == 'windows': try: host_ip = HostInformation.get_internal_ip_from_aws_dns(manager_host) if 'amazonaws' in manager_host else manager_host commands = [ @@ -139,13 +148,13 @@ def register_agent(inventory_path, manager_path): raise Exception(f'Error registering agent. Error executing: {commands} with error: {e}') result = ConnectionManager.execute_commands(inventory_path, f'Get-Content "{WAZUH_WINDOWS_CONF}"') - assert host_ip in result.get('output'), logger.error(f'Error configuring the Manager IP ({host_ip})in: {HostInformation.get_os_name_and_version_from_inventory(inventory_path)} agent') + assert host_ip in result.get('output'), logger.error(f'Error configuring the Manager IP ({host_ip}) in: {HostInformation.get_os_name_and_version_from_inventory(inventory_path)} agent') @staticmethod def set_protocol_agent_connection(inventory_path, protocol): os_type = HostInformation.get_os_type(inventory_path) - if 'linux' in os_type: + if os_type == 'linux': commands = [ f"sed -i 's/[^<]*<\/protocol>/{protocol}<\/protocol>/g' {WAZUH_CONF}", "systemctl restart wazuh-agent" @@ -155,7 +164,7 @@ def set_protocol_agent_connection(inventory_path, protocol): result = ConnectionManager.execute_commands(inventory_path, f'cat {WAZUH_CONF}') assert protocol in result.get('output'), logger.error(f'Error configuring the protocol ({protocol}) in: {HostInformation.get_os_name_and_version_from_inventory(inventory_path)} agent') - elif 'macos' in os_type: + elif os_type == 'macos': commands = [ f"sed -i '' 's/[^<]*<\/protocol>/{protocol}<\/protocol>/g' /Library/Ossec/etc/ossec.conf", "/Library/Ossec/bin/wazuh-control restart" @@ -163,7 +172,7 @@ def set_protocol_agent_connection(inventory_path, protocol): ConnectionManager.execute_commands(inventory_path, commands) assert protocol in ConnectionManager.execute_commands(inventory_path, f'cat /Library/Ossec/etc/ossec.conf'), logger.error(f'Error configuring the protocol ({protocol}) in: {HostInformation.get_os_name_and_version_from_inventory(inventory_path)} agent') - elif 'windows' in os_type : + elif os_type == 'windows': commands = [ f"(Get-Content -Path '{WAZUH_WINDOWS_CONF}') -replace '[^<]*<\/protocol>', '{protocol}' | Set-Content -Path '{WAZUH_WINDOWS_CONF}'" ] @@ -177,7 +186,7 @@ def set_protocol_agent_connection(inventory_path, protocol): def uninstall_agent(inventory_path, wazuh_version=None, wazuh_revision=None) -> None: os_type = HostInformation.get_os_type(inventory_path) commands = [] - if 'linux' in os_type: + if os_type == 'linux': distribution = HostInformation.get_linux_distribution(inventory_path) os_name = HostInformation.get_os_name_from_inventory(inventory_path) if os_name == 'opensuse' or os_name == 'suse': @@ -204,11 +213,11 @@ def uninstall_agent(inventory_path, wazuh_version=None, wazuh_revision=None) -> ] commands.extend(system_commands) - elif 'windows' in os_type: + elif os_type == 'windows': commands.extend([ f"msiexec.exe /x $env:TEMP\wazuh-agent.msi /qn" ]) - elif 'macos' in os_type: + elif os_type == 'macos': commands.extend([ "/Library/Ossec/bin/wazuh-control stop", "/bin/rm -r /Library/Ossec", diff --git a/deployability/modules/testing/tests/helpers/generic.py b/deployability/modules/testing/tests/helpers/generic.py index 219344019f..f274152fd3 100644 --- a/deployability/modules/testing/tests/helpers/generic.py +++ b/deployability/modules/testing/tests/helpers/generic.py @@ -556,9 +556,16 @@ def _checkfiles(inventory_path, os_type, directory, filters_keywords= None, hash Dict: dict of directories:hash """ if 'linux' == os_type: - command = f'sudo find {directory} -type f -exec sha256sum {{}} + {filter}' - result = ConnectionManager.execute_commands(inventory_path, command) + filters = f"| grep -v {filters_keywords[0]}" + for filter_ in filters_keywords[1:]: + filters += f" | grep -v {filter_}" + command = f'sudo find {directory} -type f -exec sha256sum {{}} + {filters}' + result = ConnectionManager.execute_commands(inventory_path, command).get('output') + elif 'macos' == os_type: + filters = f"| grep -v {filters_keywords[0]}" + for filter_ in filters_keywords[1:]: + filters += f" | grep -v {filter_}" command = f'sudo find {directory} -type f -exec shasum -a 256 {{}} \; {filter}' result = ConnectionManager.execute_commands(inventory_path, command) elif 'windows' in os_type: @@ -640,13 +647,10 @@ def perform_action_and_scan(inventory_path, callback) -> dict: elif os_type == 'windows': directories = ['C:\\Program Files', 'C:\\Program Files (x86)','C:\\Users\\vagrant'] filters_keywords = ['log','tmp','ossec-agent', 'EdgeUpdate'] - elif 'macos' in inventory_path: + elif os_type == 'macos': directories = ['/usr/bin', '/usr/sbin'] filters_keywords = ['grep'] - filters = f"| grep -v {filters_keywords[0]}" - for filter_ in filters_keywords[1:]: - filters+= f" | grep -v {filter_}" initial_scans = CheckFiles._perform_scan(inventory_path, os_type, directories, filters_keywords) callback() second_scans = CheckFiles._perform_scan(inventory_path, os_type, directories, filters_keywords) @@ -824,11 +828,12 @@ def isComponentActive(inventory_path, host_role) -> bool: os_type = HostInformation.get_os_type(inventory_path) if os_type == 'linux': - return 'active' == ConnectionManager.execute_commands(inventory_path, f'systemctl is-active {host_role}').get('output').replace("\n", "") + elif os_type == 'windows': result = ConnectionManager.execute_commands(inventory_path, "Get-Service -Name 'Wazuh'") return result.get('success') + elif os_type == 'macos': return f'com.{host_role.replace("-", ".")}' in ConnectionManager.execute_commands(inventory_path, f'launchctl list | grep com.{host_role.replace("-", ".")}') diff --git a/deployability/modules/testing/tests/helpers/utils.py b/deployability/modules/testing/tests/helpers/utils.py index c79dec259f..cb785ced49 100644 --- a/deployability/modules/testing/tests/helpers/utils.py +++ b/deployability/modules/testing/tests/helpers/utils.py @@ -70,6 +70,7 @@ def check_inventory_connection(inventory_path, attempts=10, sleep=30) -> bool: except Exception as e: logger.warning(f'Error on attempt {attempt} of {attempts}: {e}') time.sleep(sleep) + elif os_type == 'windows': if port == 5986: protocol = 'https' @@ -90,6 +91,7 @@ def check_inventory_connection(inventory_path, attempts=10, sleep=30) -> bool: except Exception as e: logger.warning(f'Error on attempt {attempt} of {attempts}: {e}') time.sleep(sleep) + elif os_type == 'macos': ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) diff --git a/deployability/modules/testing/tests/test_agent/test_install.py b/deployability/modules/testing/tests/test_agent/test_install.py index dbb0f9b71f..f5e5ba9322 100644 --- a/deployability/modules/testing/tests/test_agent/test_install.py +++ b/deployability/modules/testing/tests/test_agent/test_install.py @@ -83,7 +83,6 @@ def test_installation(wazuh_params): # Agent installation for agent_name, agent_params in wazuh_params['agents'].items(): WazuhAgent.perform_install_and_scan_for_agent(agent_params, agent_name, wazuh_params) - #WazuhAgent.install_agent(agent_params, agent_name, wazuh_params['wazuh_version'], wazuh_params['wazuh_revision'], wazuh_params['live']) # Testing installation directory for agent in wazuh_params['agents'].values():