authentik.outposts.signals
authentik outpost signals
1"""authentik outpost signals""" 2 3from django.core.cache import cache 4from django.db.models.signals import m2m_changed, post_save, pre_delete, pre_save 5from django.dispatch import receiver 6from structlog.stdlib import get_logger 7 8from authentik.brands.models import Brand 9from authentik.core.models import AuthenticatedSession, Provider 10from authentik.core.signals import impersonation_changed 11from authentik.crypto.models import CertificateKeyPair 12from authentik.outposts.models import Outpost, OutpostModel, OutpostServiceConnection 13from authentik.outposts.tasks import ( 14 CACHE_KEY_OUTPOST_DOWN, 15 outpost_controller, 16 outpost_send_update, 17 outpost_session_end, 18) 19 20LOGGER = get_logger() 21 22 23@receiver(pre_save, sender=Outpost) 24def outpost_pre_save(sender, instance: Outpost, **_): 25 """Pre-save checks for an outpost, if the name or config.kubernetes_namespace changes, 26 we call down and then wait for the up after save""" 27 old_instances = Outpost.objects.filter(pk=instance.pk) 28 if not old_instances.exists(): 29 return 30 old_instance = old_instances.first() 31 dirty = False 32 # Name changes the deployment name, need to recreate 33 dirty += old_instance.name != instance.name 34 # namespace requires re-create 35 dirty += old_instance.config.kubernetes_namespace != instance.config.kubernetes_namespace 36 if bool(dirty): 37 LOGGER.info("Outpost needs re-deployment due to changes", instance=instance) 38 cache.set(CACHE_KEY_OUTPOST_DOWN % instance.pk.hex, old_instance) 39 outpost_controller.send_with_options( 40 args=(instance.pk.hex,), 41 kwargs={"action": "down", "from_cache": True}, 42 rel_obj=instance, 43 uid=instance.name, 44 ) 45 46 47@receiver(m2m_changed, sender=Outpost.providers.through) 48def outpost_m2m_changed(sender, instance: Outpost | Provider, action: str, **_): 49 """Update outpost on m2m change, when providers are added or removed""" 50 if action not in ["post_add", "post_remove", "post_clear"]: 51 return 52 if isinstance(instance, Outpost): 53 # Rebuild permissions when providers change 54 LOGGER.debug("Rebuilding outpost service account permissions", outpost=instance) 55 instance.build_user_permissions(instance.user) 56 outpost_controller.send_with_options( 57 args=(instance.pk,), 58 rel_obj=instance.service_connection, 59 uid=instance.name, 60 ) 61 outpost_send_update.send_with_options( 62 args=(instance.pk,), 63 rel_obj=instance, 64 uid=instance.name, 65 ) 66 elif isinstance(instance, OutpostModel): 67 for outpost in instance.outpost_set.all(): 68 outpost_controller.send_with_options( 69 args=(instance.pk,), 70 rel_obj=instance.service_connection, 71 uid=instance.name, 72 ) 73 outpost_send_update.send_with_options( 74 args=(outpost.pk,), 75 rel_obj=outpost, 76 uid=outpost.name, 77 ) 78 79 80@receiver(post_save, sender=Outpost) 81def outpost_post_save(sender, instance: Outpost, created: bool, **_): 82 if created: 83 LOGGER.info("New outpost saved, ensuring initial token and user are created") 84 _ = instance.token 85 outpost_controller.send_with_options( 86 args=(instance.pk,), 87 rel_obj=instance.service_connection, 88 uid=instance.name, 89 ) 90 outpost_send_update.send_with_options( 91 args=(instance.pk,), 92 rel_obj=instance, 93 uid=instance.name, 94 ) 95 96 97def outpost_related_post_save(sender, instance: OutpostServiceConnection | OutpostModel, **_): 98 for outpost in instance.outpost_set.all(): 99 # Rebuild permissions in case provider's required objects changed 100 if isinstance(instance, OutpostModel): 101 LOGGER.info( 102 "Provider changed, rebuilding permissions and sending update", 103 outpost=outpost.name, 104 provider=instance.name if hasattr(instance, "name") else str(instance), 105 ) 106 outpost.build_user_permissions(outpost.user) 107 LOGGER.debug("Sending update to outpost", outpost=outpost.name, trigger="provider_change") 108 outpost_send_update.send_with_options( 109 args=(outpost.pk,), 110 rel_obj=outpost, 111 uid=outpost.name, 112 ) 113 114 115post_save.connect(outpost_related_post_save, sender=OutpostServiceConnection, weak=False) 116for subclass in OutpostModel.__subclasses__(): 117 post_save.connect(outpost_related_post_save, sender=subclass, weak=False) 118 119 120def outpost_reverse_related_post_save(sender, instance: CertificateKeyPair | Brand, **_): 121 for field in instance._meta.get_fields(): 122 # Each field is checked if it has a `related_model` attribute (when ForeignKeys or M2Ms) 123 # are used, and if it has a value 124 if not hasattr(field, "related_model"): 125 continue 126 if not field.related_model: 127 continue 128 if not issubclass(field.related_model, OutpostModel): 129 continue 130 131 field_name = f"{field.name}_set" 132 if not hasattr(instance, field_name): 133 continue 134 135 LOGGER.debug("triggering outpost update from field", field=field.name) 136 # Because the Outpost Model has an M2M to Provider, 137 # we have to iterate over the entire QS 138 for reverse in getattr(instance, field_name).all(): 139 if isinstance(reverse, OutpostModel): 140 for outpost in reverse.outpost_set.all(): 141 outpost_send_update.send_with_options( 142 args=(outpost.pk,), 143 rel_obj=outpost, 144 uid=outpost.name, 145 ) 146 147 148post_save.connect(outpost_reverse_related_post_save, sender=Brand, weak=False) 149post_save.connect(outpost_reverse_related_post_save, sender=CertificateKeyPair, weak=False) 150 151 152@receiver(pre_delete, sender=Outpost) 153def outpost_pre_delete_cleanup(sender, instance: Outpost, **_): 154 """Ensure that Outpost's user is deleted (which will delete the token through cascade)""" 155 instance.user.delete() 156 cache.set(CACHE_KEY_OUTPOST_DOWN % instance.pk.hex, instance) 157 outpost_controller.send_with_options( 158 args=(instance.pk.hex,), 159 kwargs={"action": "down", "from_cache": True}, 160 uid=instance.name, 161 ) 162 163 164@receiver(pre_delete, sender=AuthenticatedSession) 165def outpost_logout_revoke(sender: type[AuthenticatedSession], instance: AuthenticatedSession, **_): 166 """Catch logout by expiring sessions being deleted""" 167 if Outpost.objects.exists(): 168 outpost_session_end.send(instance.session.session_key) 169 170 171@receiver(impersonation_changed) 172def outpost_impersonation_revoke(sender, session_key: str, **_): 173 """Reauthorize outpost sessions after the browser's effective identity changes.""" 174 if Outpost.objects.exists(): 175 outpost_session_end.send(session_key)
LOGGER =
<BoundLoggerLazyProxy(logger=None, wrapper_class=None, processors=None, context_class=None, initial_values={}, logger_factory_args=())>
@receiver(pre_save, sender=Outpost)
def
outpost_pre_save(sender, instance: authentik.outposts.models.Outpost, **_):
24@receiver(pre_save, sender=Outpost) 25def outpost_pre_save(sender, instance: Outpost, **_): 26 """Pre-save checks for an outpost, if the name or config.kubernetes_namespace changes, 27 we call down and then wait for the up after save""" 28 old_instances = Outpost.objects.filter(pk=instance.pk) 29 if not old_instances.exists(): 30 return 31 old_instance = old_instances.first() 32 dirty = False 33 # Name changes the deployment name, need to recreate 34 dirty += old_instance.name != instance.name 35 # namespace requires re-create 36 dirty += old_instance.config.kubernetes_namespace != instance.config.kubernetes_namespace 37 if bool(dirty): 38 LOGGER.info("Outpost needs re-deployment due to changes", instance=instance) 39 cache.set(CACHE_KEY_OUTPOST_DOWN % instance.pk.hex, old_instance) 40 outpost_controller.send_with_options( 41 args=(instance.pk.hex,), 42 kwargs={"action": "down", "from_cache": True}, 43 rel_obj=instance, 44 uid=instance.name, 45 )
Pre-save checks for an outpost, if the name or config.kubernetes_namespace changes, we call down and then wait for the up after save
@receiver(m2m_changed, sender=Outpost.providers.through)
def
outpost_m2m_changed( sender, instance: authentik.outposts.models.Outpost | authentik.core.models.Provider, action: str, **_):
48@receiver(m2m_changed, sender=Outpost.providers.through) 49def outpost_m2m_changed(sender, instance: Outpost | Provider, action: str, **_): 50 """Update outpost on m2m change, when providers are added or removed""" 51 if action not in ["post_add", "post_remove", "post_clear"]: 52 return 53 if isinstance(instance, Outpost): 54 # Rebuild permissions when providers change 55 LOGGER.debug("Rebuilding outpost service account permissions", outpost=instance) 56 instance.build_user_permissions(instance.user) 57 outpost_controller.send_with_options( 58 args=(instance.pk,), 59 rel_obj=instance.service_connection, 60 uid=instance.name, 61 ) 62 outpost_send_update.send_with_options( 63 args=(instance.pk,), 64 rel_obj=instance, 65 uid=instance.name, 66 ) 67 elif isinstance(instance, OutpostModel): 68 for outpost in instance.outpost_set.all(): 69 outpost_controller.send_with_options( 70 args=(instance.pk,), 71 rel_obj=instance.service_connection, 72 uid=instance.name, 73 ) 74 outpost_send_update.send_with_options( 75 args=(outpost.pk,), 76 rel_obj=outpost, 77 uid=outpost.name, 78 )
Update outpost on m2m change, when providers are added or removed
@receiver(post_save, sender=Outpost)
def
outpost_post_save( sender, instance: authentik.outposts.models.Outpost, created: bool, **_):
81@receiver(post_save, sender=Outpost) 82def outpost_post_save(sender, instance: Outpost, created: bool, **_): 83 if created: 84 LOGGER.info("New outpost saved, ensuring initial token and user are created") 85 _ = instance.token 86 outpost_controller.send_with_options( 87 args=(instance.pk,), 88 rel_obj=instance.service_connection, 89 uid=instance.name, 90 ) 91 outpost_send_update.send_with_options( 92 args=(instance.pk,), 93 rel_obj=instance, 94 uid=instance.name, 95 )
@receiver(pre_delete, sender=Outpost)
def
outpost_pre_delete_cleanup(sender, instance: authentik.outposts.models.Outpost, **_):
153@receiver(pre_delete, sender=Outpost) 154def outpost_pre_delete_cleanup(sender, instance: Outpost, **_): 155 """Ensure that Outpost's user is deleted (which will delete the token through cascade)""" 156 instance.user.delete() 157 cache.set(CACHE_KEY_OUTPOST_DOWN % instance.pk.hex, instance) 158 outpost_controller.send_with_options( 159 args=(instance.pk.hex,), 160 kwargs={"action": "down", "from_cache": True}, 161 uid=instance.name, 162 )
Ensure that Outpost's user is deleted (which will delete the token through cascade)
@receiver(pre_delete, sender=AuthenticatedSession)
def
outpost_logout_revoke( sender: type[authentik.core.models.AuthenticatedSession], instance: authentik.core.models.AuthenticatedSession, **_):
165@receiver(pre_delete, sender=AuthenticatedSession) 166def outpost_logout_revoke(sender: type[AuthenticatedSession], instance: AuthenticatedSession, **_): 167 """Catch logout by expiring sessions being deleted""" 168 if Outpost.objects.exists(): 169 outpost_session_end.send(instance.session.session_key)
Catch logout by expiring sessions being deleted
@receiver(impersonation_changed)
def
outpost_impersonation_revoke(sender, session_key: str, **_):
172@receiver(impersonation_changed) 173def outpost_impersonation_revoke(sender, session_key: str, **_): 174 """Reauthorize outpost sessions after the browser's effective identity changes.""" 175 if Outpost.objects.exists(): 176 outpost_session_end.send(session_key)
Reauthorize outpost sessions after the browser's effective identity changes.