authentik.core.setup.signals

 1from os import getenv
 2
 3from django.dispatch import receiver
 4from structlog.stdlib import get_logger
 5
 6from authentik.blueprints.models import BlueprintInstance
 7from authentik.blueprints.v1.importer import Importer
 8from authentik.core.apps import Setup
 9from authentik.root.signals import post_startup
10from authentik.tenants.models import Tenant
11
12BOOTSTRAP_BLUEPRINT = "system/bootstrap.yaml"
13
14LOGGER = get_logger()
15
16
17@receiver(post_startup)
18def post_startup_setup_bootstrap(sender, **_):
19    if (
20        not getenv("AUTHENTIK_BOOTSTRAP_PASSWORD")
21        and not getenv("AUTHENTIK_BOOTSTRAP_PASSWORD_HASH")
22        and not getenv("AUTHENTIK_BOOTSTRAP_TOKEN")
23    ):
24        return
25    LOGGER.info("Configuring authentik through bootstrap environment variables")
26    content = BlueprintInstance(path=BOOTSTRAP_BLUEPRINT).retrieve()
27    # If we have bootstrap credentials set, run bootstrap tasks outside of main server
28    # sync, so that we can sure the first start actually has working bootstrap
29    # credentials
30    for tenant in Tenant.objects.filter(ready=True):
31        if Setup.get(tenant=tenant):
32            LOGGER.info("Tenant is already setup, skipping", tenant=tenant.schema_name)
33            continue
34        with tenant:
35            importer = Importer.from_string(content)
36            valid, logs = importer.validate()
37            if not valid:
38                LOGGER.warning("Blueprint invalid", tenant=tenant.schema_name)
39                for log in logs:
40                    log.log()
41            importer.apply()
42            Setup.set(True, tenant=tenant)
BOOTSTRAP_BLUEPRINT = 'system/bootstrap.yaml'
LOGGER = <BoundLoggerLazyProxy(logger=None, wrapper_class=None, processors=None, context_class=None, initial_values={}, logger_factory_args=())>
@receiver(post_startup)
def post_startup_setup_bootstrap(sender, **_):
18@receiver(post_startup)
19def post_startup_setup_bootstrap(sender, **_):
20    if (
21        not getenv("AUTHENTIK_BOOTSTRAP_PASSWORD")
22        and not getenv("AUTHENTIK_BOOTSTRAP_PASSWORD_HASH")
23        and not getenv("AUTHENTIK_BOOTSTRAP_TOKEN")
24    ):
25        return
26    LOGGER.info("Configuring authentik through bootstrap environment variables")
27    content = BlueprintInstance(path=BOOTSTRAP_BLUEPRINT).retrieve()
28    # If we have bootstrap credentials set, run bootstrap tasks outside of main server
29    # sync, so that we can sure the first start actually has working bootstrap
30    # credentials
31    for tenant in Tenant.objects.filter(ready=True):
32        if Setup.get(tenant=tenant):
33            LOGGER.info("Tenant is already setup, skipping", tenant=tenant.schema_name)
34            continue
35        with tenant:
36            importer = Importer.from_string(content)
37            valid, logs = importer.validate()
38            if not valid:
39                LOGGER.warning("Blueprint invalid", tenant=tenant.schema_name)
40                for log in logs:
41                    log.log()
42            importer.apply()
43            Setup.set(True, tenant=tenant)