Implementing MongoDB Security
1. Enabling Authentication
security:
authorization: enabled
| Method | Detail |
| CLI | --auth |
| Localhost exception | Allows creating first user before lockdown |
2. Creating User Accounts
db.createUser({
user: "appUser",
pwd: passwordPrompt(),
roles: [{ role: "readWrite", db: "app" }]
});
3. Using Built-in Roles
| Role | Scope |
| read / readWrite | DB-level data |
| dbAdmin / dbOwner | Schema + ops |
| clusterAdmin / clusterManager | Cluster ops |
| backup / restore | Backup workflows |
| root | Superuser |
4. Creating Custom Roles
db.createRole({
role: "orderReader",
privileges: [{ resource: { db:"app", collection:"orders" }, actions:["find"] }],
roles: []
});
5. Granting Roles
| Method | Detail |
| db.grantRolesToUser(user, roles) | Add roles |
| db.grantRolesToRole(role, roles) | Role inheritance |
6. Revoking Roles
| Method | Detail |
| db.revokeRolesFromUser | Remove roles |
| db.revokePrivilegesFromRole | Strip specific actions |
7. Using SCRAM Authentication
| Mechanism | Detail |
| SCRAM-SHA-256 | Default (4.0+) |
| SCRAM-SHA-1 | Legacy |
8. Using x.509 Certificate Authentication
| Aspect | Detail |
| Setup | Trust CA, create users with subject DN |
| Connection | tlsCertificateKeyFile + authMechanism=MONGODB-X509 |
9. Using LDAP Authentication
| Aspect | Detail |
| Edition | Enterprise only |
| Mechanism | PLAIN over TLS |
| Authorization | Role mapping by LDAP group |
10. Configuring Kerberos Authentication
| Aspect | Detail |
| Edition | Enterprise only |
| Mechanism | GSSAPI |
11. Implementing Role-Based Access Control
| Principle | Practice |
| Least privilege | Grant minimum required actions |
| Separation | Separate users for app / admin / backup |
| Audit | Combine with auditLog |