Skip to content

Commit

Permalink
fix(#5229): Linux os fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
pro-akim committed Apr 29, 2024
1 parent 0dabce1 commit b604866
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 37 deletions.
67 changes: 38 additions & 29 deletions deployability/modules/testing/tests/helpers/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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"
Expand All @@ -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 /'
Expand Down Expand Up @@ -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/<address>MANAGER_IP<\/address>/<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/<address>MANAGER_IP<\/address>/<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/<address>MANAGER_IP<\/address>/<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/<address>MANAGER_IP<\/address>/<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 = [
Expand All @@ -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>{protocol}<\/protocol>/g' {WAZUH_CONF}",
"systemctl restart wazuh-agent"
Expand All @@ -155,15 +164,15 @@ 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>{protocol}<\/protocol>/g' /Library/Ossec/etc/ossec.conf",
"/Library/Ossec/bin/wazuh-control restart"
]
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>', '<protocol>{protocol}</protocol>' | Set-Content -Path '{WAZUH_WINDOWS_CONF}'"
]
Expand All @@ -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':
Expand All @@ -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",
Expand Down
19 changes: 12 additions & 7 deletions deployability/modules/testing/tests/helpers/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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("-", ".")}')

Expand Down
2 changes: 2 additions & 0 deletions deployability/modules/testing/tests/helpers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down

0 comments on commit b604866

Please sign in to comment.