Skip to content

Commit

Permalink
Configure MD5 login for postgres users. Fix sed replacing Apache port
Browse files Browse the repository at this point in the history
  • Loading branch information
ccurtin committed Oct 3, 2016
1 parent 1b6e0db commit 0af664e
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 58 deletions.
112 changes: 60 additions & 52 deletions bootstrap/manage_django_db_postgres.sh
Original file line number Diff line number Diff line change
Expand Up @@ -73,55 +73,63 @@ function assign_user_to_db(){
fi
}

function configure_md5_login(){
echo -e ${BYELLOW}Update password for user "postgres"${NIL}
# Create a new password for user "postgres"
sudo -u postgres psql -tAc "\password postgres"
sudo sed -i "s/\s*local\s*all\s*all\s*peer/local all all md5/" /etc/postgresql/9.3/main/pg_hba.conf
sudo service postgresql restart
}

function update_app_settings(){

change_your_dir
continue_update_app_settings ${1}
configure_md5_login
}

### USER INPUT METHODS ###
function update_data()
{
function update_data(){

case ${1} in
db_alias)
if [ -z ${2+x} ]; then
DB_ALIAS='default'
else
DB_ALIAS=${2}
fi
echo -e ${BGREEN}using ALIAS: $DB_ALIAS ${NIL}
;;
engine)
sed -i "/^DATABASES/ {:loop n; /'$DB_ALIAS'/{:moop n; /'ENGINE':/{s/\s\+'ENGINE':.*/'ENGINE': 'django.db.backends.postgresql',/g}; t loop; /}/{s/\s\+}.*/'ENGINE': 'django.db.backends.postgresql',\\n },/}; t loop; b moop} ;b loop}" settings.py
autopep8 --in-place --aggressive --aggressive settings.py
;;
name)
DB_NAME=${2}
sed -i "/^DATABASES/ {:loop n; /'$DB_ALIAS'/{:moop n; /'NAME':/{s/\s\+'NAME':.*/'NAME': '${2}',/g}; t loop; /}/{s/\s\+}.*/'NAME': '${2}',\\n },/}; t loop; b moop} ;b loop}" settings.py
autopep8 --in-place --aggressive --aggressive settings.py
;;
user)
DB_USER=${2}
sed -i "/^DATABASES/ {:loop n; /'$DB_ALIAS'/{:moop n; /'USER':/{s/\s\+'USER':.*/'USER': '${2}',/g}; t loop; /}/{s/\s\+}.*/'USER': '${2}',\\n },/}; t loop; b moop} ;b loop}" settings.py
autopep8 --in-place --aggressive --aggressive settings.py
;;
password)
sed -i "/^DATABASES/ {:loop n; /'$DB_ALIAS'/{:moop n; /'PASSWORD':/{s/\s\+'PASSWORD':.*/'PASSWORD': '${2}',/g}; t loop; /}/{s/\s\+}.*/'PASSWORD': '${2}',\\n },/}; t loop; b moop} ;b loop}" settings.py
autopep8 --in-place --aggressive --aggressive settings.py
;;
host)
DB_HOST=${2}
sed -i "/^DATABASES/ {:loop n; /'$DB_ALIAS}'/{:moop n; /'HOST':/{s/\s\+'HOST':.*/'HOST': '${2}',/g}; t loop; /}/{s/\s\+}.*/'HOST': '${2}',\\n },/}; t loop; b moop} ;b loop}" settings.py
autopep8 --in-place --aggressive --aggressive settings.py
;;
port)
DB_PORT=${2}
sed -i "/^DATABASES/ {:loop n; /'$DB_ALIAS'/{:moop n; /'PORT':/{s/\s\+'PORT':.*/'PORT': '${2}',/g}; t loop; /}/{s/\s\+}.*/'PORT': '${2}',\\n },/}; t loop; b moop} ;b loop}" settings.py
autopep8 --in-place --aggressive --aggressive settings.py
;;
esac
if [ -z ${2+x} ]; then
DB_ALIAS='default'
else
DB_ALIAS=${2}
fi
echo -e ${BGREEN}using ALIAS: $DB_ALIAS ${NIL}
;;
engine)
sed -i "/^DATABASES/ {:loop n; /'$DB_ALIAS'/{:moop n; /'ENGINE':/{s/\s\+'ENGINE':.*/'ENGINE': 'django.db.backends.postgresql',/g}; t loop; /}/{s/\s\+}.*/'ENGINE': 'django.db.backends.postgresql',\\n },/}; t loop; b moop} ;b loop}" settings.py
autopep8 --in-place --aggressive --aggressive settings.py
;;
name)
DB_NAME=${2}
sed -i "/^DATABASES/ {:loop n; /'$DB_ALIAS'/{:moop n; /'NAME':/{s/\s\+'NAME':.*/'NAME': '${2}',/g}; t loop; /}/{s/\s\+}.*/'NAME': '${2}',\\n },/}; t loop; b moop} ;b loop}" settings.py
autopep8 --in-place --aggressive --aggressive settings.py
;;
user)
DB_USER=${2}
sed -i "/^DATABASES/ {:loop n; /'$DB_ALIAS'/{:moop n; /'USER':/{s/\s\+'USER':.*/'USER': '${2}',/g}; t loop; /}/{s/\s\+}.*/'USER': '${2}',\\n },/}; t loop; b moop} ;b loop}" settings.py
autopep8 --in-place --aggressive --aggressive settings.py
;;
password)
sed -i "/^DATABASES/ {:loop n; /'$DB_ALIAS'/{:moop n; /'PASSWORD':/{s/\s\+'PASSWORD':.*/'PASSWORD': '${2}',/g}; t loop; /}/{s/\s\+}.*/'PASSWORD': '${2}',\\n },/}; t loop; b moop} ;b loop}" settings.py
autopep8 --in-place --aggressive --aggressive settings.py
;;
host)
DB_HOST=${2}
sed -i "/^DATABASES/ {:loop n; /'$DB_ALIAS'/{:moop n; /'HOST':/{s/\s\+'HOST':.*/'HOST': '${2}',/g}; t loop; /}/{s/\s\+}.*/'HOST': '${2}',\\n },/}; t loop; b moop} ;b loop}" settings.py
autopep8 --in-place --aggressive --aggressive settings.py
;;
port)
DB_PORT=${2}
sed -i "/^DATABASES/ {:loop n; /'$DB_ALIAS'/{:moop n; /'PORT':/{s/\s\+'PORT':.*/'PORT': '${2}',/g}; t loop; /}/{s/\s\+}.*/'PORT': '${2}',\\n },/}; t loop; b moop} ;b loop}" settings.py
autopep8 --in-place --aggressive --aggressive settings.py
;;
esac


}

Expand Down Expand Up @@ -169,20 +177,20 @@ function continue_update_app_settings(){
}
### DATABASE PASSWORD
function enter_db_pass(){
# read -p "Auto-import password? [y/n] " prompt
# if [[ ${prompt,,} =~ ^(yes|y)$ ]]; then
# update_data password $NEW_DB_PASS
# else
echo "Enter the PASSWORD for $DB_USER: "
read -s DB_PASS
if [ -z ${DB_PASS} ]; then
echo -e ${RED}Invalid Input${NIL}
enter_db_pass
else
update_data password $DB_PASS
fi
# fi
}
# read -p "Auto-import password? [y/n] " prompt
# if [[ ${prompt,,} =~ ^(yes|y)$ ]]; then
# update_data password $NEW_DB_PASS
# else
echo "Enter the PASSWORD for $DB_USER: "
read -s DB_PASS
if [ -z ${DB_PASS} ]; then
echo -e ${RED}Invalid Input${NIL}
enter_db_pass
else
update_data password $DB_PASS
fi
# fi
}
### DATABASE HOST
function enter_db_host(){
read -e -i 'localhost' -p "HOST for $DB_NAME: " DB_HOST
Expand Down
7 changes: 1 addition & 6 deletions bootstrap/setup_phppgadmin.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,10 @@ sed -i 's/^allow from 127.0.0.0\/255.0.0.0 ::1\/128/# allow from 127.0.0.0\/255.
sed -i 's/^#allow from all/allow from all/' /etc/apache2/conf.d/phppgadmin
sed -i 's/^# allow from all/allow from all/' /etc/apache2/conf.d/phppgadmin
sudo service apache2 reload
echo -e ${BYELLOW}Update password for user "postgres"${NIL}
# Create a new password for user "postgres"
sudo -u postgres psql -tAc "\password postgres"
# enable user "postgres" to login
sudo sed -i "s/\s*\$conf\['extra_login_security'\] = true;/ \$conf\['extra_login_security'\] = false;/" /etc/phppgadmin/config.inc.php
sudo sed -i "s/\s*local\s*all\s*all\s*peer/local all all md5/" /etc/postgresql/9.3/main/pg_hba.conf
sudo service postgresql restart
# Update port 80 to port 8080
sudo sed -i "s/Listen 80/Listen 8080/" /etc/apache2/ports.conf
sudo sed -i "s/Listen 80\s+/Listen 8080/" /etc/apache2/ports.conf
sudo sed -i "s/:80>/:8080>/" /etc/apache2/sites-available/000-default.conf
sudo /etc/init.d/apache2 restart

Expand Down

0 comments on commit 0af664e

Please sign in to comment.