From 552f8c6a9a4597be9be32b98809dd5a1703f4655 Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Tue, 9 Feb 2021 16:08:24 +0100 Subject: [PATCH] sources/*: switch API to use slug in URL --- authentik/sources/ldap/api.py | 11 ++++++++-- authentik/sources/ldap/models.py | 10 --------- authentik/sources/ldap/tasks.py | 6 +++++- authentik/sources/oauth/api.py | 1 + authentik/sources/saml/api.py | 1 + swagger.yaml | 36 ++++++++++++++++++-------------- 6 files changed, 36 insertions(+), 29 deletions(-) diff --git a/authentik/sources/ldap/api.py b/authentik/sources/ldap/api.py index c79e87652..88b041855 100644 --- a/authentik/sources/ldap/api.py +++ b/authentik/sources/ldap/api.py @@ -1,4 +1,6 @@ """Source API Views""" +from datetime import datetime + from django.core.cache import cache from django.db.models.base import Model from drf_yasg2.utils import swagger_auto_schema @@ -58,14 +60,19 @@ class LDAPSourceViewSet(ModelViewSet): queryset = LDAPSource.objects.all() serializer_class = LDAPSourceSerializer + lookup_field = "slug" @swagger_auto_schema(responses={200: LDAPSourceSyncStatusSerializer(many=False)}) @action(methods=["GET"], detail=True) # pylint: disable=invalid-name - def sync_status(self, request: Request, pk: int) -> Response: + def sync_status(self, request: Request, slug: str) -> Response: source = self.get_object() last_sync = cache.get(source.state_cache_prefix("last_sync"), None) - return Response(LDAPSourceSyncStatusSerializer({"last_sync": last_sync}).data) + return Response( + LDAPSourceSyncStatusSerializer( + {"last_sync": datetime.fromtimestamp(last_sync)} + ).data + ) class LDAPPropertyMappingSerializer(ModelSerializer, MetaNameSerializer): diff --git a/authentik/sources/ldap/models.py b/authentik/sources/ldap/models.py index 96dea06f3..94dffecf3 100644 --- a/authentik/sources/ldap/models.py +++ b/authentik/sources/ldap/models.py @@ -91,16 +91,6 @@ class LDAPSource(Source): """Key by which the ldap source status is saved""" return f"source_ldap_{self.pk}_state_{suffix}" - @property - def ui_additional_info(self) -> str: - last_sync = cache.get(self.state_cache_prefix("last_sync"), None) - if last_sync: - last_sync = datetime.fromtimestamp(last_sync) - - return render_to_string( - "ldap/source_list_status.html", {"source": self, "last_sync": last_sync} - ) - _connection: Optional[Connection] = None @property diff --git a/authentik/sources/ldap/tasks.py b/authentik/sources/ldap/tasks.py index 3d788e171..85377de53 100644 --- a/authentik/sources/ldap/tasks.py +++ b/authentik/sources/ldap/tasks.py @@ -4,6 +4,7 @@ from time import time from django.core.cache import cache from django.utils.text import slugify from ldap3.core.exceptions import LDAPException +from structlog.stdlib import get_logger from authentik.events.monitored_tasks import MonitoredTask, TaskResult, TaskResultStatus from authentik.root.celery import CELERY_APP @@ -12,6 +13,8 @@ from authentik.sources.ldap.sync.groups import GroupLDAPSynchronizer from authentik.sources.ldap.sync.membership import MembershipLDAPSynchronizer from authentik.sources.ldap.sync.users import UserLDAPSynchronizer +LOGGER = get_logger() + @CELERY_APP.task() def ldap_sync_all(): @@ -21,7 +24,7 @@ def ldap_sync_all(): @CELERY_APP.task(bind=True, base=MonitoredTask) -def ldap_sync(self: MonitoredTask, source_pk: int): +def ldap_sync(self: MonitoredTask, source_pk: str): """Synchronization of an LDAP Source""" try: source: LDAPSource = LDAPSource.objects.get(pk=source_pk) @@ -49,4 +52,5 @@ def ldap_sync(self: MonitoredTask, source_pk: int): ) ) except LDAPException as exc: + LOGGER.debug(exc) self.set_status(TaskResult(TaskResultStatus.ERROR).with_error(exc)) diff --git a/authentik/sources/oauth/api.py b/authentik/sources/oauth/api.py index c58149005..161e63905 100644 --- a/authentik/sources/oauth/api.py +++ b/authentik/sources/oauth/api.py @@ -26,3 +26,4 @@ class OAuthSourceViewSet(ModelViewSet): queryset = OAuthSource.objects.all() serializer_class = OAuthSourceSerializer + lookup_field = "slug" diff --git a/authentik/sources/saml/api.py b/authentik/sources/saml/api.py index 37da99206..0ed982dcf 100644 --- a/authentik/sources/saml/api.py +++ b/authentik/sources/saml/api.py @@ -30,3 +30,4 @@ class SAMLSourceViewSet(ModelViewSet): queryset = SAMLSource.objects.all() serializer_class = SAMLSourceSerializer + lookup_field = "slug" diff --git a/swagger.yaml b/swagger.yaml index 1056c8348..362127189 100755 --- a/swagger.yaml +++ b/swagger.yaml @@ -4917,7 +4917,7 @@ paths: tags: - sources parameters: [] - /sources/ldap/{pbm_uuid}/: + /sources/ldap/{slug}/: get: operationId: sources_ldap_read description: LDAP Source Viewset @@ -4971,13 +4971,14 @@ paths: tags: - sources parameters: - - name: pbm_uuid + - name: slug in: path - description: A UUID string identifying this LDAP Source. + description: Internal source name, used in URLs. required: true type: string - format: uuid - /sources/ldap/{pbm_uuid}/sync_status/: + format: slug + pattern: ^[-a-zA-Z0-9_]+$ + /sources/ldap/{slug}/sync_status/: get: operationId: sources_ldap_sync_status description: LDAP Source Viewset @@ -4990,12 +4991,13 @@ paths: tags: - sources parameters: - - name: pbm_uuid + - name: slug in: path - description: A UUID string identifying this LDAP Source. + description: Internal source name, used in URLs. required: true type: string - format: uuid + format: slug + pattern: ^[-a-zA-Z0-9_]+$ /sources/oauth/: get: operationId: sources_oauth_list @@ -5063,7 +5065,7 @@ paths: tags: - sources parameters: [] - /sources/oauth/{pbm_uuid}/: + /sources/oauth/{slug}/: get: operationId: sources_oauth_read description: Source Viewset @@ -5117,12 +5119,13 @@ paths: tags: - sources parameters: - - name: pbm_uuid + - name: slug in: path - description: A UUID string identifying this Generic OAuth Source. + description: Internal source name, used in URLs. required: true type: string - format: uuid + format: slug + pattern: ^[-a-zA-Z0-9_]+$ /sources/saml/: get: operationId: sources_saml_list @@ -5190,7 +5193,7 @@ paths: tags: - sources parameters: [] - /sources/saml/{pbm_uuid}/: + /sources/saml/{slug}/: get: operationId: sources_saml_read description: SAMLSource Viewset @@ -5244,12 +5247,13 @@ paths: tags: - sources parameters: - - name: pbm_uuid + - name: slug in: path - description: A UUID string identifying this SAML Source. + description: Internal source name, used in URLs. required: true type: string - format: uuid + format: slug + pattern: ^[-a-zA-Z0-9_]+$ /stages/all/: get: operationId: stages_all_list