authentik.core.setup.signals

 1from os import getenv
 2
 3from django.core.exceptions import ImproperlyConfigured
 4from django.dispatch import receiver
 5from rest_framework.exceptions import ValidationError
 6from structlog.stdlib import get_logger
 7
 8from authentik.admin.utils import get_system_settings
 9from authentik.blueprints.models import BlueprintInstance
10from authentik.blueprints.v1.importer import Importer
11from authentik.core.apps import Setup
12from authentik.lib.validators import PasswordHashValidator
13from authentik.root.signals import post_startup
14
15BOOTSTRAP_BLUEPRINT = "system/bootstrap.yaml"
16BASE_URL_REQUIRED = (
17    "No base URL is configured. Set AUTHENTIK_WEB__BASE_URL to the URL under which "
18    "authentik is reachable, for example https://authentik.company. See "
19    "https://docs.goauthentik.io/install-config/configuration/#authentik_web__base_url"
20)
21
22LOGGER = get_logger()
23
24
25@receiver(post_startup)
26def post_startup_setup_bootstrap(sender, **_):
27    if (
28        not getenv("AUTHENTIK_BOOTSTRAP_PASSWORD")
29        and not getenv("AUTHENTIK_BOOTSTRAP_PASSWORD_HASH")
30        and not getenv("AUTHENTIK_BOOTSTRAP_TOKEN")
31    ):
32        return
33    LOGGER.info("Configuring authentik through bootstrap environment variables")
34    content = BlueprintInstance(path=BOOTSTRAP_BLUEPRINT).retrieve()
35    # If we have bootstrap credentials set, run bootstrap tasks outside of main server
36    # sync, so that we can sure the first start actually has working bootstrap
37    # credentials
38    if Setup.get():
39        LOGGER.info("Instance is already setup, skipping")
40        return
41    if password_hash := getenv("AUTHENTIK_BOOTSTRAP_PASSWORD_HASH"):
42        try:
43            PasswordHashValidator()(password_hash)
44        except ValidationError as exc:
45            raise ImproperlyConfigured(
46                "AUTHENTIK_BOOTSTRAP_PASSWORD_HASH does not match authentik's current "
47                "password hashing settings. Generate a new hash with authentik's current "
48                "settings. See https://docs.goauthentik.io/core/password-hashes/."
49            ) from exc
50    importer = Importer.from_string(content)
51    valid, logs = importer.validate()
52    if not valid:
53        LOGGER.warning("Blueprint invalid")
54        for log in logs:
55            log.log()
56        return
57    if not importer.apply():
58        LOGGER.warning("Failed to apply bootstrap blueprint")
59        return
60    Setup.set(True)
61
62
63@receiver(post_startup)
64def post_startup_require_base_url(sender, **_):
65    """Refuse to start an instance that has been set up without a base URL"""
66    if Setup.get() and not get_system_settings().base_url:
67        raise ImproperlyConfigured(BASE_URL_REQUIRED)
BOOTSTRAP_BLUEPRINT = 'system/bootstrap.yaml'
BASE_URL_REQUIRED = 'No base URL is configured. Set AUTHENTIK_WEB__BASE_URL to the URL under which authentik is reachable, for example https://authentik.company. See https://docs.goauthentik.io/install-config/configuration/#authentik_web__base_url'
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, **_):
26@receiver(post_startup)
27def post_startup_setup_bootstrap(sender, **_):
28    if (
29        not getenv("AUTHENTIK_BOOTSTRAP_PASSWORD")
30        and not getenv("AUTHENTIK_BOOTSTRAP_PASSWORD_HASH")
31        and not getenv("AUTHENTIK_BOOTSTRAP_TOKEN")
32    ):
33        return
34    LOGGER.info("Configuring authentik through bootstrap environment variables")
35    content = BlueprintInstance(path=BOOTSTRAP_BLUEPRINT).retrieve()
36    # If we have bootstrap credentials set, run bootstrap tasks outside of main server
37    # sync, so that we can sure the first start actually has working bootstrap
38    # credentials
39    if Setup.get():
40        LOGGER.info("Instance is already setup, skipping")
41        return
42    if password_hash := getenv("AUTHENTIK_BOOTSTRAP_PASSWORD_HASH"):
43        try:
44            PasswordHashValidator()(password_hash)
45        except ValidationError as exc:
46            raise ImproperlyConfigured(
47                "AUTHENTIK_BOOTSTRAP_PASSWORD_HASH does not match authentik's current "
48                "password hashing settings. Generate a new hash with authentik's current "
49                "settings. See https://docs.goauthentik.io/core/password-hashes/."
50            ) from exc
51    importer = Importer.from_string(content)
52    valid, logs = importer.validate()
53    if not valid:
54        LOGGER.warning("Blueprint invalid")
55        for log in logs:
56            log.log()
57        return
58    if not importer.apply():
59        LOGGER.warning("Failed to apply bootstrap blueprint")
60        return
61    Setup.set(True)
@receiver(post_startup)
def post_startup_require_base_url(sender, **_):
64@receiver(post_startup)
65def post_startup_require_base_url(sender, **_):
66    """Refuse to start an instance that has been set up without a base URL"""
67    if Setup.get() and not get_system_settings().base_url:
68        raise ImproperlyConfigured(BASE_URL_REQUIRED)

Refuse to start an instance that has been set up without a base URL