From 48e5823ad6f8c068525469db20599921f75cf209 Mon Sep 17 00:00:00 2001 From: Jens L Date: Thu, 21 Dec 2023 22:27:46 +0100 Subject: [PATCH] lib: fix event creation when deprecated config is detected (#7969) * lib: fix event creation when deprecated config is detected Signed-off-by: Jens Langhammer * fix black parsing errors Signed-off-by: Jens Langhammer --------- Signed-off-by: Jens Langhammer --- authentik/events/apps.py | 22 +++++++++++++++++++++ authentik/lib/config.py | 42 +++++++++++++++++++++------------------- pyproject.toml | 4 ++-- 3 files changed, 46 insertions(+), 22 deletions(-) diff --git a/authentik/events/apps.py b/authentik/events/apps.py index 34368856d..3d939d568 100644 --- a/authentik/events/apps.py +++ b/authentik/events/apps.py @@ -2,6 +2,7 @@ from prometheus_client import Gauge from authentik.blueprints.apps import ManagedAppConfig +from authentik.lib.config import CONFIG, ENV_PREFIX GAUGE_TASKS = Gauge( "authentik_system_tasks", @@ -21,3 +22,24 @@ class AuthentikEventsConfig(ManagedAppConfig): def reconcile_load_events_signals(self): """Load events signals""" self.import_module("authentik.events.signals") + + def reconcile_check_deprecations(self): + """Check for config deprecations""" + from authentik.events.models import Event, EventAction + + for key_replace, msg in CONFIG.deprecations.items(): + key, replace = key_replace + key_env = f"{ENV_PREFIX}_{key.replace('.', '__')}".upper() + replace_env = f"{ENV_PREFIX}_{replace.replace('.', '__')}".upper() + if Event.objects.filter( + action=EventAction.CONFIGURATION_ERROR, context__deprecated_option=key + ).exists(): + continue + Event.new( + EventAction.CONFIGURATION_ERROR, + deprecated_option=key, + deprecated_env=key_env, + replacement_option=replace, + replacement_env=replace_env, + message=msg, + ).save() diff --git a/authentik/lib/config.py b/authentik/lib/config.py index 3b977e8a2..dabd3a64b 100644 --- a/authentik/lib/config.py +++ b/authentik/lib/config.py @@ -113,6 +113,8 @@ class ConfigLoader: A variable like AUTHENTIK_POSTGRESQL__HOST would translate to postgresql.host""" + deprecations: dict[tuple[str, str], str] = {} + def __init__(self, **kwargs): super().__init__() self.__config = {} @@ -139,9 +141,9 @@ class ConfigLoader: self.update_from_file(env_file) self.update_from_env() self.update(self.__config, kwargs) - self.check_deprecations() + self.deprecations = self.check_deprecations() - def check_deprecations(self): + def check_deprecations(self) -> dict[str, str]: """Warn if any deprecated configuration options are used""" def _pop_deprecated_key(current_obj, dot_parts, index): @@ -154,25 +156,23 @@ class ConfigLoader: current_obj.pop(dot_part) return value + deprecation_replacements = {} for deprecation, replacement in DEPRECATIONS.items(): - if self.get(deprecation, default=UNSET) is not UNSET: - message = ( - f"'{deprecation}' has been deprecated in favor of '{replacement}'! " - + "Please update your configuration." - ) - self.log( - "warning", - message, - ) - try: - from authentik.events.models import Event, EventAction + if self.get(deprecation, default=UNSET) is UNSET: + continue + message = ( + f"'{deprecation}' has been deprecated in favor of '{replacement}'! " + + "Please update your configuration." + ) + self.log( + "warning", + message, + ) + deprecation_replacements[(deprecation, replacement)] = message - Event.new(EventAction.CONFIGURATION_ERROR, message=message).save() - except ImportError: - continue - - deprecated_attr = _pop_deprecated_key(self.__config, deprecation.split("."), 0) - self.set(replacement, deprecated_attr.value) + deprecated_attr = _pop_deprecated_key(self.__config, deprecation.split("."), 0) + self.set(replacement, deprecated_attr) + return deprecation_replacements def log(self, level: str, message: str, **kwargs): """Custom Log method, we want to ensure ConfigLoader always logs JSON even when @@ -318,7 +318,9 @@ class ConfigLoader: def set(self, path: str, value: Any, sep="."): """Set value using same syntax as get()""" - set_path_in_dict(self.raw, path, Attr(value), sep=sep) + if not isinstance(value, Attr): + value = Attr(value) + set_path_in_dict(self.raw, path, value, sep=sep) CONFIG = ConfigLoader() diff --git a/pyproject.toml b/pyproject.toml index 7c1af2c59..aa5851881 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -17,12 +17,12 @@ pythonPlatform = "All" [tool.black] line-length = 100 -target-version = ['py311'] +target-version = ['py312'] exclude = 'node_modules' [tool.ruff] line-length = 100 -target-version = "py311" +target-version = "py312" exclude = ["**/migrations/**", "**/node_modules/**"] [tool.isort]