Skip to content

Commit

Permalink
Fix #364
Browse files Browse the repository at this point in the history
Signed-off-by: Adam Hill <[email protected]>
  • Loading branch information
diginc committed Feb 17, 2019
1 parent 669d91d commit 5dfa539
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 10 deletions.
30 changes: 23 additions & 7 deletions bash_functions.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#!/bin/bash
# Some of the bash_functions use variables these core pi-hole/web scripts
. /opt/pihole/webpage.sh

docker_checks() {
warn_msg='WARNING Misconfigured DNS in /etc/resolv.conf'
Expand Down Expand Up @@ -26,8 +28,6 @@ docker_checks() {
}

fix_capabilities() {
[ ! -f /.piholeFirstBoot ] && return

setcap CAP_NET_BIND_SERVICE,CAP_NET_RAW,CAP_NET_ADMIN+ei $(which pihole-FTL) || ret=$?

if [[ $ret -ne 0 && "${DNSMASQ_USER:-root}" != "root" ]]; then
Expand Down Expand Up @@ -69,10 +69,9 @@ prepare_configs() {
# Stash and pop the user password to avoid setting the password to the hashed setupVar variable
WEBPASSWORD="${USERWEBPASSWORD}"
# Clean up old before re-writing the required setupVars
sed -i.update.bak '/PIHOLE_INTERFACE/d;/IPV4_ADDRESS/d;/IPV6_ADDRESS/d;/PIHOLE_DNS_1/d;/PIHOLE_DNS_2/d;/QUERY_LOGGING/d;/INSTALL_WEB_SERVER/d;/INSTALL_WEB_INTERFACE/d;/LIGHTTPD_ENABLED/d;' "${setupVars}"
sed -i.update.bak '/PIHOLE_INTERFACE/d;/IPV4_ADDRESS/d;/IPV6_ADDRESS/d;/QUERY_LOGGING/d;/INSTALL_WEB_SERVER/d;/INSTALL_WEB_INTERFACE/d;/LIGHTTPD_ENABLED/d;' "${setupVars}"
fi
# echo the information to the user
# P
{
echo "PIHOLE_INTERFACE=${PIHOLE_INTERFACE}"
echo "IPV4_ADDRESS=${IPV4_ADDRESS}"
Expand Down Expand Up @@ -117,6 +116,7 @@ setup_dnsmasq_dns() {
dnsType='custom'
fi;

# TODO With the addition of this to /start.sh this needs a refactor
if [ ! -f /.piholeFirstBoot ] ; then
local setupDNS1="$(grep 'PIHOLE_DNS_1' ${setupVars})"
local setupDNS2="$(grep 'PIHOLE_DNS_2' ${setupVars})"
Expand Down Expand Up @@ -299,6 +299,8 @@ generate_password() {
}

setup_web_password() {
setup_var_exists "WEBPASSWORD" && return

PASS="$1"
# Turn bash debug on while setting up password (to print it)
if [[ "$PASS" == "" ]] ; then
Expand Down Expand Up @@ -342,11 +344,11 @@ test_configs() {
setup_blocklists() {
local blocklists="$1"
# Exit/return early without setting up adlists with defaults for any of the following conditions:
# 1. NO_SETUP env is set
# 1. skip_setup_blocklists env is set
exit_string="(exiting ${FUNCNAME[0]} early)"

if [ -n "${NO_SETUP}" ]; then
echo "::: NO_SETUP requested ($exit_string)"
if [ -n "${skip_setup_blocklists}" ]; then
echo "::: skip_setup_blocklists requested ($exit_string)"
return
fi

Expand All @@ -364,3 +366,17 @@ setup_blocklists() {
echo "::: Blocklists (${adlistFile}) now set to:"
cat "${adlistFile}"
}

setup_var_exists() {
local KEY="$1"
if [ -n "$2" ]; then
local REQUIRED_VALUE="[^\n]+"
fi
if grep -Pq "^${KEY}=${REQUIRED_VALUE}" "$setupVars"; then
echo "::: Pre existing ${KEY} found"
true
else
false
fi
}

13 changes: 10 additions & 3 deletions start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,20 @@ export adlistFile='/etc/pihole/adlists.list'
# The below functions are all contained in bash_functions.sh
. /bash_functions.sh

# Some of the bash_functions use variables these core pi-hole/web scripts
. /opt/pihole/webpage.sh
# PH_TEST prevents the install from actually running (someone should rename that)
PH_TEST=true . $PIHOLE_INSTALL

echo " ::: Starting docker specific setup for docker pihole/pihole"
echo " ::: Starting docker specific checks & setup for docker pihole/pihole"

docker_checks

# TODO:
#if [ ! -f /.piholeFirstBoot ] ; then
# echo " ::: Not first container startup so not running docker's setup, re-create container to run setup again"
#else
# regular_setup_functions
#fi

fix_capabilities
generate_password
validate_env || exit 1
Expand Down
11 changes: 11 additions & 0 deletions test/test_bash_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,17 @@ def test_webPassword_env_assigns_password_to_file_or_removes_if_empty(Docker, ar
assert Docker.run('grep -q \'^WEBPASSWORD=$\' /etc/pihole/setupVars.conf').rc == 0


@pytest.mark.parametrize('entrypoint,cmd', [('--entrypoint=tail','-f /dev/null')])
@pytest.mark.parametrize('test_args', ['-e WEBPASSWORD=login', '-e WEBPASSWORD=""'])
def test_webPassword_pre_existing_trumps_all_envs(Docker, args_env, test_args):
'''When a user setup webPassword in the volume prior to first container boot,
during prior container boot, the prior volume password is left intact / setup skipped'''
Docker.run('. /opt/pihole/webpage.sh ; add_setting WEBPASSWORD volumepass')
function = Docker.run('. /bash_functions.sh ; eval `grep setup_web_password /start.sh`')

assert '::: Pre existing WEBPASSWORD found' in function.stdout
assert Docker.run('grep -q \'{}\' {}'.format('WEBPASSWORD=volumepass', '/etc/pihole/setupVars.conf')).rc == 0


@pytest.mark.parametrize('args_dns, expected_stdout', [
# No DNS passed will vary by the host this is ran on, bad idea for a test
Expand Down

0 comments on commit 5dfa539

Please sign in to comment.