|
| 1 | +#!/usr/bin/env bats |
| 2 | + |
| 3 | +# The tests in this BATS module must be run as a (passwordless) sudo-enabled user. |
| 4 | +# It is also required that the python irodsclient be installed under irods' ~/.local environment. |
| 5 | + |
| 6 | +SKIP_IINIT_FOR_PASSWORD=yes |
| 7 | + |
| 8 | +. $BATS_TEST_DIRNAME/test_support_functions |
| 9 | + |
| 10 | +export TESTUSER="john" |
| 11 | +export FIRST_PASSWORD="=i;r@o\\d&s" # somerods |
| 12 | +export SECOND_PASSWORD="otherrods" |
| 13 | +export CLIENT_AUTH_ERROR_EXITCODE=123 |
| 14 | + |
| 15 | +ssl_hash() { |
| 16 | + openssl passwd -6 "$1" |
| 17 | +} |
| 18 | + |
| 19 | +setup() { |
| 20 | + [ -f /tmp/test012_flag ] || { |
| 21 | + rm -fr ~/.irods |
| 22 | + /prc/test_harness/utility/iinit.py host localhost \ |
| 23 | + port 1247 \ |
| 24 | + zone tempZone \ |
| 25 | + user rods \ |
| 26 | + password rods \ |
| 27 | + |
| 28 | + sudo apt update |
| 29 | + sudo apt install -y db-util libpam0g-dev jq |
| 30 | + |
| 31 | + ## Because iRODS 5+ negotiates for SSL automatically: |
| 32 | + CLIENT_JSON=~/.irods/irods_environment.json |
| 33 | + jq '.irods_client_server_policy="CS_NEG_REFUSE"' >$CLIENT_JSON.$$ <$CLIENT_JSON && \ |
| 34 | + mv $CLIENT_JSON.$$ $CLIENT_JSON |
| 35 | + |
| 36 | + # if plugin installation would upgrade server, then skip test. |
| 37 | + if irods_server_package_upgradable; then |
| 38 | + skip |
| 39 | + fi |
| 40 | + |
| 41 | + sudo apt install -y irods-auth-plugin-pam-interactive-{client,server} |
| 42 | + SERVER_CONFIG=server_config.json |
| 43 | + |
| 44 | + sudo -s <<-EOF |
| 45 | + jq '.plugin_configuration.authentication.pam_interactive = { |
| 46 | + "pam_stack_name": "pam_interactive" |
| 47 | + }' <"/etc/irods/${SERVER_CONFIG}" >"/tmp/${SERVER_CONFIG}" |
| 48 | + cp -rp "/etc/irods/${SERVER_CONFIG}"{,.orig} |
| 49 | + mv -f "/tmp/${SERVER_CONFIG}" "/etc/irods/${SERVER_CONFIG}" |
| 50 | + EOF |
| 51 | + waitsrv() { |
| 52 | + while true; do |
| 53 | + sleep 5 |
| 54 | + ils >& /dev/null && break |
| 55 | + done |
| 56 | + } |
| 57 | + |
| 58 | + { sudo kill -HUP `sudo cat /tmp/irods.pid` && waitsrv; } || { |
| 59 | + echo "Couldn't properly bounce server after configuration change."; exit 1; } |
| 60 | + |
| 61 | + setup_pam_login_for_user "${FIRST_PASSWORD}" $TESTUSER |
| 62 | + sudo cp $BATS_TEST_DIRNAME/files_for_test012/pam_password /etc/pam.d/irods |
| 63 | + sudo cp $BATS_TEST_DIRNAME/files_for_test012/pam_interactive /etc/pam.d/ |
| 64 | + sudo mkdir /t012 && sudo gcc -o /t012/pam_clear_token.so -fno-stack-protector -shared -fPIC $BATS_TEST_DIRNAME/files_for_test012/pam_clear_token.c |
| 65 | + |
| 66 | + ## Switch over to scheme to be tested. |
| 67 | + jq '.irods_authentication_scheme="pam_interactive"' >$CLIENT_JSON.$$ <$CLIENT_JSON && \ |
| 68 | + mv $CLIENT_JSON.$$ $CLIENT_JSON |
| 69 | + } |
| 70 | + touch /tmp/test012_flag |
| 71 | + |
| 72 | + # Tests require only the irods_environment.json |
| 73 | + rm -f ~/.irods/.irodsA |
| 74 | +} |
| 75 | + |
| 76 | +encode_2nd_password() { |
| 77 | + db_file=/t012/pam_userdb.db |
| 78 | + sudo db_load -T -t hash "$db_file" <<<"${TESTUSER}"$'\n'"$(ssl_hash ${1})" |
| 79 | + sudo chown root:root "$db_file" |
| 80 | + sudo chmod 600 "$db_file" |
| 81 | +} |
| 82 | + |
| 83 | +SCRIPT=" |
| 84 | +import getpass |
| 85 | +import os |
| 86 | +
|
| 87 | +import irods |
| 88 | +from irods.auth import ClientAuthError |
| 89 | +from unittest.mock import patch |
| 90 | +
|
| 91 | +def getpass_new_callable(answers=()): |
| 92 | + class iterate_answers: |
| 93 | + def __init__(self,answers = answers): |
| 94 | + self.answers = answers |
| 95 | + self.count = 0 |
| 96 | + def __call__(self,*_): |
| 97 | + count = self.count |
| 98 | + self.count += 1 |
| 99 | + ans = self.answers[count] |
| 100 | + print ('*** giving answer:', ans) |
| 101 | + return ans |
| 102 | + return lambda : iterate_answers() |
| 103 | +
|
| 104 | +home = None |
| 105 | +
|
| 106 | +pw_count = 0 |
| 107 | +
|
| 108 | +with patch( |
| 109 | + 'getpass.getpass', |
| 110 | + new_callable=getpass_new_callable(answers=[os.environ['FIRST_PASSWORD'],os.environ['SECOND_PASSWORD']]) |
| 111 | +) as m: |
| 112 | + try: |
| 113 | + sess = irods.helpers.make_session(test_server_version=False) |
| 114 | + sess.set_auth_option_for_scheme('pam_interactive', irods.auth.FORCE_PASSWORD_PROMPT, True) |
| 115 | + home = sess.collections.get(f'/{sess.zone}/home/{sess.username}') |
| 116 | + except ClientAuthError as exc: |
| 117 | + # Note: The write to stdout, and the specific exit code, are necessary for the test assertions. |
| 118 | + # in test "pam_interactive_test_multistep_with_incorrect_2nd_password" below. |
| 119 | + print(f'ERROR: {exc!r}') |
| 120 | + exit(int(os.environ['CLIENT_AUTH_ERROR_EXITCODE'])) |
| 121 | + finally: |
| 122 | + pw_count = m.count |
| 123 | +
|
| 124 | +# Assert both passwords were prompted for. |
| 125 | +if pw_count < 2: |
| 126 | + print(f'************************ {pw_count = } < 2') |
| 127 | + exit(3) |
| 128 | +
|
| 129 | +# Assert home is defined, ie a session was successfully created and used to retrieve a collection object |
| 130 | +if home is None: |
| 131 | + exit(2) |
| 132 | +
|
| 133 | +username = os.environ['TESTUSER'] |
| 134 | +
|
| 135 | +# Assert home contains the expected username. |
| 136 | +if not home.path.endswith(f'/{username}'): |
| 137 | + exit(1) |
| 138 | +" |
| 139 | + |
| 140 | +@test "pam_interactive_test_multistep_with_incorrect_2nd_password" { |
| 141 | + |
| 142 | + # We are using a deliberately munged password. |
| 143 | + encode_2nd_password "_${SECOND_PASSWORD}" |
| 144 | + local STATUS="" |
| 145 | + OUTPUT=$(python -c "$SCRIPT" 2>&1) || STATUS=$? |
| 146 | + |
| 147 | + # Here, we assert the process's exit and output conform to expectation. We want to |
| 148 | + # enforce that the stdout output stream contains the thrown exception name ("ClientAuthError") |
| 149 | + # as well as that the process exits with a particular error status. |
| 150 | + [ $STATUS = $CLIENT_AUTH_ERROR_EXITCODE ] |
| 151 | + [[ $OUTPUT =~ ClientAuthError ]] |
| 152 | +} |
| 153 | + |
| 154 | +@test "pam_interactive_test_multistep_with_correct_2nd_password" { |
| 155 | + encode_2nd_password "${SECOND_PASSWORD}" |
| 156 | + python -c "$SCRIPT" |
| 157 | +} |
0 commit comments