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 25, 2024
1 parent 9245485 commit cf25706
Show file tree
Hide file tree
Showing 2 changed files with 24 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
19 changes: 19 additions & 0 deletions test_app/tests/authentication/test_backend.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from random import shuffle
from types import SimpleNamespace
from unittest import mock

import pytest
Expand Down Expand Up @@ -153,3 +154,21 @@ def test_authenticate(request, local_authenticator, github_enterprise_authentica
expected = request.getfixturevalue(expected)

assert auth_return == expected


@pytest.mark.django_db
def test_authentication_exception(expected_log):
class MockAuthenticator:
database_instance = SimpleNamespace(name='testing')

def authenticate(*args, **kwars):
raise Exception('eeekkkk')

# Patch the backends to have our Mock authenticator in it
with mock.patch(
"ansible_base.authentication.backend.get_authentication_backends",
return_value={1: MockAuthenticator()},
):
# Expect the log we emit
with expected_log('ansible_base.authentication.backend.logger', "exception", "Exception raised while trying to authenticate with"):
backend.AnsibleBaseAuth().authenticate(None)

0 comments on commit cf25706

Please sign in to comment.