From a4707ddc549115af84d2321b87d18b7bb9e89dd9 Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Sun, 3 Mar 2019 20:34:00 +0100 Subject: [PATCH] fix failing unittests --- passbook/core/policies.py | 15 +++++---------- passbook/core/requirements.txt | 1 + passbook/suspicious_policy/requirements.txt | 1 - passbook/suspicious_policy/signals.py | 4 +++- requirements.txt | 1 - 5 files changed, 9 insertions(+), 13 deletions(-) delete mode 100644 passbook/suspicious_policy/requirements.txt diff --git a/passbook/core/policies.py b/passbook/core/policies.py index 1b4f5930e..1e0399a42 100644 --- a/passbook/core/policies.py +++ b/passbook/core/policies.py @@ -1,6 +1,7 @@ """passbook core policy engine""" from logging import getLogger +from ipware import get_client_ip from celery import group from django.http import HttpRequest @@ -9,15 +10,6 @@ from passbook.core.models import Policy, User LOGGER = getLogger(__name__) - -def get_remote_ip(request: HttpRequest) -> str: - """Return the remote's IP""" - if not request: - return '0.0.0.0' # nosec - if request.META.get('HTTP_X_FORWARDED_FOR'): - return request.META.get('HTTP_X_FORWARDED_FOR') - return request.META.get('REMOTE_ADDR') - @CELERY_APP.task() def _policy_engine_task(user_pk, policy_pk, **kwargs): """Task wrapper to run policy checking""" @@ -66,8 +58,11 @@ class PolicyEngine: signatures = [] kwargs = { '__password__': getattr(self._user, '__password__', None), - 'remote_ip': get_remote_ip(self._request) } + if self._request: + kwargs['remote_ip'], _ = get_client_ip(self._request) + if not kwargs['remote_ip']: + kwargs['remote_ip'] = '255.255.255.255', for policy in self.policies: signatures.append(_policy_engine_task.s(self._user.pk, policy.pk.hex, **kwargs)) self._group = group(signatures)() diff --git a/passbook/core/requirements.txt b/passbook/core/requirements.txt index 282556637..9aca681e6 100644 --- a/passbook/core/requirements.txt +++ b/passbook/core/requirements.txt @@ -1,5 +1,6 @@ django>=2.0 django-model-utils +django-ipware djangorestframework PyYAML raven diff --git a/passbook/suspicious_policy/requirements.txt b/passbook/suspicious_policy/requirements.txt deleted file mode 100644 index 0b89a70b7..000000000 --- a/passbook/suspicious_policy/requirements.txt +++ /dev/null @@ -1 +0,0 @@ -django-ipware diff --git a/passbook/suspicious_policy/signals.py b/passbook/suspicious_policy/signals.py index 382e2c595..df3ae095d 100644 --- a/passbook/suspicious_policy/signals.py +++ b/passbook/suspicious_policy/signals.py @@ -13,7 +13,9 @@ LOGGER = getLogger(__name__) def update_score(request, username, amount): """Update score for IP and User""" - remote_ip = get_client_ip(request) + remote_ip, _ = get_client_ip(request) + if not remote_ip: + remote_ip = '255.255.255.255' ip_score, _ = IPScore.objects.update_or_create(ip=remote_ip) ip_score.score += amount ip_score.save() diff --git a/requirements.txt b/requirements.txt index f9290c23d..dbd7672d0 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,7 +4,6 @@ -r passbook/saml_idp/requirements.txt -r passbook/otp/requirements.txt -r passbook/oauth_provider/requirements.txt --r passbook/suspicious_policy/requirements.txt -r passbook/captcha_factor/requirements.txt -r passbook/admin/requirements.txt -r passbook/api/requirements.txt