Skip to main content

๐Ÿ“˜ Role-Based Access Control (RBAC)

Role-Based Access Control (RBAC) in MongoDB provides fine-grained access control for your database. It allows you to grant users specific privileges on databases, collections, and operations.

Understanding RBAC Componentsโ€‹

Rolesโ€‹

A role consists of:

  • Privileges (actions allowed on resources)
  • Roles (other roles from which this role inherits privileges)

Built-in Roles in Atlasโ€‹

  1. Organization Roles (examples)
    • Organization Owner
    • Organization Member
    • Organization Read Only

For all roles see this Documentation.

  1. Project Roles (examples)
    • Project Owner
    • Project Data Access Admin
    • Project Data Access Read/Write
    • Project Read Only

For more project level roles see this Documentation.

Managing Roles in Atlasโ€‹

## Using Atlas CLI to create a user with specific roles
atlas dbusers create \
--username dataAdmin \
--password "securePassword" \
--role "readWrite@admin" \
--projectId your-project-id

## Add additional roles
atlas dbusers update dataAdmin \
--role "readAnyDatabase@admin" \
--projectId your-project-id

Custom Roles Exampleโ€‹

## Create a custom role for sales data access
atlas customDbRoles create <role-name> --inheritedRole read@salesDB --projectId {project_id}

## Assign a custom role
atlas dbusers create --username readOnlyUser --password readOnlyPass --role <role-name> --projectId {project_id}

Role Management Best Practicesโ€‹

  1. Project-Level Access Control

    • Use Project roles for team-based access
    • Implement separate projects for development/production
    • Regular audit of project members
  2. API Access

    • Use programmatic API keys with appropriate roles
    • Rotate API keys regularly
    • Monitor API key usage

Example:

// Create programmatic API key
atlas organizations apiKeys create \
--desc "Application API Key" \
--role ORG_MEMBER

Practical Examplesโ€‹

Application-Specific Rolesโ€‹

// Create read-only analytics role
atlas customDBRoles create analyticsReader \
--privilege '{ resource: { db: "analytics", collection: "" }, actions: ["find"] }' \
--projectId your-project-id

Custom role 'analyticsReader' successfully created.

// Assign role to user
atlas dbusers create \
--username analyst \
--password "securePassword" \
--role analyticsReader \
--projectId your-project-id

Database user 'analyst' successfully created.

Next Stepsโ€‹

After understanding RBAC, lets do a challenge to test our knowledge.