policies: clear app cache when writing user, groups, policies (#5371)
Signed-off-by: Jens Langhammer <jens@goauthentik.io>
This commit is contained in:
parent
e05f028c0a
commit
4426cbec34
|
@ -5,7 +5,9 @@ from django.dispatch import receiver
|
||||||
from structlog.stdlib import get_logger
|
from structlog.stdlib import get_logger
|
||||||
|
|
||||||
from authentik.core.api.applications import user_app_cache_key
|
from authentik.core.api.applications import user_app_cache_key
|
||||||
|
from authentik.core.models import Group, User
|
||||||
from authentik.policies.apps import GAUGE_POLICIES_CACHED
|
from authentik.policies.apps import GAUGE_POLICIES_CACHED
|
||||||
|
from authentik.policies.models import Policy, PolicyBinding, PolicyBindingModel
|
||||||
from authentik.policies.types import CACHE_PREFIX
|
from authentik.policies.types import CACHE_PREFIX
|
||||||
from authentik.root.monitoring import monitoring_set
|
from authentik.root.monitoring import monitoring_set
|
||||||
|
|
||||||
|
@ -18,12 +20,14 @@ def monitoring_set_policies(sender, **kwargs):
|
||||||
GAUGE_POLICIES_CACHED.set(len(cache.keys(f"{CACHE_PREFIX}_*") or []))
|
GAUGE_POLICIES_CACHED.set(len(cache.keys(f"{CACHE_PREFIX}_*") or []))
|
||||||
|
|
||||||
|
|
||||||
@receiver(post_save)
|
@receiver(post_save, sender=Policy)
|
||||||
|
@receiver(post_save, sender=PolicyBinding)
|
||||||
|
@receiver(post_save, sender=PolicyBindingModel)
|
||||||
|
@receiver(post_save, sender=Group)
|
||||||
|
@receiver(post_save, sender=User)
|
||||||
def invalidate_policy_cache(sender, instance, **_):
|
def invalidate_policy_cache(sender, instance, **_):
|
||||||
"""Invalidate Policy cache when policy is updated"""
|
"""Invalidate Policy cache when policy is updated"""
|
||||||
from authentik.policies.models import Policy, PolicyBinding
|
if sender == Policy:
|
||||||
|
|
||||||
if isinstance(instance, Policy):
|
|
||||||
total = 0
|
total = 0
|
||||||
for binding in PolicyBinding.objects.filter(policy=instance):
|
for binding in PolicyBinding.objects.filter(policy=instance):
|
||||||
prefix = f"{CACHE_PREFIX}{binding.policy_binding_uuid.hex}_{binding.policy.pk.hex}*"
|
prefix = f"{CACHE_PREFIX}{binding.policy_binding_uuid.hex}_{binding.policy.pk.hex}*"
|
||||||
|
|
Reference in New Issue