Configure role-based access control in Azure DocumentDB

Role-based access control (RBAC) in Azure DocumentDB enables you to define and assign permissions based on the level of access required by your users. In this article, you learn how to create custom roles with specific privileges, create native users and assign roles to them, review role definitions and user assignments, and validate the resulting access. By the end, you have an end-to-end workflow for configuring and verifying role-based access in Azure DocumentDB.

Important

Custom roles and fine-grained, database- and collection-level privileges are currently in preview.

Prerequisites

  • An Azure subscription.
  • An existing Azure DocumentDB cluster.
  • MongoDB Shell installed on the client.
  • Firewall rules that allow the client to connect.
  • The built-in administrative credentials created during cluster provisioning.

Note

User and role management must be performed on the primary cluster.

Connect as an administrator

  1. In the Azure portal, open the Azure DocumentDB cluster.
  2. Under Settings, select Connection strings.
  3. Copy the native connection string.
  4. Replace the placeholders with the built-in administrative credentials.
  5. Connect with mongosh.
mongosh "mongodb+srv://<username>:<password>@<cluster-name>?tls=true&authMechanism=SCRAM-SHA-256&retrywrites=false&maxIdleTimeMS=120000"

Create a database-scoped custom role

A database-scoped custom role lets you grant a user only the privileges required for a specific database, rather than providing the same access across all databases in the cluster. Use this capability to implement least-privilege access when an application or user needs access to one database but doesn't require equivalent permissions elsewhere.   The following example shows how to create a database-scoped custom role. Replace role-name with the name of the custom role and database-name with the database to which the privileges apply.

use admin

db.runCommand({
  createRole: "<role-name>",
  privileges: [
    {
      resource: {
        db: "<database-name>",
        collection: ""
      },
      actions: ["find"]
    }
  ],
  roles: []
})

Create a collection-scoped custom role

A collection-scoped custom role lets you grant privileges on a specific collection within a database. This level of granularity is useful when a user or application requires access to a particular collection but shouldn't receive the same privileges on other collections in the database. The following example shows how to create a collection-scoped custom role. Replace role-name with the name of the custom role, database-name with the database that contains the target collection, and collection-name with the collection to which the privileges apply.   In the resource definition:  

  • db identifies the database that contains the target collection.
  • collection limits the privilege to the orders collection.
  • actions: ["find", "insert", "update", "remove"] defines the operations permitted by the role.
  • roles: [] specifies that this role doesn't inherit privileges from another role.
use admin

db.runCommand({
  createRole: "<role-name>",
  privileges: [
    {
      resource: {
        db: "<database-name>",
        collection: "<collection-name>"
      },
      actions: ["find", "insert", "update", "remove"]
    }
  ],
  roles: []
})

Create a user and assign a role

After you create a custom role, you can assign the role to a native user or a Microsoft Entra ID user. The user receives the privileges defined by the assigned role, allowing you to separate role definitions from individual user accounts and manage access according to the permissions required by each user or application. The following example shows how to create a native user and assign a custom role. Replace username with the name of the user that you want to create and role-name with the name of the custom role that you want to assign.   In the user definition:  

  • createUser: "username" specifies the name of the native user to create.
  • pwd: passwordPrompt() securely prompts for the user's password instead of including the password directly in the command.
  • role: "role-name" identifies the custom role assigned to the user.
  • db: "admin" identifies the database in which the custom role is defined.
use admin

db.runCommand({
  createUser: "<username>",
  pwd: passwordPrompt(),
  roles: [
    {
      role: "<role-name>",
      db: "admin"
    }
  ]
})

Configure password expiration

You can optionally configure an expiration date for a native user's password. After the specified date and time, the password can't be used to establish new authenticated connections.

Password expiration doesn't delete the user, remove assigned roles, or terminate existing authenticated connections.

Important

Existing authenticated connections remain active after the password expires. Renew or rotate credentials before expiration to avoid failures when applications establish new connections.

Password expiration applies only to native, password-based users. For Microsoft Entra ID users, credential expiration is managed by the identity provider.

Create a user with password expiration

Specify the expiration date by using customData.validUntil. The value must be a BSON datetime in UTC.

use admin

db.runCommand({
  createUser: "<username>",
  pwd: passwordPrompt(),
  roles: [
    {
      role: "<role-name>",
      db: "admin"
    }
  ],
  customData: {
    validUntil: ISODate("2027-01-01T00:00:00.000Z")
  }
})

Review password expiration

Use usersInfo to review the password expiration date:

use admin

db.runCommand({
  usersInfo: "<username>"
})

When you configure expiration, the response includes customData.validUntil. Users without password expiration don't include this field.

Renew or rotate an expiring password

To extend the expiration date without changing the password:

use admin

db.runCommand({
  updateUser: "<username>",
  customData: {
    validUntil: ISODate("2027-04-01T00:00:00.000Z")
  }
})

To rotate the password and set a new expiration date together:

use admin

db.runCommand({
  updateUser: "<username>",
  pwd: passwordPrompt(),
  customData: {
    validUntil: ISODate("2027-04-01T00:00:00.000Z")
  }
})

Note

Changing only password preserves the existing expiration date. If that date has already passed, specify a future validUntil value to restore new authenticated connections.

Remove password expiration

Set validUntil to null to remove password expiration without changing the password:

use admin

db.runCommand({
  updateUser: "<username>",
  customData: {
    validUntil: null
  }
})

Omitting validUntil doesn't remove an existing expiration date.

Important

Password expiration doesn't terminate existing authenticated connections. Applications using connection pooling might continue to use existing connections after expiration but fail when establishing new connections.

Grant and revoke roles and privileges

You can modify access for existing native users without recreating the user or redefining a custom role. Depending on the access change required, you can either:

  • Grant or revoke a role assigned to a user.
  • Grant or revoke privileges associated with an existing custom role.

Grant a role to a user

Use grantRolesToUser when an existing native user requires additional access through a built-in or custom role.

The following example grants a role to an existing native user. Replace username with the name of the user and role-name with the name of the role that you want to grant.

use admin

db.runCommand({
    grantRolesToUser: "<username>",
    roles: [
        { role: "<role-name>", db: "admin" }
    ]
})

Revoke a role from a user

Use revokeRolesFromUser when a user or application no longer requires the access provided by a particular role. Revoking the role removes the role assignment without deleting the user or modifying the role definition.

The following example revokes a role from an existing native user.

use admin

db.runCommand({
    revokeRolesFromUser: "<username>",
    roles: [
        { role: "<role-name>", db: "admin" }
    ]
})

Tip

Revoking a role removes that specific role assignment from the user. If other roles assigned to the user provide equivalent privileges, the user might continue to have access through those role assignments.

Grant privileges to a custom role

Use grantPrivilegesToRole to add privileges to an existing custom role without recreating the role.

The following example grants find access on a specific collection to an existing custom role. Replace role-name with the name of the custom role, database-name with the target database, and collection-name with the target collection.

use admin

db.runCommand({
    grantPrivilegesToRole: "<role-name>",
    privileges: [
        {
            resource: {
                db: "<database-name>",
                collection: "<collection-name>"
            },
            actions: ["find"]
        }
    ]
})

Revoke privileges from a custom role

Use revokePrivilegesFromRole to remove privileges from an existing custom role.

The following example revokes find access on a specific collection from an existing custom role.

use admin

db.runCommand({
    revokePrivilegesFromRole: "<role-name>",
    privileges: [
        {
            resource: {
                db: "<database-name>",
                collection: "<collection-name>"
            },
            actions: ["find"]
        }
    ]
})

Important

The updateRole command isn't currently supported. To modify an existing custom role, use grantPrivilegesToRole to add privileges and revokePrivilegesFromRole to remove privileges

Review users and assigned roles

After creating users or changing role assignments, review the existing native users and their assigned roles to verify that access is configured as intended.

use admin

db.runCommand({ usersInfo: 1 })

Review a role and its privileges

After creating a custom role, review its definition and privileges to verify that the role contains the intended permissions and resource scope. The following example retrieves information for the ordersOperator custom role and includes its privileges in the command output.

use admin
 
db.runCommand({
rolesInfo: { role: "<role-name>", db: "admin" },
showPrivileges: true
})

Note

In the current preview, rolesInfo doesn't return inheritedRoles or inheritedPrivileges for custom roles.

Drop a custom role

  When a custom role is no longer required, you can remove the role by using the dropRole command. Dropping a custom role is different from revoking the role from a user. revokeRolesFromUser removes a role assignment from a specific user, while dropRole removes the custom role definition itself. Run the following command from the admin database, replacing <role-name> with the name of the custom role that you want to remove.  

use admin
 
db.runCommand({
dropRole: "<role-name>"
})

Remove a user

When a native user no longer requires access to Azure DocumentDB, you can remove the user by using the dropUser command.Removing a user is different from revoking an individual role assignment. Run the following command from the admin database, replacing username with the name of the native user that you want to remove.

use admin

db.runCommand({ dropUser: "<username>" })