Skip to content

Commit

Permalink
Merge pull request #5186 from wazuh/tmp-testing-dtt1-agents-fix
Browse files Browse the repository at this point in the history
Adding dynamic naming for provisioned agents
  • Loading branch information
fcaffieri authored Apr 9, 2024
2 parents 9d737f6 + 4aec786 commit a56ca72
Show file tree
Hide file tree
Showing 26 changed files with 1,341 additions and 239 deletions.
4 changes: 2 additions & 2 deletions deployability/modules/allocation/static/specs/os.yml
Original file line number Diff line number Diff line change
Expand Up @@ -243,9 +243,9 @@ aws:
zone: us-east-1
user: centos
linux-centos-8-amd64:
ami: ami-0ee534f954d1b2c98
ami: ami-05f2b469e504202f7
zone: us-east-1
user: centos
user: cloud-user
linux-centos-8-arm64:
ami: ami-012947942cdc60db6
zone: us-east-1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
shell: "curl -s https://{{ 'packages.wazuh.com' if live else 'packages-dev.wazuh.com' }}/key/GPG-KEY-WAZUH | gpg --no-default-keyring --keyring gnupg-ring:/usr/share/keyrings/wazuh.gpg --import && chmod 644 /usr/share/keyrings/wazuh.gpg"

- name: Add Wazuh repository
shell: "echo \"deb [signed-by=/usr/share/keyrings/wazuh.gpg] https://{{ 'packages.wazuh.com' if live else 'packages-dev.wazuh.com' }}/{{ '4.x' if live else 'pre-release' }}/apt/" {{ 'stable' if live else 'unstable' }} main\" | tee -a /etc/apt/sources.list.d/wazuh.list"
shell: "echo \"deb [signed-by=/usr/share/keyrings/wazuh.gpg] https://{{ 'packages.wazuh.com' if live else 'packages-dev.wazuh.com' }}/{{ '4.x' if live else 'pre-release' }}/apt/ {{ 'stable' if live else 'unstable' }} main\" | tee -a /etc/apt/sources.list.d/wazuh.list"

- name: Update package information
command: "apt-get update"
Expand Down
25 changes: 25 additions & 0 deletions deployability/modules/testing/tests/helpers/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,31 @@ def get_internal_ip_from_aws_dns(dns_name):
else:
return None

@staticmethod
def get_client_keys(inventory_path) -> list[dict]:
"""
Get the client keys from the client.keys file in the host.
Args:
inventory_path (str): host's inventory path
Returns:
list: List of dictionaries with the client keys.
"""
clients = []
client_key = Executor.execute_command(inventory_path, f'cat {CLIENT_KEYS}')
lines = client_key.split('\n')[:-1]
for line in lines:
_id, name, address, password = line.strip().split()
client_info = {
"id": _id,
"name": name,
"address": address,
"password": password
}
clients.append(client_info)
return clients


class HostConfiguration:

Expand Down
4 changes: 2 additions & 2 deletions deployability/modules/testing/tests/helpers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@
class Utils:

@staticmethod
def extract_ansible_host(file_path):
def extract_ansible_host(file_path) -> str:
with open(file_path, 'r') as yaml_file:
inventory_data = yaml.safe_load(yaml_file)
return inventory_data.get('ansible_host')

@staticmethod
def check_inventory_connection(inventory_path, attempts=10, sleep=30):
def check_inventory_connection(inventory_path, attempts=10, sleep=30) -> bool:
if 'manager' in inventory_path:
match = re.search(r'/manager-linux-([^-]+)-([^-]+)-', inventory_path)
elif 'agent' in inventory_path:
Expand Down
21 changes: 16 additions & 5 deletions deployability/modules/testing/tests/test_agent/test_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from ..helpers.utils import Utils


@pytest.fixture
@pytest.fixture(scope="module", autouse=True)
def wazuh_params(request):
wazuh_version = request.config.getoption('--wazuh_version')
wazuh_revision = request.config.getoption('--wazuh_revision')
Expand All @@ -30,7 +30,7 @@ def wazuh_params(request):
}


@pytest.fixture(autouse=True)
@pytest.fixture(scope="module", autouse=True)
def setup_test_environment(wazuh_params):
targets = wazuh_params['targets']
# Clean the string and split it into key-value pairs
Expand All @@ -48,19 +48,30 @@ def setup_test_environment(wazuh_params):
# If there are no indexers, we choose wazuh-1 by default
if not wazuh_params['indexers']:
wazuh_params['indexers'].append(wazuh_params['master'])

wazuh_params['managers'] = {key: value for key, value in targets_dict.items() if key.startswith('wazuh-')}
wazuh_params['agents'] = {key + '-' + re.findall(r'agent-(.*?)/', value)[0].replace('.',''): value for key, value in targets_dict.items() if key.startswith('agent')}

updated_agents = {}
for agent_name, agent_params in wazuh_params['agents'].items():
Utils.check_inventory_connection(agent_params)
if GeneralComponentActions.isComponentActive(agent_params, 'wazuh-agent') and GeneralComponentActions.hasAgentClientKeys(agent_params):
if HostInformation.get_client_keys(agent_params) != []:
client_name = HostInformation.get_client_keys(agent_params)[0]['name']
updated_agents[client_name] = agent_params
else:
updated_agents[agent_name] = agent_params
if updated_agents != {}:
wazuh_params['agents'] = updated_agents

def test_installation(wazuh_params):
# Checking connection
for manager_name, manager_params in wazuh_params['managers'].items():
Utils.check_inventory_connection(manager_params)

# Certs creation, firewall management and Manager installation
for agent_name, agent_params in wazuh_params['agents'].items():
Utils.check_inventory_connection(agent_params)
HostConfiguration.disable_firewall(agent_params)

# Certs creation, firewall management and Manager installation
if HostInformation.dir_exists(wazuh_params['master'], WAZUH_ROOT):
logger.info(f'Manager is already installed in {HostInformation.get_os_name_and_version_from_inventory(wazuh_params["master"])}')
else:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
from ..helpers.generic import HostInformation, GeneralComponentActions, Waits
from ..helpers.manager import WazuhManager, WazuhAPI
from ..helpers.logger.logger import logger
from ..helpers.utils import Utils


@pytest.fixture
@pytest.fixture(scope="module", autouse=True)
def wazuh_params(request):
wazuh_version = request.config.getoption('--wazuh_version')
wazuh_revision = request.config.getoption('--wazuh_revision')
Expand All @@ -28,7 +28,7 @@ def wazuh_params(request):
}


@pytest.fixture(autouse=True)
@pytest.fixture(scope="module", autouse=True)
def setup_test_environment(wazuh_params):
targets = wazuh_params['targets']
# Clean the string and split it into key-value pairs
Expand All @@ -50,6 +50,18 @@ def setup_test_environment(wazuh_params):
wazuh_params['managers'] = {key: value for key, value in targets_dict.items() if key.startswith('wazuh-')}
wazuh_params['agents'] = {key + '-' + re.findall(r'agent-(.*?)/', value)[0].replace('.',''): value for key, value in targets_dict.items() if key.startswith('agent')}

updated_agents = {}
for agent_name, agent_params in wazuh_params['agents'].items():
Utils.check_inventory_connection(agent_params)
if GeneralComponentActions.isComponentActive(agent_params, 'wazuh-agent') and GeneralComponentActions.hasAgentClientKeys(agent_params):
if HostInformation.get_client_keys(agent_params) != []:
client_name = HostInformation.get_client_keys(agent_params)[0]['name']
updated_agents[client_name] = agent_params
else:
updated_agents[agent_name] = agent_params
if updated_agents != {}:
wazuh_params['agents'] = updated_agents

def test_registration(wazuh_params):
for agent_names, agent_params in wazuh_params['agents'].items():
WazuhAgent.register_agent(agent_params, wazuh_params['master'])
Expand All @@ -73,7 +85,7 @@ def test_isActive(wazuh_params):
assert GeneralComponentActions.isComponentActive(agent_params, 'wazuh-agent'), logger.error(f'{agent_names} is not active by API')

expected_condition_func = lambda: 'active' == WazuhAgent.get_agent_status(wazuh_api, agent_names)
Waits.dynamic_wait(expected_condition_func, cycles=10, waiting_time=20)
Waits.dynamic_wait(expected_condition_func, cycles=20, waiting_time=30)


def test_clientKeys(wazuh_params):
Expand Down
20 changes: 16 additions & 4 deletions deployability/modules/testing/tests/test_agent/test_restart.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
import pytest
import re

from ..helpers.generic import GeneralComponentActions
from ..helpers.generic import GeneralComponentActions, HostInformation
from ..helpers.logger.logger import logger
from ..helpers.manager import WazuhManager
from ..helpers.utils import Utils


@pytest.fixture
@pytest.fixture(scope="module", autouse=True)
def wazuh_params(request):
wazuh_version = request.config.getoption('--wazuh_version')
wazuh_revision = request.config.getoption('--wazuh_revision')
Expand All @@ -27,7 +27,7 @@ def wazuh_params(request):
}


@pytest.fixture(autouse=True)
@pytest.fixture(scope="module", autouse=True)
def setup_test_environment(wazuh_params):
targets = wazuh_params['targets']
# Clean the string and split it into key-value pairs
Expand All @@ -49,6 +49,18 @@ def setup_test_environment(wazuh_params):
wazuh_params['managers'] = {key: value for key, value in targets_dict.items() if key.startswith('wazuh-')}
wazuh_params['agents'] = {key + '-' + re.findall(r'agent-(.*?)/', value)[0].replace('.',''): value for key, value in targets_dict.items() if key.startswith('agent')}

updated_agents = {}
for agent_name, agent_params in wazuh_params['agents'].items():
Utils.check_inventory_connection(agent_params)
if GeneralComponentActions.isComponentActive(agent_params, 'wazuh-agent') and GeneralComponentActions.hasAgentClientKeys(agent_params):
if HostInformation.get_client_keys(agent_params) != []:
client_name = HostInformation.get_client_keys(agent_params)[0]['name']
updated_agents[client_name] = agent_params
else:
updated_agents[agent_name] = agent_params
if updated_agents != {}:
wazuh_params['agents'] = updated_agents

def test_restart(wazuh_params):
for agent_names, agent_params in wazuh_params['agents'].items():
GeneralComponentActions.component_restart(agent_params, 'wazuh-agent')
Expand Down
22 changes: 17 additions & 5 deletions deployability/modules/testing/tests/test_agent/test_stop.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
import re

from ..helpers.agent import WazuhAgent, WazuhAPI
from ..helpers.generic import GeneralComponentActions, Waits
from ..helpers.generic import GeneralComponentActions, Waits, HostInformation
from ..helpers.logger.logger import logger
from ..helpers.utils import Utils


@pytest.fixture
@pytest.fixture(scope="module", autouse=True)
def wazuh_params(request):
wazuh_version = request.config.getoption('--wazuh_version')
wazuh_revision = request.config.getoption('--wazuh_revision')
Expand All @@ -31,7 +31,7 @@ def wazuh_params(request):
for agent_names, agent_params in params['agents'].items():
GeneralComponentActions.component_restart(agent_params, 'wazuh-agent')

@pytest.fixture(autouse=True)
@pytest.fixture(scope="module", autouse=True)
def setup_test_environment(wazuh_params):
targets = wazuh_params['targets']
# Clean the string and split it into key-value pairs
Expand All @@ -53,6 +53,18 @@ def setup_test_environment(wazuh_params):
wazuh_params['managers'] = {key: value for key, value in targets_dict.items() if key.startswith('wazuh-')}
wazuh_params['agents'] = {key + '-' + re.findall(r'agent-(.*?)/', value)[0].replace('.',''): value for key, value in targets_dict.items() if key.startswith('agent')}

updated_agents = {}
for agent_name, agent_params in wazuh_params['agents'].items():
Utils.check_inventory_connection(agent_params)
if GeneralComponentActions.isComponentActive(agent_params, 'wazuh-agent') and GeneralComponentActions.hasAgentClientKeys(agent_params):
if HostInformation.get_client_keys(agent_params) != []:
client_name = HostInformation.get_client_keys(agent_params)[0]['name']
updated_agents[client_name] = agent_params
else:
updated_agents[agent_name] = agent_params
if updated_agents != {}:
wazuh_params['agents'] = updated_agents

def test_stop(wazuh_params):
wazuh_api = WazuhAPI(wazuh_params['master'])
for agent_names, agent_params in wazuh_params['agents'].items():
Expand All @@ -63,4 +75,4 @@ def test_stop(wazuh_params):
assert not GeneralComponentActions.isComponentActive(agent_params, 'wazuh-agent'), logger.error(f'{agent_names} is still active by command')

expected_condition_func = lambda: 'disconnected' == WazuhAgent.get_agent_status(wazuh_api, agent_names)
Waits.dynamic_wait(expected_condition_func, cycles=10, waiting_time=20)
Waits.dynamic_wait(expected_condition_func, cycles=20, waiting_time=30)
20 changes: 16 additions & 4 deletions deployability/modules/testing/tests/test_agent/test_uninstall.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
from ..helpers.generic import HostInformation, GeneralComponentActions, Waits
from ..helpers.manager import WazuhManager, WazuhAPI
from ..helpers.logger.logger import logger
from ..helpers.utils import Utils


@pytest.fixture
@pytest.fixture(scope="module", autouse=True)
def wazuh_params(request):
wazuh_version = request.config.getoption('--wazuh_version')
wazuh_revision = request.config.getoption('--wazuh_revision')
Expand All @@ -29,7 +29,7 @@ def wazuh_params(request):
}


@pytest.fixture(autouse=True)
@pytest.fixture(scope="module", autouse=True)
def setup_test_environment(wazuh_params):
targets = wazuh_params['targets']
# Clean the string and split it into key-value pairs
Expand All @@ -51,6 +51,18 @@ def setup_test_environment(wazuh_params):
wazuh_params['managers'] = {key: value for key, value in targets_dict.items() if key.startswith('wazuh-')}
wazuh_params['agents'] = {key + '-' + re.findall(r'agent-(.*?)/', value)[0].replace('.',''): value for key, value in targets_dict.items() if key.startswith('agent')}

updated_agents = {}
for agent_name, agent_params in wazuh_params['agents'].items():
Utils.check_inventory_connection(agent_params)
if GeneralComponentActions.isComponentActive(agent_params, 'wazuh-agent') and GeneralComponentActions.hasAgentClientKeys(agent_params):
if HostInformation.get_client_keys(agent_params) != []:
client_name = HostInformation.get_client_keys(agent_params)[0]['name']
updated_agents[client_name] = agent_params
else:
updated_agents[agent_name] = agent_params
if updated_agents != {}:
wazuh_params['agents'] = updated_agents

def test_uninstall(wazuh_params):
for agent_names, agent_params in wazuh_params['agents'].items():
assert GeneralComponentActions.isComponentActive(agent_params, 'wazuh-agent'), logger.error(f'{agent_names} is not Active before the installation')
Expand All @@ -76,4 +88,4 @@ def test_isActive(wazuh_params):
assert not GeneralComponentActions.isComponentActive(agent_params, 'wazuh-agent'), logger.error(f'{agent_names} is not inactive by command')

expected_condition_func = lambda: 'disconnected' == WazuhAgent.get_agent_status(wazuh_api, agent_names)
Waits.dynamic_wait(expected_condition_func, cycles=10, waiting_time=20)
Waits.dynamic_wait(expected_condition_func, cycles=20, waiting_time=30)
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from ..helpers.utils import Utils


@pytest.fixture
@pytest.fixture(scope="module", autouse=True)
def wazuh_params(request):
wazuh_version = request.config.getoption('--wazuh_version')
wazuh_revision = request.config.getoption('--wazuh_revision')
Expand All @@ -27,7 +27,7 @@ def wazuh_params(request):
}


@pytest.fixture(autouse=True)
@pytest.fixture(scope="module", autouse=True)
def setup_test_environment(wazuh_params):
targets = wazuh_params['targets']
# Clean the string and split it into key-value pairs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from ..helpers.logger.logger import logger


@pytest.fixture
@pytest.fixture(scope="module", autouse=True)
def wazuh_params(request):
wazuh_version = request.config.getoption('--wazuh_version')
wazuh_revision = request.config.getoption('--wazuh_revision')
Expand All @@ -23,7 +23,7 @@ def wazuh_params(request):
}


@pytest.fixture(autouse=True)
@pytest.fixture(scope="module", autouse=True)
def setup_test_environment(wazuh_params):
targets = wazuh_params['targets']
# Clean the string and split it into key-value pairs
Expand Down
4 changes: 2 additions & 2 deletions deployability/modules/testing/tests/test_manager/test_stop.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from ..helpers.logger.logger import logger


@pytest.fixture
@pytest.fixture(scope="module", autouse=True)
def wazuh_params(request):
wazuh_version = request.config.getoption('--wazuh_version')
wazuh_revision = request.config.getoption('--wazuh_revision')
Expand All @@ -26,7 +26,7 @@ def wazuh_params(request):
GeneralComponentActions.component_restart(worker, 'wazuh-manager')


@pytest.fixture(autouse=True)
@pytest.fixture(scope="module", autouse=True)
def setup_test_environment(wazuh_params):
targets = wazuh_params['targets']
# Clean the string and split it into key-value pairs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from ..helpers.logger.logger import logger


@pytest.fixture
@pytest.fixture(scope="module", autouse=True)
def wazuh_params(request):
wazuh_version = request.config.getoption('--wazuh_version')
wazuh_revision = request.config.getoption('--wazuh_revision')
Expand All @@ -25,7 +25,7 @@ def wazuh_params(request):
}


@pytest.fixture(autouse=True)
@pytest.fixture(scope="module", autouse=True)
def setup_test_environment(wazuh_params):
targets = wazuh_params['targets']
# Clean the string and split it into key-value pairs
Expand Down
Loading

0 comments on commit a56ca72

Please sign in to comment.