2019-10-01 08:17:39 +00:00
|
|
|
"""passbook policy task"""
|
|
|
|
from multiprocessing import Process
|
|
|
|
from multiprocessing.connection import Connection
|
|
|
|
|
2019-10-01 08:24:10 +00:00
|
|
|
from structlog import get_logger
|
|
|
|
|
2019-10-03 08:45:31 +00:00
|
|
|
from passbook.core.models import Policy
|
2019-10-02 20:28:39 +00:00
|
|
|
from passbook.policy.exceptions import PolicyException
|
2019-10-03 08:45:31 +00:00
|
|
|
from passbook.policy.struct 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
|
|
|
|
|
|
|
|
|
|
|
def _cache_key(policy, user):
|
|
|
|
return "policy_%s#%s" % (policy.uuid, user.pk)
|
|
|
|
|
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"""
|
|
|
|
|
|
|
|
ret: Connection
|
|
|
|
policy: Policy
|
2019-10-03 08:45:31 +00:00
|
|
|
request: PolicyRequest
|
2019-10-01 08:17:39 +00:00
|
|
|
|
|
|
|
def run(self):
|
|
|
|
"""Task wrapper to run policy checking"""
|
2019-10-04 08:22:06 +00:00
|
|
|
LOGGER.debug("Running policy", 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:
|
|
|
|
LOGGER.debug(exc)
|
|
|
|
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:
|
|
|
|
policy_result = not policy_result
|
2019-10-04 08:22:06 +00:00
|
|
|
LOGGER.debug("Got result", policy=self.policy, result=policy_result,
|
|
|
|
process="PolicyProcess")
|
2019-10-03 08:45:31 +00:00
|
|
|
# cache_key = _cache_key(self.policy, self.request.user)
|
2019-10-01 08:17:39 +00:00
|
|
|
# cache.set(cache_key, (self.policy.action, policy_result, message))
|
|
|
|
# LOGGER.debug("Cached entry as %s", cache_key)
|
|
|
|
self.ret.send(policy_result)
|
|
|
|
self.ret.close()
|