app_gw(minor): remove current implementation
This commit is contained in:
@ -1,7 +1,7 @@
|
||||
"""passbook policy engine"""
|
||||
from multiprocessing import Pipe
|
||||
from multiprocessing.connection import Connection
|
||||
from typing import List, Tuple
|
||||
from typing import List, Tuple, Tuple
|
||||
|
||||
from django.core.cache import cache
|
||||
from django.http import HttpRequest
|
||||
@ -14,14 +14,11 @@ from passbook.policy.struct import PolicyResult, PolicyRequest
|
||||
LOGGER = get_logger()
|
||||
|
||||
def _cache_key(policy, user):
|
||||
return "policy_%s#%s" % (policy.uuid, user.pk)
|
||||
return f"policy_{policy.pk}#{user.pk}"
|
||||
|
||||
class PolicyEngine:
|
||||
"""Orchestrate policy checking, launch tasks and return result"""
|
||||
|
||||
# __group = None
|
||||
# __cached = None
|
||||
|
||||
policies: List[Policy] = []
|
||||
__request: HttpRequest
|
||||
__user: User
|
||||
@ -53,19 +50,19 @@ class PolicyEngine:
|
||||
for policy in self.policies:
|
||||
cached_policy = cache.get(_cache_key(policy, self.__user), None)
|
||||
if cached_policy:
|
||||
LOGGER.debug("Taking result from cache for %s", policy.pk.hex)
|
||||
LOGGER.debug("Taking result from cache", policy=policy.pk.hex)
|
||||
cached_policies.append(cached_policy)
|
||||
else:
|
||||
LOGGER.debug("Looking up real class of policy...")
|
||||
# TODO: Rewrite this to lookup all policies at once
|
||||
policy = Policy.objects.get_subclass(pk=policy.pk)
|
||||
LOGGER.debug("Evaluating policy %s", policy.pk.hex)
|
||||
LOGGER.debug("Evaluating policy", policy=policy.pk.hex)
|
||||
our_end, task_end = Pipe(False)
|
||||
task = PolicyTask()
|
||||
task.ret = task_end
|
||||
task.request = request
|
||||
task.policy = policy
|
||||
LOGGER.debug("Starting Process %s", task.__class__.__name__)
|
||||
LOGGER.debug("Starting Process", class_name=task.__class__.__name__)
|
||||
task.start()
|
||||
self.__proc_list.append((our_end, task))
|
||||
# If all policies are cached, we have an empty list here.
|
||||
@ -75,13 +72,11 @@ class PolicyEngine:
|
||||
return self
|
||||
|
||||
@property
|
||||
def result(self):
|
||||
def result(self) -> Tuple[bool, List[str]]:
|
||||
"""Get policy-checking result"""
|
||||
results: List[PolicyResult] = []
|
||||
messages: List[str] = []
|
||||
for our_end, _ in self.__proc_list:
|
||||
results.append(our_end.recv())
|
||||
for policy_result in results:
|
||||
policy_result = our_end.recv()
|
||||
# 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)
|
||||
@ -92,6 +87,6 @@ class PolicyEngine:
|
||||
return True, messages
|
||||
|
||||
@property
|
||||
def passing(self):
|
||||
def passing(self) -> bool:
|
||||
"""Only get true/false if user passes"""
|
||||
return self.result[0]
|
||||
|
||||
Reference in New Issue
Block a user