Skip to content

Improve LDAP integration - #132

Merged
lubomir merged 6 commits into
mainfrom
ldap-timeout
Nov 11, 2026
Merged

lubomir merged 6 commits into
mainfrom
ldap-timeout

Conversation

@lubomir

@lubomir lubomir commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

Set OPT_NETWORK_TIMEOUT and OPT_TIMELIMIT on the LDAP client so that a slow or unresponsive LDAP server does not block requests indefinitely.

The timeout is controlled by the new AUTH_LDAP_TIMEOUT config option (default: 10 seconds).

The code already supports multiple configured servers. The second commit changes it so that iterating over the servers is done in the application itself, as that seems to be more reliable than letting python-ldap handle it.

@codecov-commenter

codecov-commenter commented Oct 16, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82.87%. Comparing base (4696759) to head (a45e461).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #132      +/-   ##
==========================================
+ Coverage   82.44%   82.87%   +0.42%     
==========================================
  Files          15       15              
  Lines        1413     1448      +35     
==========================================
+ Hits         1165     1200      +35     
  Misses        248      248              
Flag Coverage Δ
unit-tests 82.87% <100.00%> (+0.42%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@lubomir lubomir changed the title auth: add timeouts to LDAP communication Improve LDAP integration Nov 5, 2026
Set OPT_NETWORK_TIMEOUT, OPT_TIMEOUT and OPT_TIMELIMIT on the LDAP
client so that a slow or unresponsive LDAP server does not block
requests indefinitely.

OPT_NETWORK_TIMEOUT bounds the TCP connect, OPT_TIMEOUT bounds the
overall wait of synchronous calls (bind, search), and OPT_TIMELIMIT
asks the server to cap its own search time.

The timeout is controlled by the new AUTH_LDAP_TIMEOUT config option
(default: 10 seconds).
Split AUTH_LDAP_SERVER on whitespace into individual URIs and iterate
them explicitly in Python rather than passing the full string to
ldap.initialize() and relying on the OpenLDAP C library's opaque
internal failover logic.

The new _query_single_ldap_server() helper handles one full
connect/configure/bind/search/unbind cycle against a single URI and
raises ldap.LDAPError on failure. query_ldap_groups() iterates over
all URIs in order, returning from the first that succeeds, logging
per-server errors, and returning [] after all servers are exhausted.

Also:
- Escape uid with ldap.filter.escape_filter_chars() before
  interpolating into filter strings to prevent LDAP injection.
- Improve _ldap_error_description() to include both the 'desc' and
  'info' fields from LDAPError (previously 'info' was silently dropped,
  hiding e.g. 'Connection timed out' from logs).

Tests added for failover, all-servers-fail, error description format,
and uid escaping.
ldap.TIMEOUT (and potentially other C-library-raised exceptions) is
raised with no arguments, making str(error) return an empty string.
This caused the log line to show 'Error: ' with nothing after it,
giving no indication of what went wrong.

Add a final fallback of type(error).__name__ so the log always contains
at least the exception class name (e.g. 'TIMEOUT') instead of silence.

Add two direct unit tests for _ldap_error_description: one for plain
LDAPError() with no args and one for the concrete ldap.TIMEOUT subclass.
@onosek

onosek commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

Bind-credential failures are retried against every server — cts/auth.py:266
ldap.INVALID_CREDENTIALS is a subclass of ldap.LDAPError, so a wrong or expired AUTH_LDAP_BIND_PASSWORD under simple bind is treated as "server is down" and retried against each remaining URI — N bad-password attempts per request, spread across N DCs, driving the service account toward lockout much faster than before. libldap's own failover never retries on a bind rejection. Suggest failing over only on SERVER_DOWN / TIMEOUT / CONNECT_ERROR and aborting on auth and schema errors.

Suggested fix:
Retry only on errors that mean this server is unusable; treat everything else as fatal, since it will fail identically on every server.

Comment thread cts/auth.py Resolved
Errors that indicate a misconfiguration or a logical problem (wrong
credentials, bad base DN, unsupported auth method, etc.) will reproduce
identically on every replica, so retrying further servers only delays
the failure. Introduce _LDAP_NON_RETRYABLE_ERRORS and catch it before
the generic LDAPError handler to return early in those cases.

Transient errors (SERVER_DOWN, TIMEOUT, BUSY, ...) continue to trigger
failover to the next configured server as before.
Comment thread cts/auth.py Resolved
@onosek

onosek commented Nov 10, 2026

Copy link
Copy Markdown
Contributor

LGTM

…LDAP errors

Both errors indicate conditions that will reproduce identically on every
replica, so retrying a different server would not help.

- AUTH_UNKNOWN (86): client does not recognise the authentication method,
  a client-side configuration issue.
- CONFIDENTIALITY_REQUIRED (13): server requires a TLS-protected session;
  all replicas share the same policy.
@lubomir
lubomir merged commit eca98bf into main Nov 11, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants