Skip to content

Commit

Permalink
enhancement(#5219): Adding port tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pro-akim committed Apr 25, 2024
1 parent 587dd76 commit f767010
Show file tree
Hide file tree
Showing 8 changed files with 381 additions and 74 deletions.
66 changes: 52 additions & 14 deletions deployability/modules/testing/tests/helpers/dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

import requests
import socket
import json
import time

from .constants import CLUSTER_CONTROL, AGENT_CONTROL, WAZUH_CONF, WAZUH_ROOT
from .executor import Executor, WazuhAPI
Expand All @@ -21,9 +23,12 @@ def get_dashboard_version(inventory_path) -> str:
Args:
inventory_path (str): host's inventory path
Returns:
- str: Version of the dashboard.
"""

return Executor.execute_command(inventory_path,'cat /usr/share/wazuh-dashboard/VERSION')
return Executor.execute_command(inventory_path,'cat /usr/share/wazuh-dashboard/VERSION').strip()


@staticmethod
Expand All @@ -33,6 +38,9 @@ def isDashboard_active(inventory_path) -> bool:
Args:
inventory_path (str): host's inventory path
Returns:
- bool: Status of the dashboard.
"""

return '200' in Executor.execute_command(inventory_path, 'curl -Is -k https://localhost/app/login?nextUrl=%2F | head -n 1')
Expand All @@ -45,27 +53,57 @@ def isDashboardKeystore_working(inventory_path) -> bool:
Args:
inventory_path (str): host's inventory path
Returns:
- bool: Status of the dashboard keystore.
"""

return 'No such file or directory' not in Executor.execute_command(inventory_path, '/usr/share/wazuh-dashboard/bin/opensearch-dashboards-keystore list --allow-root')


@staticmethod
def areIndexes_working(wazuh_api: WazuhAPI) -> str:
def areDashboardNodes_working(wazuh_api: WazuhAPI) -> str:
"""
Function to get the status of an agent given its name.
Args:
- agents_data (list): List of dictioconaaaaaconaaaaaconaaaaaconaaaaaconaaaaaconaaaaaconaaaaaconaaaaaconaaaaaconaaaaaconaaaaaconaaaaanaries conaaaaa.
- agent_name (str): Name of the agent whoseconaaaaaconaaaaaconaaaaaconaaaaaconaaaaaconaaaaa status is to be obtained.
Returns True/False depending the status of Dashboard nodes
Returns:
- str: Status of the agent if found in the daconaaaaaconaaaaaconaaaaaconaaaaaconaaaaaconaaaaaa, otherwise returns None.
- bool: True/False depending on the status.
"""
logger.error(wazuh_api.api_url)
logger.error(wazuh_api.password)
logger.error(wazuh_api.username)
response = requests.get(f"{wazuh_api.api_url}/_cat/indices/?pretty", auth=(wazuh_api.username, wazuh_api.password), verify=False)
response = requests.get(f"{wazuh_api.api_url}/api/status", auth=(wazuh_api.username, wazuh_api.password), verify=False)

result = True
if response.status_code == 200:
for status in json.loads((response.text))['status']['statuses']:
if status['state'] == 'green' or status['state'] == 'yellow':
result = True
else:
result = False
return result

return response
else:
logger.error(f'The dashboard API returned: {response.status_code}')

@staticmethod
def isDashboard_port_opened(inventory_path, wait=10, cycles=10):
"""
Check if dashboard port is open
Args:
inventory_path (str): Dashboard inventory.
Returns:
str: Os name.
"""
wait_cycles = 0
while wait_cycles < cycles:
ports = Executor.execute_command(inventory_path, 'ss -t -a -n | grep ":443"').strip().split('\n')
for port in ports:
if 'ESTAB' in port or 'LISTEN' in port:
continue
else:
time.sleep(wait)
wait_cycles += 1
break
else:
return True
return False
34 changes: 12 additions & 22 deletions deployability/modules/testing/tests/helpers/executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,19 @@ def __init__(self, inventory_path, component=None):
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
self._authenticate()

def _extract_password(self, file_path, keyword):
if not 'true' in Executor.execute_command(self.inventory_path, f'test -f {file_path} && echo "true" || echo "false"'):
Executor.execute_command(self.inventory_path, 'tar -xvf wazuh-install-files.tar')
return Executor.execute_command(self.inventory_path, f"grep {keyword} {file_path} | head -n 1 | awk '{{print $NF}}'").replace("'", "").replace("\n", "")

def _authenticate(self):
with open(self.inventory_path, 'r') as yaml_file:
inventory_data = yaml.safe_load(yaml_file)

user = 'wazuh'

#----Patch issue https://github.com/wazuh/wazuh-packages/issues/2883-------------
file_path = Executor.execute_command(self.inventory_path, 'pwd').replace("\n","") + '/wazuh-install-files/wazuh-passwords.txt'
if not 'true' in Executor.execute_command(self.inventory_path, f'test -f {file_path} && echo "true" || echo "false"'):
Executor.execute_command(self.inventory_path, 'tar -xvf wazuh-install-files.tar')
password = Executor.execute_command(self.inventory_path, "grep api_password wazuh-install-files/wazuh-passwords.txt | head -n 1 | awk '{print $NF}'").replace("'","").replace("\n","")
#--------------------------------------------------------------------------------

file_path = Executor.execute_command(self.inventory_path, 'pwd').replace("\n", "") + '/wazuh-install-files/wazuh-passwords.txt'
password = self._extract_password(file_path, 'api_password')

login_endpoint = 'security/user/authenticate'
host = inventory_data.get('ansible_host')
port = '55000'
Expand All @@ -90,18 +90,8 @@ def _authenticate(self):

self.api_url = f'https://{host}:{port}'

if self.component == 'dashboard':
self.username = 'admin'
file_path = Executor.execute_command(self.inventory_path, 'pwd').replace("\n","") + '/wazuh-install-files/wazuh-passwords.txt'
if not 'true' in Executor.execute_command(self.inventory_path, f'test -f {file_path} && echo "true" || echo "false"'):
Executor.execute_command(self.inventory_path, 'tar -xvf wazuh-install-files.tar')
self.password = Executor.execute_command(self.inventory_path, "grep indexer_password wazuh-install-files/wazuh-passwords.txt | head -n 1 | awk '{print $NF}'").replace("'","").replace("\n","")
self.api_url = f'https://localhost'

if self.component == 'indexer':
if self.component == 'dashboard' or self.component == 'indexer':
self.username = 'admin'
file_path = Executor.execute_command(self.inventory_path, 'pwd').replace("\n","") + '/wazuh-install-files/wazuh-passwords.txt'
if not 'true' in Executor.execute_command(self.inventory_path, f'test -f {file_path} && echo "true" || echo "false"'):
Executor.execute_command(self.inventory_path, 'tar -xvf wazuh-install-files.tar')
self.password = Executor.execute_command(self.inventory_path, "").replace("'","").replace("\n","")
self.api_url = f'https://localhost:9200'
password = self._extract_password(file_path, 'indexer_password')
self.password = password
self.api_url = f'https://{host}' if self.component == 'dashboard' else f'https://127.0.0.1:9200'
77 changes: 69 additions & 8 deletions deployability/modules/testing/tests/helpers/indexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,10 @@
# Created by Wazuh, Inc. <[email protected]>.
# This program is a free software; you can redistribute it and/or modify it under the terms of GPLv2

import requests
import socket
import time

from .constants import CLUSTER_CONTROL, AGENT_CONTROL, WAZUH_CONF, WAZUH_ROOT
from .executor import Executor, WazuhAPI
from .generic import HostInformation, CheckFiles
from modules.testing.utils import logger
from .utils import Utils


class WazuhIndexer:

Expand All @@ -21,9 +16,12 @@ def get_indexer_version(inventory_path) -> str:
Args:
inventory_path (str): host's inventory path
Returns:
- str: Version of the indexer.
"""

return Executor.execute_command(inventory_path,'cat /usr/share/wazuh-indexer/VERSION')
return Executor.execute_command(inventory_path,'cat /usr/share/wazuh-indexer/VERSION').strip()


@staticmethod
Expand All @@ -33,6 +31,9 @@ def areIndexer_internalUsers_complete(inventory_path) -> bool:
Args:
inventory_path (str): host's inventory path
Returns:
- bool: True/False depending on the status.
"""

users_to_check = [
Expand All @@ -49,4 +50,64 @@ def areIndexer_internalUsers_complete(inventory_path) -> bool:

return False

return True
return True


@staticmethod
def areIndexes_working(wazuh_api: WazuhAPI, inventory_path) -> bool:
"""
Returns True/False depending on the working status of the indexes
Args:
inventory_path (str): host's inventory path
Returns:
- bool: True/False depending on the status.
"""
indexes = Executor.execute_command(inventory_path, f"curl -k -u {wazuh_api.username}:{wazuh_api.password} {wazuh_api.api_url}/_cat/indices/?pretty").strip().split('\n')
for index in indexes:
if 'red' not in index:

return True
return False


@staticmethod
def isIndexCluster_working(wazuh_api: WazuhAPI, inventory_path) -> bool:
"""
Returns True/False depending on the status of the indexer Cluster
Args:
inventory_path (str): host's inventory path
Returns:
- bool: True/False depending on the status.
"""
response = Executor.execute_command(inventory_path, f"curl -k -u {wazuh_api.username}:{wazuh_api.password} {wazuh_api.api_url}/_cat/health")
return 'green' in response


@staticmethod
def isIndexer_port_opened(inventory_path, wait=0, cycles=10):
"""
Check if indexer port is open
Args:
inventory_path (str): Indexer inventory.
Returns:
str: Os name.
"""
wait_cycles = 0
while wait_cycles < cycles:
ports = Executor.execute_command(inventory_path, 'ss -t -a -n | grep ":9200"').strip().split('\n')
for port in ports:
if 'ESTAB' in port or 'LISTEN' in port:
continue
else:
time.sleep(wait)
wait_cycles += 1
break
else:
return True
return False
77 changes: 77 additions & 0 deletions deployability/modules/testing/tests/helpers/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import requests
import socket
import time

from .constants import CLUSTER_CONTROL, AGENT_CONTROL, WAZUH_CONF, WAZUH_ROOT
from .executor import Executor, WazuhAPI
Expand Down Expand Up @@ -224,6 +225,82 @@ def assert_results(result) -> None:
assert result[category][action] == [], logger.error(f'{result[category][action]} was found in: {category}{action}')


@staticmethod
def isWazuhAPI_port_opened(inventory_path, wait=10, cycles=10) -> bool:
"""
Check if Manager API port is open
Args:
inventory_path (str): Manager inventory.
Returns:
bool: True if port is opened.
"""
wait_cycles = 0
while wait_cycles < cycles:
ports = Executor.execute_command(inventory_path, 'ss -t -a -n | grep ":55000"').strip().split('\n')
for port in ports:
if 'ESTAB' in port or 'LISTEN' in port:
continue
else:
time.sleep(wait)
wait_cycles += 1
break
else:
return True
return False

@staticmethod
def isWazuhAgent_port_opened(inventory_path, wait=10, cycles=10) -> bool:
"""
Check if Wazuh Manager Agent port is open
Args:
inventory_path (str): Manager inventory.
Returns:
bool: True if port is opened.
"""
wait_cycles = 0
while wait_cycles < cycles:
ports = Executor.execute_command(inventory_path, 'ss -t -a -n | grep ":1514"').strip().split('\n')
for port in ports:
if 'ESTAB' in port or 'LISTEN' in port:
continue
else:
time.sleep(wait)
wait_cycles += 1
break
else:
return True
return False

@staticmethod
def isWazuhAgent_enrollment_port_opened(inventory_path, wait=10, cycles=10) -> bool:
"""
Check if Wazuh Manager Agent enrollment port is open
Args:
inventory_path (str): Manager inventory.
Returns:
bool: True if port is opened.
"""
wait_cycles = 0
while wait_cycles < cycles:
ports = Executor.execute_command(inventory_path, 'ss -t -a -n | grep ":443"').strip().split('\n')
for port in ports:
if 'ESTAB' in port or 'LISTEN' in port:
continue
else:
time.sleep(wait)
wait_cycles += 1
break
else:
return True
return False


@staticmethod
def get_cluster_info(inventory_path) -> None:
"""
Expand Down
Loading

0 comments on commit f767010

Please sign in to comment.