updated indexes to delete and adapted pytests

This commit is contained in:
Carlos Anguita López 2025-11-07 13:33:51 +01:00
parent 207d66fbfc
commit e1aeeec3e9
No known key found for this signature in database
GPG Key ID: FA3E2896D509F5AE
4 changed files with 30 additions and 82 deletions

View File

@ -183,35 +183,22 @@ class AmiPostConfigurer:
self.stop_service("wazuh-server", client=client) self.stop_service("wazuh-server", client=client)
def remove_indexer_index_list(self, client: paramiko.SSHClient) -> None: def remove_wazuh_indexes(self, client: paramiko.SSHClient) -> None:
""" """
Remove the indexer index list. Remove all wazuh-* indexes.
""" """
logger.debug("Removing indexer index list") logger.debug("Removing all wazuh- indexes")
index_list: list[str] = [ base_url = "https://127.0.0.1:9200"
"wazuh-alerts",
"wazuh-archives",
"wazuh-states-vulnerabilities",
"wazuh-statistics",
"wazuh-monitoring",
]
base_url = "https://localhost:9200"
commands = []
for index in index_list:
commands.append(
f'curl -s -o /dev/null -w "%{{http_code}}" -X DELETE -u "admin:admin" -k "{base_url}/{index}-*"'
)
command = " && sudo ".join(commands) command = f'sudo curl -s -o /dev/null -w "%{{http_code}}" -X DELETE -u "admin:admin" -k "{base_url}/wazuh-*"'
command = f"sudo {command}"
_, error_output = exec_command(command=command, client=client) _, error_output = exec_command(command=command, client=client)
if error_output: if error_output:
logger.error("Error removing the indexer index list") logger.error("Error removing wazuh- indexes")
raise RuntimeError(f"Error removing the indexer index list: {error_output}") raise RuntimeError(f"Error removing wazuh- indexes: {error_output}")
logger.debug("Indexer index list removed successfully") logger.debug("wazuh- indexes removed successfully")
def run_security_init_script(self, client: paramiko.SSHClient) -> None: def run_security_init_script(self, client: paramiko.SSHClient) -> None:
""" """
@ -245,7 +232,7 @@ class AmiPostConfigurer:
None None
""" """
self.remove_indexer_index_list(client=client) self.remove_wazuh_indexes(client=client)
self.run_security_init_script(client=client) self.run_security_init_script(client=client)
self.stop_service("wazuh-indexer", client=client) self.stop_service("wazuh-indexer", client=client)

View File

@ -343,15 +343,7 @@ def main() -> None:
steps_system_config() steps_system_config()
run_command("systemctl stop wazuh-server") run_command("systemctl stop wazuh-server")
indexes = [ run_command("curl -u admin:admin -XDELETE 'https://127.0.0.1:9200/wazuh-*' -k")
"wazuh-alerts-*",
"wazuh-archives-*",
"wazuh-states-vulnerabilities-*",
"wazuh-statistics-*",
"wazuh-monitoring-*",
]
for index in indexes:
run_command(f"curl -u admin:admin -XDELETE 'https://127.0.0.1:9200/{index}' -k")
run_command("bash /usr/share/wazuh-indexer/bin/indexer-security-init.sh -ho 127.0.0.1") run_command("bash /usr/share/wazuh-indexer/bin/indexer-security-init.sh -ho 127.0.0.1")

View File

@ -155,65 +155,42 @@ def test_stop_wazuh_server(mock_ami_post_configurer, mock_exec_command, mock_par
def test_stop_wazuh_indexer(mock_ami_post_configurer, mock_exec_command, mock_paramiko, mock_logger): def test_stop_wazuh_indexer(mock_ami_post_configurer, mock_exec_command, mock_paramiko, mock_logger):
mock_ami_post_configurer.stop_wazuh_indexer(mock_paramiko.return_value) mock_ami_post_configurer.stop_wazuh_indexer(mock_paramiko.return_value)
commands = [ expected_commands = {
""" 'sudo curl -s -o /dev/null -w "%{http_code}" -X DELETE -u "admin:admin" -k "https://127.0.0.1:9200/wazuh-*"',
sudo curl -s -o /dev/null -w "%{http_code}" -X DELETE -u "admin:admin" -k "https://localhost:9200/wazuh-alerts-*" && "sudo /usr/share/wazuh-indexer/bin/indexer-security-init.sh",
sudo curl -s -o /dev/null -w "%{http_code}" -X DELETE -u "admin:admin" -k "https://localhost:9200/wazuh-archives-*" && "sudo systemctl stop wazuh-indexer",
sudo curl -s -o /dev/null -w "%{http_code}" -X DELETE -u "admin:admin" -k "https://localhost:9200/wazuh-states-vulnerabilities-*" && }
sudo curl -s -o /dev/null -w "%{http_code}" -X DELETE -u "admin:admin" -k "https://localhost:9200/wazuh-statistics-*" &&
sudo curl -s -o /dev/null -w "%{http_code}" -X DELETE -u "admin:admin" -k "https://localhost:9200/wazuh-monitoring-*"
""",
"""
sudo /usr/share/wazuh-indexer/bin/indexer-security-init.sh
""",
"""
sudo systemctl stop wazuh-indexer
""",
]
for command_call in mock_exec_command.call_args_list: called_commands = {c.kwargs["command"] for c in mock_exec_command.call_args_list}
command_call.kwargs["command"] = command_call.kwargs["command"].replace("\n", "").replace(" ", "") for cmd in expected_commands:
assert cmd in called_commands
for command in commands: mock_logger.debug.assert_any_call("Removing all wazuh- indexes")
command = command.replace("\n", "").replace(" ", "") mock_logger.debug.assert_any_call("wazuh- indexes removed successfully")
mock_exec_command.assert_any_call(command=command, client=mock_paramiko.return_value)
mock_logger.debug.assert_any_call("Removing indexer index list")
mock_logger.debug.assert_any_call("Indexer index list removed successfully")
mock_logger.debug.assert_any_call("Running indexer security init script") mock_logger.debug.assert_any_call("Running indexer security init script")
mock_logger.debug.assert_any_call("Indexer security init script executed successfully") mock_logger.debug.assert_any_call("Indexer security init script executed successfully")
mock_logger.debug.assert_any_call("Stopping wazuh-indexer service") mock_logger.debug.assert_any_call("Stopping wazuh-indexer service")
mock_logger.info_success.assert_any_call("wazuh-indexer service stopped successfully") mock_logger.info_success.assert_any_call("wazuh-indexer service stopped successfully")
def test_remove_indexer_index_list(mock_ami_post_configurer, mock_exec_command, mock_paramiko, mock_logger): def test_remove_wazuh_indexes(mock_ami_post_configurer, mock_exec_command, mock_paramiko, mock_logger):
mock_ami_post_configurer.remove_indexer_index_list(mock_paramiko.return_value) mock_ami_post_configurer.remove_wazuh_indexes(mock_paramiko.return_value)
command = """ command = 'sudo curl -s -o /dev/null -w "%{http_code}" -X DELETE -u "admin:admin" -k "https://127.0.0.1:9200/wazuh-*"'
sudo curl -s -o /dev/null -w "%{http_code}" -X DELETE -u "admin:admin" -k "https://localhost:9200/wazuh-alerts-*" &&
sudo curl -s -o /dev/null -w "%{http_code}" -X DELETE -u "admin:admin" -k "https://localhost:9200/wazuh-archives-*" &&
sudo curl -s -o /dev/null -w "%{http_code}" -X DELETE -u "admin:admin" -k "https://localhost:9200/wazuh-states-vulnerabilities-*" &&
sudo curl -s -o /dev/null -w "%{http_code}" -X DELETE -u "admin:admin" -k "https://localhost:9200/wazuh-statistics-*" &&
sudo curl -s -o /dev/null -w "%{http_code}" -X DELETE -u "admin:admin" -k "https://localhost:9200/wazuh-monitoring-*"
""".replace("\n", "").replace(" ", "")
for command_call in mock_exec_command.call_args_list:
command_call.kwargs["command"] = command_call.kwargs["command"].replace("\n", "").replace(" ", "")
mock_exec_command.assert_called_once_with(command=command, client=mock_paramiko.return_value) mock_exec_command.assert_called_once_with(command=command, client=mock_paramiko.return_value)
mock_logger.debug.assert_any_call("Removing indexer index list") mock_logger.debug.assert_any_call("Removing all wazuh- indexes")
mock_logger.debug.assert_any_call("Indexer index list removed successfully") mock_logger.debug.assert_any_call("wazuh- indexes removed successfully")
def test_remove_indexer_index_list_fail(mock_ami_post_configurer, mock_exec_command, mock_paramiko, mock_logger): def test_remove_wazuh_indexes_fail(mock_ami_post_configurer, mock_exec_command, mock_paramiko, mock_logger):
mock_exec_command.return_value = ("", "Command failed") mock_exec_command.return_value = ("", "Command failed")
with pytest.raises(Exception, match="Error removing the indexer index list: Command failed"): with pytest.raises(Exception, match="Error removing wazuh- indexes: Command failed"):
mock_ami_post_configurer.remove_indexer_index_list(mock_paramiko.return_value) mock_ami_post_configurer.remove_wazuh_indexes(mock_paramiko.return_value)
mock_logger.error.assert_called_once_with("Error removing the indexer index list")
mock_logger.error.assert_called_once_with("Error removing wazuh- indexes")
def test_run_security_init_script(mock_ami_post_configurer, mock_exec_command, mock_paramiko, mock_logger): def test_run_security_init_script(mock_ami_post_configurer, mock_exec_command, mock_paramiko, mock_logger):
mock_ami_post_configurer.run_security_init_script(mock_paramiko.return_value) mock_ami_post_configurer.run_security_init_script(mock_paramiko.return_value)

View File

@ -330,15 +330,7 @@ def test_main(
mock_run_command.assert_any_call("systemctl stop wazuh-server") mock_run_command.assert_any_call("systemctl stop wazuh-server")
expected_indexes = [ mock_run_command.assert_any_call("curl -u admin:admin -XDELETE 'https://127.0.0.1:9200/wazuh-*' -k")
"wazuh-alerts-*",
"wazuh-archives-*",
"wazuh-states-vulnerabilities-*",
"wazuh-statistics-*",
"wazuh-monitoring-*",
]
for index in expected_indexes:
mock_run_command.assert_any_call(f"curl -u admin:admin -XDELETE 'https://127.0.0.1:9200/{index}' -k")
mock_run_command.assert_any_call("bash /usr/share/wazuh-indexer/bin/indexer-security-init.sh -ho 127.0.0.1") mock_run_command.assert_any_call("bash /usr/share/wazuh-indexer/bin/indexer-security-init.sh -ho 127.0.0.1")