authentik.admin.tasks
authentik admin tasks
1"""authentik admin tasks""" 2 3from enum import StrEnum 4 5from django.core.cache import cache 6from django.utils.translation import gettext_lazy as _ 7from dramatiq import actor 8from packaging.version import parse 9from pydantic import BaseModel, Field, HttpUrl, ValidationError 10from requests import RequestException 11from structlog.stdlib import get_logger 12 13from authentik import authentik_build_hash, authentik_version 14from authentik.admin.apps import PROM_INFO 15from authentik.events.models import Event, EventAction 16from authentik.lib.config import CONFIG 17from authentik.lib.utils.http import get_http_session 18from authentik.tasks.middleware import CurrentTask 19 20LOGGER = get_logger() 21VERSION_CACHE_KEY = "goauthentik.io/admin/latest_version" 22VERSION_CACHE_TIMEOUT = 8 * 60 * 60 # 8 hours 23LOCAL_VERSION = parse(authentik_version()) 24VERSION_URL = "https://version.goauthentik.io/versions/authentik/latest.json" 25 26 27def _set_prom_info(): 28 """Set prometheus info for version""" 29 PROM_INFO.info( 30 { 31 "version": authentik_version(), 32 "latest": UpdateVersionInfo.get_cached().version, 33 "build_hash": authentik_build_hash(), 34 } 35 ) 36 37 38class UpdateVersionReason(StrEnum): 39 """Reason why an authentik update was released""" 40 41 bugfix = "bugfix" 42 feature = "feature" 43 security = "security" 44 other = "other" 45 46 47class UpdateVersionInfo(BaseModel): 48 version: str 49 changelog: str 50 changelog_url: HttpUrl 51 reason: UpdateVersionReason 52 valid: bool = Field(default=True) 53 54 @staticmethod 55 def get_invalid() -> UpdateVersionInfo: 56 return UpdateVersionInfo( 57 version="0.0.0", 58 changelog="", 59 changelog_url="https://docs.goauthentik.io", 60 reason=UpdateVersionReason.other, 61 valid=False, 62 ) 63 64 @staticmethod 65 def get_cached() -> UpdateVersionInfo: 66 return cache.get(VERSION_CACHE_KEY, UpdateVersionInfo.get_invalid()) 67 68 @staticmethod 69 def cache_invalid(): 70 cache.set( 71 VERSION_CACHE_KEY, 72 UpdateVersionInfo( 73 version="0.0.0", 74 changelog="", 75 changelog_url="", 76 reason=UpdateVersionReason.other, 77 valid=False, 78 ), 79 VERSION_CACHE_TIMEOUT, 80 ) 81 82 83@actor(description=_("Update latest version info.")) 84def update_latest_version(): 85 self = CurrentTask.get_task() 86 if CONFIG.get_bool("disable_update_check"): 87 UpdateVersionInfo.cache_invalid() 88 self.info("Version check disabled.") 89 return 90 try: 91 response = get_http_session().get(VERSION_URL) 92 response.raise_for_status() 93 data = response.json() 94 upstream_version = UpdateVersionInfo.model_validate(data.get("stable", {})) 95 cache.set(VERSION_CACHE_KEY, upstream_version, VERSION_CACHE_TIMEOUT) 96 self.info("Successfully updated latest Version") 97 _set_prom_info() 98 # Check if upstream version is newer than what we're running, 99 # and if no event exists yet, create one. 100 if LOCAL_VERSION < parse(upstream_version.version): 101 # Event has already been created, don't create duplicate 102 if Event.objects.filter( 103 action=EventAction.UPDATE_AVAILABLE, 104 context__new_version=upstream_version.version, 105 ).exists(): 106 return 107 Event.new( 108 EventAction.UPDATE_AVAILABLE, 109 message=_("New version {version} available!").format( 110 version=upstream_version.version 111 ), 112 new_version=upstream_version.version, 113 changelog=upstream_version.changelog_url, 114 reason=upstream_version.reason, 115 ).save() 116 except (RequestException, IndexError, ValidationError) as exc: 117 UpdateVersionInfo.cache_invalid() 118 raise exc
LOGGER =
<BoundLoggerLazyProxy(logger=None, wrapper_class=None, processors=None, context_class=None, initial_values={}, logger_factory_args=())>
VERSION_CACHE_KEY =
'goauthentik.io/admin/latest_version'
VERSION_CACHE_TIMEOUT =
28800
LOCAL_VERSION =
<Version('2027.2.0rc1')>
VERSION_URL =
'https://version.goauthentik.io/versions/authentik/latest.json'
class
UpdateVersionReason(enum.StrEnum):
39class UpdateVersionReason(StrEnum): 40 """Reason why an authentik update was released""" 41 42 bugfix = "bugfix" 43 feature = "feature" 44 security = "security" 45 other = "other"
Reason why an authentik update was released
bugfix =
<UpdateVersionReason.bugfix: 'bugfix'>
feature =
<UpdateVersionReason.feature: 'feature'>
security =
<UpdateVersionReason.security: 'security'>
other =
<UpdateVersionReason.other: 'other'>
class
UpdateVersionInfo(pydantic.main.BaseModel):
48class UpdateVersionInfo(BaseModel): 49 version: str 50 changelog: str 51 changelog_url: HttpUrl 52 reason: UpdateVersionReason 53 valid: bool = Field(default=True) 54 55 @staticmethod 56 def get_invalid() -> UpdateVersionInfo: 57 return UpdateVersionInfo( 58 version="0.0.0", 59 changelog="", 60 changelog_url="https://docs.goauthentik.io", 61 reason=UpdateVersionReason.other, 62 valid=False, 63 ) 64 65 @staticmethod 66 def get_cached() -> UpdateVersionInfo: 67 return cache.get(VERSION_CACHE_KEY, UpdateVersionInfo.get_invalid()) 68 69 @staticmethod 70 def cache_invalid(): 71 cache.set( 72 VERSION_CACHE_KEY, 73 UpdateVersionInfo( 74 version="0.0.0", 75 changelog="", 76 changelog_url="", 77 reason=UpdateVersionReason.other, 78 valid=False, 79 ), 80 VERSION_CACHE_TIMEOUT, 81 )
!!! abstract "Usage Documentation" Models
A base class for creating Pydantic models.
Attributes:
__class_vars__: The names of the class variables defined on the model.
__private_attributes__: Metadata about the private attributes of the model.
__signature__: The synthesized __init__ [Signature][inspect.Signature] of the model.
__pydantic_complete__: Whether model building is completed, or if there are still undefined fields.
__pydantic_core_schema__: The core schema of the model.
__pydantic_custom_init__: Whether the model has a custom `__init__` function.
__pydantic_decorators__: Metadata containing the decorators defined on the model.
This replaces `Model.__validators__` and `Model.__root_validators__` from Pydantic V1.
__pydantic_generic_metadata__: A dictionary containing metadata about generic Pydantic models.
The `origin` and `args` items map to the [`__origin__`][genericalias.__origin__]
and [`__args__`][genericalias.__args__] attributes of [generic aliases][types-genericalias],
and the `parameter` item maps to the `__parameter__` attribute of generic classes.
__pydantic_parent_namespace__: Parent namespace of the model, used for automatic rebuilding of models.
__pydantic_post_init__: The name of the post-init method for the model, if defined.
__pydantic_root_model__: Whether the model is a [`RootModel`][pydantic.root_model.RootModel].
__pydantic_serializer__: The `pydantic-core` `SchemaSerializer` used to dump instances of the model.
__pydantic_validator__: The `pydantic-core` `SchemaValidator` used to validate instances of the model.
__pydantic_fields__: A dictionary of field names and their corresponding [`FieldInfo`][pydantic.fields.FieldInfo] objects.
__pydantic_computed_fields__: A dictionary of computed field names and their corresponding [`ComputedFieldInfo`][pydantic.fields.ComputedFieldInfo] objects.
__pydantic_extra__: A dictionary containing extra values, if [`extra`][pydantic.config.ConfigDict.extra]
is set to `'allow'`.
__pydantic_fields_set__: The names of fields explicitly set during instantiation.
__pydantic_private__: Values of private attributes set on the model instance.