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