2019-10-01 09:17:39 +01:00
|
|
|
"""passbook policy task"""
|
|
|
|
from multiprocessing import Process
|
|
|
|
from multiprocessing.connection import Connection
|
2020-05-09 19:54:11 +01:00
|
|
|
from typing import Optional
|
2019-10-01 09:17:39 +01:00
|
|
|
|
2019-10-07 15:33:48 +01:00
|
|
|
from django.core.cache import cache
|
2019-10-01 09:24:10 +01:00
|
|
|
from structlog import get_logger
|
|
|
|
|
2019-10-07 15:33:48 +01:00
|
|
|
from passbook.policies.exceptions import PolicyException
|
2020-05-28 20:45:54 +01:00
|
|
|
from passbook.policies.models import PolicyBinding
|
2020-02-20 12:52:05 +00:00
|
|
|
from passbook.policies.types import PolicyRequest, PolicyResult
|
2019-10-01 09:17:39 +01:00
|
|
|
|
2019-10-04 09:22:06 +01:00
|
|
|
LOGGER = get_logger()
|
2019-10-01 09:17:39 +01:00
|
|
|
|
|
|
|
|
2020-05-24 01:06:54 +01:00
|
|
|
def cache_key(binding: PolicyBinding, request: PolicyRequest) -> str:
|
2019-10-07 15:33:48 +01:00
|
|
|
"""Generate Cache key for policy"""
|
2020-05-28 20:45:54 +01:00
|
|
|
prefix = f"policy_{binding.policy_binding_uuid.hex}_{binding.policy.pk.hex}"
|
2020-05-24 01:06:54 +01:00
|
|
|
if request.http_request:
|
|
|
|
prefix += f"_{request.http_request.session.session_key}"
|
|
|
|
if request.user:
|
|
|
|
prefix += f"#{request.user.pk}"
|
2020-02-24 12:15:27 +00:00
|
|
|
return prefix
|
2019-10-01 09:17:39 +01:00
|
|
|
|
2019-12-31 11:51:16 +00:00
|
|
|
|
2019-10-04 09:22:06 +01:00
|
|
|
class PolicyProcess(Process):
|
2019-10-01 09:17:39 +01:00
|
|
|
"""Evaluate a single policy within a seprate process"""
|
|
|
|
|
2019-10-04 12:44:26 +01:00
|
|
|
connection: Connection
|
2020-05-28 20:45:54 +01:00
|
|
|
binding: PolicyBinding
|
2019-10-03 09:45:31 +01:00
|
|
|
request: PolicyRequest
|
2019-10-01 09:17:39 +01:00
|
|
|
|
2020-05-28 20:45:54 +01:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
binding: PolicyBinding,
|
|
|
|
request: PolicyRequest,
|
|
|
|
connection: Optional[Connection],
|
|
|
|
):
|
2019-10-04 12:44:26 +01:00
|
|
|
super().__init__()
|
2020-05-28 20:45:54 +01:00
|
|
|
self.binding = binding
|
2019-10-04 12:44:26 +01:00
|
|
|
self.request = request
|
2020-06-05 11:00:27 +01:00
|
|
|
if not isinstance(self.request, PolicyRequest):
|
|
|
|
raise ValueError(f"{self.request} is not a Policy Request.")
|
2020-05-28 20:45:54 +01:00
|
|
|
if connection:
|
|
|
|
self.connection = connection
|
2019-10-04 12:44:26 +01:00
|
|
|
|
2020-05-28 20:45:54 +01:00
|
|
|
def execute(self) -> PolicyResult:
|
|
|
|
"""Run actual policy, returns result"""
|
2019-12-31 11:51:16 +00:00
|
|
|
LOGGER.debug(
|
2020-02-24 12:15:27 +00:00
|
|
|
"P_ENG(proc): Running policy",
|
2020-05-28 20:45:54 +01:00
|
|
|
policy=self.binding.policy,
|
2019-12-31 11:51:16 +00:00
|
|
|
user=self.request.user,
|
|
|
|
process="PolicyProcess",
|
|
|
|
)
|
2019-10-02 21:28:39 +01:00
|
|
|
try:
|
2020-05-28 20:45:54 +01:00
|
|
|
policy_result = self.binding.policy.passes(self.request)
|
2019-10-02 21:28:39 +01:00
|
|
|
except PolicyException as exc:
|
2020-02-24 12:15:27 +00:00
|
|
|
LOGGER.debug("P_ENG(proc): error", exc=exc)
|
2019-10-02 21:28:39 +01:00
|
|
|
policy_result = PolicyResult(False, str(exc))
|
2019-10-01 09:17:39 +01:00
|
|
|
# Invert result if policy.negate is set
|
2020-05-28 20:45:54 +01:00
|
|
|
if self.binding.negate:
|
2019-10-14 14:00:20 +01:00
|
|
|
policy_result.passing = not policy_result.passing
|
2019-12-31 11:51:16 +00:00
|
|
|
LOGGER.debug(
|
2020-02-24 12:15:27 +00:00
|
|
|
"P_ENG(proc): Finished",
|
2020-05-28 20:45:54 +01:00
|
|
|
policy=self.binding.policy,
|
2019-12-31 11:51:16 +00:00
|
|
|
result=policy_result,
|
|
|
|
process="PolicyProcess",
|
|
|
|
passing=policy_result.passing,
|
|
|
|
user=self.request.user,
|
|
|
|
)
|
2020-05-24 01:06:54 +01:00
|
|
|
key = cache_key(self.binding, self.request)
|
2019-10-07 15:33:48 +01:00
|
|
|
cache.set(key, policy_result)
|
2020-02-24 12:15:27 +00:00
|
|
|
LOGGER.debug("P_ENG(proc): Cached policy evaluation", key=key)
|
2020-05-28 20:45:54 +01:00
|
|
|
return policy_result
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
"""Task wrapper to run policy checking"""
|
|
|
|
self.connection.send(self.execute())
|