Log basic auth requests to the debug log

Part of #1087
This commit is contained in:
Akita Noek 2016-04-11 15:38:06 -04:00
parent 6182dad0d4
commit f3cae7e1f0
3 changed files with 17 additions and 3 deletions

View File

@ -3,9 +3,11 @@
# Python
import urllib
import logging
# Django
from django.utils.timezone import now as tz_now
from django.utils.encoding import smart_text
# Django REST Framework
from rest_framework import authentication
@ -16,6 +18,8 @@ from rest_framework import HTTP_HEADER_ENCODING
from awx.main.models import UnifiedJob, AuthToken
from awx.main.conf import tower_settings
logger = logging.getLogger('awx.api.authentication')
class TokenAuthentication(authentication.TokenAuthentication):
'''
Custom token authentication using tokens that expire and are associated
@ -93,7 +97,7 @@ class TokenAuthentication(authentication.TokenAuthentication):
if not token.in_valid_tokens(now=now):
token.invalidate(reason='limit_reached')
raise exceptions.AuthenticationFailed(AuthToken.reason_long('limit_reached'))
# If the user is inactive, then return an error.
if not token.user.is_active:
raise exceptions.AuthenticationFailed('User inactive or deleted')
@ -116,6 +120,16 @@ class TokenGetAuthentication(TokenAuthentication):
return super(TokenGetAuthentication, self).authenticate(request)
class LoggedBasicAuthentication(authentication.BasicAuthentication):
def authenticate(self, request):
ret = super(LoggedBasicAuthentication, self).authenticate(request)
if ret:
username = ret[0].username if ret[0] else '<none>'
logger.debug(smart_text(u"User {} performed a {} to {} through the API".format(username, request.method, request.path)))
return ret
class TaskAuthentication(authentication.BaseAuthentication):
'''
Custom authentication used for views accessed by the inventory and callback

View File

@ -72,7 +72,7 @@ from awx.api.metadata import RoleMetadata
from awx.main.utils import emit_websocket_notification
from awx.main.conf import tower_settings
logger = logging.getLogger('awx.api.generics')
logger = logging.getLogger('awx.api.views')
def api_exception_handler(exc, context):
'''

View File

@ -202,7 +202,7 @@ REST_FRAMEWORK = {
'PAGE_SIZE': 25,
'DEFAULT_AUTHENTICATION_CLASSES': (
'awx.api.authentication.TokenAuthentication',
'rest_framework.authentication.BasicAuthentication',
'awx.api.authentication.LoggedBasicAuthentication',
#'rest_framework.authentication.SessionAuthentication',
),
'DEFAULT_PERMISSION_CLASSES': (