Skip to content

Commit

Permalink
fix(#5229): Fixing validations of basic info and install
Browse files Browse the repository at this point in the history
  • Loading branch information
pro-akim committed Apr 30, 2024
1 parent e07fb3f commit a6d89aa
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 61 deletions.
61 changes: 39 additions & 22 deletions deployability/modules/testing/tests/helpers/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +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 os_type == 'macos':
if architecture == 'amd64':
commands.extend([
Expand All @@ -78,8 +79,8 @@ def install_agent(inventory_path, agent_name, wazuh_version, wazuh_revision, liv
'/Library/Ossec/bin/wazuh-control start',
'/Library/Ossec/bin/wazuh-control status'
]

commands.extend(system_commands)

logger.info(f'Installing Agent in {HostInformation.get_os_name_and_version_from_inventory(inventory_path)}')
ConnectionManager.execute_commands(inventory_path, commands)

Expand Down Expand Up @@ -132,7 +133,7 @@ def register_agent(inventory_path, manager_path):
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}')
result = ConnectionManager.execute_commands(inventory_path, f'cat {WAZUH_MACOS_CONF}').get('output')
assert host_ip in result, 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':
Expand All @@ -159,24 +160,22 @@ def set_protocol_agent_connection(inventory_path, protocol):
f"sed -i 's/<protocol>[^<]*<\/protocol>/<protocol>{protocol}<\/protocol>/g' {WAZUH_CONF}",
"systemctl restart wazuh-agent"
]

ConnectionManager.execute_commands(inventory_path, commands)
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 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')
commands = [
f"sed -i '' 's/<protocol>[^<]*<\/protocol>/<protocol>{protocol}<\/protocol>/g' {WAZUH_MACOS_CONF}",
"/Library/Ossec/bin/wazuh-control restart"
]
ConnectionManager.execute_commands(inventory_path, commands)
assert protocol in ConnectionManager.execute_commands(inventory_path, f'cat {WAZUH_MACOS_CONF}').get('output'), logger.error(f'Error configuring the protocol ({protocol}) in: {HostInformation.get_os_name_and_version_from_inventory(inventory_path)} agent')

elif os_type == 'windows':
commands = [
f"(Get-Content -Path '{WAZUH_WINDOWS_CONF}') -replace '<protocol>[^<]*<\/protocol>', '<protocol>{protocol}</protocol>' | Set-Content -Path '{WAZUH_WINDOWS_CONF}'"
]

ConnectionManager.execute_commands(inventory_path, commands)
result = ConnectionManager.execute_commands(inventory_path, f'Get-Content -Path "{WAZUH_WINDOWS_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')
Expand All @@ -186,9 +185,11 @@ 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 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':
commands.extend([
"zypper remove --no-confirm wazuh-agent",
Expand All @@ -198,25 +199,23 @@ def uninstall_agent(inventory_path, wazuh_version=None, wazuh_revision=None) ->
if distribution == 'deb':
commands.extend([
"apt-get remove --purge wazuh-agent -y"

])
elif distribution == 'rpm':
commands.extend([
"yum remove wazuh-agent -y",
f"rm -rf {WAZUH_ROOT}"
])


system_commands = [
"systemctl disable wazuh-agent",
"systemctl daemon-reload"
]

commands.extend(system_commands)

elif os_type == 'windows':
commands.extend([
f"msiexec.exe /x $env:TEMP\wazuh-agent.msi /qn"
])

elif os_type == 'macos':
commands.extend([
"/Library/Ossec/bin/wazuh-control stop",
Expand Down Expand Up @@ -303,11 +302,13 @@ def perform_action_and_scan(agent_params, action_callback) -> dict:
'/root': {'added': ['trustdb.gpg', 'lesshst'], 'removed': [], 'modified': []},
'/usr/sbin': {'added': [], 'removed': [], 'modified': []}
}

elif os_type == 'macos':
filter_data = {
'/usr/bin': {'added': [], 'removed': [], 'modified': []},
'/usr/sbin': {'added': [], 'removed': [], 'modified': []}
}

elif os_type == 'windows':
filter_data = {
'C:\\Program Files': {'added': [], 'removed': [], 'modified': []},
Expand Down Expand Up @@ -369,8 +370,10 @@ def assert_results(result, params = None) -> None:

if os_type == 'linux':
categories = ['/root', '/usr/bin', '/usr/sbin', '/boot']

elif os_type == 'windows':
categories = ['C:\\Program Files', 'C:\\Program Files (x86)','C:\\Users\\vagrant']

elif os_type == 'macos':
categories = ['/usr/bin', '/usr/sbin']

Expand All @@ -396,14 +399,16 @@ def areAgent_processes_active(agent_params):
if os_type == 'linux':
result = ConnectionManager.execute_commands(agent_params, 'pgrep wazuh')
if result.get('success'):
return bool([int(numero) for numero in result.get('output').splitlines()])
return bool([int(number) for number in result.get('output').splitlines()])
else:
return False

if os_type == 'macos':
result = ConnectionManager.execute_commands(agent_params, 'pgrep wazuh')
return bool([int(numero) for numero in result.splitlines()])

if result.get('success'):
return bool([int(number) for number in result.get('output').splitlines()])
else:
return False

elif os_type == 'windows':
result = ConnectionManager.execute_commands(agent_params, 'Get-Process -Name "wazuh-agent" | Format-Table -HideTableHeaders ProcessName')
Expand All @@ -425,14 +430,26 @@ def isAgent_port_open(agent_params):
"""

os_type = HostInformation.get_os_type(agent_params)
if 'linux' in os_type:
if os_type == 'linux':
result = ConnectionManager.execute_commands(agent_params, 'ss -t -a -n | grep ":1514" | grep ESTAB')
return result.get('success')
elif 'windows' in os_type :
if result.get('success'):
return 'ESTAB' in result.get('output')
else:
return False

elif os_type == 'windows':
result = ConnectionManager.execute_commands(agent_params, 'netstat -ano | Select-String -Pattern "TCP" | Select-String -Pattern "ESTABLISHED" | Select-String -Pattern ":1514"')
return 'ESTABLISHED' in result.get('output')
if result.get('success'):
return 'ESTABLISHED' in result.get('output')
else:
return False

elif os_type == 'macos':
return 'ESTABLISHED' in ConnectionManager.execute_commands(agent_params, 'netstat -an | grep ".1514 " | grep ESTABLISHED')
result = ConnectionManager.execute_commands(agent_params, 'netstat -an | grep ".1514 " | grep ESTABLISHED')
if result.get('success'):
return 'ESTABLISHED' in result.get('output')
else:
return False

def get_agents_information(wazuh_api: WazuhAPI) -> list:
"""
Expand Down
11 changes: 11 additions & 0 deletions deployability/modules/testing/tests/helpers/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,27 @@
MACOS_VERSION = Path(MACOS_ROOT_DIR, "VERSION")
MACOS_REVISION = Path(MACOS_ROOT_DIR, "REVISION")


# Binaries paths
BINARIES_DIR = Path(WAZUH_ROOT, "bin")
WAZUH_CONTROL = Path(BINARIES_DIR, "wazuh-control")
AGENT_CONTROL = Path(BINARIES_DIR, "agent_control")
CLUSTER_CONTROL = Path(BINARIES_DIR, "cluster_control")

MACOS_BINARIES_DIR = Path(MACOS_ROOT_DIR, "bin")
MACOS_WAZUH_CONTROL = Path(MACOS_BINARIES_DIR, "wazuh-control")

# Logs paths
LOGS_DIR = Path(WAZUH_ROOT, "logs")
WAZUH_LOG = Path(LOGS_DIR, "ossec.log")
ALERTS_DIR = Path(LOGS_DIR, "alerts")
ALERTS_JSON = Path(ALERTS_DIR, "alerts.json")

MACOS_LOGS_DIR = Path(MACOS_ROOT_DIR, "logs")
WAZUH_MACOS_LOG = Path(MACOS_LOGS_DIR, "ossec.log")
MACOS_ALERTS_DIR = Path(MACOS_LOGS_DIR, "alerts")
MACOS_ALERTS_JSON = Path(MACOS_ALERTS_DIR, "alerts.json")

# Daemons running paths
DAEMONS_DIR = Path(WAZUH_ROOT, "var", "run")
AGENTD_STATE = Path(DAEMONS_DIR, "wazuh-agentd.state")
Expand Down
13 changes: 8 additions & 5 deletions deployability/modules/testing/tests/helpers/executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,21 +113,24 @@ def _execute_command(data, command) -> dict:
class MacosExecutor():
@staticmethod
def _execute_command(data, command) -> dict:

try:
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(hostname=data.get('host'), port=data.get('port'), username=data.get('username'), password=data.get('username'))
ssh_client.connect(hostname=data.get('host'), port=data.get('port'), username=data.get('username'), password=data.get('password'))
stdin, stdout, stderr = ssh_client.exec_command(f"sudo {command}")

result = ''.join(stdout.readlines())
stdout_str = ''.join(stdout.readlines())
stderr_str = ''.join(stderr.readlines())

ssh_client.close()

return result
if stdout_str:
return {'success': True, 'output': stdout_str.replace('\n', '')}
if stderr_str:
return {'success': False, 'output': stderr_str.replace('\n', '')}
return {'success': False, 'output': None}

except Exception as e:
#return {'success': False, 'output': ret.stderr}
raise Exception(f'Error executing command: {command} with error: {e}')

# ------------------------------------------------------
Expand Down
Loading

0 comments on commit a6d89aa

Please sign in to comment.