authentik.policies.signals
authentik policy signals
1"""authentik policy signals""" 2 3from django.core.cache import cache 4from django.db.models.signals import post_save, pre_delete 5from django.dispatch import receiver 6from structlog.stdlib import get_logger 7 8from authentik.core.api.applications import user_app_cache_key 9from authentik.core.models import Group, User 10from authentik.policies.apps import GAUGE_POLICIES_CACHED 11from authentik.policies.models import Policy, PolicyBinding, PolicyBindingModel 12from authentik.policies.types import CACHE_PREFIX 13from authentik.root.monitoring import monitoring_set 14 15LOGGER = get_logger() 16 17 18@receiver(monitoring_set) 19def monitoring_set_policies(sender, **kwargs): 20 """set policy gauges""" 21 GAUGE_POLICIES_CACHED.set(cache.keys_count(f"{CACHE_PREFIX}*")) 22 23 24@receiver(post_save, sender=Policy) 25@receiver(post_save, sender=PolicyBinding) 26@receiver(post_save, sender=PolicyBindingModel) 27@receiver(post_save, sender=Group) 28@receiver(post_save, sender=User) 29@receiver(pre_delete, sender=PolicyBinding) 30def invalidate_policy_cache(sender, instance, update_fields=None, **_): 31 """Invalidate Policy cache when a policy or binding is updated. 32 33 Skips when the save touched only ``last_login`` — Django's auth flow runs 34 ``user.save(update_fields=["last_login"])`` on every successful login, and 35 the broad invalidation below would otherwise issue a full-table cache 36 scan on every login. ``last_login`` doesn't affect policy evaluation or 37 application access, so there's nothing to invalidate. 38 """ 39 if sender == User and update_fields and set(update_fields) <= {"last_login"}: 40 return 41 42 if sender in (Policy, PolicyBinding): 43 bindings = PolicyBinding.objects.filter(policy=instance) if sender == Policy else [instance] 44 total = 0 45 for binding in bindings: 46 prefix = f"{CACHE_PREFIX}{binding.policy_binding_uuid.hex}_*" 47 keys = cache.keys(prefix) or [] 48 total += len(keys) 49 cache.delete_many(keys) 50 LOGGER.debug("Invalidating policy cache", instance=instance, keys=total) 51 # Also delete user application cache 52 keys = cache.keys(user_app_cache_key("*")) or [] 53 cache.delete_many(keys)
LOGGER =
<BoundLoggerLazyProxy(logger=None, wrapper_class=None, processors=None, context_class=None, initial_values={}, logger_factory_args=())>
@receiver(monitoring_set)
def
monitoring_set_policies(sender, **kwargs):
19@receiver(monitoring_set) 20def monitoring_set_policies(sender, **kwargs): 21 """set policy gauges""" 22 GAUGE_POLICIES_CACHED.set(cache.keys_count(f"{CACHE_PREFIX}*"))
set policy gauges
@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)
@receiver(pre_delete, sender=PolicyBinding)
def
invalidate_policy_cache(sender, instance, update_fields=None, **_):
25@receiver(post_save, sender=Policy) 26@receiver(post_save, sender=PolicyBinding) 27@receiver(post_save, sender=PolicyBindingModel) 28@receiver(post_save, sender=Group) 29@receiver(post_save, sender=User) 30@receiver(pre_delete, sender=PolicyBinding) 31def invalidate_policy_cache(sender, instance, update_fields=None, **_): 32 """Invalidate Policy cache when a policy or binding is updated. 33 34 Skips when the save touched only ``last_login`` — Django's auth flow runs 35 ``user.save(update_fields=["last_login"])`` on every successful login, and 36 the broad invalidation below would otherwise issue a full-table cache 37 scan on every login. ``last_login`` doesn't affect policy evaluation or 38 application access, so there's nothing to invalidate. 39 """ 40 if sender == User and update_fields and set(update_fields) <= {"last_login"}: 41 return 42 43 if sender in (Policy, PolicyBinding): 44 bindings = PolicyBinding.objects.filter(policy=instance) if sender == Policy else [instance] 45 total = 0 46 for binding in bindings: 47 prefix = f"{CACHE_PREFIX}{binding.policy_binding_uuid.hex}_*" 48 keys = cache.keys(prefix) or [] 49 total += len(keys) 50 cache.delete_many(keys) 51 LOGGER.debug("Invalidating policy cache", instance=instance, keys=total) 52 # Also delete user application cache 53 keys = cache.keys(user_app_cache_key("*")) or [] 54 cache.delete_many(keys)
Invalidate Policy cache when a policy or binding is updated.
Skips when the save touched only last_login — Django's auth flow runs
user.save(update_fields=["last_login"]) on every successful login, and
the broad invalidation below would otherwise issue a full-table cache
scan on every login. last_login doesn't affect policy evaluation or
application access, so there's nothing to invalidate.