MongoDB LDAP Integration
Introduction
Lightweight Directory Access Protocol (LDAP) is a widely used protocol for accessing and maintaining distributed directory information services. In enterprise environments, LDAP is commonly used for centralized authentication and user management. MongoDB's LDAP integration allows organizations to leverage their existing LDAP infrastructure for authenticating and authorizing MongoDB users.
In this tutorial, we'll explore how MongoDB integrates with LDAP, the benefits of this integration, and step-by-step instructions to set up and configure LDAP authentication and authorization in your MongoDB environment.
Why Use LDAP with MongoDB?
Before diving into configuration details, let's understand why you might want to integrate MongoDB with LDAP:
- Centralized User Management: Maintain user credentials in one place for multiple systems
- Consistent Security Policies: Apply uniform access controls across your organization
- Simplified Administration: Add or remove user access without modifying MongoDB directly
- Enterprise Compliance: Meet organizational security requirements for centralized authentication
Prerequisites
- MongoDB Enterprise Server (LDAP integration is an Enterprise feature)
- A running LDAP server (like OpenLDAP or Active Directory)
- Basic knowledge of LDAP concepts and MongoDB security
LDAP Integration Options
MongoDB supports two main types of LDAP integration:
- LDAP Authentication: Verifies user credentials against an LDAP server
- LDAP Authorization: Uses LDAP to determine what actions users can perform in MongoDB
Let's explore both options in detail.
Setting up LDAP Authentication
LDAP authentication allows MongoDB to verify user credentials against your LDAP directory.
Step 1: Update MongoDB Configuration
Edit your MongoDB configuration file (typically mongod.conf
) to include LDAP authentication settings:
security:
authorization: "enabled"
ldap:
servers: "ldap.example.com"
transportSecurity: "tls"
userToDNMapping:
'[
{
match: "(.+)",
ldapQuery: "ou=Users,dc=example,dc=com??sub?(uid={0})"
}
]'
bind:
queryUser: "cn=mongodb,dc=example,dc=com"
queryPassword: "secret_password"
Let's break down this configuration:
servers
: Your LDAP server's addresstransportSecurity
: Secures the connection with TLSuserToDNMapping
: Maps MongoDB usernames to LDAP Distinguished Names (DNs)bind
: Credentials MongoDB uses to query the LDAP server
Step 2: Restart MongoDB Service
After updating the configuration, restart your MongoDB service:
sudo systemctl restart mongod
Step 3: Create MongoDB Users with LDAP Authentication
Now, create MongoDB users that will authenticate via LDAP:
use admin
db.createUser({
user: "john.doe",
roles: [
{ role: "readWrite", db: "sales" }
],
mechanisms: ["PLAIN"]
})
Note that we specify "PLAIN"
as the authentication mechanism because LDAP authentication uses it.
Step 4: Connect Using LDAP Authentication
Users can now connect using their LDAP credentials:
mongosh --host mongodb.example.com --authenticationMechanism PLAIN \
--authenticationDatabase '$external' -u john.doe -p
After entering their LDAP password, MongoDB will authenticate against the LDAP server and grant access according to their roles.
Setting up LDAP Authorization
LDAP authorization takes integration further by determining user permissions based on LDAP group membership.
Step 1: Update MongoDB Configuration
Extend your configuration to include authorization settings:
security:
authorization: "enabled"
ldap:
servers: "ldap.example.com"
transportSecurity: "tls"
userToDNMapping:
'[
{
match: "(.+)",
ldapQuery: "ou=Users,dc=example,dc=com??sub?(uid={0})"
}
]'
authz:
queryTemplate: "ou=Groups,dc=example,dc=com??sub?(&(objectClass=groupOfNames)(member={USER}))"
The authz.queryTemplate
specifies how MongoDB retrieves a user's LDAP groups.
Step 2: Map LDAP Groups to MongoDB Roles
Create a role mapping file (ldap-roles.json
) to map LDAP groups to MongoDB roles:
{
"roles": [
{
"role": "readWrite",
"db": "sales",
"ldapGroups": ["cn=sales_users,ou=Groups,dc=example,dc=com"]
},
{
"role": "dbAdmin",
"db": "sales",
"ldapGroups": ["cn=sales_admins,ou=Groups,dc=example,dc=com"]
}
]
}
Step 3: Configure MongoDB to Use the Role Mapping File
Update your MongoDB configuration:
security:
ldap:
authz:
roleMapping: "/etc/mongodb/ldap-roles.json"
Step 4: Restart MongoDB Service
Restart your MongoDB service to apply the new configuration:
sudo systemctl restart mongod
Now, when users authenticate via LDAP, their permissions will be determined by their LDAP group memberships.
Authentication Process Visualization
Here's a diagram showing how the LDAP authentication flow works with MongoDB:
Real-World Example: Enterprise Multi-Team Setup
Let's walk through a practical example for a company with different departments using MongoDB.
Scenario:
- Marketing Team: Needs read-only access to the
marketing
database - Analytics Team: Needs read/write access to the
analytics
database - Platform Team: Needs admin access to all databases
LDAP Structure:
dc=example,dc=com
├── ou=Users
│ ├── uid=mark.user
│ ├── uid=anna.analyst
│ └── uid=paul.admin
└── ou=Groups
├── cn=marketing_readers
├── cn=analytics_users
└── cn=platform_admins
Configuration:
security:
authorization: "enabled"
ldap:
servers: "ldap.example.com"
transportSecurity: "tls"
userToDNMapping:
'[
{
match: "(.+)",
ldapQuery: "ou=Users,dc=example,dc=com??sub?(uid={0})"
}
]'
bind:
queryUser: "cn=mongodb,dc=example,dc=com"
queryPassword: "secret_password"
authz:
queryTemplate: "ou=Groups,dc=example,dc=com??sub?(&(objectClass=groupOfNames)(member={USER}))"
roleMapping: "/etc/mongodb/ldap-roles.json"
Role Mapping File (ldap-roles.json
):
{
"roles": [
{
"role": "read",
"db": "marketing",
"ldapGroups": ["cn=marketing_readers,ou=Groups,dc=example,dc=com"]
},
{
"role": "readWrite",
"db": "analytics",
"ldapGroups": ["cn=analytics_users,ou=Groups,dc=example,dc=com"]
},
{
"role": "root",
"db": "admin",
"ldapGroups": ["cn=platform_admins,ou=Groups,dc=example,dc=com"]
}
]
}
With this setup:
- Marketing users like Mark can only read from the marketing database
- Analytics users like Anna can read and write to the analytics database
- Platform admins like Paul have full access to all databases
Troubleshooting LDAP Integration
When setting up LDAP integration with MongoDB, you might encounter some common issues:
1. Connection Issues
If MongoDB can't connect to your LDAP server:
# Test LDAP connectivity from MongoDB server
ldapsearch -H ldap://ldap.example.com -D "cn=mongodb,dc=example,dc=com" -W -b "dc=example,dc=com"
2. Authentication Failures
Check the MongoDB logs for detailed error messages:
tail -f /var/log/mongodb/mongod.log | grep LDAP
3. Role Mapping Issues
Ensure your LDAP groups match exactly what's in the role mapping file:
# Verify LDAP group membership for a user
ldapsearch -H ldap://ldap.example.com -D "cn=mongodb,dc=example,dc=com" -W \
-b "ou=Groups,dc=example,dc=com" "(&(objectClass=groupOfNames)(member=uid=john.doe,ou=Users,dc=example,dc=com))"
Best Practices
- Use TLS/SSL: Always secure the connection between MongoDB and your LDAP server
- Dedicated Bind Account: Create a service account in LDAP specifically for MongoDB
- Least Privilege: Grant minimal necessary permissions to LDAP users and groups
- Regular Audits: Periodically review LDAP groups and role mappings
- Monitor Logs: Set up alerts for failed authentication attempts
Summary
MongoDB's LDAP integration provides a powerful way to centralize user authentication and authorization in enterprise environments. By leveraging your existing LDAP infrastructure, you can:
- Authenticate users against your central directory
- Determine MongoDB access permissions based on LDAP group membership
- Simplify user management across your organization
This integration is particularly valuable for organizations with strict compliance requirements or those managing access for many users across multiple systems.
Additional Resources
Exercises
- Set up a test LDAP server (like OpenLDAP in Docker) and configure MongoDB to authenticate against it.
- Create a role mapping file that grants different permissions to different LDAP groups.
- Configure MongoDB to use both LDAP authentication and authorization.
- Write a script to audit which MongoDB users have access to which databases based on LDAP groups.
- Implement a monitoring solution that alerts on failed LDAP authentication attempts.
By completing these exercises, you'll gain hands-on experience with MongoDB's LDAP integration and be better prepared to implement it in production environments.
If you spot any mistakes on this website, please let me know at [email protected]. I’d greatly appreciate your feedback! :)