Read and read/write privileges with secondary native users on Azure DocumentDB

Azure DocumentDB supports secondary native DocumentDB users with specialized read-write and read-only roles, enabling secure delegation of data access. The built-in administrative account, created during cluster provisioning, has full privileges, including user management. Secondary users are automatically replicated to cluster replicas, but user management must be performed on the primary cluster.

Prerequisites

  • An Azure subscription. If you don't have an Azure subscription, create a free account.
  • An existing Azure DocumentDB cluster

Native users and roles

Azure DocumentDB clusters include a built-in administrative account created during cluster provisioning. This account has the privileges required to manage native users and roles.

For applications and delegated access, create separate native users instead of sharing the built-in administrative account. Native users receive access through their assigned roles and are replicated to cluster replicas. User and role management must be performed on the primary cluster.

Azure DocumentDB RBAC uses the following components:

  • A native user is an identity that can authenticate to the cluster.
  • A role is a named set of permissions that can be assigned to a native user.
  • A resource identifies the database or collection to which access applies.
  • An action identifies an operation that can be performed on the scoped resource.
  • A privilege combines a resource with one or more permitted actions.
  • A role assignment associates a role with a native user.

The following example illustrates a privilege scoped to the orders collection in the sales database with allowed privileges:

{
  resource: { db: "sales", collection: "orders" },
  actions: ["find", "insert", "update", "delete"]
}

Choose an access scope

Select a role and scope based on the minimum access required by the user or application.

Access requirement Recommended approach Scope
Read data across databases Assign the readAnyDatabase built-in role. Cluster
Read and write data across databases Assign the readWriteAnyDatabase built-in role. Cluster
Perform supported cluster administration Assign the clusterAdmin built-in role. Cluster
Perform supported data operations within one database Create and assign a database-scoped custom role. Database
Perform supported data operations on a specific collection Create and assign a collection-scoped custom role. Collection

Native roles

Azure DocumentDB supports native role-based access control for secondary users with the following roles:

Description
readWriteAnyDatabase Full read-write permissions, including database management
clusterAdmin Full read-write permissions, including database management
readAnyDatabase Read-only permissions

Manage users and roles by using interactive Mongo Shell (mongosh). ARM REST APIs, ARM-based Azure CLI commands, Bicep, and Terraform don't support secondary-user role assignments. For Microsoft Entra ID secondary users, see Manage secondary Microsoft Entra ID identities.

Important

For users assigned the readWriteAnyDatabase role, an extra step is currently required to apply the role privileges to all existing collections. Run the following command on each applicable database:

const targetDb = db.getSiblingDB("<database-name>");
targetDb.getCollectionInfos({ type: "collection" }).forEach(({ name }) => {
 printjson(targetDb.runCommand({ validate: name, repair: true }));
});

Custom roles

Custom roles let you define supported actions and associate them with a database or collection resource. Use custom roles to implement least-privilege access when a built-in cluster-wide role grants broader access than required.

Database-scoped roles

Use a database-scoped custom role when a user or application requires the same supported data permissions throughout one database but doesn't require equivalent access to other databases in the cluster.

A database-scoped privilege identifies the target database and uses an empty collection value to represent database-level scope:

resource: { db: "<database-name>", collection: "" }

Collection-scoped roles

Use a collection-scoped custom role when access must be limited to a specific collection or when permissions differ between collections in the same database.

A collection-scoped privilege identifies both the target database and collection:

resource: { db: "<database-name>", collection: "<collection-name>" }

Supported custom-role privileges

Custom roles support the following documented data actions:

Action Description
find Read documents from the scoped resource.
insert Insert documents into the scoped resource.
update Update documents in the scoped resource.
delete Delete documents from the scoped resource.

Custom roles don't currently provide separate privileges for index management, database lifecycle operations, or collection lifecycle operations.

Role inheritance

A custom role can inherit permissions from a supported built-in role. Inherited permissions are combined with the privileges defined directly in the custom role.

Important

Custom-to-custom role inheritance and inheritance from a combination of custom and built-in roles aren't supported in the current preview.

Role assignments and effective access

A native user's effective access is determined by the roles assigned to that user. A user can receive access through directly assigned built-in or custom roles.

When you review or change access, consider both parts of the configuration:

  • Review the user's role assignments to confirm that the expected roles are assigned.
  • Review each custom role to confirm that its resource scope and permitted actions are correct.
  • Validate access by connecting as the native user and testing both permitted operations and expected authorization failures.

Revoking one role doesn't necessarily remove the user's effective access to an operation. The user might continue to receive equivalent access through another assigned role.

User and role lifecycle

Native users and roles have separate lifecycles:

  • Creating a native user establishes the identity and assigns its initial roles.
  • Granting a role adds an assignment to an existing user.
  • Revoking a role removes a specific assignment while retaining the user and role definition.
  • Removing a user deletes the native user account.
  • Dropping a custom role removes the custom role definition.

Deleting a user isn't the same as revoking a role. Similarly, dropping a custom role isn't the same as removing that role assignment from a specific user.

For commands and complete procedures, see Configure role-based access control.

Preview limitations

The current preview has the following limitations:

  • updateRole isn't supported. Use grantPrivilegesToRole and revokePrivilegesFromRole to change an existing custom role's privileges. Use grantRolesToUser and revokeRolesFromUser when only a user's assignments must change.
  • Custom roles can inherit roles only through supported built-in-role combinations.
  • Role assignments are subject to the supported role-count limit for the release.
  • rolesInfo doesn't return inheritedRoles or inheritedPrivileges for custom roles.
  • Custom roles support only the documented data privileges.
  • Custom roles are subject to the supported privilege-document limit for the release.