Implementing MongoDB Security

1. Enabling Authentication

security:
  authorization: enabled
MethodDetail
CLI--auth
Localhost exceptionAllows 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

RoleScope
read / readWriteDB-level data
dbAdmin / dbOwnerSchema + ops
clusterAdmin / clusterManagerCluster ops
backup / restoreBackup workflows
rootSuperuser

4. Creating Custom Roles

db.createRole({
  role: "orderReader",
  privileges: [{ resource: { db:"app", collection:"orders" }, actions:["find"] }],
  roles: []
});

5. Granting Roles

MethodDetail
db.grantRolesToUser(user, roles)Add roles
db.grantRolesToRole(role, roles)Role inheritance

6. Revoking Roles

MethodDetail
db.revokeRolesFromUserRemove roles
db.revokePrivilegesFromRoleStrip specific actions

7. Using SCRAM Authentication

MechanismDetail
SCRAM-SHA-256Default (4.0+)
SCRAM-SHA-1Legacy

8. Using x.509 Certificate Authentication

AspectDetail
SetupTrust CA, create users with subject DN
ConnectiontlsCertificateKeyFile + authMechanism=MONGODB-X509

9. Using LDAP Authentication

AspectDetail
EditionEnterprise only
MechanismPLAIN over TLS
AuthorizationRole mapping by LDAP group

10. Configuring Kerberos Authentication

AspectDetail
EditionEnterprise only
MechanismGSSAPI

11. Implementing Role-Based Access Control

PrinciplePractice
Least privilegeGrant minimum required actions
SeparationSeparate users for app / admin / backup
AuditCombine with auditLog