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

Installation assistant Wazuh API default password change #1548

Merged
merged 45 commits into from
Jun 8, 2022
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
fd27e47
Added function to generate new passwords for the API
miguelfdez99 May 13, 2022
b6866c9
Added function to change API passswords
miguelfdez99 May 13, 2022
171d256
Added function to update wazuh-wui API passsword
miguelfdez99 May 13, 2022
9f4155d
Update API status function to get the password from the passwords file
miguelfdez99 May 13, 2022
2822e6b
Change version to test it in jenkins
miguelfdez99 May 13, 2022
362e7f7
Change version to test it in jenkins
miguelfdez99 May 13, 2022
f769633
Fix typo
miguelfdez99 May 13, 2022
06b1918
Change version
miguelfdez99 May 16, 2022
888ecfe
Change regular expression characters
miguelfdez99 May 16, 2022
59e05f3
Added file check
miguelfdez99 May 16, 2022
424c75c
Change version
miguelfdez99 May 16, 2022
a8b3617
Delete newline
miguelfdez99 May 16, 2022
3ede3ff
Delete newline
miguelfdez99 May 16, 2022
342ed72
Added functions from installCommon to passwords
miguelfdez99 May 20, 2022
c563e90
Added API option to password tool
miguelfdez99 May 23, 2022
6297b26
Update API password function
miguelfdez99 May 24, 2022
c77d1d9
Added new options to password tool
miguelfdez99 May 24, 2022
c4362c2
Change pattern for API password generation
miguelfdez99 May 24, 2022
bb0d889
Change shift call twice to shift 2
miguelfdez99 May 25, 2022
9e22306
Change version to test it in jenkins
miguelfdez99 May 25, 2022
6fe1ccb
Update version
miguelfdez99 May 25, 2022
409e15a
Added storing API passwords in different file
miguelfdez99 May 27, 2022
d3e7a30
Change strings quoting
miguelfdez99 May 27, 2022
b07aba8
Change dashboard revision version
miguelfdez99 May 30, 2022
f59e46c
Merge branch '4.3' of https://github.com/wazuh/wazuh-packages into 15…
DFolchA Jun 2, 2022
39d59da
Remove pasword api file and fix typos
DFolchA Jun 2, 2022
ae865dc
Set version to 4.3.3
DFolchA Jun 3, 2022
2e046bc
Use correct file variable
DFolchA Jun 3, 2022
f04bb3f
Update version in test
DFolchA Jun 3, 2022
a501b5e
Use old generate password function
DFolchA Jun 6, 2022
83e3500
Modify passwords_generatePassword to generate correct password format
DFolchA Jun 6, 2022
86a2a61
Merge branch '4.3' of https://github.com/wazuh/wazuh-packages into 15…
DFolchA Jun 6, 2022
5a99ea0
Correct password string possible values
DFolchA Jun 6, 2022
eecad12
Add " to variable
DFolchA Jun 6, 2022
c910060
Add ' to installCommon_readPasswordFileUsers
DFolchA Jun 7, 2022
1ccfd05
Remove ' from password generation
DFolchA Jun 7, 2022
91d0ba5
Set pasword file strings between ''
DFolchA Jun 7, 2022
bcd7dc3
Add print to test
DFolchA Jun 7, 2022
9bf4c86
Get api password correctly from file
DFolchA Jun 7, 2022
57ec492
Remove ' and " when getting sfileusers and sfilepasswords
DFolchA Jun 8, 2022
ca5a7b5
Remove echo
DFolchA Jun 8, 2022
c8c49c9
Removed blank spaces
alberpilot Jun 8, 2022
b4c8c78
Remove unnecessary function
DFolchA Jun 8, 2022
ac09720
Merge branch '1523-unattended-change-password-api' of https://github.…
DFolchA Jun 8, 2022
d1488db
Merge branch '4.3' of https://github.com/wazuh/wazuh-packages into 15…
DFolchA Jun 8, 2022
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
2 changes: 1 addition & 1 deletion tests/unattended/install/test_unattended.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def get_wazuh_api_status():
host = get_indexer_ip()
port = 55000
user = 'wazuh'
password = 'wazuh'
password = get_password("wazuh")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be used the variable user a function parameter?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done 39d59da

login_endpoint = 'security/user/authenticate'

login_url = f"{protocol}://{host}:{port}/{login_endpoint}"
Expand Down
2 changes: 1 addition & 1 deletion tests/unattended/unit/suites/test-dashboard.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ source "${base_dir}"/bach.sh
@setup-test {
@ignore common_logger
k_certs_path="/etc/wazuh-dashboard/certs/"
wazuh_version="4.3.1"
wazuh_version="4.3.0"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update version

elasticsearch_oss_version="7.10.2"
wazuh_kibana_plugin_revision="1"
repobaseurl="https://packages.wazuh.com/4.x"
Expand Down
4 changes: 2 additions & 2 deletions unattended_installer/install_functions/installCommon.sh
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ function installCommon_installPrerequisites() {

function installCommon_readPasswordFileUsers() {

filecorrect=$(grep -Ev '^#|^\s*$' "${p_file}" | grep -Pzc '\A(\s*username:[ \t]+\w+\s*password:[ \t]+[A-Za-z0-9_\-]+\s*)+\Z')
filecorrect=$(grep -Ev '^#|^\s*$' "${p_file}" | grep -Pzc '\A(\s*username:[ \t]+\w+\s*password:[ \t]+[A-Za-z0-9.*+?]+\s*)+\Z')
if [[ "${filecorrect}" -ne 1 ]]; then
common_logger -e "The password file doesn't have a correct format.

Expand Down Expand Up @@ -527,4 +527,4 @@ function installCommon_startService() {
exit 1
fi

}
}
5 changes: 5 additions & 0 deletions unattended_installer/install_functions/installMain.sh
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@ function main() {
installCommon_changePasswords
installCommon_startService "wazuh-dashboard"
dashboard_initialize
passwords_updateDashborad_WUI_Password

fi

Expand All @@ -306,6 +307,7 @@ function main() {
filebeat_install
filebeat_configure
installCommon_changePasswords
passwords_changePasswordAPI
installCommon_startService "filebeat"
fi

Expand All @@ -329,7 +331,10 @@ function main() {
dashboard_configure
installCommon_startService "wazuh-dashboard"
installCommon_changePasswords
passwords_changePasswordAPI
dashboard_initializeAIO
passwords_updateDashborad_WUI_Password

fi

# -------------- Offline case ------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion unattended_installer/install_functions/installVariables.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

## Package vars
readonly wazuh_major="4.3"
readonly wazuh_version="4.3.1"
readonly wazuh_version="4.3.0"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update version

readonly wazuh_revision_deb="1"
readonly wazuh_revision_rpm="1"
readonly indexer_revision_deb="1"
Expand Down
82 changes: 81 additions & 1 deletion unattended_installer/passwords_tool/passwordsFunctions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ function passwords_generatePasswordFile() {
echo " password: ${passwords[${i}]}" >> "${gen_file}"
echo "" >> "${gen_file}"
done
passwords_createPasswordAPI
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 [shellcheck] reported by reviewdog 🐶
Use passwords_createPasswordAPI "$@" if function's $1 should mean script's $1. SC2119


}

Expand Down Expand Up @@ -217,7 +218,7 @@ function passwords_readAdmincerts() {
}

function passwords_readFileUsers() {
filecorrect=$(grep -Ev '^#|^\s*$' "${p_file}" | grep -Pzc '\A(\s*username:[ \t]+\w+\s*password:[ \t]+[A-Za-z0-9_\-]+\s*)+\Z')
filecorrect=$(grep -Ev '^#|^\s*$' "${p_file}" | grep -Pzc '\A(\s*username:[ \t]+\w+\s*password:[ \t]+[A-Za-z0-9.*+?]+\s*)+\Z')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ [shellcheck] reported by reviewdog 🐶
p_file is referenced but not assigned. SC2154

if [[ "${filecorrect}" -ne 1 ]]; then
common_logger -e "The password file doesn't have a correct format.

Expand Down Expand Up @@ -380,3 +381,82 @@ function passwords_runSecurityAdmin() {
fi

}

function passwords_genereatePasswordSpecialChar() {

choose() { echo ${1:RANDOM%${#1}:1} $RANDOM; }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 [shellcheck] reported by reviewdog 🐶
Double quote to prevent globbing and word splitting. SC2086

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[shellcheck (suggestion)] reported by reviewdog 🐶

Suggested change
choose() { echo ${1:RANDOM%${#1}:1} $RANDOM; }
choose() { echo "${1:RANDOM%${#1}:1}" $RANDOM; }

pass="$({ choose '.*+?'
choose '0123456789'
choose 'abcdefghijklmnopqrstuvwxyz'
choose 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
for i in $( seq 1 $(( 20 + RANDOM % 8 )) )
do
choose '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
done
} | sort -R | awk '{printf "%s",$1}')"

}

function passwords_createPasswordAPI() {

passwords_genereatePasswordSpecialChar
password_wazuh="${pass}"
passwords_genereatePasswordSpecialChar
password_wazuh_wui="${pass}"

echo "# New password for wazuh API" >> "${gen_file}"
echo " username: wazuh" >> "${gen_file}"
echo " password: $password_wazuh" >> "${gen_file}"
echo "" >> "${gen_file}"
echo "# New password for wazuh-wui API" >> "${gen_file}"
echo " username: wazuh_wui" >> "${gen_file}"
echo " password: $password_wazuh_wui" >> "${gen_file}"
echo "" >> "${gen_file}"

}

function passwords_changePasswordAPI() {
alberpilot marked this conversation as resolved.
Show resolved Hide resolved

#Change API password tool

if [[ -n "${api}" ]]; then
if [[ -n "${adminAPI}" ]]; then
common_logger -nl $'Changing API user '${nuser}' password'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 [shellcheck] reported by reviewdog 🐶
Double quote to prevent globbing and word splitting. SC2086

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[shellcheck (suggestion)] reported by reviewdog 🐶

Suggested change
common_logger -nl $'Changing API user '${nuser}' password'
common_logger -nl $'Changing API user '"${nuser}"' password'

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use the same notation as the rest of the script:

"string ${var} "

WAZUH_PASS_API='{"password":"'"$password"'"}'
TOKEN_API=$(curl -s -u "${adminUser}":"${adminPassword}" -k -X GET "https://localhost:55000/security/user/authenticate?raw=true")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ [shellcheck] reported by reviewdog 🐶
adminUser is referenced but not assigned. SC2154

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ [shellcheck] reported by reviewdog 🐶
adminPassword is referenced but not assigned. SC2154

eval 'curl -s -k -X PUT -H "Authorization: Bearer $TOKEN_API" -H "Content-Type: application/json" -d "$WAZUH_PASS_API" "https://localhost:55000/security/users/${id}" -o /dev/null'
common_logger -nl $'API password changed'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same

common_logger -nl $'The new password for user '${nuser}' is '${password}''
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 [shellcheck] reported by reviewdog 🐶
Double quote to prevent globbing and word splitting. SC2086

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 [shellcheck] reported by reviewdog 🐶
Double quote to prevent globbing and word splitting. SC2086

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[shellcheck (suggestion)] reported by reviewdog 🐶

Suggested change
common_logger -nl $'The new password for user '${nuser}' is '${password}''
common_logger -nl $'The new password for user '"${nuser}"' is '"${password}"''

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same

else
common_logger -nl $'Changing API user '${nuser}' password'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 [shellcheck] reported by reviewdog 🐶
Double quote to prevent globbing and word splitting. SC2086

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[shellcheck (suggestion)] reported by reviewdog 🐶

Suggested change
common_logger -nl $'Changing API user '${nuser}' password'
common_logger -nl $'Changing API user '"${nuser}"' password'

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same

WAZUH_PASS_API='{"password":"'"$password"'"}'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ [shellcheck] reported by reviewdog 🐶
WAZUH_PASS_API appears unused. Verify use (or export if used externally). SC2034

TOKEN_API=$(curl -s -u "${nuser}":"${currentPassword}" -k -X GET "https://localhost:55000/security/user/authenticate?raw=true")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ [shellcheck] reported by reviewdog 🐶
TOKEN_API appears unused. Verify use (or export if used externally). SC2034

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ [shellcheck] reported by reviewdog 🐶
currentPassword is referenced but not assigned. SC2154

eval 'curl -s -k -X PUT -H "Authorization: Bearer $TOKEN_API" -H "Content-Type: application/json" -d "$WAZUH_PASS_API" "https://localhost:55000/security/users/${id}" -o /dev/null'
common_logger -nl $'API password changed'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same

common_logger -nl $'The new password for user '${nuser}' is '${password}''
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 [shellcheck] reported by reviewdog 🐶
Double quote to prevent globbing and word splitting. SC2086

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 [shellcheck] reported by reviewdog 🐶
Double quote to prevent globbing and word splitting. SC2086

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[shellcheck (suggestion)] reported by reviewdog 🐶

Suggested change
common_logger -nl $'The new password for user '${nuser}' is '${password}''
common_logger -nl $'The new password for user '"${nuser}"' is '"${password}"''

fi
else
password_wazuh=$(< /tmp/wazuh-install-files/passwords.wazuh awk '$2 == "wazuh" {getline;print;}' | awk -F': ' '{print $2}')
password_wazuh_wui=$(< /tmp/wazuh-install-files/passwords.wazuh awk '$2 == "wazuh_wui" {getline;print;}' | awk -F': ' '{print $2}')
WAZUH_PASS='{"password":"'"$password_wazuh"'"}'
WAZUH_WUI_PASS='{"password":"'"$password_wazuh_wui"'"}'

TOKEN=$(curl -s -u wazuh:wazuh -k -X GET "https://localhost:55000/security/user/authenticate?raw=true")
eval 'curl -s -k -X PUT -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" -d "$WAZUH_PASS" "https://localhost:55000/security/users/1" -o /dev/null'

TOKEN_WUI=$(curl -s -u wazuh-wui:wazuh-wui -k -X GET "https://localhost:55000/security/user/authenticate?raw=true")
eval 'curl -s -k -X PUT -H "Authorization: Bearer $TOKEN_WUI" -H "Content-Type: application/json" -d "$WAZUH_WUI_PASS" "https://localhost:55000/security/users/2" -o /dev/null'
fi

}

function passwords_updateDashborad_WUI_Password() {

if [ -f "/usr/share/wazuh-dashboard/data/wazuh/config/wazuh.yml" ]; then
password_wazuh_wui=$(< /tmp/wazuh-install-files/passwords.wazuh awk '$2 == "wazuh_wui" {getline;print;}' | awk -F': ' '{print $2}')
eval 'sed -i "s|password: wazuh-wui|password: ${password_wazuh_wui}|g" /usr/share/wazuh-dashboard/data/wazuh/config/wazuh.yml'
else
echo "ERROR: File /usr/share/wazuh-dashboard/data/wazuh/config/wazuh.yml does not exist"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use logger

fi

}
71 changes: 69 additions & 2 deletions unattended_installer/passwords_tool/passwordsMain.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,19 @@ function getHelp() {
echo -e " -a, --change-all"
echo -e " Changes all the Wazuh indexer user passwords and prints them on screen."
echo -e ""
echo -e " -ai, --api <currentPassword>"
echo -e " Change the Wazuh API password given the current password, it needs --id-api ,--user and --password."
echo -e " If not an administrator --admin-user and --admin-password need to be provided."
echo -e ""
echo -e " -au, --admin-user <adminUser>"
echo -e " Admin user for Wazuh API it is needed when the user given it is not an administrator"
echo -e ""
echo -e " -ap, --admin-password <adminPassword>"
echo -e " Password for Wazuh API admin user, it is needed when the user given it is not an administrator"
echo -e ""
echo -e " -id, --id-api <id>"
echo -e " ID for Wazuh API user to be changed"
echo -e ""
echo -e " -u, --user <user>"
echo -e " Indicates the name of the user whose password will be changed."
echo -e " If no password specified it will generate a random one."
Expand Down Expand Up @@ -71,6 +84,48 @@ function main() {
changeall=1
shift 1
;;
"-A"|"--api")
api=1
if [ -z ${2} ]; then
echo "Argument --api-id needs a second argument"
getHelp
exit 1
fi
currentPassword=${2}
shift
shift
;;
"-au"|"--admin-user")
adminAPI=1
if [ -z ${2} ]; then
echo "Argument --admin needs a second argument"
getHelp
exit 1
fi
adminUser=${2}
shift
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use shift 2 instead of shift twice

shift
;;
"-ap"|"--admin-password")
if [ -z ${2} ]; then
echo "Argument --admin needs a second argument"
getHelp
exit 1
fi
adminPassword=${2}
shift
shift
;;
"-id"|"--id-api")
if [ -z ${2} ]; then
echo "Argument --id-api needs a second argument"
getHelp
exit 1
fi
id=${2}
shift
shift
;;
"-u"|"--user")
if [ -z ${2} ]; then
echo "Argument --user needs a second argument"
Expand Down Expand Up @@ -155,6 +210,8 @@ function main() {
common_checkSystem
common_checkInstalled

if [ -z "${api}" ]; then

if [ -n "${p_file}" ] && [ ! -f "${p_file}" ]; then
getHelp
fi
Expand Down Expand Up @@ -212,10 +269,20 @@ function main() {
passwords_changePassword
passwords_runSecurityAdmin

else
else
if [ -z "${currentPassword}" ] || [ -z "${id}" ] || [ -z "${nuser}" ] || [ -z "${password}" ]; then
getHelp
fi

if [ -n "${adminAPI}" ]; then
if [ -z "${currentPassword}" ] || [ -z "${id}" ] || [ -z "${nuser}" ] || [ -z "${password}" ] || [ -z "${adminUser}" ] || [ -z "${adminPassword}" ]; then
getHelp
fi
fi
passwords_changePasswordAPI
fi
else
getHelp

fi

}