diff --git a/bash_functions.sh b/bash_functions.sh index d6b1115..09dd7a7 100644 --- a/bash_functions.sh +++ b/bash_functions.sh @@ -270,13 +270,7 @@ load_web_password_secret() { fi; } -generate_password() { - if [ -z "${WEBPASSWORD+x}" ] ; then - # Not set at all, give the user a random pass - WEBPASSWORD=$(tr -dc _A-Z-a-z-0-9 < /dev/urandom | head -c 8) - echo "Assigning random password: $WEBPASSWORD" - fi; -} + setup_web_password() { if [ -z "${WEBPASSWORD+x}" ] ; then @@ -285,8 +279,9 @@ setup_web_password() { # Exit if setupvars already has a password setup_var_exists "WEBPASSWORD" && return - # Generate new password - generate_password + # Generate new random password + WEBPASSWORD=$(tr -dc _A-Z-a-z-0-9 < /dev/urandom | head -c 8) + echo "Assigning random password: $WEBPASSWORD" else # ENV WEBPASSWORD is set an will be used echo "::: Assigning password defined by Environment Variable" diff --git a/test/test_bash_functions.py b/test/test_bash_functions.py index 6929c2b..02d0bd8 100644 --- a/test/test_bash_functions.py +++ b/test/test_bash_functions.py @@ -180,7 +180,7 @@ def test_debian_setup_php_env(Docker, expected_lines, repeat_function): def test_webPassword_random_generation(Docker): ''' When a user sets webPassword env the admin password gets set to that ''' - function = Docker.run('. /bash_functions.sh ; eval `grep generate_password /start.sh`') + function = Docker.run('. /bash_functions.sh ; eval `grep setup_web_password /start.sh`') assert 'assigning random password' in function.stdout.lower() @@ -203,11 +203,18 @@ def test_webPassword_env_assigns_password_to_file_or_removes_if_empty(Docker, ar @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') +def test_env_always_updates_password(Docker, args_env, test_args): + '''When a user sets the WEBPASSWORD environment variable, ensure it always sets the password''' function = Docker.run('. /bash_functions.sh ; eval `grep setup_web_password /start.sh`') - assert '::: Pre existing WEBPASSWORD found' in function.stdout + assert '::: Assigning password defined by Environment Variable' in function.stdout + + +@pytest.mark.parametrize('entrypoint,cmd', [('--entrypoint=tail','-f /dev/null')]) +def test_setupvars_trumps_random_password_if_set(Docker, args_env, test_args): + '''If a password is already set in setupVars, and no password is set in the environment variable, do not generate a random password''' + Docker.run('. /opt/pihole/utils.sh ; addOrEditKeyValPair /etc/pihole/setupVars.conf 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