Repository: goauthentik/authentik

Issue:
SCIM exhausts all workers
**Describe the bug**

We do not have a SCIM provider configured but there are 8 workers constantly doing SCIM tasks and the number of entries in Redis continues to grow indefinitely (more than 200K right now).

**To Reproduce**
Steps to reproduce the behavior:

1. Have an LDAP source with at least 900 users and another 900 groups, with a total of 147k memberships.
2. Just wait for LDAP sync.
3. See how SCIM tasks take up all the workers despite not having a SCIM provider configured.

**Expected behavior**

Without a SCIM provider, there is no SCIM tasks to do.

**Logs**

All worker logs are similar to the following one:

```
{"event": "Task started", "level": "info", "logger": "authentik.root.celery", "pid": 62722, "task_id": "d8c04738-ba7f-4645-93fd-c5e634c0119c", "task_name": "scim_signal_direct", "timestamp": "2023-09-11T10:34:28.028322"}
{"event": "Task finished", "level": "info", "logger": "authentik.root.celery", "pid": 62722, "state": "SUCCESS", "task_id": "d8c04738-ba7f-4645-93fd-c5e634c0119c", "task_name": "scim_signal_direct", "timestamp": "2023-09-11T10:34:28.065772"}
{"event": "Task started", "level": "info", "logger": "authentik.root.celery", "pid": 62723, "task_id": "9fcf5591-1e26-4572-8f55-1dd50bc789fb", "task_name": "scim_signal_direct", "timestamp": "2023-09-11T10:34:28.472488"}
{"event": "Task finished", "level": "info", "logger": "authentik.root.celery", "pid": 62723, "state": "SUCCESS", "task_id": "9fcf5591-1e26-4572-8f55-1dd50bc789fb", "task_name": "scim_signal_direct", "timestamp": "2023-09-11T10:34:28.518844"}
{"event": "Task started", "level": "info", "logger": "authentik.root.celery", "pid": 62724, "task_id": "7aa43f29-11ba-44e4-9687-e3b002580e6b", "task_name": "scim_signal_direct", "timestamp": "2023-09-11T10:34:28.906288"}
{"event": "Task finished", "level": "info", "logger": "authentik.root.celery", "pid": 62724, "state": "SUCCESS", "task_id": "7aa43f29-11ba-44e4-9687-e3b002580e6b", "task_name": "scim_signal_direct", "timestamp": "2023-09-11T10:34:28.947772"}
{"event": "Task started", "level": "info", "logger": "authentik.root.celery", "pid": 62725, "task_id": "074a50e4-8454-4684-acff-eb46c5a25eff", "task_name": "scim_signal_direct", "timestamp": "2023-09-11T10:34:29.384486"}
{"event": "Task finished", "level": "info", "logger": "authentik.root.celery", "pid": 62725, "state": "SUCCESS", "task_id": "074a50e4-8454-4684-acff-eb46c5a25eff", "task_name": "scim_signal_direct", "timestamp": "2023-09-11T10:34:29.422690"}
{"event": "Task started", "level": "info", "logger": "authentik.root.celery", "pid": 62726, "task_id": "c155e461-1185-40a9-8018-685f399eb750", "task_name": "scim_signal_direct", "timestamp": "2023-09-11T10:34:29.841684"}
{"event": "Task finished", "level": "info", "logger": "authentik.root.celery", "pid": 62726, "state": "SUCCESS", "task_id": "c155e461-1185-40a9-8018-685f399eb750", "task_name": "scim_signal_direct", "timestamp": "2023-09-11T10:34:29.881108"}
{"event": "Task started", "level": "info", "logger": "authentik.root.celery", "pid": 62727, "task_id": "2ebd8a0e-83ca-4224-b14c-fdb4ad1e7c72", "task_name": "scim_signal_direct", "timestamp": "2023-09-11T10:34:30.313036"}
{"event": "Task finished", "level": "info", "logger": "authentik.root.celery", "pid": 62727, "state": "SUCCESS", "task_id": "2ebd8a0e-83ca-4224-b14c-fdb4ad1e7c72", "task_name": "scim_signal_direct", "timestamp": "2023-09-11T10:34:30.355698"}
{"event": "Task started", "level": "info", "logger": "authentik.root.celery", "pid": 62728, "task_id": "8c9a7cec-657d-49f5-a6a6-1e72a5f2d1c6", "task_name": "scim_signal_direct", "timestamp": "2023-09-11T10:34:30.793799"}
{"event": "Task finished", "level": "info", "logger": "authentik.root.celery", "pid": 62728, "state": "SUCCESS", "task_id": "8c9a7cec-657d-49f5-a6a6-1e72a5f2d1c6", "task_name": "scim_signal_direct", "timestamp": "2023-09-11T10:34:30.836449"}
{"event": "Task started", "level": "info", "logger": "authentik.root.celery", "pid": 62729, "task_id": "70fbfbfb-a63e-45df-bd99-fa6ece27181e", "task_name": "scim_signal_direct", "timestamp": "2023-09-11T10:34:31.210426"}
{"event": "Task finished", "level": "info", "logger": "authentik.root.celery", "pid": 62729, "state": "SUCCESS", "task_id": "70fbfbfb-a63e-45df-bd99-fa6ece27181e", "task_name": "scim_signal_direct", "timestamp": "2023-09-11T10:34:31.251003"}
{"event": "Task started", "level": "info", "logger": "authentik.root.celery", "pid": 62730, "task_id": "197ba0ec-424f-440d-9b2d-436cb34cb031", "task_name": "scim_signal_direct", "timestamp": "2023-09-11T10:34:31.684274"}
{"event": "Task finished", "level": "info", "logger": "authentik.root.celery", "pid": 62730, "state": "SUCCESS", "task_id": "197ba0ec-424f-440d-9b2d-436cb34cb031", "task_name": "scim_signal_direct", "timestamp": "2023-09-11T10:34:31.724073"}
{"event": "Task started", "level": "info", "logger": "authentik.root.celery", "pid": 62699, "task_id": "34e4f35a-8d6c-4a3a-96db-58fd30dc1878", "task_name": "scim_signal_direct", "timestamp": "2023-09-11T10:34:32.330238"}
{"event": "Task finished", "level": "info", "logger": "authentik.root.celery", "pid": 62699, "state": "SUCCESS", "task_id": "34e4f35a-8d6c-4a3a-96db-58fd30dc1878", "task_name": "scim_signal_direct", "timestamp": "2023-09-11T10:34:32.368228"}
{"event": "Task started", "level": "info", "logger": "authentik.root.celery", "pid": 62732, "task_id": "fd2ffb15-bf94-4684-b49d-011b61d1d459", "task_name": "scim_signal_direct", "timestamp": "2023-09-11T10:35:03.788787"}
{"event": "Task finished", "level": "info", "logger": "authentik.root.celery", "pid": 62732, "state": "SUCCESS", "task_id": "fd2ffb15-bf94-4684-b49d-011b61d1d459", "task_name": "scim_signal_direct", "timestamp": "2023-09-11T10:35:03.828492"}
{"event": "Task started", "level": "info", "logger": "authentik.root.celery", "pid": 62733, "task_id": "6b536317-decd-4fa9-a39c-083bdd1cf2ac", "task_name": "scim_signal_direct", "timestamp": "2023-09-11T10:35:08.961920"}
{"event": "Task finished", "level": "info", "logger": "authentik.root.celery", "pid": 62733, "state": "SUCCESS", "task_id": "6b536317-decd-4fa9-a39c-083bdd1cf2ac", "task_name": "scim_signal_direct", "timestamp": "2023-09-11T10:35:09.002660"}
{"event": "Task started", "level": "info", "logger": "authentik.root.celery", "pid": 62734, "task_id": "94fde81c-11cd-4811-8bb0-279f7b1b4fa8", "task_name": "scim_signal_direct", "timestamp": "2023-09-11T10:35:19.022284"}
{"event": "Task finished", "level": "info", "logger": "authentik.root.celery", "pid": 62734, "state": "SUCCESS", "task_id": "94fde81c-11cd-4811-8bb0-279f7b1b4fa8", "task_name": "scim_signal_direct", "timestamp": "2023-09-11T10:35:19.061598"}
```

**Version and Deployment (please complete the following information):**

-   authentik version: 2023.6.1
-   Deployment: helm

**Additional context**

Ask whatever you need.



Discussion:

Resolving patch:
diff --git a/authentik/providers/scim/signals.py b/authentik/providers/scim/signals.py
--- a/authentik/providers/scim/signals.py
+++ b/authentik/providers/scim/signals.py
@@ -23,6 +23,8 @@ def post_save_provider(sender: type[Model], instance, created: bool, **_):
 @receiver(post_save, sender=Group)
 def post_save_scim(sender: type[Model], instance: User | Group, created: bool, **_):
     """Post save handler"""
+    if not SCIMProvider.objects.filter(backchannel_application__isnull=False).exists():
+        return
     scim_signal_direct.delay(class_to_path(instance.__class__), instance.pk, PatchOp.add.value)
 
 
@@ -30,6 +32,8 @@ def post_save_scim(sender: type[Model], instance: User | Group, created: bool, *
 @receiver(pre_delete, sender=Group)
 def pre_delete_scim(sender: type[Model], instance: User | Group, **_):
     """Pre-delete handler"""
+    if not SCIMProvider.objects.filter(backchannel_application__isnull=False).exists():
+        return
     scim_signal_direct.delay(class_to_path(instance.__class__), instance.pk, PatchOp.remove.value)
 
 
@@ -40,6 +44,8 @@ def m2m_changed_scim(
     """Sync group membership"""
     if action not in ["post_add", "post_remove"]:
         return
+    if not SCIMProvider.objects.filter(backchannel_application__isnull=False).exists():
+        return
     # reverse: instance is a Group, pk_set is a list of user pks
     # non-reverse: instance is a User, pk_set is a list of groups
     if reverse:


Test patch: