Add /check-header endpoint with caching, distributed locking, and LDAP authorization for Kerberos/SPNEGO#23
Add /check-header endpoint with caching, distributed locking, and LDAP authorization for Kerberos/SPNEGO#23semidark wants to merge 18 commits into
/check-header endpoint with caching, distributed locking, and LDAP authorization for Kerberos/SPNEGO#23Conversation
…O support Adds /check-header endpoint that trusts username from X-Ldap-User header (set by NGINX after Kerberos authentication) and performs LDAP group membership checks for authorization. Includes caching with TTL and per-key locking to prevent thundering herd on LDAP.
- Fix test cache pollution with autouse fixture that resets cache - Fix all ruff linting issues (line length, imports, style) - Use module-level Settings() instance in cache.py for consistency - Add field validator for header_auth_cache_ttl (must be >= 0) - Add header_auth_enabled setting to conditionally enable feature - Document private _connection attribute access from starsessions - Use keyword-only arg for authorized parameter in set_cached_authorization
- Add LRU-based cleanup for per-key locks to prevent unbounded memory growth - Track last access time per lock, prune oldest 10% when exceeding 10,000 locks - Add comprehensive Kerberos/SPNEGO configuration documentation to nginx.rst - Document new environment variables: HEADER_AUTH_ENABLED, LDAP_TRUSTED_USER_HEADER, HEADER_AUTH_CACHE_TTL - Add tests for LRU lock cleanup behavior
The module contains authorization caching logic specific to the header-based auth feature (/check-header endpoint). The new name better reflects its purpose and leaves room for other caching modules in the future.
- Add Kerberos/SPNEGO to Features list - Add HEADER_AUTH_ENABLED and HEADER_AUTH_CACHE_TTL to optional variables - Add new Kerberos/SPNEGO Authentication section with Nginx example
…tting Apply the same security setting to the /check-header endpoint used for Kerberos/SPNEGO authentication. When allow_authorization_filter_header is False, the X-Authorization-Filter header is ignored. Also update test fixtures to patch settings consistently across all modules.
- Add header_auth.py and header_auth_cache.py to project structure - Add note about installing test dependencies with uv sync --group test - Condense from 247 to 188 lines while keeping essential information - Update testing commands to reflect actual usage
|
I know this probably is a bit out of scope for your implementation, but i needed this and took your implementation as the base for my AD (Kerberos / SPNEGO / SSO) solution. With this PR I wanted to ask if you would be interested in adding this Kerberos SSO functionality to the main Repository. PS: The security findings i posted over the last days were the result of me getting to know the Codebase :-) |
|
I have not forgotten about you @semidark. I both want to help you out but I also don't want to add code to the codebase with code that may not commonly be used. I know I am being hypocritical here since I included the Duo MFA workflow, which we use heavily at Caltech. I have two ideas, one of which you could implement immediately, and the other is probably a longer term project:
What are your thoughts? |
|
@semidark Thank you for the pull request! I think you should fork this repository, rename your fork project I'd like to deal with nginx external auth the way Apache used to with its |
Summary
This PR implements a stateless authorization endpoint for ActiveDirectory (Kerberos/SPNEGO) authentication, enabling SSO scenarios where NGINX handles authentication with the SPNEGO module and this service performs LDAP group authorization.
New Features
/check-headerEndpointnginx_ldap_auth/app/header_auth.pyX-Ldap-Userby default) set by NGINX after Kerberos/SPNEGO authenticationX-Authorization-Filterheader for dynamic authorization filters (configurable viaallow_authorization_filter_header)200 OK- User is authorized401 Unauthorized- Missing username header403 Forbidden- User failed LDAP authorization filter500 Internal Server Error- LDAP connection/query errorAuthorization Cache
nginx_ldap_auth/app/header_auth_cache.pyheader_auth_cache_ttlsetting (default: 300 seconds, 0 to disable)Distributed Locking for Thundering Herd Protection
asyncio.Lockfor in-memory backend or Redis SETNX for distributed scenariosConfiguration Settings
nginx_ldap_auth/settings.pyheader_auth_enabled- Enable/disable the/check-headerendpoint (default:True)ldap_trusted_user_header- Header containing the authenticated username (default:X-Ldap-User)header_auth_cache_ttl- Cache TTL in seconds (default:300, set to0to disable)allow_authorization_filter_header- Allow per-request filter via header (default:True)NGINX Integration Example
Benefits
Testing
test/test_header_auth.pyincluding cache hit/miss scenarios, lock contention handling, Redis backend simulation, and edge casesDocumentation
Migration Notes
header_auth_cache_ttlis set to0