Repository: goauthentik/authentik

Issue:
MembershipLDAPSynchronizer task is never run
**Describe the bug**
I have added an ActiveDirectory LDAP server as a source to Authentik. No matter whether I click the `Run sync now` button in the frontend or the `ldap_sync` CLI command, group memberships are never synchronized. When I run the same code from `MembershipLDAPSynchronizer` manually in a Django shell, it successfully synchronizes all group memberships:

![image (1)](https://github.com/goauthentik/authentik/assets/8849554/a9797d76-64ec-47fb-93ed-e5f8f7b82cad)

![Screenshot from 2023-07-25 08-55-51](https://github.com/goauthentik/authentik/assets/8849554/e184971d-a375-450d-a8b5-514d630be222)

![image](https://github.com/goauthentik/authentik/assets/8849554/73911a56-454d-42e7-baca-616b35a39c9a)



**To Reproduce**
I just added an AD LDAP source and tried to sync it. All settings are set according to the AD LDAP guide in the docs.


**Logs**
<details>
<summary><code>python manage.py ldap_sync stw-ldap</code></summary>
<pre>
authentik@authentik-server-678dbf866-jb5m9:/$ python manage.py ldap_sync stw-ldap
{"event": "Loaded config", "level": "debug", "logger": "authentik.lib.config", "timestamp": 1690267667.8442097, "file": "/authentik/lib/default.yml"}
{"event": "Loaded environment variables", "level": "debug", "logger": "authentik.lib.config", "timestamp": 1690267667.844809, "count": 46}
{"event": "Loaded app settings", "level": "debug", "logger": "authentik.lib.config", "timestamp": 1690267668.33449, "path": "authentik.admin.settings"}
{"event": "Loaded app settings", "level": "debug", "logger": "authentik.lib.config", "timestamp": 1690267668.335498, "path": "authentik.crypto.settings"}
{"event": "Loaded app settings", "level": "debug", "logger": "authentik.lib.config", "timestamp": 1690267668.3362067, "path": "authentik.events.settings"}
{"event": "Loaded app settings", "level": "debug", "logger": "authentik.lib.config", "timestamp": 1690267668.3375206, "path": "authentik.outposts.settings"}
{"event": "Loaded app settings", "level": "debug", "logger": "authentik.lib.config", "timestamp": 1690267668.3396988, "path": "authentik.policies.reputation.settings"}
{"event": "Loaded app settings", "level": "debug", "logger": "authentik.lib.config", "timestamp": 1690267668.341953, "path": "authentik.providers.scim.settings"}
{"event": "Loaded app settings", "level": "debug", "logger": "authentik.lib.config", "timestamp": 1690267668.3431263, "path": "authentik.sources.ldap.settings"}
{"event": "Loaded app settings", "level": "debug", "logger": "authentik.lib.config", "timestamp": 1690267668.3440619, "path": "authentik.sources.plex.settings"}
{"event": "Loaded app settings", "level": "debug", "logger": "authentik.lib.config", "timestamp": 1690267668.3453057, "path": "authentik.stages.authenticator_static.settings"}
{"event": "Loaded app settings", "level": "debug", "logger": "authentik.lib.config", "timestamp": 1690267668.3457792, "path": "authentik.stages.authenticator_totp.settings"}
{"event": "Loaded app settings", "level": "debug", "logger": "authentik.lib.config", "timestamp": 1690267668.3462336, "path": "authentik.stages.authenticator_validate.settings"}
{"event": "Loaded app settings", "level": "debug", "logger": "authentik.lib.config", "timestamp": 1690267668.3506007, "path": "authentik.blueprints.settings"}
{"event": "Booting authentik", "level": "info", "logger": "authentik.lib.config", "timestamp": 1690267668.350768, "version": "2023.6.1"}
{"event": "Enabled authentik enterprise", "level": "info", "logger": "authentik.lib.config", "timestamp": 1690267668.3521569}
{"event": "Loaded app settings", "level": "debug", "logger": "authentik.lib.config", "timestamp": 1690267668.352413, "path": "authentik.enterprise.settings"}
{"event": "Loaded GeoIP database", "last_write": 1688989663.0, "level": "info", "logger": "authentik.events.geo", "pid": 25949, "timestamp": "2023-07-25T06:47:48.814003"}
{"event": "Task published", "level": "info", "logger": "authentik.root.celery", "pid": 25949, "task_id": "167e36c0-1731-4349-90aa-0b2ec166d22b", "task_name": "authentik.blueprints.v1.tasks.blueprints_discovery", "timestamp": "2023-07-25T06:47:49.390292"}
{"event": "Task published", "level": "info", "logger": "authentik.root.celery", "pid": 25949, "task_id": "324143ef-103b-4c6d-8d32-4dd40aa92267", "task_name": "authentik.blueprints.v1.tasks.clear_failed_blueprints", "timestamp": "2023-07-25T06:47:49.391951"}
{"event": "Task published", "level": "info", "logger": "authentik.root.celery", "pid": 25949, "task_id": "daed2fbf-92cd-4637-862f-4f18488c403d", "task_name": "authentik.sources.ldap.tasks.ldap_sync", "timestamp": "2023-07-25T06:47:53.678855"}
{"event": "Task published", "level": "info", "logger": "authentik.root.celery", "pid": 25949, "task_id": "3f3579c8-7c69-4d20-997c-b46f0bda000d", "task_name": "authentik.sources.ldap.tasks.ldap_sync", "timestamp": "2023-07-25T06:47:53.680623"}
{"event": "Task published", "level": "info", "logger": "authentik.root.celery", "pid": 25949, "task_id": "784714fd-afa3-4d01-b2b3-14ef7d4f946f", "task_name": "authentik.sources.ldap.tasks.ldap_sync", "timestamp": "2023-07-25T06:47:53.682028"}
{"event": "Task published", "level": "info", "logger": "authentik.root.celery", "pid": 25949, "task_id": "d02e7264-d676-4ee7-9e73-d744b8e9317c", "task_name": "authentik.sources.ldap.tasks.ldap_sync", "timestamp": "2023-07-25T06:47:53.683808"}
{"event": "Task published", "level": "info", "logger": "authentik.root.celery", "pid": 25949, "task_id": "5b5cbf2c-989f-4303-b322-66a27a5867ba", "task_name": "authentik.sources.ldap.tasks.ldap_sync", "timestamp": "2023-07-25T06:47:53.685122"}
{"event": "Task published", "level": "info", "logger": "authentik.root.celery", "pid": 25949, "task_id": "0124d225-864d-4ddb-bc96-090dde7da2aa", "task_name": "authentik.sources.ldap.tasks.ldap_sync", "timestamp": "2023-07-25T06:47:53.686446"}
{"event": "Task published", "level": "info", "logger": "authentik.root.celery", "pid": 25949, "task_id": "c62ee3f0-d10f-4388-a625-032ba6e7daf5", "task_name": "authentik.sources.ldap.tasks.ldap_sync", "timestamp": "2023-07-25T06:47:53.687701"}
{"event": "Task published", "level": "info", "logger": "authentik.root.celery", "pid": 25949, "task_id": "f49c88d8-1ffb-4c09-a1b3-cb8944c48999", "task_name": "authentik.sources.ldap.tasks.ldap_sync", "timestamp": "2023-07-25T06:47:53.688952"}
{"event": "Task published", "level": "info", "logger": "authentik.root.celery", "pid": 25949, "task_id": "5ed05d40-b1ea-4600-9614-42a79389b112", "task_name": "authentik.sources.ldap.tasks.ldap_sync", "timestamp": "2023-07-25T06:47:53.690793"}
{"event": "Task published", "level": "info", "logger": "authentik.root.celery", "pid": 25949, "task_id": "8fe94248-eeef-42e8-b709-21c4d54076e6", "task_name": "authentik.sources.ldap.tasks.ldap_sync", "timestamp": "2023-07-25T06:47:53.692037"}
{"event": "Task published", "level": "info", "logger": "authentik.root.celery", "pid": 25949, "task_id": "77a1c3ac-4781-4cef-a9e6-8718612348f5", "task_name": "authentik.sources.ldap.tasks.ldap_sync", "timestamp": "2023-07-25T06:47:53.693468"}
{"event": "Task published", "level": "info", "logger": "authentik.root.celery", "pid": 25949, "task_id": "8a2b5100-fcc2-487c-b0a9-1782b5975fce", "task_name": "authentik.sources.ldap.tasks.ldap_sync", "timestamp": "2023-07-25T06:47:53.694806"}
{"event": "Task published", "level": "info", "logger": "authentik.root.celery", "pid": 25949, "task_id": "7d054338-c4da-480f-9ce4-31619be33d64", "task_name": "authentik.sources.ldap.tasks.ldap_sync", "timestamp": "2023-07-25T06:47:53.696167"}
{"event": "Task published", "level": "info", "logger": "authentik.root.celery", "pid": 25949, "task_id": "df4ba4a1-e3a2-4110-a9ff-db7cdc399426", "task_name": "authentik.sources.ldap.tasks.ldap_sync", "timestamp": "2023-07-25T06:47:53.697466"}
{"event": "Task published", "level": "info", "logger": "authentik.root.celery", "pid": 25949, "task_id": "4e6be9b3-db20-416f-adf9-47544f9e88c1", "task_name": "authentik.sources.ldap.tasks.ldap_sync", "timestamp": "2023-07-25T06:47:53.698702"}
{"event": "Task published", "level": "info", "logger": "authentik.root.celery", "pid": 25949, "task_id": "f6f21f00-5afa-498f-9373-fda363ca6ba9", "task_name": "authentik.sources.ldap.tasks.ldap_sync", "timestamp": "2023-07-25T06:47:53.699940"}
{"event": "Task published", "level": "info", "logger": "authentik.root.celery", "pid": 25949, "task_id": "210200ee-c557-44ee-908f-1a8520280815", "task_name": "authentik.sources.ldap.tasks.ldap_sync", "timestamp": "2023-07-25T06:47:53.701375"}
{"event": "Task published", "level": "info", "logger": "authentik.root.celery", "pid": 25949, "task_id": "373cf4a4-ebad-4723-b03f-64b7b8412eab", "task_name": "authentik.sources.ldap.tasks.ldap_sync", "timestamp": "2023-07-25T06:47:53.702602"}
{"event": "Task published", "level": "info", "logger": "authentik.root.celery", "pid": 25949, "task_id": "cb350259-8079-49c7-a14f-989a91b93a7e", "task_name": "authentik.sources.ldap.tasks.ldap_sync", "timestamp": "2023-07-25T06:47:53.703804"}
{"event": "Task published", "level": "info", "logger": "authentik.root.celery", "pid": 25949, "task_id": "f23e9d55-f51d-4d87-b742-91ef1ebad3d4", "task_name": "authentik.sources.ldap.tasks.ldap_sync", "timestamp": "2023-07-25T06:47:53.704996"}
{"event": "Task published", "level": "info", "logger": "authentik.root.celery", "pid": 25949, "task_id": "c7033486-5e12-421f-80c2-650e591abedf", "task_name": "authentik.sources.ldap.tasks.ldap_sync", "timestamp": "2023-07-25T06:47:53.706223"}
{"event": "Task published", "level": "info", "logger": "authentik.root.celery", "pid": 25949, "task_id": "4a78ede8-d018-433d-ae65-ec3218ab1280", "task_name": "authentik.sources.ldap.tasks.ldap_sync", "timestamp": "2023-07-25T06:47:53.707422"}
{"event": "Task published", "level": "info", "logger": "authentik.root.celery", "pid": 25949, "task_id": "54fadecc-eca0-4426-856a-e908090b6dbb", "task_name": "authentik.sources.ldap.tasks.ldap_sync", "timestamp": "2023-07-25T06:47:53.708607"}
{"event": "Task published", "level": "info", "logger": "authentik.root.celery", "pid": 25949, "task_id": "893b8371-bad9-4a73-867a-9843df4b9b2b", "task_name": "authentik.sources.ldap.tasks.ldap_sync", "timestamp": "2023-07-25T06:47:53.709820"}
{"event": "Task published", "level": "info", "logger": "authentik.root.celery", "pid": 25949, "task_id": "e370ae09-5312-4726-b6f8-0c24a63d3827", "task_name": "authentik.sources.ldap.tasks.ldap_sync", "timestamp": "2023-07-25T06:47:53.711054"}
{"event": "Task published", "level": "info", "logger": "authentik.root.celery", "pid": 25949, "task_id": "fd67c776-6ae3-46b6-a21b-313d85bcbfbc", "task_name": "authentik.sources.ldap.tasks.ldap_sync", "timestamp": "2023-07-25T06:47:53.712411"}
{"event": "Task published", "level": "info", "logger": "authentik.root.celery", "pid": 25949, "task_id": "60e82712-3d97-45d3-adb4-ddd86e50c350", "task_name": "authentik.sources.ldap.tasks.ldap_sync", "timestamp": "2023-07-25T06:47:53.713608"}
{"event": "Task published", "level": "info", "logger": "authentik.root.celery", "pid": 25949, "task_id": "aa6321d4-ad67-4413-9371-39df7d36e849", "task_name": "authentik.sources.ldap.tasks.ldap_sync", "timestamp": "2023-07-25T06:47:53.714880"}
{"event": "Task published", "level": "info", "logger": "authentik.root.celery", "pid": 25949, "task_id": "99e8fc2f-4777-4616-b6e7-51217afff436", "task_name": "authentik.sources.ldap.tasks.ldap_sync", "timestamp": "2023-07-25T06:47:53.716289"}
{"event": "Task published", "level": "info", "logger": "authentik.root.celery", "pid": 25949, "task_id": "9c31b360-14a0-4bb0-9cab-2adb0b43c0e3", "task_name": "authentik.sources.ldap.tasks.ldap_sync", "timestamp": "2023-07-25T06:47:53.717624"}
{"event": "Task published", "level": "info", "logger": "authentik.root.celery", "pid": 25949, "task_id": "3c36f7a4-391b-4f5e-b55b-85a4b383602e", "task_name": "authentik.sources.ldap.tasks.ldap_sync", "timestamp": "2023-07-25T06:47:53.718868"}
{"event": "Task published", "level": "info", "logger": "authentik.root.celery", "pid": 25949, "task_id": "5ebe056b-20d2-476b-b3af-d14573b61e59", "task_name": "authentik.sources.ldap.tasks.ldap_sync", "timestamp": "2023-07-25T06:47:53.720297"}
{"event": "Task published", "level": "info", "logger": "authentik.root.celery", "pid": 25949, "task_id": "95dc2a8d-6834-407b-9d3c-dde6463056d8", "task_name": "authentik.sources.ldap.tasks.ldap_sync", "timestamp": "2023-07-25T06:47:53.721550"}
{"event": "Task published", "level": "info", "logger": "authentik.root.celery", "pid": 25949, "task_id": "d90ca839-a2a5-4455-a7c2-6e73eeb5e5af", "task_name": "authentik.sources.ldap.tasks.ldap_sync", "timestamp": "2023-07-25T06:47:53.722773"}
{"event": "Task published", "level": "info", "logger": "authentik.root.celery", "pid": 25949, "task_id": "135c05de-2271-443b-81f1-bbf1c92c2cf8", "task_name": "authentik.sources.ldap.tasks.ldap_sync", "timestamp": "2023-07-25T06:47:53.725007"}
</pre>
</details>

**Version and Deployment (please complete the following information):**

-   authentik version: 2023.6.1
-   Deployment: helm


Discussion:

Resolving patch:
diff --git a/authentik/sources/ldap/tasks.py b/authentik/sources/ldap/tasks.py
--- a/authentik/sources/ldap/tasks.py
+++ b/authentik/sources/ldap/tasks.py
@@ -59,7 +59,7 @@ def ldap_sync_paginator(source: LDAPSource, sync: type[BaseLDAPSynchronizer]) ->
     signatures = []
     for page in sync_inst.get_objects():
         page_cache_key = CACHE_KEY_PREFIX + str(uuid4())
-        cache.set(page_cache_key, page)
+        cache.set(page_cache_key, page, 60 * 60 * int(CONFIG.get("ldap.task_timeout_hours")))
         page_sync = ldap_sync.si(source.pk, class_to_path(sync), page_cache_key)
         signatures.append(page_sync)
     return signatures
@@ -86,6 +86,12 @@ def ldap_sync(self: MonitoredTask, source_pk: str, sync_class: str, page_cache_k
         sync_inst: BaseLDAPSynchronizer = sync(source)
         page = cache.get(page_cache_key)
         if not page:
+            error_message = (
+                f"Could not find page in cache: {page_cache_key}. "
+                + "Try increasing ldap.task_timeout_hours"
+            )
+            LOGGER.warning(error_message)
+            self.set_status(TaskResult(TaskResultStatus.ERROR, [error_message]))
             return
         cache.touch(page_cache_key)
         count = sync_inst.sync(page)


Test patch:
diff --git a/authentik/sources/ldap/tests/test_sync.py b/authentik/sources/ldap/tests/test_sync.py
--- a/authentik/sources/ldap/tests/test_sync.py
+++ b/authentik/sources/ldap/tests/test_sync.py
@@ -8,12 +8,14 @@
 from authentik.core.models import Group, User
 from authentik.core.tests.utils import create_test_admin_user
 from authentik.events.models import Event, EventAction
+from authentik.events.monitored_tasks import TaskInfo, TaskResultStatus
 from authentik.lib.generators import generate_key
+from authentik.lib.utils.reflection import class_to_path
 from authentik.sources.ldap.models import LDAPPropertyMapping, LDAPSource
 from authentik.sources.ldap.sync.groups import GroupLDAPSynchronizer
 from authentik.sources.ldap.sync.membership import MembershipLDAPSynchronizer
 from authentik.sources.ldap.sync.users import UserLDAPSynchronizer
-from authentik.sources.ldap.tasks import ldap_sync_all
+from authentik.sources.ldap.tasks import ldap_sync, ldap_sync_all
 from authentik.sources.ldap.tests.mock_ad import mock_ad_connection
 from authentik.sources.ldap.tests.mock_slapd import mock_slapd_connection
 
@@ -33,6 +35,14 @@ def setUp(self):
             additional_group_dn="ou=groups",
         )
 
+    def test_sync_missing_page(self):
+        """Test sync with missing page"""
+        connection = MagicMock(return_value=mock_ad_connection(LDAP_PASSWORD))
+        with patch("authentik.sources.ldap.models.LDAPSource.connection", connection):
+            ldap_sync.delay(self.source.pk, class_to_path(UserLDAPSynchronizer), "foo").get()
+        status = TaskInfo.by_name("ldap_sync:ldap:users:foo")
+        self.assertEqual(status.result.status, TaskResultStatus.ERROR)
+
     def test_sync_error(self):
         """Test user sync"""
         self.source.property_mappings.set(