Skip to content

Commit

Permalink
🐛 FIX: refactor #64, works good
Browse files Browse the repository at this point in the history
  • Loading branch information
apolopena committed Mar 20, 2021
1 parent d2e9bba commit a2c223e
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 20 deletions.
3 changes: 0 additions & 3 deletions .example.starter.env

This file was deleted.

8 changes: 8 additions & 0 deletions bash/.bash_aliases
Original file line number Diff line number Diff line change
@@ -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"
54 changes: 46 additions & 8 deletions bash/change-passwords.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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"
Expand All @@ -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)"
18 changes: 9 additions & 9 deletions bash/helpers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#

version () {
echo "helpers.sh version 0.0.4"
echo "helpers.sh version 0.0.5"
}

# start_server
Expand Down Expand Up @@ -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'
Expand All @@ -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
;;

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions starter.env.example
Original file line number Diff line number Diff line change
@@ -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=

0 comments on commit a2c223e

Please sign in to comment.