2019-02-16 09:24:31 +00:00
|
|
|
"""passbook core policy engine"""
|
|
|
|
from logging import getLogger
|
|
|
|
|
|
|
|
from celery import group
|
2019-03-03 19:54:23 +00:00
|
|
|
from ipware import get_client_ip
|
2019-02-16 09:24:31 +00:00
|
|
|
|
|
|
|
from passbook.core.celery import CELERY_APP
|
|
|
|
from passbook.core.models import Policy, User
|
|
|
|
|
|
|
|
LOGGER = getLogger(__name__)
|
|
|
|
|
|
|
|
@CELERY_APP.task()
|
|
|
|
def _policy_engine_task(user_pk, policy_pk, **kwargs):
|
|
|
|
"""Task wrapper to run policy checking"""
|
|
|
|
policy_obj = Policy.objects.filter(pk=policy_pk).select_subclasses().first()
|
|
|
|
user_obj = User.objects.get(pk=user_pk)
|
|
|
|
for key, value in kwargs.items():
|
|
|
|
setattr(user_obj, key, value)
|
|
|
|
LOGGER.debug("Running policy `%s`#%s for user %s...", policy_obj.name,
|
|
|
|
policy_obj.pk.hex, user_obj)
|
2019-03-03 16:12:53 +00:00
|
|
|
policy_result = policy_obj.passes(user_obj)
|
|
|
|
# Handle policy result correctly if result, message or just result
|
|
|
|
message = None
|
|
|
|
if isinstance(policy_result, (tuple, list)):
|
|
|
|
policy_result, message = policy_result
|
|
|
|
# Invert result if policy.negate is set
|
|
|
|
if policy_obj.negate:
|
|
|
|
policy_result = not policy_result
|
2019-03-03 16:21:58 +00:00
|
|
|
LOGGER.debug("Policy %r#%s got %s", policy_obj.name, policy_obj.pk.hex, policy_result)
|
2019-03-03 16:12:53 +00:00
|
|
|
return policy_obj.action, policy_result, message
|
2019-02-16 09:24:31 +00:00
|
|
|
|
|
|
|
class PolicyEngine:
|
|
|
|
"""Orchestrate policy checking, launch tasks and return result"""
|
|
|
|
|
|
|
|
policies = None
|
|
|
|
_group = None
|
2019-03-03 19:26:25 +00:00
|
|
|
_request = None
|
|
|
|
_user = None
|
2019-02-16 09:24:31 +00:00
|
|
|
|
|
|
|
def __init__(self, policies):
|
|
|
|
self.policies = policies
|
2019-03-03 19:26:25 +00:00
|
|
|
self._request = None
|
|
|
|
self._user = None
|
2019-02-16 09:24:31 +00:00
|
|
|
|
|
|
|
def for_user(self, user):
|
|
|
|
"""Check policies for user"""
|
2019-03-03 19:26:25 +00:00
|
|
|
self._user = user
|
|
|
|
return self
|
|
|
|
|
|
|
|
def with_request(self, request):
|
|
|
|
"""Set request"""
|
|
|
|
self._request = request
|
|
|
|
return self
|
|
|
|
|
|
|
|
def build(self):
|
|
|
|
"""Build task group"""
|
2019-02-16 09:24:31 +00:00
|
|
|
signatures = []
|
|
|
|
kwargs = {
|
2019-03-03 19:26:25 +00:00
|
|
|
'__password__': getattr(self._user, '__password__', None),
|
2019-02-16 09:24:31 +00:00
|
|
|
}
|
2019-03-03 19:34:00 +00:00
|
|
|
if self._request:
|
|
|
|
kwargs['remote_ip'], _ = get_client_ip(self._request)
|
|
|
|
if not kwargs['remote_ip']:
|
2019-03-03 19:54:23 +00:00
|
|
|
kwargs['remote_ip'] = '255.255.255.255'
|
2019-02-16 09:24:31 +00:00
|
|
|
for policy in self.policies:
|
2019-03-03 19:26:25 +00:00
|
|
|
signatures.append(_policy_engine_task.s(self._user.pk, policy.pk.hex, **kwargs))
|
2019-02-16 09:24:31 +00:00
|
|
|
self._group = group(signatures)()
|
|
|
|
return self
|
|
|
|
|
|
|
|
@property
|
|
|
|
def result(self):
|
|
|
|
"""Get policy-checking result"""
|
2019-02-26 14:40:58 +00:00
|
|
|
messages = []
|
2019-03-03 16:12:53 +00:00
|
|
|
for policy_action, policy_result, policy_message in self._group.get():
|
|
|
|
passing = (policy_action == Policy.ACTION_ALLOW and policy_result) or \
|
|
|
|
(policy_action == Policy.ACTION_DENY and not policy_result)
|
|
|
|
if policy_message:
|
2019-02-26 14:40:58 +00:00
|
|
|
messages.append(policy_message)
|
2019-03-03 16:12:53 +00:00
|
|
|
if not passing:
|
2019-02-26 14:40:58 +00:00
|
|
|
return False, messages
|
|
|
|
return True, messages
|