Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sources/ldap: add rate limit delay, increase timeouts, add request to-restart #12056

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions authentik/sources/ldap/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from django.db import connection, models
from django.templatetags.static import static
from django.utils.translation import gettext_lazy as _
from ldap3 import ALL, NONE, RANDOM, Connection, Server, ServerPool, Tls
from ldap3 import ALL, NONE, RANDOM, RESTARTABLE, Connection, Server, ServerPool, Tls
from ldap3.core.exceptions import LDAPException, LDAPInsufficientAccessRightsResult, LDAPSchemaError
from rest_framework.serializers import Serializer

Expand All @@ -20,7 +20,7 @@
from authentik.lib.config import CONFIG
from authentik.lib.models import DomainlessURLValidator

LDAP_TIMEOUT = 15
LDAP_TIMEOUT = 60
LDAP_UNIQUENESS = "ldap_uniq"
LDAP_DISTINGUISHED_NAME = "distinguishedName"

Expand Down Expand Up @@ -213,8 +213,8 @@ def connection(
connection_kwargs.setdefault("password", self.bind_password)
conn = Connection(
server or self.server(**server_kwargs),
raise_exceptions=True,
receive_timeout=LDAP_TIMEOUT,
raise_exceptions=CONFIG.get("ldap.raise_exceptions", True),
client_strategy=RESTARTABLE,
**connection_kwargs,
)

Expand Down
5 changes: 5 additions & 0 deletions authentik/sources/ldap/sync/base.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Sync LDAP Users and groups into authentik"""

from collections.abc import Generator
from time import sleep

from django.conf import settings
from ldap3 import DEREF_ALWAYS, SUBTREE, Connection
Expand Down Expand Up @@ -91,11 +92,14 @@
controls=None,
paged_size=None,
paged_criticality=False,
rate_limit_delay=None,
):
"""Search in pages, returns each page"""
cookie = True
if not paged_size:
paged_size = CONFIG.get_int("ldap.page_size", 50)
if rate_limit_delay is None:
rate_limit_delay = CONFIG.get_int("ldap.rate_limit_delay", 0)

Check warning on line 102 in authentik/sources/ldap/sync/base.py

View check run for this annotation

Codecov / codecov/patch

authentik/sources/ldap/sync/base.py#L101-L102

Added lines #L101 - L102 were not covered by tests
while cookie:
self._connection.search(
search_base,
Expand All @@ -118,4 +122,5 @@
]
except KeyError:
cookie = None
sleep(rate_limit_delay)

Check warning on line 125 in authentik/sources/ldap/sync/base.py

View check run for this annotation

Codecov / codecov/patch

authentik/sources/ldap/sync/base.py#L125

Added line #L125 was not covered by tests
yield self._connection.response
Loading