Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename vulnerability states index for E2E Vulnerability tests #5402

Merged
merged 6 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ All notable changes to this project will be documented in this file.

### Changed

- Update vulnerability state index name ([#5402](https://github.com/wazuh/wazuh-qa/pull/5402)) \- (Framework)
- Include new package information from wdb ([#5350](https://github.com/wazuh/wazuh-qa/pull/5350)) \- (Tests)
- Disable debug evidences for Vulnerability Detector E2E tests by default ([#5331](https://github.com/wazuh/wazuh-qa/pull/5331)) \- (Tests)
- Include CVE-2023-4822 vulnerability to grafana packages ([#5332](https://github.com/wazuh/wazuh-qa/pull/5332)) \- (Framework)
Expand Down
37 changes: 35 additions & 2 deletions deps/wazuh_testing/wazuh_testing/end_to_end/indexer_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,19 @@
This module provides functions to interact with the Wazuh Indexer API.

Functions:
- get_indexer_values: Retrieves values from the Indexer API.
- get_wazuh_states_vulnerabilities_indexname(cluster_name: str) -> str:
Generate the Wazuh states vulnerabilities index name for a given cluster.
- create_vulnerability_states_indexer_filter(target_agent: str = None,
greater_than_timestamp: str = None) -> dict
Create a filter for the Indexer API for the vulnerability state index.
- create_alerts_filter(target_agent: str = None, greater_than_timestamp: str = None) -> dict
Create a filter for the Indexer API for the alerts index.
- get_indexer_values(host_manager: HostManager, credentials: dict = {'user': 'admin', 'password': 'changeme'},
index: str = 'wazuh-alerts*', filter: dict = None, size: int = 10000) -> Dict
Get values from the Wazuh Indexer API.
- delete_index(host_manager: HostManager, credentials: dict = {'user': 'admin', 'password': 'changeme'},
index: str = 'wazuh-alerts*')
Delete index from the Wazuh Indexer API.

Copyright (C) 2015, Wazuh Inc.
Created by Wazuh, Inc. <info@wazuh.com>.
Expand All @@ -18,7 +30,28 @@
from wazuh_testing.tools.system import HostManager


WAZUH_STATES_VULNERABILITIES_INDEXNAME = 'wazuh-states-vulnerabilities'
WAZUH_STATES_VULNERABILITIES_INDEXNAME_TEMPLATE = 'wazuh-states-vulnerabilities-{cluster_name}'


def get_wazuh_states_vulnerabilities_indexname(cluster_name: str = 'wazuh') -> str:
"""
Generate the Wazuh states vulnerabilities index name for a given cluster.

This function takes a cluster name as input and returns the corresponding
Wazuh states vulnerabilities index name by inserting the cluster name into
a predefined template.

Args:
cluster_name (str): The name of the cluster to be included in the index name.

Returns:
str: The formatted Wazuh states vulnerabilities index name.

Example:
>>> get_wazuh_states_vulnerabilities_indexname('cluster1')
'wazuh-states-vulnerabilities-cluster1'
"""
return WAZUH_STATES_VULNERABILITIES_INDEXNAME_TEMPLATE.format(cluster_name=cluster_name)


def create_vulnerability_states_indexer_filter(target_agent: str = None,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

from wazuh_testing.tools.system import HostManager
from wazuh_testing.end_to_end.indexer_api import get_indexer_values, create_vulnerability_states_indexer_filter, \
create_alerts_filter, WAZUH_STATES_VULNERABILITIES_INDEXNAME
create_alerts_filter, get_wazuh_states_vulnerabilities_indexname
from wazuh_testing.end_to_end.regex import REGEX_PATTERNS
from collections import namedtuple

Expand Down Expand Up @@ -275,7 +275,7 @@ def parse_vulnerability_from_state(state):


def get_vulnerabilities_from_states_by_agent(host_manager: HostManager, agents: List[str],
greater_than_timestamp: str = None) -> dict:
greater_than_timestamp: str = None, cluster_name='wazuh') -> dict:
"""Get vulnerabilities from the vulnerability state index by agent.

Args:
Expand All @@ -302,11 +302,12 @@ def get_vulnerabilities_from_states_by_agent(host_manager: HostManager, agents:
for agent in agents:
agent_all_vulnerabilities = []
try:
index = get_wazuh_states_vulnerabilities_indexname(cluster_name)
states_filter = create_vulnerability_states_indexer_filter(target_agent=agent,
greater_than_timestamp=greater_than_timestamp)
agent_all_vulnerabilities = get_indexer_values(host_manager,
filter=states_filter,
index=WAZUH_STATES_VULNERABILITIES_INDEXNAME,
index=index,
credentials={'user': indexer_user,
'password': indexer_password}
)['hits']['hits']
Expand Down
19 changes: 10 additions & 9 deletions deps/wazuh_testing/wazuh_testing/tools/performance/statistic.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,9 @@ def _parse_api_data(self):
"""Read the data generated by Wazuh API."""

API_URL = f"https://{self.ip}:{self.port}"
CLUSTER_NAME = 'wazuh'
DAEMONS_ENDPOINT= f"/manager/daemons/stats?daemons_list={self.daemon}&wait_for_complete=true"
VULNS_ENDOPOINT= f"/wazuh-states-vulnerabilities/_count"
VULNS_ENDOPOINT= f"/wazuh-states-vulnerabilities-{CLUSTER_NAME}/_count"
ALERTS_ENDPOINT= f"/wazuh-alerts-4.x-*/_count"
TOKEN_ENDPOINT="/security/user/authenticate"

Expand All @@ -164,7 +165,7 @@ def _parse_api_data(self):
max_retries = 3
token_response = None
daemon_response = None
data = None
data = None

if(self.target == "vulnerabilities"):
for _ in range(max_retries):
Expand Down Expand Up @@ -244,22 +245,22 @@ def _write_csv(self, data, target, csv_file):
csv_header = headers.agentd_header

header = not isfile(csv_file)

with open(csv_file, 'a+') as log:

if header:
log.write(f'{",".join(csv_header)}\n')

timestamp = datetime.fromtimestamp(time()).strftime('%Y-%m-%d %H:%M:%S')

if self.use_state_file == False:
if target not in ["vulnerabilities", "alerts"]:
format = r"%Y-%m-%dT%H:%M:%S+%f:00"
datetime_timestamp = datetime.strptime(data['timestamp'], format)
datetime_uptime = datetime.strptime(data['uptime'], format)
interval = (datetime_timestamp - datetime_uptime).total_seconds()

if target == "analysis":
if target == "analysis":
metrics = data['metrics']
decoded = metrics['events']['received_breakdown']['decoded_breakdown']
decoded_modules = decoded['modules_breakdown']
Expand Down Expand Up @@ -425,11 +426,11 @@ def _write_csv(self, data, target, csv_file):
ag_bd['tables']['syscheck']['fim_file'], # 17
ag_bd['tables']['syscheck']['fim_registry'], # 18
ag_bd['tables']['syscheck']['fim_registry_key'], # 19
ag_bd['tables']['syscheck']['fim_registry_value'], # 20
ag_bd['tables']['syscheck']['fim_registry_value'], # 20
ag_bd['tables']['syscollector']['syscollector_hotfixes'], # 21
ag_bd['tables']['syscollector']['syscollector_hwinfo'], # 22
ag_bd['tables']['syscollector']['syscollector_hwinfo'], # 22
ag_bd['tables']['syscollector']['syscollector_network_address'], # 23
ag_bd['tables']['syscollector']['syscollector_network_iface'], # 24
ag_bd['tables']['syscollector']['syscollector_network_iface'], # 24
ag_bd['tables']['syscollector']['syscollector_network_protocol'], # 25
ag_bd['tables']['syscollector']['syscollector_osinfo'], # 26
ag_bd['tables']['syscollector']['syscollector_packages'], # 27
Expand All @@ -438,7 +439,7 @@ def _write_csv(self, data, target, csv_file):
vulnerability_data, # 30
received_breakdown['global'], # 31
glob_bd['db']['backup'], # 32
glob_bd['db']['sql'], # 33
glob_bd['db']['sql'], # 33
glob_bd['db']['vacuum'], # 34
glob_bd['db']['get_fragmentation'], # 35
glob_bd['tables']['agent']['delete-agent'], # 36
Expand Down
10 changes: 10 additions & 0 deletions deps/wazuh_testing/wazuh_testing/tools/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -884,6 +884,16 @@ def get_api_credentials(self):

return user, password

def get_cluster_name(self):
manager_list = self.get_group_hosts('manager')
if not manager_list:
raise ValueError("No manager is defined in the environment")

first_manager_vars = self.inventory_manager.get_host(manager_list[0])
cluster_name = first_manager_vars.vars.get('cluster_name', 'wazuh')

return cluster_name

def get_indexer_credentials(self):
default_user = 'admin'
default_password = 'changeme'
Expand Down
6 changes: 4 additions & 2 deletions tests/end_to_end/test_vulnerability_detector/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def test_example(host_manager):
restore_configuration,
save_indexer_credentials_into_keystore)
from wazuh_testing.end_to_end.indexer_api import (
WAZUH_STATES_VULNERABILITIES_INDEXNAME, delete_index)
get_wazuh_states_vulnerabilities_indexname, delete_index)
from wazuh_testing.end_to_end.logs import (get_hosts_alerts, get_hosts_logs,
truncate_remote_host_group_files)
from wazuh_testing.end_to_end.remote_operations_handler import (
Expand Down Expand Up @@ -162,7 +162,9 @@ def delete_states_vulnerability_index(host_manager: HostManager):
"""
yield
logging.error("Delete vulnerability index")
delete_index(host_manager, index=WAZUH_STATES_VULNERABILITIES_INDEXNAME)

cluster_name = host_manager.get_cluster_name()
delete_index(host_manager, index=get_wazuh_states_vulnerabilities_indexname(cluster_name))


def collect_e2e_environment_data(logs_path, host_manager) -> None:
Expand Down
Loading