Skip to content

Commit 64e7296

Browse files
relroddmzoneill
authored andcommitted
Never fatal.
Signed-off-by: Rick Elrod <[email protected]>
1 parent 942200b commit 64e7296

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

ansible_base/authentication/backend.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,11 @@ def authenticate(self, request, *args, **kwargs):
3737
last_modified = None if last_modified_item is None else last_modified_item.get('modified')
3838

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

4246
# Social Auth pipeline can return status string when update_user_claims fails (authentication maps deny access)
4347
if user == SOCIAL_AUTH_PIPELINE_FAILED_STATUS:

test_app/tests/authentication/test_backend.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from random import shuffle
2+
from types import SimpleNamespace
23
from unittest import mock
34

45
import pytest
@@ -153,3 +154,21 @@ def test_authenticate(request, local_authenticator, github_enterprise_authentica
153154
expected = request.getfixturevalue(expected)
154155

155156
assert auth_return == expected
157+
158+
159+
@pytest.mark.django_db
160+
def test_authentication_exception(expected_log):
161+
class MockAuthenticator:
162+
database_instance = SimpleNamespace(name='testing')
163+
164+
def authenticate(*args, **kwars):
165+
raise Exception('eeekkkk')
166+
167+
# Patch the backends to have our Mock authenticator in it
168+
with mock.patch(
169+
"ansible_base.authentication.backend.get_authentication_backends",
170+
return_value={1: MockAuthenticator()},
171+
):
172+
# Expect the log we emit
173+
with expected_log('ansible_base.authentication.backend.logger', "exception", "Exception raised while trying to authenticate with"):
174+
backend.AnsibleBaseAuth().authenticate(None)

0 commit comments

Comments
 (0)