From 2e44c1cdfc27c902b03ffd85073de1ef328edd6d Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Sat, 17 Apr 2021 11:29:51 +0200 Subject: [PATCH] sources/ldap: improve error handling during sync Signed-off-by: Jens Langhammer --- authentik/sources/ldap/sync/groups.py | 19 ++++++++++++------- authentik/sources/ldap/sync/users.py | 3 ++- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/authentik/sources/ldap/sync/groups.py b/authentik/sources/ldap/sync/groups.py index 149797218..291dbb67a 100644 --- a/authentik/sources/ldap/sync/groups.py +++ b/authentik/sources/ldap/sync/groups.py @@ -1,9 +1,11 @@ """Sync LDAP Users and groups into authentik""" import ldap3 import ldap3.core.exceptions +from django.core.exceptions import FieldError from django.db.utils import IntegrityError from authentik.core.models import Group +from authentik.events.models import Event, EventAction from authentik.sources.ldap.sync.base import LDAP_UNIQUENESS, BaseLDAPSynchronizer @@ -47,14 +49,17 @@ class GroupLDAPSynchronizer(BaseLDAPSynchronizer): "defaults": defaults, } ) - except IntegrityError as exc: - self._logger.warning("Failed to create group", exc=exc) - self._logger.warning( - ( - "To merge new group with existing group, set the group's " + except (IntegrityError, FieldError) as exc: + Event.new( + EventAction.CONFIGURATION_ERROR, + message=( + f"Failed to create group: {str(exc)} " + "To merge new group with existing group, set the groups's " f"Attribute '{LDAP_UNIQUENESS}' to '{uniq}'" - ) - ) + ), + source=self._source, + dn=group_dn, + ).save() else: self._logger.debug("Synced group", group=ak_group.name, created=created) group_count += 1 diff --git a/authentik/sources/ldap/sync/users.py b/authentik/sources/ldap/sync/users.py index 55ffbca1c..a7fd98186 100644 --- a/authentik/sources/ldap/sync/users.py +++ b/authentik/sources/ldap/sync/users.py @@ -3,6 +3,7 @@ from datetime import datetime import ldap3 import ldap3.core.exceptions +from django.core.exceptions import FieldError from django.db.utils import IntegrityError from pytz import UTC @@ -48,7 +49,7 @@ class UserLDAPSynchronizer(BaseLDAPSynchronizer): "defaults": defaults, } ) - except IntegrityError as exc: + except (IntegrityError, FieldError) as exc: Event.new( EventAction.CONFIGURATION_ERROR, message=(