Skip to content

Commit

Permalink
Replace REMOTE_ADDR with ansible_base.lib.utils.requests.get_remote_host
Browse files Browse the repository at this point in the history
  • Loading branch information
dmzoneill committed May 9, 2024
1 parent 20f054d commit 7bce7b2
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
9 changes: 6 additions & 3 deletions awx/api/generics.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
# django-ansible-base
from ansible_base.rest_filters.rest_framework.field_lookup_backend import FieldLookupBackend
from ansible_base.lib.utils.models import get_all_field_names
from ansible_base.lib.utils.requests import get_remote_host
from ansible_base.rbac.models import RoleEvaluation, RoleDefinition
from ansible_base.rbac.permission_registry import permission_registry

Expand Down Expand Up @@ -93,8 +94,9 @@ def get(self, request, *args, **kwargs):

def post(self, request, *args, **kwargs):
ret = super(LoggedLoginView, self).post(request, *args, **kwargs)
ip = get_remote_host(request) # request.META.get('REMOTE_ADDR', None)
if request.user.is_authenticated:
logger.info(smart_str(u"User {} logged in from {}".format(self.request.user.username, request.META.get('REMOTE_ADDR', None))))
logger.info(smart_str(u"User {} logged in from {}".format(self.request.user.username, ip)))
ret.set_cookie(
'userLoggedIn', 'true', secure=getattr(settings, 'SESSION_COOKIE_SECURE', False), samesite=getattr(settings, 'USER_COOKIE_SAMESITE', 'Lax')
)
Expand All @@ -103,7 +105,7 @@ def post(self, request, *args, **kwargs):
return ret
else:
if 'username' in self.request.POST:
logger.warning(smart_str(u"Login failed for user {} from {}".format(self.request.POST.get('username'), request.META.get('REMOTE_ADDR', None))))
logger.warning(smart_str(u"Login failed for user {} from {}".format(self.request.POST.get('username'), ip)))
ret.status_code = 401
return ret

Expand Down Expand Up @@ -208,11 +210,12 @@ def finalize_response(self, request, response, *args, **kwargs):
return response

if response.status_code >= 400:
ip = get_remote_host(request) # request.META.get('REMOTE_ADDR', None)
msg_data = {
'status_code': response.status_code,
'user_name': request.user,
'url_path': request.path,
'remote_addr': request.META.get('REMOTE_ADDR', None),
'remote_addr': ip,
}

if type(response.data) is dict:
Expand Down
3 changes: 2 additions & 1 deletion awx/sso/backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from django.conf import settings as django_settings
from django.core.signals import setting_changed
from django.utils.encoding import force_str
from ansible_base.lib.utils.requests import get_remote_host

# django-auth-ldap
from django_auth_ldap.backend import LDAPSettings as BaseLDAPSettings
Expand Down Expand Up @@ -257,7 +258,7 @@ def _get_client_ip(self, request):
if x_forwarded_for:
ip = x_forwarded_for.split(',')[0]
else:
ip = request.META.get('REMOTE_ADDR')
ip = get_remote_host(request) # request.META.get('REMOTE_ADDR')
return ip


Expand Down

0 comments on commit 7bce7b2

Please sign in to comment.