2019-10-01 08:17:39 +00:00
|
|
|
"""passbook policy engine"""
|
2020-02-17 14:20:30 +00:00
|
|
|
from multiprocessing import Pipe, set_start_method
|
2019-10-01 08:17:39 +00:00
|
|
|
from multiprocessing.connection import Connection
|
2020-09-09 15:20:37 +00:00
|
|
|
from typing import Iterator, List, Optional
|
2019-10-01 08:17:39 +00:00
|
|
|
|
|
|
|
from django.core.cache import cache
|
|
|
|
from django.http import HttpRequest
|
2020-09-06 14:12:17 +00:00
|
|
|
from sentry_sdk.hub import Hub
|
2020-08-20 18:39:21 +00:00
|
|
|
from sentry_sdk.tracing import Span
|
2019-10-01 08:17:39 +00:00
|
|
|
from structlog import get_logger
|
|
|
|
|
2020-05-16 16:07:00 +00:00
|
|
|
from passbook.core.models import User
|
2020-05-28 19:45:54 +00:00
|
|
|
from passbook.policies.models import Policy, PolicyBinding, PolicyBindingModel
|
2019-10-07 14:33:48 +00:00
|
|
|
from passbook.policies.process import PolicyProcess, cache_key
|
2020-02-20 12:52:05 +00:00
|
|
|
from passbook.policies.types import PolicyRequest, PolicyResult
|
2019-10-01 08:17:39 +00:00
|
|
|
|
|
|
|
LOGGER = get_logger()
|
2020-02-17 14:20:30 +00:00
|
|
|
# This is only really needed for macOS, because Python 3.8 changed the default to spawn
|
|
|
|
# spawn causes issues with objects that aren't picklable, and also the django setup
|
|
|
|
set_start_method("fork")
|
2019-10-14 13:00:20 +00:00
|
|
|
|
2020-02-17 14:37:51 +00:00
|
|
|
|
2019-10-04 11:44:26 +00:00
|
|
|
class PolicyProcessInfo:
|
2019-10-04 11:49:27 +00:00
|
|
|
"""Dataclass to hold all information and communication channels to a process"""
|
2019-10-04 11:44:26 +00:00
|
|
|
|
|
|
|
process: PolicyProcess
|
|
|
|
connection: Connection
|
2019-10-14 14:08:24 +00:00
|
|
|
result: Optional[PolicyResult]
|
2020-05-28 19:45:54 +00:00
|
|
|
binding: PolicyBinding
|
2019-10-04 11:44:26 +00:00
|
|
|
|
2020-05-28 19:45:54 +00:00
|
|
|
def __init__(
|
|
|
|
self, process: PolicyProcess, connection: Connection, binding: PolicyBinding
|
|
|
|
):
|
2019-10-04 11:44:26 +00:00
|
|
|
self.process = process
|
|
|
|
self.connection = connection
|
2020-05-28 19:45:54 +00:00
|
|
|
self.binding = binding
|
2019-10-14 14:08:24 +00:00
|
|
|
self.result = None
|
2019-10-04 11:44:26 +00:00
|
|
|
|
2019-12-31 11:51:16 +00:00
|
|
|
|
2019-10-01 08:17:39 +00:00
|
|
|
class PolicyEngine:
|
|
|
|
"""Orchestrate policy checking, launch tasks and return result"""
|
|
|
|
|
2020-09-09 15:20:37 +00:00
|
|
|
use_cache: bool
|
2019-10-28 16:40:57 +00:00
|
|
|
request: PolicyRequest
|
2019-10-01 08:17:39 +00:00
|
|
|
|
2020-05-28 19:45:54 +00:00
|
|
|
__pbm: PolicyBindingModel
|
2020-02-17 14:37:51 +00:00
|
|
|
__cached_policies: List[PolicyResult]
|
|
|
|
__processes: List[PolicyProcessInfo]
|
2019-10-01 08:17:39 +00:00
|
|
|
|
2020-05-28 19:45:54 +00:00
|
|
|
def __init__(
|
|
|
|
self, pbm: PolicyBindingModel, user: User, request: HttpRequest = None
|
|
|
|
):
|
|
|
|
if not isinstance(pbm, PolicyBindingModel):
|
|
|
|
raise ValueError(f"{pbm} is not instance of PolicyBindingModel")
|
|
|
|
self.__pbm = pbm
|
2019-10-28 16:40:57 +00:00
|
|
|
self.request = PolicyRequest(user)
|
|
|
|
if request:
|
|
|
|
self.request.http_request = request
|
2020-02-17 14:37:51 +00:00
|
|
|
self.__cached_policies = []
|
2019-10-04 11:44:26 +00:00
|
|
|
self.__processes = []
|
2020-09-09 15:20:37 +00:00
|
|
|
self.use_cache = True
|
2019-10-01 08:17:39 +00:00
|
|
|
|
2020-09-09 15:20:37 +00:00
|
|
|
def _iter_bindings(self) -> Iterator[PolicyBinding]:
|
2019-10-04 08:22:06 +00:00
|
|
|
"""Make sure all Policies are their respective classes"""
|
2020-05-28 19:45:54 +00:00
|
|
|
return PolicyBinding.objects.filter(target=self.__pbm, enabled=True).order_by(
|
|
|
|
"order"
|
2019-12-31 11:51:16 +00:00
|
|
|
)
|
2019-10-04 08:22:06 +00:00
|
|
|
|
2020-05-28 19:45:54 +00:00
|
|
|
def _check_policy_type(self, policy: Policy):
|
|
|
|
"""Check policy type, make sure it's not the root class as that has no logic implemented"""
|
2020-09-13 22:28:23 +00:00
|
|
|
# pyright: reportGeneralTypeIssues=false
|
2020-05-28 19:45:54 +00:00
|
|
|
if policy.__class__ == Policy:
|
|
|
|
raise TypeError(f"Policy '{policy}' is root type")
|
|
|
|
|
2019-12-31 11:51:16 +00:00
|
|
|
def build(self) -> "PolicyEngine":
|
2020-08-20 18:39:21 +00:00
|
|
|
"""Build wrapper which monitors performance"""
|
2020-09-06 14:12:17 +00:00
|
|
|
with Hub.current.start_span(op="policy.engine.build") as span:
|
2020-08-20 18:39:21 +00:00
|
|
|
span: Span
|
|
|
|
span.set_data("pbm", self.__pbm)
|
|
|
|
span.set_data("request", self.request)
|
|
|
|
for binding in self._iter_bindings():
|
|
|
|
self._check_policy_type(binding.policy)
|
|
|
|
key = cache_key(binding, self.request)
|
|
|
|
cached_policy = cache.get(key, None)
|
|
|
|
if cached_policy and self.use_cache:
|
|
|
|
LOGGER.debug(
|
|
|
|
"P_ENG: Taking result from cache",
|
|
|
|
policy=binding.policy,
|
|
|
|
cache_key=key,
|
|
|
|
)
|
|
|
|
self.__cached_policies.append(cached_policy)
|
|
|
|
continue
|
|
|
|
LOGGER.debug("P_ENG: Evaluating policy", policy=binding.policy)
|
|
|
|
our_end, task_end = Pipe(False)
|
|
|
|
task = PolicyProcess(binding, self.request, task_end)
|
|
|
|
LOGGER.debug("P_ENG: Starting Process", policy=binding.policy)
|
|
|
|
task.start()
|
|
|
|
self.__processes.append(
|
|
|
|
PolicyProcessInfo(process=task, connection=our_end, binding=binding)
|
2020-05-23 20:01:38 +00:00
|
|
|
)
|
2020-08-20 18:39:21 +00:00
|
|
|
# If all policies are cached, we have an empty list here.
|
|
|
|
for proc_info in self.__processes:
|
|
|
|
proc_info.process.join(proc_info.binding.timeout)
|
|
|
|
# Only call .recv() if no result is saved, otherwise we just deadlock here
|
|
|
|
if not proc_info.result:
|
|
|
|
proc_info.result = proc_info.connection.recv()
|
|
|
|
return self
|
2019-10-01 08:17:39 +00:00
|
|
|
|
|
|
|
@property
|
2020-05-28 19:45:54 +00:00
|
|
|
def result(self) -> PolicyResult:
|
2019-10-01 08:17:39 +00:00
|
|
|
"""Get policy-checking result"""
|
|
|
|
messages: List[str] = []
|
2020-02-17 14:37:51 +00:00
|
|
|
process_results: List[PolicyResult] = [
|
|
|
|
x.result for x in self.__processes if x.result
|
|
|
|
]
|
|
|
|
for result in process_results + self.__cached_policies:
|
2020-05-23 20:01:38 +00:00
|
|
|
LOGGER.debug(
|
|
|
|
"P_ENG: result", passing=result.passing, messages=result.messages
|
|
|
|
)
|
2020-02-17 14:37:51 +00:00
|
|
|
if result.messages:
|
|
|
|
messages += result.messages
|
|
|
|
if not result.passing:
|
2020-05-28 19:45:54 +00:00
|
|
|
return PolicyResult(False, *messages)
|
|
|
|
return PolicyResult(True, *messages)
|
2019-10-01 08:17:39 +00:00
|
|
|
|
|
|
|
@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"""
|
2020-05-28 19:45:54 +00:00
|
|
|
return self.result.passing
|