diff --git a/authentik/blueprints/api.py b/authentik/blueprints/api.py index faa446320..97e610114 100644 --- a/authentik/blueprints/api.py +++ b/authentik/blueprints/api.py @@ -1,4 +1,6 @@ """Serializer mixin for managed models""" +from dataclasses import asdict + from drf_spectacular.utils import extend_schema, inline_serializer from rest_framework.decorators import action from rest_framework.fields import CharField, DateTimeField, JSONField @@ -84,7 +86,7 @@ class BlueprintInstanceViewSet(UsedByMixin, ModelViewSet): def available(self, request: Request) -> Response: """Get blueprints""" files: list[BlueprintFile] = blueprints_find.delay().get() - return Response([sanitize_dict(file) for file in files]) + return Response([sanitize_dict(asdict(file)) for file in files]) @permission_required("authentik_blueprints.view_blueprintinstance") @extend_schema( diff --git a/authentik/blueprints/v1/tasks.py b/authentik/blueprints/v1/tasks.py index a41531ab2..a36fb0ed3 100644 --- a/authentik/blueprints/v1/tasks.py +++ b/authentik/blueprints/v1/tasks.py @@ -21,7 +21,6 @@ from authentik.events.monitored_tasks import ( TaskResultStatus, prefill_task, ) -from authentik.events.utils import sanitize_dict from authentik.lib.config import CONFIG from authentik.root.celery import CELERY_APP @@ -36,17 +35,6 @@ class BlueprintFile: last_m: int meta: Optional[BlueprintMetadata] = field(default=None) - @staticmethod - def from_raw(raw: dict, path: Path) -> "BlueprintFile": - """Create blueprint file from raw YAML""" - root = Path(CONFIG.y("blueprints_dir")) - metadata = raw.get("metadata", None) - version = raw.get("version", 1) - file_hash = sha512(path.read_bytes()).hexdigest() - blueprint = BlueprintFile(path.relative_to(root), version, file_hash, path.stat().st_mtime) - blueprint.meta = from_dict(BlueprintMetadata, metadata) if metadata else None - return blueprint - @CELERY_APP.task( throws=(DatabaseError, ProgrammingError, InternalError), @@ -64,11 +52,14 @@ def blueprints_find(): raw_blueprint = None if not raw_blueprint: continue + metadata = raw_blueprint.get("metadata", None) version = raw_blueprint.get("version", 1) if version != 1: continue - blueprint = BlueprintFile.from_raw(raw_blueprint, path) - blueprints.append(sanitize_dict(asdict(blueprint))) + file_hash = sha512(path.read_bytes()).hexdigest() + blueprint = BlueprintFile(path.relative_to(root), version, file_hash, path.stat().st_mtime) + blueprint.meta = from_dict(BlueprintMetadata, metadata) if metadata else None + blueprints.append(blueprint) return blueprints @@ -79,11 +70,8 @@ def blueprints_find(): def blueprints_discover(self: MonitoredTask): """Find blueprints and check if they need to be created in the database""" count = 0 - root = Path(CONFIG.y("blueprints_dir")) for blueprint in blueprints_find(): - check_blueprint_v1_file( - BlueprintFile.from_raw(blueprint, Path(root, blueprint.get("path"))) - ) + check_blueprint_v1_file(blueprint) count += 1 self.set_status( TaskResult(