2019-10-01 08:17:39 +00:00
|
|
|
"""passbook policy engine"""
|
|
|
|
from multiprocessing import Pipe
|
|
|
|
from multiprocessing.connection import Connection
|
2019-10-04 07:27:29 +00:00
|
|
|
from typing import List, Tuple, Tuple
|
2019-10-01 08:17:39 +00:00
|
|
|
|
|
|
|
from django.core.cache import cache
|
|
|
|
from django.http import HttpRequest
|
|
|
|
from structlog import get_logger
|
|
|
|
|
2019-10-03 08:45:31 +00:00
|
|
|
from passbook.core.models import Policy, User
|
2019-10-01 08:17:39 +00:00
|
|
|
from passbook.policy.task import PolicyTask
|
2019-10-03 08:45:31 +00:00
|
|
|
from passbook.policy.struct import PolicyResult, PolicyRequest
|
2019-10-01 08:17:39 +00:00
|
|
|
|
|
|
|
LOGGER = get_logger()
|
|
|
|
|
|
|
|
def _cache_key(policy, user):
|
2019-10-04 07:27:29 +00:00
|
|
|
return f"policy_{policy.pk}#{user.pk}"
|
2019-10-01 08:17:39 +00:00
|
|
|
|
|
|
|
class PolicyEngine:
|
|
|
|
"""Orchestrate policy checking, launch tasks and return result"""
|
|
|
|
|
|
|
|
policies: List[Policy] = []
|
|
|
|
__request: HttpRequest
|
|
|
|
__user: User
|
|
|
|
|
|
|
|
__proc_list: List[Tuple[Connection, PolicyTask]] = []
|
|
|
|
|
|
|
|
def __init__(self, policies, user: User = None, request: HttpRequest = None):
|
|
|
|
self.policies = policies
|
|
|
|
self.__request = request
|
|
|
|
self.__user = user
|
|
|
|
|
|
|
|
def for_user(self, user: User) -> 'PolicyEngine':
|
|
|
|
"""Check policies for user"""
|
|
|
|
self.__user = user
|
|
|
|
return self
|
|
|
|
|
|
|
|
def with_request(self, request: HttpRequest) -> 'PolicyEngine':
|
|
|
|
"""Set request"""
|
|
|
|
self.__request = request
|
|
|
|
return self
|
|
|
|
|
|
|
|
def build(self) -> 'PolicyEngine':
|
|
|
|
"""Build task group"""
|
|
|
|
if not self.__user:
|
|
|
|
raise ValueError("User not set.")
|
|
|
|
cached_policies = []
|
2019-10-03 08:45:31 +00:00
|
|
|
request = PolicyRequest(self.__user)
|
|
|
|
request.http_request = self.__request
|
2019-10-01 08:17:39 +00:00
|
|
|
for policy in self.policies:
|
|
|
|
cached_policy = cache.get(_cache_key(policy, self.__user), None)
|
|
|
|
if cached_policy:
|
2019-10-04 07:27:29 +00:00
|
|
|
LOGGER.debug("Taking result from cache", policy=policy.pk.hex)
|
2019-10-01 08:17:39 +00:00
|
|
|
cached_policies.append(cached_policy)
|
|
|
|
else:
|
2019-10-02 20:28:58 +00:00
|
|
|
LOGGER.debug("Looking up real class of policy...")
|
|
|
|
# TODO: Rewrite this to lookup all policies at once
|
2019-10-03 08:45:31 +00:00
|
|
|
policy = Policy.objects.get_subclass(pk=policy.pk)
|
2019-10-04 07:27:29 +00:00
|
|
|
LOGGER.debug("Evaluating policy", policy=policy.pk.hex)
|
2019-10-01 08:17:39 +00:00
|
|
|
our_end, task_end = Pipe(False)
|
|
|
|
task = PolicyTask()
|
|
|
|
task.ret = task_end
|
2019-10-03 08:45:31 +00:00
|
|
|
task.request = request
|
2019-10-01 08:17:39 +00:00
|
|
|
task.policy = policy
|
2019-10-04 07:27:29 +00:00
|
|
|
LOGGER.debug("Starting Process", class_name=task.__class__.__name__)
|
2019-10-01 08:17:39 +00:00
|
|
|
task.start()
|
|
|
|
self.__proc_list.append((our_end, task))
|
|
|
|
# If all policies are cached, we have an empty list here.
|
|
|
|
if self.__proc_list:
|
|
|
|
for _, running_proc in self.__proc_list:
|
|
|
|
running_proc.join()
|
|
|
|
return self
|
|
|
|
|
|
|
|
@property
|
2019-10-04 07:27:29 +00:00
|
|
|
def result(self) -> Tuple[bool, List[str]]:
|
2019-10-01 08:17:39 +00:00
|
|
|
"""Get policy-checking result"""
|
|
|
|
messages: List[str] = []
|
|
|
|
for our_end, _ in self.__proc_list:
|
2019-10-04 07:27:29 +00:00
|
|
|
policy_result = our_end.recv()
|
2019-10-01 08:17:39 +00:00
|
|
|
# passing = (policy_action == Policy.ACTION_ALLOW and policy_result) or \
|
|
|
|
# (policy_action == Policy.ACTION_DENY and not policy_result)
|
|
|
|
LOGGER.debug('Result=%r => %r', policy_result, policy_result.passing)
|
|
|
|
if policy_result.messages:
|
|
|
|
messages += policy_result.messages
|
|
|
|
if not policy_result.passing:
|
|
|
|
return False, messages
|
|
|
|
return True, messages
|
|
|
|
|
|
|
|
@property
|
2019-10-04 07:27:29 +00:00
|
|
|
def passing(self) -> bool:
|
2019-10-01 08:17:39 +00:00
|
|
|
"""Only get true/false if user passes"""
|
|
|
|
return self.result[0]
|