core: fix saving of policy not correctly clearing it's cache
This commit is contained in:
		@ -5,16 +5,19 @@ from multiprocessing.connection import Connection
 | 
			
		||||
from django.core.cache import cache
 | 
			
		||||
from structlog import get_logger
 | 
			
		||||
 | 
			
		||||
from passbook.core.models import Policy
 | 
			
		||||
from passbook.core.models import Policy, User
 | 
			
		||||
from passbook.policies.exceptions import PolicyException
 | 
			
		||||
from passbook.policies.types import PolicyRequest, PolicyResult
 | 
			
		||||
 | 
			
		||||
LOGGER = get_logger()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def cache_key(policy, user):
 | 
			
		||||
def cache_key(policy: Policy, user: User = None) -> str:
 | 
			
		||||
    """Generate Cache key for policy"""
 | 
			
		||||
    return f"policy_{policy.pk}#{user.pk}"
 | 
			
		||||
    prefix = f"policy_{policy.pk}"
 | 
			
		||||
    if user:
 | 
			
		||||
        prefix += f"#{user.pk}"
 | 
			
		||||
    return prefix
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class PolicyProcess(Process):
 | 
			
		||||
@ -33,7 +36,7 @@ class PolicyProcess(Process):
 | 
			
		||||
    def run(self):
 | 
			
		||||
        """Task wrapper to run policy checking"""
 | 
			
		||||
        LOGGER.debug(
 | 
			
		||||
            "Running policy",
 | 
			
		||||
            "P_ENG(proc): Running policy",
 | 
			
		||||
            policy=self.policy,
 | 
			
		||||
            user=self.request.user,
 | 
			
		||||
            process="PolicyProcess",
 | 
			
		||||
@ -41,13 +44,13 @@ class PolicyProcess(Process):
 | 
			
		||||
        try:
 | 
			
		||||
            policy_result = self.policy.passes(self.request)
 | 
			
		||||
        except PolicyException as exc:
 | 
			
		||||
            LOGGER.debug(exc)
 | 
			
		||||
            LOGGER.debug("P_ENG(proc): error", exc=exc)
 | 
			
		||||
            policy_result = PolicyResult(False, str(exc))
 | 
			
		||||
        # Invert result if policy.negate is set
 | 
			
		||||
        if self.policy.negate:
 | 
			
		||||
            policy_result.passing = not policy_result.passing
 | 
			
		||||
        LOGGER.debug(
 | 
			
		||||
            "Got result",
 | 
			
		||||
            "P_ENG(proc): Finished",
 | 
			
		||||
            policy=self.policy,
 | 
			
		||||
            result=policy_result,
 | 
			
		||||
            process="PolicyProcess",
 | 
			
		||||
@ -56,5 +59,5 @@ class PolicyProcess(Process):
 | 
			
		||||
        )
 | 
			
		||||
        key = cache_key(self.policy, self.request.user)
 | 
			
		||||
        cache.set(key, policy_result)
 | 
			
		||||
        LOGGER.debug("Cached policy evaluation", key=key)
 | 
			
		||||
        LOGGER.debug("P_ENG(proc): Cached policy evaluation", key=key)
 | 
			
		||||
        self.connection.send(policy_result)
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user