Skip to content

Commit

Permalink
Merge pull request #4205 from wazuh/wazuh-jenkins3487-Unexpected-Wind…
Browse files Browse the repository at this point in the history
…ows-agent-result

Unexpected Windows error during report gathering in Integration test
  • Loading branch information
davidjiglesias authored Sep 8, 2023
2 parents 7ba82ef + 26e4b59 commit 5d3c597
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 24 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ All notable changes to this project will be documented in this file.
Wazuh commit: TBD \
Release report: TBD

### Fixed

- Enhancing the handling of authd and remoted simulators in case of restart failures ([#Wazuh-jenkins#3487](https://github.com/wazuh/wazuh-qa/pull/4205)) \- (Tests)

## [4.5.2] - TBD

Wazuh commit: TBD \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ def start_agent(request, get_configuration):
Args:
get_configuration (fixture): Get configurations from the module.
"""
agent_restart_failure = False
metadata = get_configuration['metadata']
authd_simulator = AuthdSimulator(server_address=SERVER_ADDRESS,
enrollment_port=1515,
Expand All @@ -130,16 +131,22 @@ def start_agent(request, get_configuration):
truncate_file(CLIENT_KEYS_PATH)
time.sleep(1)

control_service('stop')
agent_auth_pat = 'bin' if platform.system() == 'Linux' else ''
call([f'{WAZUH_PATH}/{agent_auth_pat}/agent-auth', '-m', SERVER_ADDRESS])
control_service('start')
try:
control_service('stop')
agent_auth_pat = 'bin' if platform.system() == 'Linux' else ''
call([f'{WAZUH_PATH}/{agent_auth_pat}/agent-auth', '-m', SERVER_ADDRESS])
control_service('start')

yield
except Exception:
print("Failure to restart the agent")
agent_restart_failure = True

yield agent_restart_failure

remoted_simulator.stop()
authd_simulator.shutdown()


@pytest.fixture(scope="function")
def remove_ip_from_iptables(request, get_configuration):
"""Remove the testing IP address from `iptables` if it exists.
Expand Down Expand Up @@ -279,6 +286,9 @@ def test_execd_firewall_drop(set_debug_mode, get_configuration, test_version, co
tags:
- simulator
'''
# Check if the agent is restarted properly"
assert not start_agent, 'The agent failed to restart successfully after enrolling the authentication simulator.'

metadata = get_configuration['metadata']
expected = metadata['results']
ossec_log_monitor = FileMonitor(LOG_FILE_PATH)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ def start_agent(request, get_configuration):
Args:
get_configuration (fixture): Get configurations from the module.
"""
agent_restart_failure = False
metadata = get_configuration['metadata']
authd_simulator = AuthdSimulator(server_address=SERVER_ADDRESS,
enrollment_port=1515,
Expand All @@ -125,13 +126,18 @@ def start_agent(request, get_configuration):
truncate_file(CLIENT_KEYS_PATH)
time.sleep(1)

control_service('stop')
agent_auth_pat = 'bin' if platform.system() == 'Linux' else ''
subprocess.call([f'{WAZUH_PATH}/{agent_auth_pat}/agent-auth', '-m',
SERVER_ADDRESS])
control_service('start')
try:
control_service('stop')
agent_auth_pat = 'bin' if platform.system() == 'Linux' else ''
subprocess.call([f'{WAZUH_PATH}/{agent_auth_pat}/agent-auth', '-m',
SERVER_ADDRESS])
control_service('start')

yield
except Exception:
print("Failure to restart the agent")
agent_restart_failure = True

yield agent_restart_failure

remoted_simulator.stop()
authd_simulator.shutdown()
Expand Down Expand Up @@ -230,6 +236,10 @@ def test_execd_restart(set_debug_mode, get_configuration, test_version,
tags:
- simulator
'''

# Check if the agent is restarted properly"
assert not start_agent, 'The agent failed to restart successfully after enrolling the authentication simulator.'

metadata = get_configuration['metadata']
expected = metadata['results']
ossec_log_monitor = FileMonitor(LOG_FILE_PATH)
Expand Down
33 changes: 20 additions & 13 deletions tests/integration/test_enrollment/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ def configure_socket_listener(request, get_current_test_case):
"""
Configures the socket listener to start listening on the socket.
"""
socket_listener_opened = True
if 'message' in get_current_test_case and 'response' in get_current_test_case['message']:
response = get_current_test_case['message']['response'].format(host_name=get_host_name()).encode()
else:
Expand All @@ -80,22 +81,28 @@ def configure_socket_listener(request, get_current_test_case):
def receiver_callback(received_event):
return response if not expected or expected == received_event else "".encode()

socket_listener = ManInTheMiddle(address=(manager_address, MANAGER_PORT), family=address_family,
connection_protocol='SSL', func=receiver_callback)
socket_listener.start()
socket_listener.listener.set_ssl_configuration(connection_protocol=ssl.PROTOCOL_TLSv1_2,
certificate=AGENT_CERT_PATH,
keyfile=AGENT_KEY_PATH,
options=None,
cert_reqs=ssl.CERT_OPTIONAL)
try:
socket_listener = ManInTheMiddle(address=(manager_address, MANAGER_PORT), family=address_family,
connection_protocol='SSL', func=receiver_callback)

while not socket_listener.queue.empty():
socket_listener.queue.get_nowait()
socket_listener.event.clear()
socket_listener.start()
socket_listener.listener.set_ssl_configuration(connection_protocol=ssl.PROTOCOL_TLSv1_2,
certificate=AGENT_CERT_PATH,
keyfile=AGENT_KEY_PATH,
options=None,
cert_reqs=ssl.CERT_OPTIONAL)

setattr(request.module, 'socket_listener', socket_listener)
while not socket_listener.queue.empty():
socket_listener.queue.get_nowait()
socket_listener.event.clear()

yield
setattr(request.module, 'socket_listener', socket_listener)

except Exception:
print("Unexpected exception occurred during Man In the Middle initialization")
socket_listener_opened = False

yield socket_listener_opened

socket_listener.shutdown()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ def test_agent_auth_enrollment(configure_environment, shutdown_agentd, get_curre
- Error logs related to the wrong configuration block
"""

# Check if socket listener is opened
assert configure_socket_listener, 'The agent failed configuring socket listener to start listening on the socket.'

if 'agent-auth' in get_current_test_case.get('skips', []):
pytest.skip('This test does not apply to agent-auth')

Expand Down
3 changes: 3 additions & 0 deletions tests/integration/test_enrollment/test_agentd_enrollment.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,9 @@ def test_agentd_enrollment(configure_environment, override_wazuh_conf, get_curre
- Error logs related to the wrong configuration block
"""

# Check if socket listener is opened
assert configure_socket_listener, 'The agent failed configuring socket listener to start listening on the socket.'

if 'expected_error' in get_current_test_case:
log_monitor = request.module.log_monitor
expected_error_dict = get_current_test_case['expected_error']
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,9 @@ def test_agentd_server_address_configuration(configure_local_internal_options_mo
- agentd
'''

# Check if socket listener is opened
assert configure_socket_listener, 'The agent failed configuring socket listener to start listening on the socket.'

cfg = get_configuration['metadata']
manager_address = cfg['server_address']

Expand Down

0 comments on commit 5d3c597

Please sign in to comment.