Azure CosmosDB .Net 库中出现 RBAC 权限错误的原因是什么?

问题描述 投票:0回答:1

在测试 Azure CosmosDB noSQL 的基本功能时,我不断收到此错误:

“请求被阻止,因为主体 [PrincipalID] 没有执行操作所需的 RBAC 权限”

(完全错误)

“响应状态码不表示成功:Forbidden (403); 子状态:5301;活动ID:[活动ID];原因: ({\"code\":\"Forbidden\",\"message\":\"请求被身份验证阻止 [cosmosdbaccountname] :请求被阻止,因为主体 [PrincipalID] 没有执行所需的 RBAC 权限 操作 [Microsoft.DocumentDB/databaseAccounts/readMetadata] 上 资源[/]。请求Uri: https://[cosmosdbaccountname].documents.azure.com/; 请求方式: 得到; 标头: 授权长度: [授权长度]; 标头:缓存控制长度: 8; 标头:用户代理长度:[UserAgentLength]; 标题: x-ms-版本长度:10; 标题: x-ms-cosmos-sdk-supportedcapability 长度:1; 标题:接受 长度:16; ,请求 URI:/,RequestStats:,SDK: Windows/10.0.19045 cosmos-netstandard-sdk/3.37.1);"}Microsoft.Azure.Cosmos.CosmosException"

Azure 门户:
azure portal

我是该数据库的所有者和唯一用户,我尝试通过再次将该角色添加到我的帐户来确保我是所有者。我使用 az login 和 azd auth login 登录。我已经有了数据库和容器。

这是我使用它的代码(以英国标准为例)

using Azure.Identity;
using Microsoft.Azure.Cosmos;

CosmosClient client = new CosmosClient(
    accountEndpoint: "https://[censored].documents.azure.com:[censored]/",
    tokenCredential: new DefaultAzureCredential()
);
Database database = client.GetDatabase("SampleDB");
Container container = database.GetContainer("British-Standards");

British_Standard standard = new()
{
    id = "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb",
    Standard_Name = "BS 5720: Ventilation and Air Conditioning",
    Standard_Description = "This standard provides guidance on the design,"+
        " installation, testing, and commissioning of ventilation and air conditioning systems in buildings.",
    Standard_Category = (StandardCategories)1 //Enum
};

ItemResponse<British_Standard> response = await container.CreateItemAsync(standard); //error here

还尝试了 CreateContainerIfNotExists 方法,该方法本身就出现错误。
首先在 vscode 中进行了测试,然后在 Visual Studio 中进行了调试器测试。

c# azure azure-cosmosdb azure-cosmosdb-sqlapi
1个回答
0
投票

请求被阻止,因为主体 [PrincipalID] 不具备对资源执行操作 [Microsoft.DocumentDB/databaseAccounts/readMetadata] 所需的 RBAC 权限

根据错误,您需要向服务主体提供

Microsoft.DocumentDB/databaseAccounts/readMetadata
。要解决此问题,您需要创建自定义角色以向服务主体分配所需的权限。

您可以按照以下步骤创建角色并将其分配给服务主体:

  • 使用
    New-AzCosmosDBSqlRoleDefinition
    创建新的角色定义。
$parameters = @{
    ResourceGroupName = "RGName"
    AccountName = "CosmosAccName"
    RoleName = "Azure Cosmos DB for NoSQL Data Plane Owner"
    Type = "CustomRole"
    AssignableScope = @(
        "/"
    )
    DataAction = @(
        "Microsoft.DocumentDB/databaseAccounts/readMetadata",
        "Microsoft.DocumentDB/databaseAccounts/sqlDatabases/containers/*",
        "Microsoft.DocumentDB/databaseAccounts/sqlDatabases/containers/items/*"
    )
}
New-AzCosmosDBSqlRoleDefinition @parameters

  • 使用 [
    Get-AzCosmosDBSqlRoleDefinition
    ] 列出与 Azure Cosmos DB 关联的所有角色定义。
$parameters = @{
    ResourceGroupName = "RGName"
    AccountName = "CosmosAccName"
}
Get-AzCosmosDBSqlRoleDefinition @parameters

enter image description here

  • 使用
    Get-AzCosmosDBAccount
    获取您当前帐户的 ID。
$parameters = @{
    ResourceGroupName = "RGName"
    Name = "CosmosAccName"
}    
Get-AzCosmosDBAccount @parameters | Select -Property Id

enter image description here

  • 使用
    New-AzCosmosDBSqlRoleAssignment
    分配新角色。使用
    Get-AzCosmosDBSqlRoleAssignment
    来检查列出适用于 NoSQL 的 Azure Cosmos DB 帐户的所有角色分配。
$parameters = @{
    ResourceGroupName = "RGName"
    AccountName = "CosmosAccName"
    RoleDefinitionId = "Id you will get from above created role"
    PrincipalId = "Object ID that you will get from Enterprise applications"
    Scope = "ID obtain fromlast step"
}    
New-AzCosmosDBSqlRoleAssignment @parameters
$parameters = @{
    ResourceGroupName = "RGName"
    AccountName = "CosmosAccName"
}
Get-AzCosmosDBSqlRoleAssignment @parameters

enter image description here

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.