Skip to content

Commit b3d8fd0

Browse files
committed
emit logs for authentication failures and successes
1 parent 9f58e7b commit b3d8fd0

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

ansible_base/authentication/authenticator_plugins/local.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,15 @@ def authenticate(self, request, username=None, password=None, **kwargs):
5252
if new_username != username:
5353
return None
5454

55+
auth_log_headers = (
56+
f"HTTP_USER_AGENT: {request.META['HTTP_USER_AGENT'] if 'HTTP_USER_AGENT' in request.META else 'UNKNOWN'} "
57+
f"HTTP_X_FORWARDED_FOR: {request.META['HTTP_X_FORWARDED_FOR'] if 'HTTP_X_FORWARDED_FOR' in request.META else 'UNKNOWN'} "
58+
f"REMOTE_ADDR: {request.META['REMOTE_ADDR'] if 'REMOTE_ADDR' in request.META else 'UNKNOWN'} "
59+
f"REMOTE_HOST: {request.META['REMOTE_HOST'] if 'REMOTE_HOST' in request.META else 'UNKNOWN'}"
60+
)
61+
62+
logger.warning(f"Login attempt for user: {username} {auth_log_headers}")
63+
5564
user = super().authenticate(request, username, password, **kwargs)
5665

5766
# This auth class doesn't create any new local users, but we still need to make sure
@@ -69,5 +78,8 @@ def authenticate(self, request, username=None, password=None, **kwargs):
6978
"is_superuser": user.is_superuser,
7079
},
7180
)
81+
logger.warning(f"Successful login for user: {username} {auth_log_headers}")
82+
else:
83+
logger.warning(f"Failed login for user: {username} {auth_log_headers}")
7284

7385
return update_user_claims(user, self.database_instance, [])

ansible_base/authentication/authenticator_plugins/oidc.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,29 @@ class AuthenticatorPlugin(SocialAuthMixin, OpenIdConnectAuth, AbstractAuthentica
221221
def groups_claim(self):
222222
return self.setting('GROUPS_CLAIM')
223223

224+
def authenticate(self, *args, **kwargs):
225+
request = args[0]
226+
227+
auth_log_headers = (
228+
f"HTTP_USER_AGENT: {request.META['HTTP_USER_AGENT'] if 'HTTP_USER_AGENT' in request.META else 'UNKNOWN'} "
229+
f"HTTP_X_FORWARDED_FOR: {request.META['HTTP_X_FORWARDED_FOR'] if 'HTTP_X_FORWARDED_FOR' in request.META else 'UNKNOWN'} "
230+
f"REMOTE_ADDR: {request.META['REMOTE_ADDR'] if 'REMOTE_ADDR' in request.META else 'UNKNOWN'} "
231+
f"REMOTE_HOST: {request.META['REMOTE_HOST'] if 'REMOTE_HOST' in request.META else 'UNKNOWN'}"
232+
)
233+
234+
if "backend" in kwargs and kwargs["backend"].name == self.name:
235+
logger.warning(f"Login attempt for {auth_log_headers}")
236+
237+
user = super().authenticate(*args, **kwargs)
238+
239+
if "backend" in kwargs and kwargs["backend"].name == self.name:
240+
if user:
241+
logger.warning(f"Successful login for {user} {auth_log_headers}")
242+
else:
243+
logger.warning(f"Failed login {auth_log_headers}")
244+
245+
return user
246+
224247
def extra_data(self, user, backend, response, *args, **kwargs):
225248
for perm in ["is_superuser", get_setting('ANSIBLE_BASE_SOCIAL_AUDITOR_FLAG')]:
226249
if perm in response:

0 commit comments

Comments
 (0)