Skip to content

Commit 104904e

Browse files
committed
fix: stale LDAP pool connections and refactor client error handling
1 parent 0245602 commit 104904e

6 files changed

Lines changed: 463 additions & 293 deletions

File tree

‎README.md‎

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ settings = LDAPSettings(
101101
# Connection pool
102102
pool_size=10,
103103
pool_timeout=30.0,
104+
pool_max_idle_seconds=1800.0, # Discard idle connections after 30 min (AD/LDAP stale fix)
104105

105106
# Caching (optional)
106107
cache_enabled=True,
@@ -119,6 +120,7 @@ export LDAP_BIND_DN="cn=admin,dc=example,dc=com"
119120
export LDAP_BIND_PASSWORD="secret"
120121
export LDAP_CACHE_ENABLED="true"
121122
export LDAP_CACHE_TTL="300"
123+
export LDAP_POOL_MAX_IDLE_SECONDS="1800"
122124
```
123125

124126
**Important: Passwords with Special Characters**
@@ -140,19 +142,46 @@ export LDAP_BIND_PASSWORD=pass$word
140142

141143
## Active Directory Example
142144

145+
`bind_dn` / `bind_password` are for a **service account** (connection pool and LDAP searches). End users authenticate with their own username and password via HTTP Basic Auth on protected routes — do not use the service password for user login.
146+
143147
```python
144148
settings = LDAPSettings(
145149
ldap_url="ldaps://ad.example.com:636",
146150
ldap_base_dn="dc=example,dc=com",
147-
bind_dn="CN=Service Account,CN=Users,DC=example,DC=com",
148-
bind_password="password",
151+
# Service account (UPN format recommended)
152+
bind_dn="ldap-service@example.com",
153+
bind_password="service-account-password",
149154
user_search_filter="(sAMAccountName={username})",
150-
user_search_base="CN=Users,DC=example,DC=com",
155+
user_search_base="DC=example,DC=com", # Use domain root unless you intentionally restrict OUs
151156
group_search_filter="(member:1.2.840.113556.1.4.1941:={user_dn})", # Recursive group search
152157
group_attribute="cn",
158+
pool_max_idle_seconds=900, # Refresh idle connections; AD often drops them after ~15 min
153159
)
154160
```
155161

162+
HTTP Basic login accepts plain username, UPN (`user@example.com`), or NetBIOS (`DOMAIN\user`) — the client normalizes these for `sAMAccountName` search.
163+
164+
## Connection Pool and Long-Running Services
165+
166+
LDAP and Active Directory often close idle TCP connections. Without refresh, a backend that runs for hours may start returning `401 Invalid credentials` for valid users until restart.
167+
168+
`fastapi-ldap` mitigates this by:
169+
170+
- Rebinding to the service account on every pool checkout and return
171+
- Discarding connections idle longer than `pool_max_idle_seconds` (default **1800** / 30 minutes)
172+
- Replacing dead connections and retrying authentication once on connection errors
173+
174+
For AD deployments with short idle timeouts, lower the idle limit:
175+
176+
```python
177+
LDAPSettings(
178+
...
179+
pool_max_idle_seconds=900, # 15 minutes
180+
)
181+
```
182+
183+
Set `pool_max_idle_seconds=0` to disable age-based discard (not recommended for production AD).
184+
156185
## API Reference
157186

158187
### Dependencies
@@ -168,8 +197,8 @@ settings = LDAPSettings(
168197
- `username` - Username
169198
- `email` - Email address (optional)
170199
- `display_name` - Display name (optional)
171-
- `groups` - Frozen set of group names
172-
- `attributes` - Additional LDAP attributes
200+
- `groups` - Frozen set of group names (from group search)
201+
- `attributes` - Additional LDAP attributes (`str` or `list[str]` for multi-valued fields such as AD `memberOf`)
173202
- `has_group(group)` - Check if user belongs to a group
174203
- `has_any_group(groups)` - Check if user belongs to any group
175204
- `has_all_groups(groups)` - Check if user belongs to all groups
@@ -183,9 +212,11 @@ settings = LDAPSettings(
183212

184213
- **TLS is enabled by default** - Disable only for testing in secure environments
185214
- **Anonymous binds are disabled by default** - Must be explicitly enabled
215+
- **Use a dedicated service account** for `bind_dn` with least-privilege read access; never use Domain Admin
186216
- **Authentication failures are indistinguishable** - Prevents user enumeration
187217
- **No credential logging** - Passwords are never logged
188-
- **Fail-closed behavior** - If LDAP is unavailable, authentication fails securely
218+
- **Fail-closed behavior** - If LDAP is unavailable, authentication fails securely (503 for infra errors, 401 for bad credentials)
219+
189220

190221
## Architecture
191222

0 commit comments

Comments
 (0)