Skip to content

Commit

Permalink
enhancement(#4843): Removing uninstall at the end, API password and u…
Browse files Browse the repository at this point in the history
…ser as parameter
  • Loading branch information
pro-akim committed Mar 19, 2024
1 parent 11b6ac8 commit 33bc706
Show file tree
Hide file tree
Showing 13 changed files with 56 additions and 377 deletions.
2 changes: 1 addition & 1 deletion deployability/modules/testing/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,4 @@ def targets(request) -> dict | None:
def live(request) -> bool | None:
live_value = request.config.getoption('live')

return live_value.lower() == 'true' if live_value else None
return live_value.lower() == 'true' if live_value else None
2 changes: 1 addition & 1 deletion deployability/modules/testing/tests/helpers/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from .manager import WazuhManager
from .agent import WazuhAgent
from .generic import HostConfiguration, HostInformation, HostMonitor, CheckFiles
from .generic import HostConfiguration, HostInformation, HostMonitor, CheckFiles
2 changes: 1 addition & 1 deletion deployability/modules/testing/tests/helpers/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,4 +236,4 @@ def agent_status_report(wazuh_api: WazuhAPI) -> dict:
Dict: Agent status report.
"""
response = requests.get(f"{wazuh_api.api_url}/agents/summary/status", headers=wazuh_api.headers, verify=False)
return eval(response.text)['data']
return eval(response.text)['data']
25 changes: 15 additions & 10 deletions deployability/modules/testing/tests/helpers/executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,26 +61,30 @@ def execute_commands(inventory_path, commands=[]) -> dict:


class WazuhAPI:
def __init__(self, inventory_path):
def __init__(self, inventory_path, wazuh_user='wazuh', wazuh_password='wazuh'):
self.inventory_path = inventory_path
self.api_url = None
self.headers = None
self.wazuh_user = wazuh_user
self.wazuh_password = self._get_wazuh_api_password()
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
self._authenticate()


def _get_wazuh_api_password(self):
#------------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')
return Executor.execute_command(self.inventory_path, "grep api_password wazuh-install-files/wazuh-passwords.txt | 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","")
#--------------------------------------------------------------------------------

user = self.wazuh_user
password = self.wazuh_password
login_endpoint = 'security/user/authenticate'
host = inventory_data.get('ansible_host')
port = '55000'
Expand All @@ -96,3 +100,4 @@ def _authenticate(self):
'Content-Type': 'application/json',
'Authorization': f'Bearer {token}'
}

25 changes: 24 additions & 1 deletion deployability/modules/testing/tests/helpers/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -519,4 +519,27 @@ def isComponentActive(inventory_path, host_role) -> bool:
Returns:
bool: True/False
"""
return 'active' == Executor.execute_command(inventory_path, f'systemctl is-active {host_role}')
return 'active' == Executor.execute_command(inventory_path, f'systemctl is-active {host_role}')



class Waits:

@staticmethod
def dynamic_wait(expected_condition_func, cycles=10, waiting_time=10) -> None:
"""
Waits the process during assigned cycles for the assigned seconds
Args:
expected_condition_func (lambda function): The function that returns True when the expected condition is met
cycles(int): Number of cycles
waiting_Time(int): Number of seconds per cycle
"""
for _ in range(cycles):
if expected_condition_func():
break
else:
time.sleep(waiting_time)
else:
raise RuntimeError(f'Time out')
2 changes: 1 addition & 1 deletion deployability/modules/testing/tests/helpers/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,4 +202,4 @@ def get_manager_logs(wazuh_api: WazuhAPI) -> list:
List: The logs of the manager.
"""
response = requests.get(f"{wazuh_api.api_url}/manager/logs", headers=wazuh_api.headers, verify=False)
return eval(response.text)['data']['affected_items']
return eval(response.text)['data']['affected_items']
Loading

0 comments on commit 33bc706

Please sign in to comment.