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

Add localfile wildcard support for windows IT #3906

Closed
wants to merge 22 commits into from
Closed
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
e63d59f
feat(#3786): add basic test structure
Deblintrake09 Feb 7, 2023
39c8dd0
docs(#3786): update changelog
Deblintrake09 Feb 7, 2023
62d8892
feat(#3786): add new event checkers
Deblintrake09 Feb 8, 2023
515b505
feat(#3786): add create_files_in_folder fixture
Deblintrake09 Feb 8, 2023
e7e59c7
fix(#3786): update test and add new test cases
Deblintrake09 Feb 8, 2023
d7cc427
docs(#3786): update test cases description
Deblintrake09 Feb 8, 2023
abbf65b
style(#3786): add separating whiteline
Deblintrake09 Feb 8, 2023
2b09880
merge(#3786): '4.5' to 3786-logcollector-wildcards
Deblintrake09 Feb 16, 2023
d8f80a8
fix(#3786): fix cofiguration_ignore_binaries test
Deblintrake09 Feb 17, 2023
06f00ea
fix(#3786): fixed callback and test cases
Deblintrake09 Feb 17, 2023
35f4ac6
docs(#3786): fix docu typo
Deblintrake09 Mar 14, 2023
0dfc0ff
fix(#3786): add xfail for test_ignore_binaries
Deblintrake09 Apr 25, 2023
684e2df
merge(#3786): '4.5' into dev branch
Deblintrake09 Apr 25, 2023
51bca08
style(#3786): remove empty whitelines
Deblintrake09 Apr 25, 2023
4aa4d7d
fix(#3786): test_configuration_log_format callback
Deblintrake09 Apr 27, 2023
ffca56f
refactor(#3786): rename test and test cases
Deblintrake09 Apr 28, 2023
a56704b
refactor(#3786): move test_win_location_wildcards
Deblintrake09 Apr 28, 2023
ae13d71
fix(#3786): fix callback import
Deblintrake09 Apr 28, 2023
2a2f98c
merge(#3786): '4.5' in 3786-logcollector-wildcards
Deblintrake09 May 2, 2023
5de9cca
docs(#3906): Update CHANGELOG.md
Deblintrake09 May 4, 2023
a562c29
merge(#3786): '4.5'->3786-logcollector-wildcards
Deblintrake09 Jun 22, 2023
aba453f
merge(#3786): 4.6.0 in 3786-logcollector-wildcards
Deblintrake09 Jul 3, 2023
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 @@ -9,6 +9,7 @@ Release report: TBD

### Added

- Add Windows location wildcards tests ([#3906](https:/wazuh/wazuh-qa/pull/3906)) \- (Tests)
Deblintrake09 marked this conversation as resolved.
Show resolved Hide resolved
- Add tests with new options to avoid FIM synchronization overlapping. ([#3318](https:/wazuh/wazuh-qa/pull/3318)) \- (Framework + tests)
- Add Logcollector millisecond granularity support test case ([#3910](https:/wazuh/wazuh-qa/pull/3910)) \- (Tests)
- Add Windows System folders FIM monitoring tests ([#3720](https:/wazuh/wazuh-qa/pull/3720)) \- (Tests)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from wazuh_testing.tools.monitoring import FileMonitor


# Event detectors
def make_logcollector_callback(pattern, prefix=LOG_COLLECTOR_PREFIX, escape=False):
"""Create a callback function from a text pattern.

Expand Down Expand Up @@ -132,3 +133,43 @@ def check_ignore_restrict_message_not_found(message, regex, tag, prefix):
with pytest.raises(TimeoutError):
log_found = check_ignore_restrict_message(message=message, regex=regex, tag=tag, prefix=prefix)
assert log_found is False, ERR_MSG_UNEXPECTED_IGNORE_EVENT


def check_wildcard_pattern_expanded(file_path, location_regex, prefix, error_message=None, file_monitor=None,
timeout=T_10, escape=False):
"""Create a callback to detect "New file that matches the '{file_path}' pattern: '(.*)'" line.

Args:
file_path (str): file path that is being monitored
location_regex (str): path configured in location tag
prefix (str): Daemon that generates the error log.
error_message (str): Error message.
file_monitor (FileMonitor): Log monitor.
timeout (int): Timeout to check the log.
escape (bool): Flag to escape special characters in the pattern.

Returns: True if the expected message has been found, False otherwise.
"""
callback_msg = f".*New file that matches the '{location_regex}' pattern: '{file_path}'"

return check_logcollector_event(file_monitor=file_monitor, timeout=timeout, callback=callback_msg,
error_message=error_message, prefix=prefix, escape=escape)


def check_wildcard_pattern_no_match(regex, prefix, error_message=None, file_monitor=None, timeout=T_10, escape=False):
juliamagan marked this conversation as resolved.
Show resolved Hide resolved
"""Create a callback to detect "DEBUG: No file/folder that matches ..." line.

Args:
regex (str): regex pattern configured in location tag for monitoring
prefix (str): Daemon that generates the error log.
error_message (str): Error message.
file_monitor (FileMonitor): Log monitor.
timeout (int): Timeout to check the log.
escape (bool): Flag to escape special characters in the pattern.

Returns: True if the expected message has been found, False otherwise.
"""
callback_msg = f".*expand_win32_wildcards.*DEBUG: No .* that matches {regex}"

return check_logcollector_event(file_monitor=file_monitor, timeout=timeout, callback=callback_msg,
error_message=error_message, prefix=prefix, escape=escape)
6 changes: 3 additions & 3 deletions deps/wazuh_testing/wazuh_testing/tools/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ def delete_file(file_path):

def delete_path_recursively(path):
if os.path.exists(path):
shutil.rmtree(path, onerror=on_write_error)
shutil.rmtree(path, ignore_errors=True, onerror=on_write_error)
damarisg marked this conversation as resolved.
Show resolved Hide resolved


def on_write_error(function, path, exc_info):
Expand Down Expand Up @@ -466,10 +466,10 @@ def recursive_directory_creation(path):
if parent != '' and not os.path.exists(parent):
split = os.path.split(parent)
recursive_directory_creation(split[0])
os.mkdir(parent)
os.mkdir(parent, mode=0o0777)

if not os.path.exists(path):
os.mkdir(path)
os.mkdir(path, mode=0o0777)


def move_everything_from_one_directory_to_another(source_directory, destination_directory):
Expand Down
20 changes: 19 additions & 1 deletion tests/integration/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
from wazuh_testing.tools import (PREFIX, LOG_FILE_PATH, WAZUH_CONF, get_service, ALERT_FILE_PATH,
WAZUH_LOCAL_INTERNAL_OPTIONS)
from wazuh_testing.tools.configuration import get_wazuh_conf, set_section_wazuh_conf, write_wazuh_conf
from wazuh_testing.tools.file import truncate_file, recursive_directory_creation, remove_file, copy, write_file
from wazuh_testing.tools.file import (truncate_file, recursive_directory_creation, remove_file, copy, write_file,
delete_path_recursively)
from wazuh_testing.tools.monitoring import QueueMonitor, FileMonitor, SocketController, close_sockets
from wazuh_testing.tools.services import control_service, check_daemon_status, delete_dbs
from wazuh_testing.tools.time import TimeMachine
Expand Down Expand Up @@ -1204,6 +1205,23 @@ def copy_file(source_path, destination_path):
remove_file(file)


@pytest.fixture()
def create_files_in_folder(folder_path, file_list):
"""Create a list of files, inside a given path. Deletes it at the end.

Args:
folder_path (str): folder path to create.
file_list (List): list of file names to create
"""
recursive_directory_creation(folder_path)
for file in file_list:
write_file(os.path.join(folder_path, file))

yield

delete_path_recursively(folder_path)


@pytest.fixture(scope='function')
def create_file(new_file_path):
"""Create an empty file.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import os
import pytest
import sys
import re

from wazuh_testing.tools.configuration import load_wazuh_configurations
import wazuh_testing.generic_callbacks as gc
Expand All @@ -64,7 +65,10 @@
from wazuh_testing.tools.services import control_service
from wazuh_testing.tools.file import truncate_file
import wazuh_testing.api as api
from wazuh_testing.tools.monitoring import LOG_COLLECTOR_DETECTOR_PREFIX, WINDOWS_AGENT_DETECTOR_PREFIX, FileMonitor
from wazuh_testing.tools.monitoring import FileMonitor
from wazuh_testing.modules.logcollector import LOG_COLLECTOR_PREFIX, WINDOWS_AGENT_PREFIX
from wazuh_testing.modules.logcollector.event_monitor import check_wildcard_pattern_no_match


import subprocess as sb

Expand All @@ -83,10 +87,8 @@
force_restart_after_restoring = True
location = r'C:\testing\files*'
wazuh_configuration = 'ossec.conf'
prefix = WINDOWS_AGENT_DETECTOR_PREFIX

else:
prefix = LOG_COLLECTOR_DETECTOR_PREFIX
location = '/tmp/testing/files*'
wazuh_configuration = 'etc/ossec.conf'

Expand Down Expand Up @@ -145,10 +147,8 @@ def check_ignore_binaries_valid(cfg):
wazuh_log_monitor = FileMonitor(LOG_FILE_PATH)

if sys.platform == 'win32':
log_callback = logcollector.callback_invalid_location_pattern(cfg['location'])
wazuh_log_monitor.start(timeout=5, callback=log_callback,
error_message=logcollector.GENERIC_CALLBACK_ERROR_INVALID_LOCATION)

check_wildcard_pattern_no_match(re.escape(cfg['location']), WINDOWS_AGENT_PREFIX, escape=False)

if wazuh_component == 'wazuh-manager':
real_configuration = cfg.copy()
real_configuration.pop('valid_value')
Expand All @@ -167,17 +167,17 @@ def check_ignore_binaries_invalid(cfg):
"""
wazuh_log_monitor = FileMonitor(LOG_FILE_PATH)

log_callback = gc.callback_invalid_value('ignore_binaries', cfg['ignore_binaries'], prefix)
log_callback = gc.callback_invalid_value('ignore_binaries', cfg['ignore_binaries'], LOG_COLLECTOR_PREFIX)
wazuh_log_monitor.start(timeout=5, callback=log_callback,
error_message=gc.GENERIC_CALLBACK_ERROR_MESSAGE)

log_callback = gc.callback_error_in_configuration('ERROR', prefix,
log_callback = gc.callback_error_in_configuration('ERROR', LOG_COLLECTOR_PREFIX,
conf_path=f'{wazuh_configuration}')
wazuh_log_monitor.start(timeout=5, callback=log_callback,
error_message=gc.GENERIC_CALLBACK_ERROR_MESSAGE)

if sys.platform != 'win32':
log_callback = gc.callback_error_in_configuration('CRITICAL', prefix,
log_callback = gc.callback_error_in_configuration('CRITICAL', LOG_COLLECTOR_PREFIX,
conf_path=f'{wazuh_configuration}')
wazuh_log_monitor.start(timeout=5, callback=log_callback,
error_message=gc.GENERIC_CALLBACK_ERROR_MESSAGE)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
- sections:
- section: localfile
elements:
- log_format:
value: syslog
- location:
value: LOCATION

- section: sca
elements:
- enabled:
value: 'no'

- section: rootcheck
elements:
- disabled:
value: 'yes'

- section: syscheck
elements:
- disabled:
value: 'yes'

- section: wodle
attributes:
- name: syscollector
elements:
- disabled:
value: 'yes'
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
- name: Test single asterisk wildcard
juliamagan marked this conversation as resolved.
Show resolved Hide resolved
description: Test location tag with one asterisk wildcard
configuration_parameters:
LOCATION: c:\testfol*\subfolder\test
metadata:
matches: true
location: c:\testfol*\subfolder\test

- name: Test single question mark wildcard
description: Test location tag with one question mark wildcard
configuration_parameters:
LOCATION: c:\testfolde?\subfolder\test
metadata:
location: c:\testfolde?\subfolder\test
matches: true

- name: Test partial words with asterisk wildcards
description: Test location tag with words completed using asterisk wildcard
configuration_parameters:
LOCATION: c:\test*\sub*\t*
metadata:
location: c:\test*\sub*\t*
matches: true

- name: Test mixed wildcards
description: Test location tag with mixed asterisk and question mark wildcards
configuration_parameters:
LOCATION: c:\testf?lder\*\tes?
metadata:
location: c:\testf?lder\*\tes?
matches: true

- name: Test mixed wildcards - path asterisk only
description: Test location tag were filepath uses asterisk only
configuration_parameters:
LOCATION: c:\*\*\?es?
metadata:
location: c:\*\*\?es?
matches: true

- name: Test invalid wildcards - no match
description: Test location tag where wildcards do not match a valid file
configuration_parameters:
LOCATION: c:\testfolder\subfolder\tes?.log
metadata:
matches: false
location: c:\testfolder\subfolder\tes?.log
Loading