RBAC permission issue while accessing cosmos db using managed identity

siddharth bansal 346 Reputation points
2023-06-20T13:59:56.9166667+00:00

I want to read/write data in Cosmos DB from azure function, added code in azure functions(c#) to access cosmos db using managed identity ,role for azure function in Cosmos db is Document db Account Contributor & Cosmos DB Account Reader role & As per the microsoft document ,readmetadata is included in Data Contributor role but still getting below error :

Response status code does not indicate success: Forbidden (403); Substatus: 5301; ActivityId:<>; Reason: (Request blocked by Auth

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,679 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. TP 117.2K Reputation points
    2023-06-28T12:56:05.6666667+00:00

    Hi Siddharth,

    Please confirm that you have assigned Cosmos DB Built-in Data contributor to the managed identity using New-AzCosmosDBSqlRoleAssignment or az cosmosdb sql role assignment create . For example, you would use command similar to below :

    New-AzCosmosDBSqlRoleAssignment -AccountName my-cosmosdb -ResourceGroupName cosmos-rg -RoleDefinitionId 00000000-0000-0000-0000-000000000002 -Scope "/" -PrincipalId "<object (principal) ID of managed identity>"
    
    

    For reference, please see articles below:

    Configure role-based access control with Azure Active Directory for your Azure Cosmos DB account

    https://learn.microsoft.com/en-us/azure/cosmos-db/how-to-setup-rbac

    https://learn.microsoft.com/en-us/powershell/module/az.cosmosdb/new-azcosmosdbsqlroleassignment

    Thanks.

    -TP


  2. Ryan Hill 30,181 Reputation points Microsoft Employee
    2023-11-13T16:47:27.7333333+00:00

    Hi @siddharth bansal

    The readMetadata is not included on those roles. This document actually walks you through creating a custom role for that permission and assigning it to your managed identity.

    Create a role definition JSON file to create and the definition and role; then assign this role to your managed identity.

    {
        "RoleName": "Read Azure Cosmos DB Metadata",
        "Type": "CustomRole",
        "AssignableScopes": ["/"],
        "Permissions": [{
            "DataActions": [
                "Microsoft.DocumentDB/databaseAccounts/readMetadata"
            ]
        }]
    }
    
    az cosmosdb sql role definition create --resource-group $resourceGroupName --account-name $cosmosName --body @definition.json
    
    az cosmosdb sql role assignment create --resource-group $resourceGroupName --account-name $cosmosName --role-definition-name "Read Azure Cosmos DB Metadata" --principal-id $principal --scope $scope
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.