From c63e0b6d7c88231e1cd30e3231d5da3f26b0e56d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Correa=20Rodr=C3=ADguez?= Date: Fri, 30 Aug 2024 13:06:18 +0200 Subject: [PATCH 1/3] Added not empty token variable check --- install_functions/manager.sh | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/install_functions/manager.sh b/install_functions/manager.sh index 862f76b..0d83620 100644 --- a/install_functions/manager.sh +++ b/install_functions/manager.sh @@ -45,6 +45,24 @@ function manager_startCluster() { function manager_checkService() { common_logger "Checking Wazuh API connection" eval "TOKEN=$(curl -k -s -X POST -u "wazuh-wui:wazuh-wui" https://127.0.0.1:55000/security/user/authenticate/run_as?raw=true -d '{"user_name":"wzread"}' -H "content-type:application/json")" + + max_attempts=5 + attempt=0 + seconds=3 + + while [[ -z "${TOKEN}" && "${attempt}" -lt "${max_attempts}" ]]; do + attempt=$((attempt+1)) + common_logger "Attempt $attempt: Trying to get Wazuh API token" + sleep "${seconds}" + TOKEN=$(curl -k -s -X POST -u "wazuh-wui:wazuh-wui" https://127.0.0.1:55000/security/user/authenticate/run_as?raw=true -d '{"user_name":"wzread"}' -H "content-type:application/json") + done + + if [[ -z "${TOKEN}" ]]; then + common_logger -e "Failed to obtain Wazuh API token after $max_attempts attempts." + installCommon_rollBack + exit 1 + fi + wm_error=$(curl -k -s -X GET "https://127.0.0.1:55000/agents/outdated?pretty=true" -H "Authorization: Bearer ${TOKEN}") if [[ ${wm_error,,} =~ '"error": 0' ]]; then From 4886372567876f0bb2ceb927cc20a25ef172bb2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Correa=20Rodr=C3=ADguez?= Date: Fri, 30 Aug 2024 13:10:54 +0200 Subject: [PATCH 2/3] Updated CHANGELOG for #45 --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 85e2091..7fe0fd6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ All notable changes to this project will be documented in this file. ### Fixed - Fixed Wazuh API validation ([#29](https://github.com/wazuh/wazuh-installation-assistant/pull/29)) +- Fixed token variable empty in Wazuh manager check ([#45](https://github.com/wazuh/wazuh-installation-assistant/pull/45)) ## [4.9.1] From 5dc4d73be1a9f647a06105e661221f54ca049205 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Correa=20Rodr=C3=ADguez?= Date: Fri, 30 Aug 2024 14:01:29 +0200 Subject: [PATCH 3/3] Stored token curl command in a variable --- install_functions/manager.sh | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/install_functions/manager.sh b/install_functions/manager.sh index 0d83620..edc7bd5 100644 --- a/install_functions/manager.sh +++ b/install_functions/manager.sh @@ -44,7 +44,8 @@ function manager_startCluster() { function manager_checkService() { common_logger "Checking Wazuh API connection" - eval "TOKEN=$(curl -k -s -X POST -u "wazuh-wui:wazuh-wui" https://127.0.0.1:55000/security/user/authenticate/run_as?raw=true -d '{"user_name":"wzread"}' -H "content-type:application/json")" + token_command="curl -k -s -X POST -u \"wazuh-wui:wazuh-wui\" https://127.0.0.1:55000/security/user/authenticate/run_as?raw=true -d '{\"user_name\":\"wzread\"}' -H \"content-type:application/json\"" + TOKEN=$(eval "${token_command}") max_attempts=5 attempt=0 @@ -54,7 +55,7 @@ function manager_checkService() { attempt=$((attempt+1)) common_logger "Attempt $attempt: Trying to get Wazuh API token" sleep "${seconds}" - TOKEN=$(curl -k -s -X POST -u "wazuh-wui:wazuh-wui" https://127.0.0.1:55000/security/user/authenticate/run_as?raw=true -d '{"user_name":"wzread"}' -H "content-type:application/json") + TOKEN=$(eval "${token_command}") done if [[ -z "${TOKEN}" ]]; then