authentik.admin.apps
authentik admin app config
1"""authentik admin app config""" 2 3from django.db import DEFAULT_DB_ALIAS 4from django.db.models.signals import post_migrate 5from prometheus_client import Info 6 7from authentik.blueprints.apps import ManagedAppConfig 8from authentik.lib.config import CONFIG 9from authentik.lib.utils.time import fqdn_rand 10from authentik.outposts.apps import MANAGED_OUTPOST 11from authentik.tasks.schedules.common import ScheduleSpec 12 13PROM_INFO = Info("authentik_version", "Currently running authentik version") 14 15 16def ensure_system_settings(*args, using=DEFAULT_DB_ALIAS, **kwargs): 17 """Make sure the system settings exist""" 18 from authentik.admin.models import SystemSettings 19 20 SystemSettings.objects.using(using).get_or_create(pk=True) 21 22 23class AuthentikAdminConfig(ManagedAppConfig): 24 """authentik admin app config""" 25 26 name = "authentik.admin" 27 label = "authentik_admin" 28 verbose_name = "authentik Admin" 29 default = True 30 31 @ManagedAppConfig.reconcile 32 def system_settings(self): 33 """Make sure the system settings exist, especially after a migration""" 34 post_migrate.connect(ensure_system_settings) 35 ensure_system_settings() 36 self._backfill_base_url() 37 38 def _backfill_base_url(self): 39 """Backfill base_url when it hasn't been set yet. Sources: AUTHENTIK_WEB__BASE_URL config 40 value, then the embedded outpost's configured host.""" 41 from django.core.exceptions import ValidationError 42 43 from authentik.admin.models import SystemSettings 44 from authentik.admin.utils import get_system_settings, normalize_base_url 45 from authentik.outposts.models import Outpost 46 47 settings = get_system_settings() 48 if settings.base_url: 49 return # Already set 50 base_url = normalize_base_url(CONFIG.get("web.base_url", "")) 51 if not base_url: 52 outpost = Outpost.objects.filter(managed=MANAGED_OUTPOST).first() 53 if outpost: 54 base_url = normalize_base_url(outpost.config.authentik_host) 55 if base_url: 56 try: 57 SystemSettings._meta.get_field("base_url").run_validators(base_url) 58 except ValidationError: 59 self.logger.warning("Discarding invalid base_url", base_url=base_url) 60 base_url = "" 61 if not base_url: # No source available 62 return 63 SystemSettings.objects.filter(pk=settings.pk).update(base_url=base_url) 64 self.logger.info("Backfilled base_url", base_url=base_url) 65 66 @ManagedAppConfig.reconcile 67 def clear_update_notifications(self): 68 """Clear update notifications on startup if the notification was for the version 69 we're running now.""" 70 from packaging.version import parse 71 72 from authentik.admin.tasks import LOCAL_VERSION 73 from authentik.events.models import EventAction, Notification 74 75 for notification in Notification.objects.filter(event__action=EventAction.UPDATE_AVAILABLE): 76 if "new_version" not in notification.event.context: 77 continue 78 notification_version = notification.event.context["new_version"] 79 if LOCAL_VERSION >= parse(notification_version): 80 notification.delete() 81 82 @property 83 def schedule_specs(self) -> list[ScheduleSpec]: 84 from authentik.admin.tasks import update_latest_version 85 86 return [ 87 ScheduleSpec( 88 actor=update_latest_version, 89 crontab=f"{fqdn_rand('admin_latest_version')} * * * *", 90 paused=CONFIG.get_bool("disable_update_check"), 91 ), 92 ]
PROM_INFO =
prometheus_client.metrics.Info(authentik_version)
def
ensure_system_settings(*args, using='default', **kwargs):
17def ensure_system_settings(*args, using=DEFAULT_DB_ALIAS, **kwargs): 18 """Make sure the system settings exist""" 19 from authentik.admin.models import SystemSettings 20 21 SystemSettings.objects.using(using).get_or_create(pk=True)
Make sure the system settings exist
24class AuthentikAdminConfig(ManagedAppConfig): 25 """authentik admin app config""" 26 27 name = "authentik.admin" 28 label = "authentik_admin" 29 verbose_name = "authentik Admin" 30 default = True 31 32 @ManagedAppConfig.reconcile 33 def system_settings(self): 34 """Make sure the system settings exist, especially after a migration""" 35 post_migrate.connect(ensure_system_settings) 36 ensure_system_settings() 37 self._backfill_base_url() 38 39 def _backfill_base_url(self): 40 """Backfill base_url when it hasn't been set yet. Sources: AUTHENTIK_WEB__BASE_URL config 41 value, then the embedded outpost's configured host.""" 42 from django.core.exceptions import ValidationError 43 44 from authentik.admin.models import SystemSettings 45 from authentik.admin.utils import get_system_settings, normalize_base_url 46 from authentik.outposts.models import Outpost 47 48 settings = get_system_settings() 49 if settings.base_url: 50 return # Already set 51 base_url = normalize_base_url(CONFIG.get("web.base_url", "")) 52 if not base_url: 53 outpost = Outpost.objects.filter(managed=MANAGED_OUTPOST).first() 54 if outpost: 55 base_url = normalize_base_url(outpost.config.authentik_host) 56 if base_url: 57 try: 58 SystemSettings._meta.get_field("base_url").run_validators(base_url) 59 except ValidationError: 60 self.logger.warning("Discarding invalid base_url", base_url=base_url) 61 base_url = "" 62 if not base_url: # No source available 63 return 64 SystemSettings.objects.filter(pk=settings.pk).update(base_url=base_url) 65 self.logger.info("Backfilled base_url", base_url=base_url) 66 67 @ManagedAppConfig.reconcile 68 def clear_update_notifications(self): 69 """Clear update notifications on startup if the notification was for the version 70 we're running now.""" 71 from packaging.version import parse 72 73 from authentik.admin.tasks import LOCAL_VERSION 74 from authentik.events.models import EventAction, Notification 75 76 for notification in Notification.objects.filter(event__action=EventAction.UPDATE_AVAILABLE): 77 if "new_version" not in notification.event.context: 78 continue 79 notification_version = notification.event.context["new_version"] 80 if LOCAL_VERSION >= parse(notification_version): 81 notification.delete() 82 83 @property 84 def schedule_specs(self) -> list[ScheduleSpec]: 85 from authentik.admin.tasks import update_latest_version 86 87 return [ 88 ScheduleSpec( 89 actor=update_latest_version, 90 crontab=f"{fqdn_rand('admin_latest_version')} * * * *", 91 paused=CONFIG.get_bool("disable_update_check"), 92 ), 93 ]
authentik admin app config
name =
'authentik.admin'
@ManagedAppConfig.reconcile
def
system_settings(self):
32 @ManagedAppConfig.reconcile 33 def system_settings(self): 34 """Make sure the system settings exist, especially after a migration""" 35 post_migrate.connect(ensure_system_settings) 36 ensure_system_settings() 37 self._backfill_base_url()
Make sure the system settings exist, especially after a migration
@ManagedAppConfig.reconcile
def
clear_update_notifications(self):
67 @ManagedAppConfig.reconcile 68 def clear_update_notifications(self): 69 """Clear update notifications on startup if the notification was for the version 70 we're running now.""" 71 from packaging.version import parse 72 73 from authentik.admin.tasks import LOCAL_VERSION 74 from authentik.events.models import EventAction, Notification 75 76 for notification in Notification.objects.filter(event__action=EventAction.UPDATE_AVAILABLE): 77 if "new_version" not in notification.event.context: 78 continue 79 notification_version = notification.event.context["new_version"] 80 if LOCAL_VERSION >= parse(notification_version): 81 notification.delete()
Clear update notifications on startup if the notification was for the version we're running now.
schedule_specs: list[authentik.tasks.schedules.common.ScheduleSpec]
83 @property 84 def schedule_specs(self) -> list[ScheduleSpec]: 85 from authentik.admin.tasks import update_latest_version 86 87 return [ 88 ScheduleSpec( 89 actor=update_latest_version, 90 crontab=f"{fqdn_rand('admin_latest_version')} * * * *", 91 paused=CONFIG.get_bool("disable_update_check"), 92 ), 93 ]
Get a list of schedule specs that must exist