We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
docker compose run --rm server ldap_sync - doesn't work anymore to debug
**Describe the bug**
this command doesnt work anymore as described here (https://goauthentik.io/docs/troubleshooting/ldap_source)
```
docker compose run --rm server ldap_sync nxnet
```
it will just create a backgorund taks instead of running it in foreground!

**To Reproduce**
```
docker compose run --rm server ldap_sync SLUGofLDAPsource
```

**Expected behavior**
it will run an LDAP- synchronization in the foreground and see any errors or warnings that might happen directly

**Logs**
{"event": "Task published", "level": "info", "logger": "authentik.root.celery", "pid": 7, "task_id": "17af668f-1d9f-4732-a0eb-044c4a16beed", "task_name": "authentik.sources.ldap.tasks.ldap_sync", "timestamp": "2023-07-20T15:10:12.575247"}

**Version and Deployment (please complete the following information):**

-   authentik version: 2023.6.1
-   Deployment: docker compose



--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `authentik/sources/ldap/management/commands/ldap_sync.py`
Content:
```
1 """LDAP Sync"""
2 from django.core.management.base import BaseCommand
3 from structlog.stdlib import get_logger
4 
5 from authentik.sources.ldap.models import LDAPSource
6 from authentik.sources.ldap.tasks import ldap_sync_single
7 
8 LOGGER = get_logger()
9 
10 
11 class Command(BaseCommand):
12     """Run sync for an LDAP Source"""
13 
14     def add_arguments(self, parser):
15         parser.add_argument("source_slugs", nargs="+", type=str)
16 
17     def handle(self, **options):
18         for source_slug in options["source_slugs"]:
19             source = LDAPSource.objects.filter(slug=source_slug).first()
20             if not source:
21                 LOGGER.warning("Source does not exist", slug=source_slug)
22                 continue
23             ldap_sync_single(source)
24
```

Path: `authentik/sources/ldap/sync/users.py`
Content:
```
1 """Sync LDAP Users into authentik"""
2 from typing import Generator
3 
4 from django.core.exceptions import FieldError
5 from django.db.utils import IntegrityError
6 from ldap3 import ALL_ATTRIBUTES, ALL_OPERATIONAL_ATTRIBUTES, SUBTREE
7 
8 from authentik.core.models import User
9 from authentik.events.models import Event, EventAction
10 from authentik.sources.ldap.sync.base import LDAP_UNIQUENESS, BaseLDAPSynchronizer
11 from authentik.sources.ldap.sync.vendor.freeipa import FreeIPA
12 from authentik.sources.ldap.sync.vendor.ms_ad import MicrosoftActiveDirectory
13 
14 
15 class UserLDAPSynchronizer(BaseLDAPSynchronizer):
16     """Sync LDAP Users into authentik"""
17 
18     @staticmethod
19     def name() -> str:
20         return "users"
21 
22     def get_objects(self, **kwargs) -> Generator:
23         return self.search_paginator(
24             search_base=self.base_dn_users,
25             search_filter=self._source.user_object_filter,
26             search_scope=SUBTREE,
27             attributes=[ALL_ATTRIBUTES, ALL_OPERATIONAL_ATTRIBUTES],
28             **kwargs,
29         )
30 
31     def sync(self, page_data: list) -> int:
32         """Iterate over all LDAP Users and create authentik_core.User instances"""
33         if not self._source.sync_users:
34             self.message("User syncing is disabled for this Source")
35             return -1
36         user_count = 0
37         for user in page_data:
38             if "attributes" not in user:
39                 continue
40             attributes = user.get("attributes", {})
41             user_dn = self._flatten(user.get("entryDN", user.get("dn")))
42             if self._source.object_uniqueness_field not in attributes:
43                 self.message(
44                     f"Cannot find uniqueness field in attributes: '{user_dn}'",
45                     attributes=attributes.keys(),
46                     dn=user_dn,
47                 )
48                 continue
49             uniq = self._flatten(attributes[self._source.object_uniqueness_field])
50             try:
51                 defaults = self.build_user_properties(user_dn, **attributes)
52                 self._logger.debug("Creating user with attributes", **defaults)
53                 if "username" not in defaults:
54                     raise IntegrityError("Username was not set by propertymappings")
55                 ak_user, created = self.update_or_create_attributes(
56                     User, {f"attributes__{LDAP_UNIQUENESS}": uniq}, defaults
57                 )
58             except (IntegrityError, FieldError, TypeError, AttributeError) as exc:
59                 Event.new(
60                     EventAction.CONFIGURATION_ERROR,
61                     message=(
62                         f"Failed to create user: {str(exc)} "
63                         "To merge new user with existing user, set the user's "
64                         f"Attribute '{LDAP_UNIQUENESS}' to '{uniq}'"
65                     ),
66                     source=self._source,
67                     dn=user_dn,
68                 ).save()
69             else:
70                 self._logger.debug("Synced User", user=ak_user.username, created=created)
71                 user_count += 1
72                 MicrosoftActiveDirectory(self._source).sync(attributes, ak_user, created)
73                 FreeIPA(self._source).sync(attributes, ak_user, created)
74         return user_count
75
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/authentik/sources/ldap/management/commands/ldap_sync.py b/authentik/sources/ldap/management/commands/ldap_sync.py
--- a/authentik/sources/ldap/management/commands/ldap_sync.py
+++ b/authentik/sources/ldap/management/commands/ldap_sync.py
@@ -3,7 +3,10 @@
 from structlog.stdlib import get_logger
 
 from authentik.sources.ldap.models import LDAPSource
-from authentik.sources.ldap.tasks import ldap_sync_single
+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_paginator
 
 LOGGER = get_logger()
 
@@ -20,4 +23,10 @@
             if not source:
                 LOGGER.warning("Source does not exist", slug=source_slug)
                 continue
-            ldap_sync_single(source)
+            tasks = (
+                ldap_sync_paginator(source, UserLDAPSynchronizer)
+                + ldap_sync_paginator(source, GroupLDAPSynchronizer)
+                + ldap_sync_paginator(source, MembershipLDAPSynchronizer)
+            )
+            for task in tasks:
+                task()
diff --git a/authentik/sources/ldap/sync/users.py b/authentik/sources/ldap/sync/users.py
--- a/authentik/sources/ldap/sync/users.py
+++ b/authentik/sources/ldap/sync/users.py
@@ -49,7 +49,7 @@
             uniq = self._flatten(attributes[self._source.object_uniqueness_field])
             try:
                 defaults = self.build_user_properties(user_dn, **attributes)
-                self._logger.debug("Creating user with attributes", **defaults)
+                self._logger.debug("Writing user with attributes", **defaults)
                 if "username" not in defaults:
                     raise IntegrityError("Username was not set by propertymappings")
                 ak_user, created = self.update_or_create_attributes(

{"golden_diff": "diff --git a/authentik/sources/ldap/management/commands/ldap_sync.py b/authentik/sources/ldap/management/commands/ldap_sync.py\n--- a/authentik/sources/ldap/management/commands/ldap_sync.py\n+++ b/authentik/sources/ldap/management/commands/ldap_sync.py\n@@ -3,7 +3,10 @@\n from structlog.stdlib import get_logger\n \n from authentik.sources.ldap.models import LDAPSource\n-from authentik.sources.ldap.tasks import ldap_sync_single\n+from authentik.sources.ldap.sync.groups import GroupLDAPSynchronizer\n+from authentik.sources.ldap.sync.membership import MembershipLDAPSynchronizer\n+from authentik.sources.ldap.sync.users import UserLDAPSynchronizer\n+from authentik.sources.ldap.tasks import ldap_sync_paginator\n \n LOGGER = get_logger()\n \n@@ -20,4 +23,10 @@\n             if not source:\n                 LOGGER.warning(\"Source does not exist\", slug=source_slug)\n                 continue\n-            ldap_sync_single(source)\n+            tasks = (\n+                ldap_sync_paginator(source, UserLDAPSynchronizer)\n+                + ldap_sync_paginator(source, GroupLDAPSynchronizer)\n+                + ldap_sync_paginator(source, MembershipLDAPSynchronizer)\n+            )\n+            for task in tasks:\n+                task()\ndiff --git a/authentik/sources/ldap/sync/users.py b/authentik/sources/ldap/sync/users.py\n--- a/authentik/sources/ldap/sync/users.py\n+++ b/authentik/sources/ldap/sync/users.py\n@@ -49,7 +49,7 @@\n             uniq = self._flatten(attributes[self._source.object_uniqueness_field])\n             try:\n                 defaults = self.build_user_properties(user_dn, **attributes)\n-                self._logger.debug(\"Creating user with attributes\", **defaults)\n+                self._logger.debug(\"Writing user with attributes\", **defaults)\n                 if \"username\" not in defaults:\n                     raise IntegrityError(\"Username was not set by propertymappings\")\n                 ak_user, created = self.update_or_create_attributes(\n", "issue": "docker compose run --rm server ldap_sync - doesn't work anymore to debug\n**Describe the bug**\r\nthis command doesnt work anymore as described here (https://goauthentik.io/docs/troubleshooting/ldap_source)\r\n```\r\ndocker compose run --rm server ldap_sync nxnet\r\n```\r\nit will just create a backgorund taks instead of running it in foreground!\r\n\r\n**To Reproduce**\r\n```\r\ndocker compose run --rm server ldap_sync SLUGofLDAPsource\r\n```\r\n\r\n**Expected behavior**\r\nit will run an LDAP- synchronization in the foreground and see any errors or warnings that might happen directly\r\n\r\n**Logs**\r\n{\"event\": \"Task published\", \"level\": \"info\", \"logger\": \"authentik.root.celery\", \"pid\": 7, \"task_id\": \"17af668f-1d9f-4732-a0eb-044c4a16beed\", \"task_name\": \"authentik.sources.ldap.tasks.ldap_sync\", \"timestamp\": \"2023-07-20T15:10:12.575247\"}\r\n\r\n**Version and Deployment (please complete the following information):**\r\n\r\n-   authentik version: 2023.6.1\r\n-   Deployment: docker compose\r\n\r\n\n", "before_files": [{"content": "\"\"\"LDAP Sync\"\"\"\nfrom django.core.management.base import BaseCommand\nfrom structlog.stdlib import get_logger\n\nfrom authentik.sources.ldap.models import LDAPSource\nfrom authentik.sources.ldap.tasks import ldap_sync_single\n\nLOGGER = get_logger()\n\n\nclass Command(BaseCommand):\n    \"\"\"Run sync for an LDAP Source\"\"\"\n\n    def add_arguments(self, parser):\n        parser.add_argument(\"source_slugs\", nargs=\"+\", type=str)\n\n    def handle(self, **options):\n        for source_slug in options[\"source_slugs\"]:\n            source = LDAPSource.objects.filter(slug=source_slug).first()\n            if not source:\n                LOGGER.warning(\"Source does not exist\", slug=source_slug)\n                continue\n            ldap_sync_single(source)\n", "path": "authentik/sources/ldap/management/commands/ldap_sync.py"}, {"content": "\"\"\"Sync LDAP Users into authentik\"\"\"\nfrom typing import Generator\n\nfrom django.core.exceptions import FieldError\nfrom django.db.utils import IntegrityError\nfrom ldap3 import ALL_ATTRIBUTES, ALL_OPERATIONAL_ATTRIBUTES, SUBTREE\n\nfrom authentik.core.models import User\nfrom authentik.events.models import Event, EventAction\nfrom authentik.sources.ldap.sync.base import LDAP_UNIQUENESS, BaseLDAPSynchronizer\nfrom authentik.sources.ldap.sync.vendor.freeipa import FreeIPA\nfrom authentik.sources.ldap.sync.vendor.ms_ad import MicrosoftActiveDirectory\n\n\nclass UserLDAPSynchronizer(BaseLDAPSynchronizer):\n    \"\"\"Sync LDAP Users into authentik\"\"\"\n\n    @staticmethod\n    def name() -> str:\n        return \"users\"\n\n    def get_objects(self, **kwargs) -> Generator:\n        return self.search_paginator(\n            search_base=self.base_dn_users,\n            search_filter=self._source.user_object_filter,\n            search_scope=SUBTREE,\n            attributes=[ALL_ATTRIBUTES, ALL_OPERATIONAL_ATTRIBUTES],\n            **kwargs,\n        )\n\n    def sync(self, page_data: list) -> int:\n        \"\"\"Iterate over all LDAP Users and create authentik_core.User instances\"\"\"\n        if not self._source.sync_users:\n            self.message(\"User syncing is disabled for this Source\")\n            return -1\n        user_count = 0\n        for user in page_data:\n            if \"attributes\" not in user:\n                continue\n            attributes = user.get(\"attributes\", {})\n            user_dn = self._flatten(user.get(\"entryDN\", user.get(\"dn\")))\n            if self._source.object_uniqueness_field not in attributes:\n                self.message(\n                    f\"Cannot find uniqueness field in attributes: '{user_dn}'\",\n                    attributes=attributes.keys(),\n                    dn=user_dn,\n                )\n                continue\n            uniq = self._flatten(attributes[self._source.object_uniqueness_field])\n            try:\n                defaults = self.build_user_properties(user_dn, **attributes)\n                self._logger.debug(\"Creating user with attributes\", **defaults)\n                if \"username\" not in defaults:\n                    raise IntegrityError(\"Username was not set by propertymappings\")\n                ak_user, created = self.update_or_create_attributes(\n                    User, {f\"attributes__{LDAP_UNIQUENESS}\": uniq}, defaults\n                )\n            except (IntegrityError, FieldError, TypeError, AttributeError) as exc:\n                Event.new(\n                    EventAction.CONFIGURATION_ERROR,\n                    message=(\n                        f\"Failed to create user: {str(exc)} \"\n                        \"To merge new user with existing user, set the user's \"\n                        f\"Attribute '{LDAP_UNIQUENESS}' to '{uniq}'\"\n                    ),\n                    source=self._source,\n                    dn=user_dn,\n                ).save()\n            else:\n                self._logger.debug(\"Synced User\", user=ak_user.username, created=created)\n                user_count += 1\n                MicrosoftActiveDirectory(self._source).sync(attributes, ak_user, created)\n                FreeIPA(self._source).sync(attributes, ak_user, created)\n        return user_count\n", "path": "authentik/sources/ldap/sync/users.py"}], "after_files": [{"content": "\"\"\"LDAP Sync\"\"\"\nfrom django.core.management.base import BaseCommand\nfrom structlog.stdlib import get_logger\n\nfrom authentik.sources.ldap.models import LDAPSource\nfrom authentik.sources.ldap.sync.groups import GroupLDAPSynchronizer\nfrom authentik.sources.ldap.sync.membership import MembershipLDAPSynchronizer\nfrom authentik.sources.ldap.sync.users import UserLDAPSynchronizer\nfrom authentik.sources.ldap.tasks import ldap_sync_paginator\n\nLOGGER = get_logger()\n\n\nclass Command(BaseCommand):\n    \"\"\"Run sync for an LDAP Source\"\"\"\n\n    def add_arguments(self, parser):\n        parser.add_argument(\"source_slugs\", nargs=\"+\", type=str)\n\n    def handle(self, **options):\n        for source_slug in options[\"source_slugs\"]:\n            source = LDAPSource.objects.filter(slug=source_slug).first()\n            if not source:\n                LOGGER.warning(\"Source does not exist\", slug=source_slug)\n                continue\n            tasks = (\n                ldap_sync_paginator(source, UserLDAPSynchronizer)\n                + ldap_sync_paginator(source, GroupLDAPSynchronizer)\n                + ldap_sync_paginator(source, MembershipLDAPSynchronizer)\n            )\n            for task in tasks:\n                task()\n", "path": "authentik/sources/ldap/management/commands/ldap_sync.py"}, {"content": "\"\"\"Sync LDAP Users into authentik\"\"\"\nfrom typing import Generator\n\nfrom django.core.exceptions import FieldError\nfrom django.db.utils import IntegrityError\nfrom ldap3 import ALL_ATTRIBUTES, ALL_OPERATIONAL_ATTRIBUTES, SUBTREE\n\nfrom authentik.core.models import User\nfrom authentik.events.models import Event, EventAction\nfrom authentik.sources.ldap.sync.base import LDAP_UNIQUENESS, BaseLDAPSynchronizer\nfrom authentik.sources.ldap.sync.vendor.freeipa import FreeIPA\nfrom authentik.sources.ldap.sync.vendor.ms_ad import MicrosoftActiveDirectory\n\n\nclass UserLDAPSynchronizer(BaseLDAPSynchronizer):\n    \"\"\"Sync LDAP Users into authentik\"\"\"\n\n    @staticmethod\n    def name() -> str:\n        return \"users\"\n\n    def get_objects(self, **kwargs) -> Generator:\n        return self.search_paginator(\n            search_base=self.base_dn_users,\n            search_filter=self._source.user_object_filter,\n            search_scope=SUBTREE,\n            attributes=[ALL_ATTRIBUTES, ALL_OPERATIONAL_ATTRIBUTES],\n            **kwargs,\n        )\n\n    def sync(self, page_data: list) -> int:\n        \"\"\"Iterate over all LDAP Users and create authentik_core.User instances\"\"\"\n        if not self._source.sync_users:\n            self.message(\"User syncing is disabled for this Source\")\n            return -1\n        user_count = 0\n        for user in page_data:\n            if \"attributes\" not in user:\n                continue\n            attributes = user.get(\"attributes\", {})\n            user_dn = self._flatten(user.get(\"entryDN\", user.get(\"dn\")))\n            if self._source.object_uniqueness_field not in attributes:\n                self.message(\n                    f\"Cannot find uniqueness field in attributes: '{user_dn}'\",\n                    attributes=attributes.keys(),\n                    dn=user_dn,\n                )\n                continue\n            uniq = self._flatten(attributes[self._source.object_uniqueness_field])\n            try:\n                defaults = self.build_user_properties(user_dn, **attributes)\n                self._logger.debug(\"Writing user with attributes\", **defaults)\n                if \"username\" not in defaults:\n                    raise IntegrityError(\"Username was not set by propertymappings\")\n                ak_user, created = self.update_or_create_attributes(\n                    User, {f\"attributes__{LDAP_UNIQUENESS}\": uniq}, defaults\n                )\n            except (IntegrityError, FieldError, TypeError, AttributeError) as exc:\n                Event.new(\n                    EventAction.CONFIGURATION_ERROR,\n                    message=(\n                        f\"Failed to create user: {str(exc)} \"\n                        \"To merge new user with existing user, set the user's \"\n                        f\"Attribute '{LDAP_UNIQUENESS}' to '{uniq}'\"\n                    ),\n                    source=self._source,\n                    dn=user_dn,\n                ).save()\n            else:\n                self._logger.debug(\"Synced User\", user=ak_user.username, created=created)\n                user_count += 1\n                MicrosoftActiveDirectory(self._source).sync(attributes, ak_user, created)\n                FreeIPA(self._source).sync(attributes, ak_user, created)\n        return user_count\n", "path": "authentik/sources/ldap/sync/users.py"}]}