Skip to content

PSMDB-2003 Implement caching for userToDN mapping#1797

Merged
igorsol merged 3 commits intov7.0from
psmdb-2003
Apr 21, 2026
Merged

PSMDB-2003 Implement caching for userToDN mapping#1797
igorsol merged 3 commits intov7.0from
psmdb-2003

Conversation

@igorsol
Copy link
Copy Markdown

@igorsol igorsol commented Apr 8, 2026

Cache the result of LDAPManagerImpl::mapUserToDN() to reduce LDAP server roundtrips during authentication and authorization. The cache uses an LRU eviction policy (via LRUCache) and per-entry TTL.

Two new server parameters control the cache:

  • ldapUserToDNCacheTTLSeconds (default 30, 0 disables)
  • ldapUserToDNCacheSize (default 10000, 0 disables)

Synchronization uses two synchronization mechanisms:

  • synchronized_value: cache configuration snapshot is protected by synchronized_value which allows us to minimize blocking on configuration changes.
  • Inner Mutex: serializes concurrent cache access from mapUserToDN().

The cache is invalidated on runtime changes to ldapUserToDNMapping, ldapUserToDNCacheTTLSeconds, ldapUserToDNCacheSize, ldapServers, ldapQueryUser, and ldapQueryPassword.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Implements an in-memory cache for LDAPManagerImpl::mapUserToDN() results to reduce LDAP roundtrips during auth flows, and wires cache invalidation to relevant LDAP-related runtime parameter updates.

Changes:

  • Added an LRU+TTL cache for user→DN mapping results, plus an explicit cache invalidation API on LDAPManager.
  • Introduced new server parameters ldapUserToDNCacheTTLSeconds and ldapUserToDNCacheSize, with runtime update hooks that invalidate the cache.
  • Updated LDAP option update paths (servers/bind creds/mapping) to invalidate the new cache when inputs affecting DN mapping change.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/mongo/db/ldap/ldap_manager.h Extends the LDAPManager interface with invalidateUserToDNCache().
src/mongo/db/ldap/ldap_manager_impl.h Adds cache data structures and locking members to LDAPManagerImpl.
src/mongo/db/ldap/ldap_manager_impl.cpp Implements cache invalidation and integrates cache lookups/updates into mapUserToDN().
src/mongo/db/ldap_options.idl Adds new cache-related server parameters and on_update hooks for cache invalidation.
src/mongo/db/ldap_options.h Declares new global params and on-update callbacks.
src/mongo/db/ldap_options.cpp Implements invalidation helpers and on-update handlers to clear cache on relevant param updates.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/mongo/db/ldap/ldap_manager_impl.cpp Outdated
Comment thread src/mongo/db/ldap/ldap_manager_impl.cpp Outdated
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 7 out of 7 changed files in this pull request and generated 1 comment.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/mongo/db/ldap/ldap_manager_impl.cpp Outdated
igorsol added 2 commits April 9, 2026 21:08
Cache the result of LDAPManagerImpl::mapUserToDN() to reduce LDAP
server roundtrips during authentication and authorization. The cache
uses an LRU eviction policy (via LRUCache) and per-entry TTL.

Two new server parameters control the cache:
- ldapUserToDNCacheTTLSeconds (default 30, 0 disables)
- ldapUserToDNCacheSize (default 10000, 0 disables)

Synchronization uses a two-level locking scheme:
- RWMutex: mapUserToDN() holds a shared lock for its entire duration;
  invalidateUserToDNCache() holds an exclusive lock to drain and reset.
- Inner Mutex: serializes concurrent cache access from mapUserToDN().

The cache is invalidated on runtime changes to ldapUserToDNMapping,
ldapUserToDNCacheTTLSeconds, ldapUserToDNCacheSize, ldapServers,
ldapQueryUser, and ldapQueryPassword.

ldapUserToDNMapping server parameter parsed on update and BSONArray
result is stored in LDAPManagerImpl::_userToDNMappingSnapshot thus
removing json parsing from each call to mapUserToDN()
Add a new ldapauthz JS test for ldapUserToDNMapping cache behavior.
Cover authentication with cache enabled and disabled, runtime mapping
updates, and cache invalidation when ldapUserToDNCacheTTLSeconds or
ldapUserToDNCacheSize changes.

Configure a short ldapUserCacheInvalidationInterval and wait after
setParameter updates so each auth attempt re-resolves the DN instead of
reusing the $external user cache.
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 7 out of 7 changed files in this pull request and generated 3 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/mongo/db/ldap/ldap_manager_impl.h Outdated
Comment thread src/mongo/db/ldap/ldap_manager_impl.cpp
Comment thread src/mongo/db/ldap/ldap_manager_impl.h
@igorsol igorsol requested review from ktrushin and plebioda April 9, 2026 22:37
Comment thread jstests/ldapauthz/ldapauthz_user_to_dn_cache.js
Replace RWMutex-based synchronization with synchronized_value<shared_ptr>
to avoid blocking mapUserToDN() callers during cache invalidation and
avoid blocking admin setParameter commands on LDAP query timeouts.

Cache configuration is now encapsulated in an immutable UserToDNCacheHolder
that is atomically swapped on parameter changes. In-flight mapUserToDN()
calls continue working with their snapshot while new calls pick up the
fresh cache immediately.
jsTestLog("Test: ldapUserToDNCacheTTLSeconds change invalidates the DN cache");
setParam(conn, {ldapUserToDNMapping: ldapQueryMapping});
assert(authLDAPUser(conn), "Auth should succeed to populate cache");
setParam(conn, {ldapUserToDNCacheTTLSeconds: 60, ldapUserToDNMapping: brokenMapping});
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I can see, changing ldapUserToDNMapping also invalidates the cache so that the cache would be invalidated even if ldapUserToDNCacheTTLSeconds hadn't been changed.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We will address this in separate task

@igorsol igorsol merged commit 58801bc into v7.0 Apr 21, 2026
1 of 2 checks passed
@igorsol igorsol deleted the psmdb-2003 branch April 21, 2026 13:11
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