-
Notifications
You must be signed in to change notification settings - Fork 73
#777 test pam_interactive (including multistep capability) automatically #824
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
d-w-moore
wants to merge
6
commits into
irods:main
Choose a base branch
from
d-w-moore:777.m
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0d75d3b
[_777] bats wrapper for running original pam_interactive test script
d-w-moore c1bb6c5
developments prior to debug session with Kory
d-w-moore c6d6eae
db_load stuff
d-w-moore 8ba2194
got both tests in test012_pam_interactive_multistep.bats now pass
d-w-moore d969aff
add dummy file to force rerunning tests in github actions
d-w-moore 66e67e6
skip test if server not latest version
d-w-moore File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -38,6 +38,7 @@ | |
|
|
||
| _logger = logging.getLogger(__name__) | ||
|
|
||
| _logger.debug("hello from paminteractive") | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this line needed? |
||
|
|
||
| def login(conn, **extra_opt): | ||
| """The entry point for the pam_interactive authentication scheme.""" | ||
|
|
@@ -230,7 +231,7 @@ def native_auth(self, request): | |
| def next(self, request): | ||
| prompt = request.get("msg", {}).get("prompt", "") | ||
| if prompt: | ||
| _logger.info("Server prompt: %s", prompt) | ||
| _logger.debug("Server prompt: %s", prompt) | ||
|
|
||
| server_req = request.copy() | ||
| self._patch_state(server_req) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| /* | ||
| To build, you need the PAM development library. Once installed, | ||
| run the following: | ||
|
|
||
| gcc -fPIC -fno-stack-protector -o pam_clear_token.o -c main.c | ||
| gcc -shared -o pam_clear_token.so pam_clear_token.o | ||
| */ | ||
|
|
||
| #include <security/pam_modules.h> | ||
| #include <security/pam_ext.h> | ||
| #include <security/pam_appl.h> | ||
|
|
||
| #include <stdlib.h> | ||
|
|
||
| PAM_EXTERN int pam_sm_authenticate(pam_handle_t* pamh, int flags, int argc, const char** argv) | ||
| { | ||
| (void) flags; | ||
| (void) argc; | ||
| (void) argv; | ||
|
|
||
| // Clear the current auth token. | ||
| pam_set_item(pamh, PAM_AUTHTOK, NULL); | ||
| pam_set_item(pamh, PAM_OLDAUTHTOK, NULL); | ||
|
|
||
| return PAM_SUCCESS; | ||
| } | ||
|
|
||
| PAM_EXTERN int pam_sm_setcred(pam_handle_t* pamh, int flags, int argc, const char** argv) | ||
| { | ||
| (void) pamh; | ||
| (void) flags; | ||
| (void) argc; | ||
| (void) argv; | ||
|
|
||
| return PAM_SUCCESS; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| # This file is for testing PAM authentication with iRODS | ||
| # using the pam_interactive authentication scheme. | ||
|
|
||
| # Prompt for the first password, from /etc/shadow. | ||
| auth required pam_unix.so | ||
|
|
||
| # This is a custom PAM module that clears the success token. Without | ||
| # this, the "auth" lines which follow are skipped. | ||
| auth required /t012/pam_clear_token.so | ||
|
|
||
| # Prompt for the second password, from the user database file created | ||
| # earlier. The use of "crypt=crypt" is required for this to work. It | ||
| # tells the module that the passwords are encrypted. | ||
| auth required pam_userdb.so db=/t012/pam_userdb crypt=crypt | ||
|
|
||
| # Do the normal user account stuff. | ||
| account required pam_unix.so |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| # This file is for testing PAM authentication with iRODS | ||
| # using the pam_password authentication scheme. | ||
|
|
||
| auth required pam_env.so | ||
| auth sufficient pam_unix.so | ||
| auth requisite pam_succeed_if.so uid >= 500 quiet | ||
| auth required pam_deny.so |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| #!/usr/bin/env bats | ||
|
|
||
| # The tests in this BATS module must be run as a (passwordless) sudo-enabled user. | ||
| # It is also required that the python irodsclient be installed under irods' ~/.local environment. | ||
|
|
||
| . $BATS_TEST_DIRNAME/test_support_functions | ||
|
|
||
| setup() { | ||
| [ -f /tmp/test011_flag ] || { | ||
| rm -fr ~/.irods | ||
| /prc/test_harness/utility/iinit.py host localhost \ | ||
| port 1247 \ | ||
| zone tempZone \ | ||
| user rods \ | ||
| password rods \ | ||
|
|
||
| ## Because iRODS 5+ negotiates for SSL automatically: | ||
| CLIENT_JSON=~/.irods/irods_environment.json | ||
| jq '.irods_client_server_policy="CS_NEG_REFUSE"' >$CLIENT_JSON.$$ <$CLIENT_JSON && \ | ||
| mv $CLIENT_JSON.$$ $CLIENT_JSON | ||
|
|
||
| # if plugin installation would upgrade server, then skip test. | ||
| if irods_server_package_upgradable; then | ||
| skip | ||
| fi | ||
|
|
||
| sudo apt install -y irods-auth-plugin-pam-interactive-{client,server} | ||
|
|
||
| setup_pam_login_for_user "rods" alice | ||
|
|
||
| # Tests require only the irods_environment.json | ||
| rm -f ~/.irods/.irodsA | ||
|
|
||
| ## Switch over to scheme to be tested. | ||
| jq '.irods_authentication_scheme="pam_interactive"' >$CLIENT_JSON.$$ <$CLIENT_JSON && \ | ||
| mv $CLIENT_JSON.$$ $CLIENT_JSON | ||
| } | ||
| touch /tmp/test011_flag | ||
| } | ||
|
|
||
| original_test_suite() | ||
| { | ||
| local USER="alice" | ||
| local PASSWORD="rods" | ||
| sudo chpasswd <<<"$USER:$PASSWORD" | ||
| python -m unittest irods.test.pam_interactive_test_must_run_manually | ||
| } | ||
|
|
||
| @test "original_pam_interactive_tests" { | ||
| original_test_suite | ||
| } |
157 changes: 157 additions & 0 deletions
157
irods/test/scripts/test012_pam_interactive_multistep.bats
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,157 @@ | ||
| #!/usr/bin/env bats | ||
|
|
||
| # The tests in this BATS module must be run as a (passwordless) sudo-enabled user. | ||
| # It is also required that the python irodsclient be installed under irods' ~/.local environment. | ||
|
|
||
| SKIP_IINIT_FOR_PASSWORD=yes | ||
|
|
||
| . $BATS_TEST_DIRNAME/test_support_functions | ||
|
|
||
| export TESTUSER="john" | ||
| export FIRST_PASSWORD="=i;r@o\\d&s" # somerods | ||
| export SECOND_PASSWORD="otherrods" | ||
| export CLIENT_AUTH_ERROR_EXITCODE=123 | ||
|
|
||
| ssl_hash() { | ||
| openssl passwd -6 "$1" | ||
| } | ||
|
|
||
| setup() { | ||
| [ -f /tmp/test012_flag ] || { | ||
| rm -fr ~/.irods | ||
| /prc/test_harness/utility/iinit.py host localhost \ | ||
| port 1247 \ | ||
| zone tempZone \ | ||
| user rods \ | ||
| password rods \ | ||
|
|
||
| sudo apt update | ||
| sudo apt install -y db-util libpam0g-dev jq | ||
|
|
||
| ## Because iRODS 5+ negotiates for SSL automatically: | ||
| CLIENT_JSON=~/.irods/irods_environment.json | ||
| jq '.irods_client_server_policy="CS_NEG_REFUSE"' >$CLIENT_JSON.$$ <$CLIENT_JSON && \ | ||
| mv $CLIENT_JSON.$$ $CLIENT_JSON | ||
|
|
||
| # if plugin installation would upgrade server, then skip test. | ||
| if irods_server_package_upgradable; then | ||
| skip | ||
| fi | ||
|
|
||
| sudo apt install -y irods-auth-plugin-pam-interactive-{client,server} | ||
| SERVER_CONFIG=server_config.json | ||
|
|
||
| sudo -s <<-EOF | ||
| jq '.plugin_configuration.authentication.pam_interactive = { | ||
| "pam_stack_name": "pam_interactive" | ||
| }' <"/etc/irods/${SERVER_CONFIG}" >"/tmp/${SERVER_CONFIG}" | ||
| cp -rp "/etc/irods/${SERVER_CONFIG}"{,.orig} | ||
| mv -f "/tmp/${SERVER_CONFIG}" "/etc/irods/${SERVER_CONFIG}" | ||
| EOF | ||
| waitsrv() { | ||
| while true; do | ||
| sleep 5 | ||
| ils >& /dev/null && break | ||
| done | ||
| } | ||
|
|
||
| { sudo kill -HUP `sudo cat /tmp/irods.pid` && waitsrv; } || { | ||
| echo "Couldn't properly bounce server after configuration change."; exit 1; } | ||
|
|
||
| setup_pam_login_for_user "${FIRST_PASSWORD}" $TESTUSER | ||
| sudo cp $BATS_TEST_DIRNAME/files_for_test012/pam_password /etc/pam.d/irods | ||
| sudo cp $BATS_TEST_DIRNAME/files_for_test012/pam_interactive /etc/pam.d/ | ||
| 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 | ||
|
|
||
| # Tests require only the irods_environment.json | ||
| rm -f ~/.irods/.irodsA | ||
|
|
||
| ## Switch over to scheme to be tested. | ||
| jq '.irods_authentication_scheme="pam_interactive"' >$CLIENT_JSON.$$ <$CLIENT_JSON && \ | ||
| mv $CLIENT_JSON.$$ $CLIENT_JSON | ||
| } | ||
| touch /tmp/test012_flag | ||
| } | ||
|
|
||
| encode_2nd_password() { | ||
| db_file=/t012/pam_userdb.db | ||
| sudo db_load -T -t hash "$db_file" <<<"${TESTUSER}"$'\n'"$(ssl_hash ${1})" | ||
| sudo chown root:root "$db_file" | ||
| sudo chmod 600 "$db_file" | ||
| } | ||
|
|
||
| SCRIPT=" | ||
| import getpass | ||
| import os | ||
|
|
||
| import irods | ||
| from irods.auth import ClientAuthError | ||
| from unittest.mock import patch | ||
|
|
||
| def getpass_new_callable(answers=()): | ||
| class iterate_answers: | ||
| def __init__(self,answers = answers): | ||
| self.answers = answers | ||
| self.count = 0 | ||
| def __call__(self,*_): | ||
| count = self.count | ||
| self.count += 1 | ||
| ans = self.answers[count] | ||
| print ('*** giving answer:', ans) | ||
| return ans | ||
| return lambda : iterate_answers() | ||
|
|
||
| home = None | ||
|
|
||
| pw_count = 0 | ||
|
|
||
| with patch( | ||
| 'getpass.getpass', | ||
| new_callable=getpass_new_callable(answers=[os.environ['FIRST_PASSWORD'],os.environ['SECOND_PASSWORD']]) | ||
| ) as m: | ||
| try: | ||
| sess = irods.helpers.make_session(test_server_version=False) | ||
| sess.set_auth_option_for_scheme('pam_interactive', irods.auth.FORCE_PASSWORD_PROMPT, True) | ||
| home = sess.collections.get(f'/{sess.zone}/home/{sess.username}') | ||
| except ClientAuthError as exc: | ||
| # Note: The write to stdout, and the specific exit code, are necessary for the test assertions. | ||
| # in test "pam_interactive_test_multistep_with_incorrect_2nd_password" below. | ||
| print(f'ERROR: {exc!r}') | ||
| exit(int(os.environ['CLIENT_AUTH_ERROR_EXITCODE'])) | ||
| finally: | ||
| pw_count = m.count | ||
|
|
||
| # Assert both passwords were prompted for. | ||
| if pw_count < 2: | ||
| print(f'************************ {pw_count = } < 2') | ||
| exit(3) | ||
|
|
||
| # Assert home is defined, ie a session was successfully created and used to retrieve a collection object | ||
| if home is None: | ||
| exit(2) | ||
|
|
||
| username = os.environ['TESTUSER'] | ||
|
|
||
| # Assert home contains the expected username. | ||
| if not home.path.endswith(f'/{username}'): | ||
| exit(1) | ||
| " | ||
|
|
||
| @test "pam_interactive_test_multistep_with_incorrect_2nd_password" { | ||
|
|
||
| # We are using a deliberately munged password. | ||
| encode_2nd_password "_${SECOND_PASSWORD}" | ||
| local STATUS="" | ||
| OUTPUT=$(python -c "$SCRIPT" 2>&1) || STATUS=$? | ||
|
|
||
| # Here, we assert the process's exit and output conform to expectation. We want to | ||
| # enforce that the stdout output stream contains the thrown exception name ("ClientAuthError") | ||
| # as well as that the process exits with a particular error status. | ||
| [ $STATUS = $CLIENT_AUTH_ERROR_EXITCODE ] | ||
| [[ $OUTPUT =~ ClientAuthError ]] | ||
| } | ||
|
|
||
| @test "pam_interactive_test_multistep_with_correct_2nd_password" { | ||
| encode_2nd_password "${SECOND_PASSWORD}" | ||
| python -c "$SCRIPT" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this file needed?