authentik.outposts.apps

authentik outposts app config

  1"""authentik outposts app config"""
  2
  3from urllib.parse import urlparse
  4
  5from prometheus_client import Gauge
  6from structlog.stdlib import get_logger
  7
  8from authentik.blueprints.apps import ManagedAppConfig
  9from authentik.lib.config import CONFIG
 10from authentik.lib.utils.time import fqdn_rand
 11from authentik.tasks.schedules.common import ScheduleSpec
 12
 13LOGGER = get_logger()
 14
 15GAUGE_OUTPOSTS_CONNECTED = Gauge(
 16    "authentik_outposts_connected",
 17    "Currently connected outposts",
 18    ["outpost", "uid", "expected"],
 19    multiprocess_mode="livesum",
 20)
 21GAUGE_OUTPOSTS_LAST_UPDATE = Gauge(
 22    "authentik_outposts_last_update",
 23    "Last update from any outpost",
 24    ["outpost", "uid", "version"],
 25    multiprocess_mode="livemax",
 26)
 27MANAGED_OUTPOST = "goauthentik.io/outposts/embedded"
 28MANAGED_OUTPOST_NAME = "authentik Embedded Outpost"
 29
 30
 31def set_outpost_host(base_url: str):
 32    """Set the outpost's authentik_host from base_url"""
 33    from authentik.outposts.models import Outpost
 34
 35    if not base_url:
 36        return
 37
 38    outpost = Outpost.objects.filter(managed=MANAGED_OUTPOST).first()
 39    if not outpost:
 40        return
 41
 42    config = outpost.config
 43    if urlparse(config.authentik_host).scheme in ["http", "https"]:
 44        return
 45
 46    config.authentik_host = base_url
 47    outpost.config = config
 48    outpost.save()
 49    LOGGER.info("Set outpost host", authentik_host=base_url)
 50
 51
 52class AuthentikOutpostConfig(ManagedAppConfig):
 53    """authentik outposts app config"""
 54
 55    name = "authentik.outposts"
 56    label = "authentik_outposts"
 57    verbose_name = "authentik Outpost"
 58    default = True
 59
 60    @ManagedAppConfig.reconcile
 61    def embedded_outpost(self):
 62        """Ensure embedded outpost"""
 63        from authentik.admin.utils import get_system_settings
 64        from authentik.outposts.models import (
 65            DockerServiceConnection,
 66            KubernetesServiceConnection,
 67            Outpost,
 68            OutpostType,
 69        )
 70
 71        if not CONFIG.get_bool("outposts.disable_embedded_outpost", False):
 72            if outpost := Outpost.objects.filter(name=MANAGED_OUTPOST_NAME, managed="").first():
 73                outpost.managed = MANAGED_OUTPOST
 74                outpost.save()
 75                return
 76            outpost, created = Outpost.objects.update_or_create(
 77                defaults={
 78                    "type": OutpostType.PROXY,
 79                    "name": MANAGED_OUTPOST_NAME,
 80                },
 81                managed=MANAGED_OUTPOST,
 82            )
 83            if created:
 84                if KubernetesServiceConnection.objects.exists():
 85                    outpost.service_connection = KubernetesServiceConnection.objects.first()
 86                elif DockerServiceConnection.objects.exists():
 87                    outpost.service_connection = DockerServiceConnection.objects.first()
 88                outpost.save()
 89            # Note: Because this needs a setting from `authentik.admin`, this only works if
 90            # `authentik.outposts` comes *after* `authentik.admin` in INSTALLED_APPS. It's unclear
 91            # if (or how) that can be enforced.
 92            set_outpost_host(get_system_settings().base_url)
 93        else:
 94            Outpost.objects.filter(managed=MANAGED_OUTPOST).delete()
 95
 96    @property
 97    def schedule_specs(self) -> list[ScheduleSpec]:
 98        from authentik.outposts.tasks import outpost_connection_discovery, outpost_token_ensurer
 99
100        return [
101            ScheduleSpec(
102                actor=outpost_token_ensurer,
103                crontab=f"{fqdn_rand('outpost_token_ensurer')} */8 * * *",
104            ),
105            ScheduleSpec(
106                actor=outpost_connection_discovery,
107                crontab=f"{fqdn_rand('outpost_connection_discovery')} */8 * * *",
108                send_on_startup=True,
109                paused=not CONFIG.get_bool("outposts.discover"),
110            ),
111        ]
LOGGER = <BoundLoggerLazyProxy(logger=None, wrapper_class=None, processors=None, context_class=None, initial_values={}, logger_factory_args=())>
GAUGE_OUTPOSTS_CONNECTED = prometheus_client.metrics.Gauge(authentik_outposts_connected)
GAUGE_OUTPOSTS_LAST_UPDATE = prometheus_client.metrics.Gauge(authentik_outposts_last_update)
MANAGED_OUTPOST = 'goauthentik.io/outposts/embedded'
MANAGED_OUTPOST_NAME = 'authentik Embedded Outpost'
def set_outpost_host(base_url: str):
32def set_outpost_host(base_url: str):
33    """Set the outpost's authentik_host from base_url"""
34    from authentik.outposts.models import Outpost
35
36    if not base_url:
37        return
38
39    outpost = Outpost.objects.filter(managed=MANAGED_OUTPOST).first()
40    if not outpost:
41        return
42
43    config = outpost.config
44    if urlparse(config.authentik_host).scheme in ["http", "https"]:
45        return
46
47    config.authentik_host = base_url
48    outpost.config = config
49    outpost.save()
50    LOGGER.info("Set outpost host", authentik_host=base_url)

Set the outpost's authentik_host from base_url

class AuthentikOutpostConfig(authentik.blueprints.apps.ManagedAppConfig):
 53class AuthentikOutpostConfig(ManagedAppConfig):
 54    """authentik outposts app config"""
 55
 56    name = "authentik.outposts"
 57    label = "authentik_outposts"
 58    verbose_name = "authentik Outpost"
 59    default = True
 60
 61    @ManagedAppConfig.reconcile
 62    def embedded_outpost(self):
 63        """Ensure embedded outpost"""
 64        from authentik.admin.utils import get_system_settings
 65        from authentik.outposts.models import (
 66            DockerServiceConnection,
 67            KubernetesServiceConnection,
 68            Outpost,
 69            OutpostType,
 70        )
 71
 72        if not CONFIG.get_bool("outposts.disable_embedded_outpost", False):
 73            if outpost := Outpost.objects.filter(name=MANAGED_OUTPOST_NAME, managed="").first():
 74                outpost.managed = MANAGED_OUTPOST
 75                outpost.save()
 76                return
 77            outpost, created = Outpost.objects.update_or_create(
 78                defaults={
 79                    "type": OutpostType.PROXY,
 80                    "name": MANAGED_OUTPOST_NAME,
 81                },
 82                managed=MANAGED_OUTPOST,
 83            )
 84            if created:
 85                if KubernetesServiceConnection.objects.exists():
 86                    outpost.service_connection = KubernetesServiceConnection.objects.first()
 87                elif DockerServiceConnection.objects.exists():
 88                    outpost.service_connection = DockerServiceConnection.objects.first()
 89                outpost.save()
 90            # Note: Because this needs a setting from `authentik.admin`, this only works if
 91            # `authentik.outposts` comes *after* `authentik.admin` in INSTALLED_APPS. It's unclear
 92            # if (or how) that can be enforced.
 93            set_outpost_host(get_system_settings().base_url)
 94        else:
 95            Outpost.objects.filter(managed=MANAGED_OUTPOST).delete()
 96
 97    @property
 98    def schedule_specs(self) -> list[ScheduleSpec]:
 99        from authentik.outposts.tasks import outpost_connection_discovery, outpost_token_ensurer
100
101        return [
102            ScheduleSpec(
103                actor=outpost_token_ensurer,
104                crontab=f"{fqdn_rand('outpost_token_ensurer')} */8 * * *",
105            ),
106            ScheduleSpec(
107                actor=outpost_connection_discovery,
108                crontab=f"{fqdn_rand('outpost_connection_discovery')} */8 * * *",
109                send_on_startup=True,
110                paused=not CONFIG.get_bool("outposts.discover"),
111            ),
112        ]

authentik outposts app config

label = 'authentik_outposts'
verbose_name = 'authentik Outpost'
default = True
@ManagedAppConfig.reconcile
def embedded_outpost(self):
61    @ManagedAppConfig.reconcile
62    def embedded_outpost(self):
63        """Ensure embedded outpost"""
64        from authentik.admin.utils import get_system_settings
65        from authentik.outposts.models import (
66            DockerServiceConnection,
67            KubernetesServiceConnection,
68            Outpost,
69            OutpostType,
70        )
71
72        if not CONFIG.get_bool("outposts.disable_embedded_outpost", False):
73            if outpost := Outpost.objects.filter(name=MANAGED_OUTPOST_NAME, managed="").first():
74                outpost.managed = MANAGED_OUTPOST
75                outpost.save()
76                return
77            outpost, created = Outpost.objects.update_or_create(
78                defaults={
79                    "type": OutpostType.PROXY,
80                    "name": MANAGED_OUTPOST_NAME,
81                },
82                managed=MANAGED_OUTPOST,
83            )
84            if created:
85                if KubernetesServiceConnection.objects.exists():
86                    outpost.service_connection = KubernetesServiceConnection.objects.first()
87                elif DockerServiceConnection.objects.exists():
88                    outpost.service_connection = DockerServiceConnection.objects.first()
89                outpost.save()
90            # Note: Because this needs a setting from `authentik.admin`, this only works if
91            # `authentik.outposts` comes *after* `authentik.admin` in INSTALLED_APPS. It's unclear
92            # if (or how) that can be enforced.
93            set_outpost_host(get_system_settings().base_url)
94        else:
95            Outpost.objects.filter(managed=MANAGED_OUTPOST).delete()

Ensure embedded outpost

schedule_specs: list[authentik.tasks.schedules.common.ScheduleSpec]
 97    @property
 98    def schedule_specs(self) -> list[ScheduleSpec]:
 99        from authentik.outposts.tasks import outpost_connection_discovery, outpost_token_ensurer
100
101        return [
102            ScheduleSpec(
103                actor=outpost_token_ensurer,
104                crontab=f"{fqdn_rand('outpost_token_ensurer')} */8 * * *",
105            ),
106            ScheduleSpec(
107                actor=outpost_connection_discovery,
108                crontab=f"{fqdn_rand('outpost_connection_discovery')} */8 * * *",
109                send_on_startup=True,
110                paused=not CONFIG.get_bool("outposts.discover"),
111            ),
112        ]

Get a list of schedule specs that must exist