Skip to content

Securing MongoDB Deployments with Role-Based Access Control

CoreConceptAugust 3, 20269 min read

Deploying MongoDB without mandatory authentication enabled exposes database ports (default 27017) directly to public internet scanners. Automated bot networks continuously scan cloud ranges for unauthenticated MongoDB instances, causing widespread data exfiltration and ransomware compromises.

Securing production MongoDB instances requires establishing Role-Based Access Control (RBAC) under the Principle of Least Privilege. By enforcing SCRAM-SHA-256 authentication, restricting application users to scoped custom roles, and mandating TLS 1.3 transport encryption, engineering teams harden databases against internal and external threats.

MongoDB Role-Based Access Control & security hardening pillars
MongoDB Role-Based Access Control & security hardening pillars

Mental Model: Authentication & Principle of Least Privilege

MongoDB security is built around two complementary pillars: Authentication (verifying who a user or application is) and Authorization (enforcing what operations that authenticated user is permitted to perform).

By default, fresh MongoDB installations allow unrestricted administrative access without credentials. Enabling --auth forces all client connections to authenticate against the admin or specific target database before executing any command.

Applying the Principle of Least Privilege dictates that application services (e.g., an order service) should never log in using full dbAdminAnyDatabase or root credentials. Instead, each microservice must receive a dedicated database user bound strictly to its required CRUD permissions on its specific collection. For broader infrastructure hardening strategies, review security hardening best practices and postgres partitioning vs sharding.

MongoDB client SCRAM authentication and RBAC authorization flow
MongoDB client SCRAM authentication and RBAC authorization flow

Quick reference

  • Enabling security.authorization in mongod.conf blocks unauthenticated connection access.
  • SCRAM-SHA-256 provides salted challenge-response authentication preventing password sniffing.
  • Principle of Least Privilege restricts application users strictly to necessary collections.
  • Prevents catastrophic dropDatabase or createIndex commands executed by compromised API tokens.
  • Separate administrative user accounts from daily operational application connections.

Remember this

Enable security.authorization in mongod.conf and assign application microservices scoped roles.

Enabling SCRAM-SHA-256 & Admin User Provisioning

MongoDB uses Salted Challenge Response Authentication Mechanism (SCRAM) to authenticate clients securely over network sockets. SCRAM-SHA-256 uses SHA-256 cryptographic hashing with randomized salt iterations, ensuring plaintext passwords are never transmitted over the wire or stored in system collections.

Provisioning a secure MongoDB cluster follows a strict 2-step sequence. First, connect to localhost while authorization is disabled and create the root superuser in the admin database using db.createUser().

Second, update /etc/mongod.conf to set security.authorization: enabled and restart the mongod service process. All subsequent client connections must now provide valid credentials via connection URIs (mongodb://app_user:pass@host:27017/analytics_db?authSource=admin).

Quick reference

  • SCRAM-SHA-256 prevents replay attacks and credential sniffing over un-encrypted sockets.
  • Create root admin user in the admin database before enabling security.authorization.
  • Update mongod.conf with security.authorization: enabled and restart systemd process.
  • Store MongoDB connection URIs exclusively in secure KMS environment secrets.
  • Configure connection pooling options (maxPoolSize) alongside auth credentials.

Remember this

Provision your root admin user prior to enabling authorization in mongod.conf to prevent lockout.

Custom Security Roles & Granular Action Sets

While MongoDB includes built-in convenience roles (read, readWrite, dbAdmin), production enterprise workloads often require finer granularity. Custom Roles allow defining precise privilege sets consisting of allowed actions on targeted resource collections.

For example, an analytics reporting worker requires permission to execute find and aggregate pipelines on the orders collection, but must be explicitly barred from insert, update, delete, or index creation.

Using db.createRole(), define custom privileges specifying allowed action arrays (such as ["find", "aggregate"]) scoped strictly to resource: { db: "store", collection: "orders" }. Assigning custom roles prevents compromised worker nodes from dropping collections or altering schema indexes.

MongoDB client SCRAM authentication and RBAC authorization flow
MongoDB client SCRAM authentication and RBAC authorization flow

Quick reference

  • Built-in roles (readWrite) may grant overly broad permissions on administrative operations.
  • db.createRole() defines granular action privileges on explicit collection resources.
  • Restrict reporting services to read-only actions (find, aggregate) without update/delete.
  • Prevent application users from executing server-wide commands (listDatabases, shutdown).
  • Audit user roles periodically using db.getUser() and db.getRole() commands.

Remember this

Create custom roles using db.createRole() to limit application service privileges to exact collection actions.

TLS/SSL Encryption & Network Access Hardening

Authentication and RBAC alone do not protect data if network traffic travels unencrypted over public networks. TLS 1.3 Transport Encryption encrypts all client-to-server and replica-set inter-node traffic, preventing eavesdropping and man-in-the-middle attacks.

Configure net.tls.mode: requireTLS in mongod.conf, supplying valid X.509 CA certificate bundles (net.tls.certificateKeyFile). In multi-node replica sets, enforce X.509 mutual authentication (security.clusterAuthMode: x509) for internal node heartbeats.

Finally, restrict network binding in mongod.conf (net.bindIp: 127.0.0.1,10.0.1.45) and configure cloud firewall rules (AWS Security Groups, GCP Firewall) to block external internet access to port 27017 entirely.

Quick reference

  • Require TLS 1.3 encryption (net.tls.mode: requireTLS) for all client socket connections.
  • Enforce X.509 mutual certificate authentication for inter-node replica set heartbeats.
  • Bind MongoDB sockets strictly to internal private VPC IP addresses (net.bindIp).
  • Block port 27017 from public internet access using cloud security group firewalls.
  • Rotate TLS X.509 certificates annually using automated cert-manager scripts.

Remember this

Combine TLS 1.3 transport encryption with VPC private IP binding to isolate MongoDB network traffic.

Key takeaway

To verify MongoDB RBAC enforcement, attempt connecting with an unauthenticated client shell. Confirm that db.orders.find() returns an unauthorized error.

Share:

Related Articles

Traditional perimeter-based security ('Castle and Moat') assumes that all traffic inside a private network or Kubernetes

Read

Containers are the foundation of modern cloud deployment, but default container images often ship with bloated Linux OS

Read

Traditional Static Application Security Testing (SAST) tools generate long lists of static warnings that engineers must

Read

Keep learning

Follow a structured path or browse all courses to go deeper.