mirror of
https://github.com/wazuh/wazuh-virtual-machines.git
synced 2025-12-10 00:07:25 -06:00
updated indexes to delete and adapted pytests
This commit is contained in:
parent
207d66fbfc
commit
e1aeeec3e9
@ -183,35 +183,22 @@ class AmiPostConfigurer:
|
||||
|
||||
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] = [
|
||||
"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}-*"'
|
||||
)
|
||||
base_url = "https://127.0.0.1:9200"
|
||||
|
||||
command = " && sudo ".join(commands)
|
||||
command = f"sudo {command}"
|
||||
command = f'sudo curl -s -o /dev/null -w "%{{http_code}}" -X DELETE -u "admin:admin" -k "{base_url}/wazuh-*"'
|
||||
_, error_output = exec_command(command=command, client=client)
|
||||
if error_output:
|
||||
logger.error("Error removing the indexer index list")
|
||||
raise RuntimeError(f"Error removing the indexer index list: {error_output}")
|
||||
logger.error("Error removing wazuh- indexes")
|
||||
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:
|
||||
"""
|
||||
@ -245,7 +232,7 @@ class AmiPostConfigurer:
|
||||
None
|
||||
"""
|
||||
|
||||
self.remove_indexer_index_list(client=client)
|
||||
self.remove_wazuh_indexes(client=client)
|
||||
self.run_security_init_script(client=client)
|
||||
self.stop_service("wazuh-indexer", client=client)
|
||||
|
||||
|
||||
@ -343,15 +343,7 @@ def main() -> None:
|
||||
steps_system_config()
|
||||
|
||||
run_command("systemctl stop wazuh-server")
|
||||
indexes = [
|
||||
"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("curl -u admin:admin -XDELETE 'https://127.0.0.1:9200/wazuh-*' -k")
|
||||
|
||||
run_command("bash /usr/share/wazuh-indexer/bin/indexer-security-init.sh -ho 127.0.0.1")
|
||||
|
||||
|
||||
@ -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):
|
||||
mock_ami_post_configurer.stop_wazuh_indexer(mock_paramiko.return_value)
|
||||
|
||||
commands = [
|
||||
"""
|
||||
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-*"
|
||||
""",
|
||||
"""
|
||||
sudo /usr/share/wazuh-indexer/bin/indexer-security-init.sh
|
||||
""",
|
||||
"""
|
||||
sudo systemctl stop wazuh-indexer
|
||||
""",
|
||||
]
|
||||
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 /usr/share/wazuh-indexer/bin/indexer-security-init.sh",
|
||||
"sudo systemctl stop wazuh-indexer",
|
||||
}
|
||||
|
||||
for command_call in mock_exec_command.call_args_list:
|
||||
command_call.kwargs["command"] = command_call.kwargs["command"].replace("\n", "").replace(" ", "")
|
||||
called_commands = {c.kwargs["command"] for c in mock_exec_command.call_args_list}
|
||||
for cmd in expected_commands:
|
||||
assert cmd in called_commands
|
||||
|
||||
for command in commands:
|
||||
command = command.replace("\n", "").replace(" ", "")
|
||||
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("Removing all wazuh- indexes")
|
||||
mock_logger.debug.assert_any_call("wazuh- indexes removed successfully")
|
||||
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("Stopping wazuh-indexer service")
|
||||
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):
|
||||
mock_ami_post_configurer.remove_indexer_index_list(mock_paramiko.return_value)
|
||||
def test_remove_wazuh_indexes(mock_ami_post_configurer, mock_exec_command, mock_paramiko, mock_logger):
|
||||
mock_ami_post_configurer.remove_wazuh_indexes(mock_paramiko.return_value)
|
||||
|
||||
command = """
|
||||
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(" ", "")
|
||||
command = 'sudo curl -s -o /dev/null -w "%{http_code}" -X DELETE -u "admin:admin" -k "https://127.0.0.1:9200/wazuh-*"'
|
||||
|
||||
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("Indexer index list removed successfully")
|
||||
mock_logger.debug.assert_any_call("Removing all wazuh- indexes")
|
||||
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")
|
||||
|
||||
with pytest.raises(Exception, match="Error removing the indexer index list: Command failed"):
|
||||
mock_ami_post_configurer.remove_indexer_index_list(mock_paramiko.return_value)
|
||||
|
||||
mock_logger.error.assert_called_once_with("Error removing the indexer index list")
|
||||
with pytest.raises(Exception, match="Error removing wazuh- indexes: Command failed"):
|
||||
mock_ami_post_configurer.remove_wazuh_indexes(mock_paramiko.return_value)
|
||||
|
||||
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):
|
||||
mock_ami_post_configurer.run_security_init_script(mock_paramiko.return_value)
|
||||
|
||||
@ -330,15 +330,7 @@ def test_main(
|
||||
|
||||
mock_run_command.assert_any_call("systemctl stop wazuh-server")
|
||||
|
||||
expected_indexes = [
|
||||
"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("curl -u admin:admin -XDELETE 'https://127.0.0.1:9200/wazuh-*' -k")
|
||||
|
||||
mock_run_command.assert_any_call("bash /usr/share/wazuh-indexer/bin/indexer-security-init.sh -ho 127.0.0.1")
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user