diff --git a/.example.starter.env b/.example.starter.env deleted file mode 100644 index 751a4ce9..00000000 --- a/.example.starter.env +++ /dev/null @@ -1,3 +0,0 @@ -# These values will be used by the script bash/change-passwords.sh -PHPMYADMIN_SUPER_USER_PW= -PHPMYADMIN_CONTROL_PW= \ No newline at end of file diff --git a/bash/.bash_aliases b/bash/.bash_aliases new file mode 100644 index 00000000..6e88b6ee --- /dev/null +++ b/bash/.bash_aliases @@ -0,0 +1,8 @@ +# This file is sourced into ~/.bashrc +# Add any alias you would like here + +# Updates all passwords related to phpmyadmin from values set in .starter.env +# Requires .starter.env to have all phpmyadmin related keys set with values +# Empty string value will break the script +# See .starter.env.example for the required phpmyadmin keys +alias update_pma_pws="bash $GITPOD_REPO_ROOT/bash/change-passwords.sh phpmyadmin" \ No newline at end of file diff --git a/bash/change-passwords.sh b/bash/change-passwords.sh index 1dc77750..c0f8460f 100644 --- a/bash/change-passwords.sh +++ b/bash/change-passwords.sh @@ -8,18 +8,19 @@ # # NOTE: # This script should be always run at least once by the user as an mandatory additional layer of security -# This script requires the file .starter.env to exist along will all the key value pairs as set +# This script requires the file .starter.env to exist along will all the key value pairs as set # in example.starter.ini # Load spinner . bash/third-party/spinner.sh -change_phpmyadmin_passwords() { +phpmyadmin() { # Keep keys in sequence. Add new keys to the end of the array local keys=(PHPMYADMIN_SUPERUSER_PW PHPMYADMIN_CONTROLUSER_PW) - local name="change_phpmyadmin_passwords" + local name="change-passwords.sh phpmyadmin" local err="$name ERROR:" + local config_file="public/phpmyadmin/config.inc.php" local all_zeros='^0$|^0*0$' local exit_codes local values @@ -30,7 +31,7 @@ change_phpmyadmin_passwords() { local code="$?" exit_codes+=$code # show error message of called function - [ $code != 0 ] && echo "$value\n" + [ $code != 0 ] && echo "$value" done if [[ ! $(echo ${exit_codes[@]} | tr -d '[:space:]') =~ $all_zeros ]]; then @@ -45,14 +46,43 @@ change_phpmyadmin_passwords() { "${keys[0]}") msg="Changing password for phpmyadmin user 'pmasu' to the value found in .starter.env" start_spinner "$msg" - mysql -e "ALTER USER 'pmasu'@'localhost' IDENTIFIED BY '${values[$i]}'; FLUSH PRIVILEGES;" + mysql -e "ALTER USER 'pmasu'@'%' IDENTIFIED BY '${values[$i]}'; FLUSH PRIVILEGES;" stop_spinner $? ;; "${keys[1]}") msg="Changing password for phpmyadmin user 'pma' to the value found in .starter.env" start_spinner "$msg" mysql -e "ALTER USER 'pma'@'localhost' IDENTIFIED BY '${values[$i]}'; FLUSH PRIVILEGES;" - stop_spinner $? + err_code=$? + stop_spinner $err_code + if [ $err_code == 0 ]; then + msg="Updating control user password in $config_file" + line="\$cfg['Servers'][\$i]['controlpass'] =" + _edit="\$cfg['Servers'][\$i]['controlpass'] = '${values[$i]}';" + start_spinner "$msg" + # Match the line where the password for the controluser is set + line_num=$(awk '/^\$cfg.*'controlpass'.*=.*;$/ {print FNR}' $config_file) + if [ -z $line_num ]; then + stop_spinner 1 + echo "ERROR: No line found beginning with: $line \n\tin the file: $config_file" + echo "You will need to manually update the control user password in $config_file" + else + sed -i "$line_num c\\$_edit" $config_file + err_code=$? + stop_spinner $err_code + unset _edit + [ $err_code == 0 ] && + echo -e "\e[38;5;171mPROCESS COMPLETE\e[0m" && + echo -e "\e[1;33mCheck the console output for any possible failures.\e[0m" && + echo -en "\e[1;36m" && + echo "If you are logged into phpmyadmin, log out and log back in." && + echo "For additional security you can delete .starter.env" && + echo "Just make sure you remember your passwords from that file." && + echo "If you ever loose your passwords you may always set them again" && + echo "using this script and new values set in .starter.env" && + echo -e "\e[0m" + fi + fi ;; *) echo "$err unidentified key $value" @@ -62,6 +92,14 @@ change_phpmyadmin_passwords() { done } -echo "$(change_phpmyadmin_passwords)" - +# Call functions from this script gracefully +if declare -f "$1" > /dev/null +then + # call arguments verbatim + "$@" +else + echo "utils.sh: '$1' is not a known function name." >&2 + exit 1 +fi +#echo -e "$(change_phpmyadmin_passwords)" diff --git a/bash/helpers.sh b/bash/helpers.sh index 3f70b459..df354727 100644 --- a/bash/helpers.sh +++ b/bash/helpers.sh @@ -13,7 +13,7 @@ # version () { - echo "helpers.sh version 0.0.4" + echo "helpers.sh version 0.0.5" } # start_server @@ -128,7 +128,7 @@ show_first_run_summary() { # # Usage (output will either be the value of the key or an error message): # value="$(get_starter_env_value PHPMYADMIN_CONTROL_PW)" -# echo $value +# echo $value get_starter_env_val() { local err='get_starter_env_val ERROR:' local file='.starter.env' @@ -138,19 +138,19 @@ get_starter_env_val() { '0') echo $value ;; - + '3') echo "$err no file: $file" exit 1 ;; '4') - echo -e "$err no var '$1' in file $file" + echo -e "$err no var '$1' found in file $file" exit 1 ;; '5') - echo "$err no value for var: '$1' in file $file" + echo "$err no value found for '$1' found in file $file" exit 1 ;; @@ -226,11 +226,11 @@ get_installs() { # parses starter.ini for installation information # Echos 1 if any install key in list (see installs varaible in get_install function) in starter.ini # has a value of 1. -# Echos 0 if no keys in the list have a value of 1 +# Echos 0 if no keys in the list have a value of 1 has_installs() { local result=$(echo $(get_installs) | grep -oP '\d' | tr -d '[:space:]') local pattern='.*[1-9].*' - if [[ $result =~ $pattern ]]; then + if [[ $result =~ $pattern ]]; then echo 1 else echo 0 @@ -246,7 +246,7 @@ has_only_phpmyadmin_install() { local result=$(echo $(get_installs) | grep -oP '\d' | tr -d '[:space:]') local all_zeros='^0$|^0*0$' # if the string starts with a 1 phpmyadmin is installed - if [[ $result =~ ^1 ]]; then + if [[ $result =~ ^1 ]]; then # trim the first character from the string local installs="${result:1}" # if the trimmed string is all zeros @@ -271,7 +271,7 @@ has_only_frontend_scaffolding_install() { local result=$(echo $(get_installs) | grep -oP '\d' | tr -d '[:space:]') local all_zeros='^0$|^0*0$' # if the string starts with a 0 phpmyadmin is not installed - if [[ $result =~ ^0 ]]; then + if [[ $result =~ ^0 ]]; then # trim the first character from the string local installs="${result:1}" # Trim the next three characters in the string (there are only three possible front end scaffolding) diff --git a/starter.env.example b/starter.env.example new file mode 100644 index 00000000..c8624f6e --- /dev/null +++ b/starter.env.example @@ -0,0 +1,5 @@ +# These values will be used by the script bash/change-passwords.sh +# All varaiable mus be present in .starter.env +# All varaibles must have a value set +PHPMYADMIN_SUPERUSER_PW= +PHPMYADMIN_CONTROLUSER_PW= \ No newline at end of file