Skip to content

Commit

Permalink
Never fatal.
Browse files Browse the repository at this point in the history
Signed-off-by: Rick Elrod <[email protected]>
  • Loading branch information
relrod authored and john-westcott-iv committed Nov 22, 2024
1 parent ba3e77a commit f2b4e61
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
6 changes: 5 additions & 1 deletion ansible_base/authentication/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ def authenticate(self, request, *args, **kwargs):
last_modified = None if last_modified_item is None else last_modified_item.get('modified')

for authenticator_id, authenticator_object in get_authentication_backends(last_modified).items():
user = authenticator_object.authenticate(request, *args, **kwargs)
try:
user = authenticator_object.authenticate(request, *args, **kwargs)
except Exception:
logger.exception(f"Exception raised while trying to authenticate with {authenticator_object.database_instance.name}")
continue

# Social Auth pipeline can return status string when update_user_claims fails (authentication maps deny access)
if user == SOCIAL_AUTH_PIPELINE_FAILED_STATUS:
Expand Down
15 changes: 15 additions & 0 deletions test_app/tests/authentication/test_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,18 @@ def test_authenticate(request, local_authenticator, github_enterprise_authentica
expected = request.getfixturevalue(expected)

assert auth_return == expected


# Make the local authenticator authenticate method raise an exception
@mock.patch('ansible_base.authentication.authenticator_plugins.local.AuthenticatorPlugin.authenticate', side_effect=Exception("eeekkkk"))
def test_authentication_exception(expected_log, local_authenticator):
# Patch the backends to have the local authenticator in it
with mock.patch(
"ansible_base.authentication.backend.get_authentication_backends",
return_value={local_authenticator.id: local_authenticator},
):
# We expect am exception
with pytest.raises(Exception):
# Expect the log we emit
with expected_log('ansible_base.authentication.backened', "exception", "Exception raised while trying to authenticate with"):
backend.AnsibleBaseAuth().authenticate(None)

0 comments on commit f2b4e61

Please sign in to comment.