Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions ansible_base/authentication/authenticator_plugins/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ def authenticate(self, request, username=None, password=None, **kwargs):
if new_username != username:
return None

auth_log_headers = (
f"HTTP_USER_AGENT: {request.META['HTTP_USER_AGENT'] if 'HTTP_USER_AGENT' in request.META else 'UNKNOWN'} "
f"HTTP_X_FORWARDED_FOR: {request.META['HTTP_X_FORWARDED_FOR'] if 'HTTP_X_FORWARDED_FOR' in request.META else 'UNKNOWN'} "
f"REMOTE_ADDR: {request.META['REMOTE_ADDR'] if 'REMOTE_ADDR' in request.META else 'UNKNOWN'} "
f"REMOTE_HOST: {request.META['REMOTE_HOST'] if 'REMOTE_HOST' in request.META else 'UNKNOWN'}"
)

logger.info(f"Login attempt for user: {username} {auth_log_headers}")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we make this a debug log level or do we feel like these headers are info level worthy?


user = super().authenticate(request, username, password, **kwargs)

# This auth class doesn't create any new local users, but we still need to make sure
Expand All @@ -69,5 +78,8 @@ def authenticate(self, request, username=None, password=None, **kwargs):
"is_superuser": user.is_superuser,
},
)
logger.info(f"Successful login for user: {username} {auth_log_headers}")
else:
logger.info(f"Failed login for user: {username} {auth_log_headers}")
Comment on lines +81 to +83
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are logging the auth headers here again, I think we can drop it out of these messages.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think its valuable to have the status and the headers in a single log entry. It lets you easily determine where the successes/failures are coming from. I think we can probably drop the attempt log to debug


return update_user_claims(user, self.database_instance, [])
23 changes: 23 additions & 0 deletions ansible_base/authentication/authenticator_plugins/oidc.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,29 @@ class AuthenticatorPlugin(SocialAuthMixin, OpenIdConnectAuth, AbstractAuthentica
def groups_claim(self):
return self.setting('GROUPS_CLAIM')

def authenticate(self, *args, **kwargs):
request = args[0]

auth_log_headers = (
f"HTTP_USER_AGENT: {request.META['HTTP_USER_AGENT'] if 'HTTP_USER_AGENT' in request.META else 'UNKNOWN'} "
f"HTTP_X_FORWARDED_FOR: {request.META['HTTP_X_FORWARDED_FOR'] if 'HTTP_X_FORWARDED_FOR' in request.META else 'UNKNOWN'} "
f"REMOTE_ADDR: {request.META['REMOTE_ADDR'] if 'REMOTE_ADDR' in request.META else 'UNKNOWN'} "
f"REMOTE_HOST: {request.META['REMOTE_HOST'] if 'REMOTE_HOST' in request.META else 'UNKNOWN'}"
)

if "backend" in kwargs and kwargs["backend"].name == self.name:
logger.info(f"Login attempt for {auth_log_headers}")

user = super().authenticate(*args, **kwargs)

if "backend" in kwargs and kwargs["backend"].name == self.name:
if user:
logger.info(f"Successful login for {user} {auth_log_headers}")
else:
logger.info(f"Failed login {auth_log_headers}")

return user

Comment on lines +224 to +246
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we move this to the SocialAuthMixin so that all of the classes derived from that get this logging? Same comments about logging the auth_log_headers as debug and removing it from the "Successful/Failed" login messages.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably a good idea.

def extra_data(self, user, backend, response, *args, **kwargs):
for perm in ["is_superuser", get_setting('ANSIBLE_BASE_SOCIAL_AUDITOR_FLAG')]:
if perm in response:
Expand Down
Loading