From b127369624653a5c19fa3f98be8bc9ce924b6563 Mon Sep 17 00:00:00 2001 From: rafabailon Date: Fri, 8 Mar 2024 13:59:41 +0100 Subject: [PATCH 1/5] feat: change db management and fix the test --- .../roles/manager-role/tasks/main.yml | 9 ++++++--- .../test_synchronization/test_synchronization.py | 12 ++++++------ 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/tests/system/provisioning/one_manager_agent/roles/manager-role/tasks/main.yml b/tests/system/provisioning/one_manager_agent/roles/manager-role/tasks/main.yml index b47f1cd9a3..65970cf9dd 100644 --- a/tests/system/provisioning/one_manager_agent/roles/manager-role/tasks/main.yml +++ b/tests/system/provisioning/one_manager_agent/roles/manager-role/tasks/main.yml @@ -79,7 +79,10 @@ - name: Start Wazuh command: /var/ossec/bin/wazuh-control restart -- name: Create script to check BD +- name: Copy wdb-query.py script copy: - src: ../../../../../scripts/system_query_db.py - dest: /var/ + src: ../../../../scripts/wdb-query.py + dest: /var/ossec/bin/wdb-query.py + owner: root + group: root + mode: '0644' \ No newline at end of file diff --git a/tests/system/test_fim/test_synchronization/test_synchronization.py b/tests/system/test_fim/test_synchronization/test_synchronization.py index c823a0356e..515b96afda 100644 --- a/tests/system/test_fim/test_synchronization/test_synchronization.py +++ b/tests/system/test_fim/test_synchronization/test_synchronization.py @@ -46,10 +46,11 @@ from time import sleep +from system import execute_wdb_query from wazuh_testing.tools.system_monitoring import HostMonitor from wazuh_testing.tools.system import HostManager, clean_environment from wazuh_testing.tools import WAZUH_LOGS_PATH -from wazuh_testing.fim import create_folder_file, query_db +from wazuh_testing.fim import create_folder_file pytestmark = [pytest.mark.one_manager_agent_env] @@ -67,8 +68,6 @@ ] tmp_path = os.path.join(local_path, 'tmp') scheduled_mode = 'testdir1' -db_path = '/var/ossec/queue/db/001.db' -db_script = '/var/system_query_db.py' enviroment_files = [('wazuh-manager', os.path.join(WAZUH_LOGS_PATH, 'ossec.log')), ('wazuh-agent1', os.path.join(WAZUH_LOGS_PATH, 'ossec.log'))] @@ -135,7 +134,7 @@ def test_synchronization(folder_path, case, host): else: host_manager.run_command('wazuh-agent1', f'rm -rf {folder_path}') folder_path = f"'/{folder_path}/{folder_path}.txt'" - query = " select * from fim_entry where full_path='\"{}\"'".format(folder_path) + query = "001 select * from fim_entry where full_path='\"{}\"'".format(folder_path) # Start host host_manager.run_command(host, '/var/ossec/bin/wazuh-control start') @@ -150,8 +149,9 @@ def test_synchronization(folder_path, case, host): if (case == 'delete'): # Execute query to DB sleep(5) - result = query_db(host_manager, db_script, db_path, f'\"{query}\"') - assert not json.loads(result) + result = execute_wdb_query(query, 'wazuh-manager', host_manager) + + assert result != '[]' finally: host_manager.run_command('wazuh-agent1', f'rm -rf {folder_path}') From c102442124a7296f30a90420b4d42106d5b13444 Mon Sep 17 00:00:00 2001 From: rafabailon Date: Fri, 8 Mar 2024 14:50:19 +0100 Subject: [PATCH 2/5] feat: change condition in assert --- .../test_fim/test_synchronization/test_synchronization.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/system/test_fim/test_synchronization/test_synchronization.py b/tests/system/test_fim/test_synchronization/test_synchronization.py index 515b96afda..05a53946d1 100644 --- a/tests/system/test_fim/test_synchronization/test_synchronization.py +++ b/tests/system/test_fim/test_synchronization/test_synchronization.py @@ -151,7 +151,7 @@ def test_synchronization(folder_path, case, host): sleep(5) result = execute_wdb_query(query, 'wazuh-manager', host_manager) - assert result != '[]' + assert result == '[]' finally: host_manager.run_command('wazuh-agent1', f'rm -rf {folder_path}') From 0fa86091d2bbf35fdf53e3355d5234f5bd714f7e Mon Sep 17 00:00:00 2001 From: rafabailon Date: Fri, 8 Mar 2024 15:39:30 +0100 Subject: [PATCH 3/5] feat: remove unused script --- tests/system/scripts/system_query_db.py | 13 ------------- 1 file changed, 13 deletions(-) delete mode 100644 tests/system/scripts/system_query_db.py diff --git a/tests/system/scripts/system_query_db.py b/tests/system/scripts/system_query_db.py deleted file mode 100644 index 3bed1df244..0000000000 --- a/tests/system/scripts/system_query_db.py +++ /dev/null @@ -1,13 +0,0 @@ -import sqlite3 -import argparse - -parser = argparse.ArgumentParser() - -parser.add_argument('--db_path', type=str, required=True) -parser.add_argument('--query', type=str, required=True) - -args = parser.parse_args() -conn = sqlite3.connect(args.db_path) -cursor = conn.cursor() -cursor.execute(args.query) -print(cursor.fetchall()) From e13544c3c3b5e7fb952b25aca5aeefc7db54a659 Mon Sep 17 00:00:00 2001 From: rafabailon Date: Mon, 11 Mar 2024 12:05:59 +0100 Subject: [PATCH 4/5] feat: add logic to obtain the agent id --- .../one_manager_agent/roles/manager-role/tasks/main.yml | 3 ++- .../test_fim/test_synchronization/test_synchronization.py | 8 +++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/tests/system/provisioning/one_manager_agent/roles/manager-role/tasks/main.yml b/tests/system/provisioning/one_manager_agent/roles/manager-role/tasks/main.yml index 65970cf9dd..8430d2f53e 100644 --- a/tests/system/provisioning/one_manager_agent/roles/manager-role/tasks/main.yml +++ b/tests/system/provisioning/one_manager_agent/roles/manager-role/tasks/main.yml @@ -85,4 +85,5 @@ dest: /var/ossec/bin/wdb-query.py owner: root group: root - mode: '0644' \ No newline at end of file + mode: '0644' + \ No newline at end of file diff --git a/tests/system/test_fim/test_synchronization/test_synchronization.py b/tests/system/test_fim/test_synchronization/test_synchronization.py index 05a53946d1..d103e2d815 100644 --- a/tests/system/test_fim/test_synchronization/test_synchronization.py +++ b/tests/system/test_fim/test_synchronization/test_synchronization.py @@ -41,8 +41,8 @@ """ import os -import json import pytest +import re from time import sleep @@ -134,7 +134,9 @@ def test_synchronization(folder_path, case, host): else: host_manager.run_command('wazuh-agent1', f'rm -rf {folder_path}') folder_path = f"'/{folder_path}/{folder_path}.txt'" - query = "001 select * from fim_entry where full_path='\"{}\"'".format(folder_path) + query = " select * from fim_entry where full_path='\"{}\"'".format(folder_path) + agent_info = host_manager.run_command(host, '/var/ossec/bin/manage_agents -l') + agent_id = re.search(r'ID: (\d+)', agent_info).group(1) # Start host host_manager.run_command(host, '/var/ossec/bin/wazuh-control start') @@ -149,7 +151,7 @@ def test_synchronization(folder_path, case, host): if (case == 'delete'): # Execute query to DB sleep(5) - result = execute_wdb_query(query, 'wazuh-manager', host_manager) + result = execute_wdb_query(f"{agent_id} {query}", 'wazuh-manager', host_manager) assert result == '[]' From d4744a39e1775ffe1bf805a0941ef8897af49805 Mon Sep 17 00:00:00 2001 From: rafabailon Date: Mon, 11 Mar 2024 16:48:47 +0100 Subject: [PATCH 5/5] feat: modify function to get agent id --- tests/system/__init__.py | 4 ++-- .../one_manager_agent/roles/manager-role/tasks/main.yml | 1 - .../test_fim/test_synchronization/test_synchronization.py | 5 ++--- 3 files changed, 4 insertions(+), 6 deletions(-) diff --git a/tests/system/__init__.py b/tests/system/__init__.py index 6e2c9d52c1..21c18d2fdd 100644 --- a/tests/system/__init__.py +++ b/tests/system/__init__.py @@ -22,9 +22,9 @@ # Functions -def get_agent_id(host_manager): +def get_agent_id(host_manager, node='wazuh-master'): # Gets the first agent id in the master's client.keys file - return host_manager.run_command('wazuh-master', f'cut -c 1-3 {WAZUH_PATH}/etc/client.keys') + return host_manager.run_command(node, f'cut -c 1-3 {WAZUH_PATH}/etc/client.keys') def get_id_from_agent(agent, host_manager): diff --git a/tests/system/provisioning/one_manager_agent/roles/manager-role/tasks/main.yml b/tests/system/provisioning/one_manager_agent/roles/manager-role/tasks/main.yml index 8430d2f53e..e45c2781fc 100644 --- a/tests/system/provisioning/one_manager_agent/roles/manager-role/tasks/main.yml +++ b/tests/system/provisioning/one_manager_agent/roles/manager-role/tasks/main.yml @@ -86,4 +86,3 @@ owner: root group: root mode: '0644' - \ No newline at end of file diff --git a/tests/system/test_fim/test_synchronization/test_synchronization.py b/tests/system/test_fim/test_synchronization/test_synchronization.py index d103e2d815..cc80d17ee8 100644 --- a/tests/system/test_fim/test_synchronization/test_synchronization.py +++ b/tests/system/test_fim/test_synchronization/test_synchronization.py @@ -46,7 +46,7 @@ from time import sleep -from system import execute_wdb_query +from system import execute_wdb_query, get_agent_id from wazuh_testing.tools.system_monitoring import HostMonitor from wazuh_testing.tools.system import HostManager, clean_environment from wazuh_testing.tools import WAZUH_LOGS_PATH @@ -135,8 +135,7 @@ def test_synchronization(folder_path, case, host): host_manager.run_command('wazuh-agent1', f'rm -rf {folder_path}') folder_path = f"'/{folder_path}/{folder_path}.txt'" query = " select * from fim_entry where full_path='\"{}\"'".format(folder_path) - agent_info = host_manager.run_command(host, '/var/ossec/bin/manage_agents -l') - agent_id = re.search(r'ID: (\d+)', agent_info).group(1) + agent_id = get_agent_id(host_manager, 'wazuh-manager') # Start host host_manager.run_command(host, '/var/ossec/bin/wazuh-control start')