-
Notifications
You must be signed in to change notification settings - Fork 81
[WIP] Emit logs for authentication failures and successes (local and oidc authenticator plugins #726
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: devel
Are you sure you want to change the base?
[WIP] Emit logs for authentication failures and successes (local and oidc authenticator plugins #726
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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}") | ||
|
||
user = super().authenticate(request, username, password, **kwargs) | ||
|
||
# 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): | |
"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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
|
||
return update_user_claims(user, self.database_instance, []) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: | ||
|
There was a problem hiding this comment.
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?