Skip to content

Commit

Permalink
Change all sleeps for monitorings and check wdb socket instead of glo…
Browse files Browse the repository at this point in the history
…bal.db
  • Loading branch information
yanazaeva committed Dec 22, 2021
1 parent 4b5d228 commit 03237e5
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 42 deletions.
24 changes: 12 additions & 12 deletions tests/system/provisioning/basic_cluster/playbook.yml
Original file line number Diff line number Diff line change
Expand Up @@ -113,18 +113,6 @@
roles:
- name: "roles/agent-role"

- name: Wazuh Agent2
hosts: wazuh-agent2
vars:
manager_hostname: wazuh-worker1
agent_id: "{{ agent2_id }}"
agent_hostname: "{{ agent2_hostname }}"
agent_key: "{{ agent2_key }}"
vars_files:
- ./vars/configurations.yml
roles:
- name: "roles/agent-role"

- name: Wazuh Agent3
hosts: wazuh-agent3
vars:
Expand All @@ -136,3 +124,15 @@
- ./vars/configurations.yml
roles:
- name: "roles/agent-role"

- name: Wazuh Agent2
hosts: wazuh-agent2
vars:
manager_hostname: wazuh-worker1
agent_id: "{{ agent2_id }}"
agent_hostname: "{{ agent2_hostname }}"
agent_key: "{{ agent2_key }}"
vars_files:
- ./vars/configurations.yml
roles:
- name: "roles/agent-role"
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@
dest: /wazuh
version: "{{ wazuh_branch }}"

- name: "Clone wazuh-qa repository"
git:
repo: "https:/wazuh/wazuh-qa"
dest: /wazuh-qa
version: "master"
depth: '1'

- name: Install master
args:
chdir: /wazuh
Expand Down Expand Up @@ -85,6 +92,7 @@
authd.debug=2
wazuh_clusterd.debug=2
wazuh_db.debug=2
wazuh_modules.debug=2
- name: Register agents
blockinfile:
Expand All @@ -100,3 +108,18 @@

- name: Start Wazuh
command: /var/ossec/bin/wazuh-control restart

- name: "Install necessary dependencies"
command: /var/ossec/framework/python/bin/python3.9 -m pip install lockfile filetype certifi testinfra

- name: "Create a file to call the socket"
copy:
dest: "/send_msg.py"
content: |
import sys
sys.path.append('/wazuh-qa/deps/wazuh_testing')
from wazuh_testing import wazuh_db
result = wazuh_db.query_wdb(f'global sql select * from agent where id={sys.argv[1]}')
if result != []:
print(result)
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@
dest: /wazuh
version: "{{ wazuh_branch }}"

- name: "Clone wazuh-qa repository"
git:
repo: "https:/wazuh/wazuh-qa"
dest: /wazuh-qa
version: "master"
depth: '1'

- name: Install worker
args:
chdir: /wazuh
Expand Down Expand Up @@ -85,6 +92,22 @@
authd.debug=2
wazuh_clusterd.debug=2
wazuh_db.debug=2
wazuh_modules.debug=2
- name: Restart Wazuh
command: /var/ossec/bin/wazuh-control restart

- name: "Install necessary dependencies"
command: /var/ossec/framework/python/bin/python3.9 -m pip install lockfile filetype certifi testinfra

- name: "Create a file to call the socket"
copy:
dest: "/send_msg.py"
content: |
import sys
sys.path.append('/wazuh-qa/deps/wazuh_testing')
from wazuh_testing import wazuh_db
result = wazuh_db.query_wdb(f'global sql select * from agent where id={sys.argv[1]}')
if result != []:
print(result)
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
---
wazuh-master:
- regex: ".*Agent key generated for agent.*"
path: "/var/ossec/logs/ossec.log"
wazuh-worker2:
- regex: '.*Received 1 shared files to update from .*'
path: "/var/ossec/logs/cluster.log"
timeout: 60
- regex: '.*Worker wazuh-worker2.*Integrity sync.*Finished in.*'
path: "/var/ossec/logs/cluster.log"
timeout: 60
- regex: '.*Worker wazuh-worker1.*Integrity check.*Finished in.*'
- regex: '.*Worker wazuh-worker2.*Integrity sync.*Starting.*'
path: "/var/ossec/logs/cluster.log"
timeout: 60
- regex: '.*Worker wazuh-worker2.*Integrity sync.*Finished in.*'
path: "/var/ossec/logs/cluster.log"
timeout: 60

wazuh-master:
- regex: '.*Worker wazuh-worker2.*Integrity check.*Finished in.*'
path: "/var/ossec/logs/cluster.log"
timeout: 60
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Copyright (C) 2015-2021, Wazuh Inc.
# Created by Wazuh, Inc. <[email protected]>.
# This program is free software; you can redistribute it and/or modify it under the terms of GPLv2

import datetime
import os
from os.path import join, dirname, abspath
from time import sleep
from time import time

import pytest
from wazuh_testing.tools import WAZUH_PATH, WAZUH_LOGS_PATH
Expand All @@ -20,15 +20,12 @@
managers_hosts = [master_host, worker_host]
inventory_path = join(dirname(dirname(dirname(abspath(__file__)))), 'provisioning', 'basic_cluster', 'inventory.yml')
host_manager = HostManager(inventory_path)
time_to_sync = 60

# Each file should exist in all hosts specified in 'hosts'.
files = [{'path': join(WAZUH_PATH, 'queue', 'rids', '{id}'), 'hosts': managers_hosts},
{'path': join(WAZUH_PATH, 'queue', 'agent-groups', '{id}'), 'hosts': managers_hosts},
{'path': join(WAZUH_PATH, 'queue', 'diff', '{name}'), 'hosts': [worker_host]},
{'path': join(WAZUH_PATH, 'queue', 'db', '{id}.db'), 'hosts': [worker_host]}]
db_queries = ["select * from agent where id={id}",
"select * from belongs where id_agent={id}"]


@pytest.fixture(scope='function')
Expand All @@ -38,16 +35,24 @@ def register_agent():
host_manager.get_host(agent_host).ansible('command', f'service wazuh-agent restart', check=False)

# Wait until the agent is reconnected
timeout = time() + 180
while True:
if int(host_manager.run_shell('wazuh-worker2',
f'{WAZUH_PATH}/bin/cluster_control -a | '
f'grep active | wc -l')) == 4 and int(host_manager.run_shell('wazuh-master',
f'{WAZUH_PATH}/bin/cluster_control -a | '
f'grep active | wc -l')) == 4 or time() > timeout:
break
HostMonitor(inventory_path=inventory_path, messages_path=messages_path, tmp_path=tmp_path).run()
sleep(time_to_sync)


def test_agent_files_deletion(register_agent):
"""Check that when an agent is deleted, all its related files in managers are also removed."""
# Clean ossec.log and cluster.log
host_manager.clear_file(host='wazuh-master', file_path=os.path.join(WAZUH_LOGS_PATH, 'ossec.log'))
host_manager.clear_file(host='wazuh-master', file_path=os.path.join(WAZUH_LOGS_PATH, 'cluster.log'))
host_manager.control_service(host='wazuh-master', service='wazuh', state="restarted")
for hosts in managers_hosts:
host_manager.clear_file(host=hosts, file_path=os.path.join(WAZUH_LOGS_PATH, 'ossec.log'))
host_manager.clear_file(host=hosts, file_path=os.path.join(WAZUH_LOGS_PATH, 'cluster.log'))
host_manager.control_service(host=hosts, service='wazuh', state="restarted")

# Get the current ID and name of the agent that is reporting to worker_host.
master_token = host_manager.get_api_token(master_host)
Expand All @@ -70,22 +75,18 @@ def test_agent_files_deletion(register_agent):
assert result, f'This file should exist in {host} but could not be found: ' \
f'{file["path"].format(id=agent_id, name=agent_name)}'

# Check that agent information exists in global.db
# Check that agent information is in the wdb socket
for host in managers_hosts:
for query in db_queries:
result = host_manager.run_command(
host,
f'sqlite3 {join(WAZUH_PATH, "queue", "db", "global.db")} "{query.format(id=agent_id, name=agent_name)}"'
)
assert result, f'This db query should have returned something in {host}, but it did not: ' \
f'{query.format(id=agent_id, name=agent_name)}'
result = host_manager.run_command(host, f"/var/ossec/framework/python/bin/python3.9 /send_msg.py {agent_id}")
assert result, f'This db query should have returned something in {host}, but it did not: {result}'

# Remove the agent
response = host_manager.make_api_call(host=master_host, method='DELETE', token=master_token,
endpoint=f'/agents?agents_list={agent_id}&status=all&older_than=0s')
assert response['status'] == 200, f'Failed when trying to remove agent {agent_id}: {response}'

# Wait until information is synced to all workers
sleep(time_to_sync)
HostMonitor(inventory_path=inventory_path, messages_path=messages_path, tmp_path=tmp_path).run()

# Check that agent-related files where removed from each node.
for file in files:
Expand All @@ -96,12 +97,8 @@ def test_agent_files_deletion(register_agent):
assert not result, f'This file should not exist in {host} but it was found: ' \
f'{file["path"].format(id=agent_id, name=agent_name)}'

# Check that agent information does not exist anymore in global.db
# Check that agent information is not in the wdb socket
for host in managers_hosts:
for query in db_queries:
result = host_manager.run_command(
host,
f'sqlite3 {join(WAZUH_PATH, "queue", "db", "global.db")} "{query.format(id=agent_id, name=agent_name)}"'
)
assert not result, f'This db query should have not returned anything in {host}, but it did: ' \
f'{query.format(id=agent_id, name=agent_name)} -> {result}'
result = host_manager.run_command(host,
f"/var/ossec/framework/python/bin/python3.9 /send_msg.py {agent_id}")
assert not result, f'This db query should have not returned anything in {host}, but it did: {result}'

0 comments on commit 03237e5

Please sign in to comment.