root: simplify task signal imports (#8454)

* *: deduplicate boilerplate for importing related models

Signed-off-by: Jens Langhammer <jens@goauthentik.io>

* also auto-import .checks

Signed-off-by: Jens Langhammer <jens@goauthentik.io>

* fix error during prometheus metrics from #8435

Signed-off-by: Jens Langhammer <jens@goauthentik.io>

---------

Signed-off-by: Jens Langhammer <jens@goauthentik.io>
This commit is contained in:
Jens L
2024-02-08 13:44:33 +01:00
committed by GitHub
parent c90792d876
commit ebd05be2c4
24 changed files with 31 additions and 95 deletions

View File

@ -15,7 +15,3 @@ class AuthentikAdminConfig(ManagedAppConfig):
label = "authentik_admin" label = "authentik_admin"
verbose_name = "authentik Admin" verbose_name = "authentik Admin"
default = True default = True
def reconcile_global_load_admin_signals(self):
"""Load admin signals"""
self.import_module("authentik.admin.signals")

View File

@ -21,10 +21,27 @@ class ManagedAppConfig(AppConfig):
self.logger = get_logger().bind(app_name=app_name) self.logger = get_logger().bind(app_name=app_name)
def ready(self) -> None: def ready(self) -> None:
self.import_related()
self.reconcile_global() self.reconcile_global()
self.reconcile_tenant() self.reconcile_tenant()
return super().ready() return super().ready()
def import_related(self):
"""Automatically import related modules which rely on just being imported
to register themselves (mainly django signals and celery tasks)"""
def import_relative(rel_module: str):
try:
module_name = f"{self.name}.{rel_module}"
import_module(module_name)
self.logger.info("Imported related module", module=module_name)
except ModuleNotFoundError:
pass
import_relative("checks")
import_relative("tasks")
import_relative("signals")
def import_module(self, path: str): def import_module(self, path: str):
"""Load module""" """Load module"""
import_module(path) import_module(path)

View File

@ -14,10 +14,6 @@ class AuthentikCoreConfig(ManagedAppConfig):
mountpoint = "" mountpoint = ""
default = True default = True
def reconcile_global_load_core_signals(self):
"""Load core signals"""
self.import_module("authentik.core.signals")
def reconcile_global_debug_worker_hook(self): def reconcile_global_debug_worker_hook(self):
"""Dispatch startup tasks inline when debugging""" """Dispatch startup tasks inline when debugging"""
if settings.DEBUG: if settings.DEBUG:

View File

@ -17,10 +17,6 @@ class AuthentikCryptoConfig(ManagedAppConfig):
verbose_name = "authentik Crypto" verbose_name = "authentik Crypto"
default = True default = True
def reconcile_global_load_crypto_tasks(self):
"""Load crypto tasks"""
self.import_module("authentik.crypto.tasks")
def _create_update_cert(self): def _create_update_cert(self):
from authentik.crypto.builder import CertificateBuilder from authentik.crypto.builder import CertificateBuilder
from authentik.crypto.models import CertificateKeyPair from authentik.crypto.models import CertificateKeyPair

View File

@ -17,10 +17,6 @@ class AuthentikEnterpriseConfig(EnterpriseConfig):
verbose_name = "authentik Enterprise" verbose_name = "authentik Enterprise"
default = True default = True
def reconcile_global_load_enterprise_signals(self):
"""Load enterprise signals"""
self.import_module("authentik.enterprise.signals")
def enabled(self): def enabled(self):
"""Return true if enterprise is enabled and valid""" """Return true if enterprise is enabled and valid"""
return self.check_enabled() or settings.TEST return self.check_enabled() or settings.TEST

View File

@ -12,7 +12,3 @@ class AuthentikEnterpriseProviderRAC(EnterpriseConfig):
default = True default = True
mountpoint = "" mountpoint = ""
ws_mountpoint = "authentik.enterprise.providers.rac.urls" ws_mountpoint = "authentik.enterprise.providers.rac.urls"
def reconcile_global_load_rac_signals(self):
"""Load rac signals"""
self.import_module("authentik.enterprise.providers.rac.signals")

View File

@ -5,9 +5,9 @@ from celery.schedules import crontab
from authentik.lib.utils.time import fqdn_rand from authentik.lib.utils.time import fqdn_rand
CELERY_BEAT_SCHEDULE = { CELERY_BEAT_SCHEDULE = {
"enterprise_calculate_license": { "enterprise_update_usage": {
"task": "authentik.enterprise.tasks.calculate_license", "task": "authentik.enterprise.tasks.enterprise_update_usage",
"schedule": crontab(minute=fqdn_rand("calculate_license"), hour="*/2"), "schedule": crontab(minute=fqdn_rand("enterprise_update_usage"), hour="*/2"),
"options": {"queue": "authentik_scheduled"}, "options": {"queue": "authentik_scheduled"},
} }
} }

View File

@ -1,11 +1,12 @@
"""Enterprise tasks""" """Enterprise tasks"""
from authentik.enterprise.models import LicenseKey from authentik.enterprise.models import LicenseKey
from authentik.events.system_tasks import SystemTask from authentik.events.system_tasks import SystemTask, prefill_task
from authentik.root.celery import CELERY_APP from authentik.root.celery import CELERY_APP
@CELERY_APP.task(base=SystemTask) @CELERY_APP.task(bind=True, base=SystemTask)
def calculate_license(): @prefill_task
"""Calculate licensing status""" def enterprise_update_usage(self: SystemTask):
"""Update enterprise license status"""
LicenseKey.get_total().record_usage() LicenseKey.get_total().record_usage()

View File

@ -35,10 +35,6 @@ class AuthentikEventsConfig(ManagedAppConfig):
verbose_name = "authentik Events" verbose_name = "authentik Events"
default = True default = True
def reconcile_global_load_events_signals(self):
"""Load events signals"""
self.import_module("authentik.events.signals")
def reconcile_global_check_deprecations(self): def reconcile_global_check_deprecations(self):
"""Check for config deprecations""" """Check for config deprecations"""
from authentik.events.models import Event, EventAction from authentik.events.models import Event, EventAction

View File

@ -642,17 +642,16 @@ class SystemTask(SerializerModel, ExpiringModel):
def update_metrics(self): def update_metrics(self):
"""Update prometheus metrics""" """Update prometheus metrics"""
duration = max(self.finish_timestamp - self.start_timestamp, 0)
# TODO: Deprecated metric - remove in 2024.2 or later # TODO: Deprecated metric - remove in 2024.2 or later
GAUGE_TASKS.labels( GAUGE_TASKS.labels(
tenant=connection.schema_name, tenant=connection.schema_name,
task_name=self.name, task_name=self.name,
task_uid=self.uid or "", task_uid=self.uid or "",
status=self.status.lower(), status=self.status.lower(),
).set(duration) ).set(self.duration)
SYSTEM_TASK_TIME.labels( SYSTEM_TASK_TIME.labels(
tenant=connection.schema_name, tenant=connection.schema_name,
).observe(duration) ).observe(self.duration)
SYSTEM_TASK_STATUS.labels( SYSTEM_TASK_STATUS.labels(
tenant=connection.schema_name, tenant=connection.schema_name,
task_name=self.name, task_name=self.name,

View File

@ -31,10 +31,6 @@ class AuthentikFlowsConfig(ManagedAppConfig):
verbose_name = "authentik Flows" verbose_name = "authentik Flows"
default = True default = True
def reconcile_global_load_flows_signals(self):
"""Load flows signals"""
self.import_module("authentik.flows.signals")
def reconcile_global_load_stages(self): def reconcile_global_load_stages(self):
"""Ensure all stages are loaded""" """Ensure all stages are loaded"""
from authentik.flows.models import Stage from authentik.flows.models import Stage

View File

@ -30,10 +30,6 @@ class AuthentikOutpostConfig(ManagedAppConfig):
verbose_name = "authentik Outpost" verbose_name = "authentik Outpost"
default = True default = True
def reconcile_global_load_outposts_signals(self):
"""Load outposts signals"""
self.import_module("authentik.outposts.signals")
def reconcile_tenant_embedded_outpost(self): def reconcile_tenant_embedded_outpost(self):
"""Ensure embedded outpost""" """Ensure embedded outpost"""
from authentik.outposts.models import ( from authentik.outposts.models import (

View File

@ -35,7 +35,3 @@ class AuthentikPoliciesConfig(ManagedAppConfig):
label = "authentik_policies" label = "authentik_policies"
verbose_name = "authentik Policies" verbose_name = "authentik Policies"
default = True default = True
def reconcile_global_load_policies_signals(self):
"""Load policies signals"""
self.import_module("authentik.policies.signals")

View File

@ -12,11 +12,3 @@ class AuthentikPolicyReputationConfig(ManagedAppConfig):
label = "authentik_policies_reputation" label = "authentik_policies_reputation"
verbose_name = "authentik Policies.Reputation" verbose_name = "authentik Policies.Reputation"
default = True default = True
def reconcile_global_load_policies_reputation_signals(self):
"""Load policies.reputation signals"""
self.import_module("authentik.policies.reputation.signals")
def reconcile_global_load_policies_reputation_tasks(self):
"""Load policies.reputation tasks"""
self.import_module("authentik.policies.reputation.tasks")

View File

@ -10,7 +10,3 @@ class AuthentikProviderProxyConfig(ManagedAppConfig):
label = "authentik_providers_proxy" label = "authentik_providers_proxy"
verbose_name = "authentik Providers.Proxy" verbose_name = "authentik Providers.Proxy"
default = True default = True
def reconcile_global_load_providers_proxy_signals(self):
"""Load proxy signals"""
self.import_module("authentik.providers.proxy.signals")

View File

@ -10,7 +10,3 @@ class AuthentikProviderSCIMConfig(ManagedAppConfig):
label = "authentik_providers_scim" label = "authentik_providers_scim"
verbose_name = "authentik Providers.SCIM" verbose_name = "authentik Providers.SCIM"
default = True default = True
def reconcile_global_load_signals(self):
"""Load signals"""
self.import_module("authentik.providers.scim.signals")

View File

@ -10,7 +10,3 @@ class AuthentikRBACConfig(ManagedAppConfig):
label = "authentik_rbac" label = "authentik_rbac"
verbose_name = "authentik RBAC" verbose_name = "authentik RBAC"
default = True default = True
def reconcile_global_load_rbac_signals(self):
"""Load rbac signals"""
self.import_module("authentik.rbac.signals")

View File

@ -69,7 +69,6 @@ TENANT_APPS = [
"authentik.admin", "authentik.admin",
"authentik.api", "authentik.api",
"authentik.crypto", "authentik.crypto",
"authentik.events",
"authentik.flows", "authentik.flows",
"authentik.outposts", "authentik.outposts",
"authentik.policies.dummy", "authentik.policies.dummy",
@ -509,5 +508,9 @@ try:
except ImportError: except ImportError:
pass pass
# Import events after other apps since it relies on tasks and other things from all apps
# being imported for @prefill_task
TENANT_APPS.append("authentik.events")
SHARED_APPS = list(OrderedDict.fromkeys(SHARED_APPS + TENANT_APPS)) SHARED_APPS = list(OrderedDict.fromkeys(SHARED_APPS + TENANT_APPS))
INSTALLED_APPS = list(OrderedDict.fromkeys(SHARED_APPS + TENANT_APPS)) INSTALLED_APPS = list(OrderedDict.fromkeys(SHARED_APPS + TENANT_APPS))

View File

@ -10,7 +10,3 @@ class AuthentikSourceLDAPConfig(ManagedAppConfig):
label = "authentik_sources_ldap" label = "authentik_sources_ldap"
verbose_name = "authentik Sources.LDAP" verbose_name = "authentik Sources.LDAP"
default = True default = True
def reconcile_global_load_sources_ldap_signals(self):
"""Load sources.ldap signals"""
self.import_module("authentik.sources.ldap.signals")

View File

@ -11,7 +11,3 @@ class AuthentikSourceSAMLConfig(ManagedAppConfig):
verbose_name = "authentik Sources.SAML" verbose_name = "authentik Sources.SAML"
mountpoint = "source/saml/" mountpoint = "source/saml/"
default = True default = True
def reconcile_global_load_sources_saml_signals(self):
"""Load sources.saml signals"""
self.import_module("authentik.sources.saml.signals")

View File

@ -10,7 +10,3 @@ class AuthentikStageAuthenticatorDuoConfig(ManagedAppConfig):
label = "authentik_stages_authenticator_duo" label = "authentik_stages_authenticator_duo"
verbose_name = "authentik Stages.Authenticator.Duo" verbose_name = "authentik Stages.Authenticator.Duo"
default = True default = True
def reconcile_global_load_tasks(self):
"""Load tasks"""
self.import_module("authentik.stages.authenticator_duo.tasks")

View File

@ -10,7 +10,3 @@ class AuthentikStageAuthenticatorStaticConfig(ManagedAppConfig):
label = "authentik_stages_authenticator_static" label = "authentik_stages_authenticator_static"
verbose_name = "authentik Stages.Authenticator.Static" verbose_name = "authentik Stages.Authenticator.Static"
default = True default = True
def reconcile_global_load_stages_authenticator_static_signals(self):
"""Load stages.authenticator_static signals"""
self.import_module("authentik.stages.authenticator_static.signals")

View File

@ -1,11 +1,7 @@
"""authentik email stage config""" """authentik email stage config"""
from structlog.stdlib import get_logger
from authentik.blueprints.apps import ManagedAppConfig from authentik.blueprints.apps import ManagedAppConfig
LOGGER = get_logger()
class AuthentikStageEmailConfig(ManagedAppConfig): class AuthentikStageEmailConfig(ManagedAppConfig):
"""authentik email stage config""" """authentik email stage config"""
@ -14,7 +10,3 @@ class AuthentikStageEmailConfig(ManagedAppConfig):
label = "authentik_stages_email" label = "authentik_stages_email"
verbose_name = "authentik Stages.Email" verbose_name = "authentik Stages.Email"
default = True default = True
def reconcile_global_load_stages_emails_tasks(self):
"""Load stages.emails tasks"""
self.import_module("authentik.stages.email.tasks")

View File

@ -28,10 +28,6 @@ class AuthentikTenantsConfig(ManagedAppConfig):
verbose_name = "authentik Tenants" verbose_name = "authentik Tenants"
default = True default = True
def reconcile_global_load_checks(self):
"""Load tenant checks"""
self.import_module("authentik.tenants.checks")
def reconcile_global_default_tenant(self): def reconcile_global_default_tenant(self):
"""Make sure default tenant exists, especially after a migration""" """Make sure default tenant exists, especially after a migration"""
post_migrate.connect(ensure_default_tenant) post_migrate.connect(ensure_default_tenant)