使用 Azure 资源管理器模板添加 Azure 角色分配条件

Azure 角色分配条件是一项额外检查,你可选择将其添加到角色分配中,来提供更精细的访问控制。 例如,为了读取对象,你可添加要求对象具有特定标记的条件。 本文介绍如何使用 Azure 资源管理器模板为角色分配添加条件。

先决条件

必须使用以下版本:

  • 2020-03-01-preview 或更高版本
  • 2020-04-01-preview 或更高版本,如果要对角色分配使用 description 属性
  • 2022-04-01 是第一个稳定版本

有关添加角色分配条件的先决条件的详细信息,请参阅有关条件的先决条件

添加条件

以下模板演示了如何为存储 Blob 数据读取者角色分配条件。 该条件检查容器名称是否等于“blobs-example-container”。

若要使用模板,必须指定以下输入:

  • 要为其分配角色的用户、组、托管标识或应用程序的 ID。
  • 主体的类型,例如 UserGroupServicePrincipal。 有关详细信息,请参阅新服务主体
{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "principalId": {
            "type": "string",
            "metadata": {
                "description": "Principal ID to assign the role to"
            }
        },
        "principalType": {
            "type": "string",
            "metadata": {
                "description": "Type of principal"
            }
        },
        "roleAssignmentGuid": {
            "type": "string",
            "defaultValue": "[newGuid()]",
            "metadata": {
                "description": "New GUID used to identify the role assignment"
            }
        }
    },
    "variables": {
        "StorageBlobDataReader": "[concat(subscription().Id, '/providers/Microsoft.Authorization/roleDefinitions/2a2b9908-6ea1-4ae2-8e65-a410df84e7d1')]" // ID for Storage Blob Data Reader role, but can be any valid role ID
    },
    "resources": [
        {
            "name": "[parameters('roleAssignmentGuid')]",
            "type": "Microsoft.Authorization/roleAssignments",
            "apiVersion": "2022-04-01", // API version to call the role assignment PUT.
            "properties": {
                "roleDefinitionId": "[variables('StorageBlobDataReader')]",
                "principalId": "[parameters('principalId')]",
                "principalType": "[parameters('principalType')]",
                "description": "Role assignment condition created with an ARM template",
                "condition": "((!(ActionMatches{'Microsoft.Storage/storageAccounts/blobServices/containers/blobs/read'})) OR (@Resource[Microsoft.Storage/storageAccounts/blobServices/containers:name] StringEquals 'blobs-example-container'))", // Role assignment condition
                "conditionVersion": "2.0"
            }
        }
    ]
}

角色分配的范围是根据部署级别确定的。 下面是 New-AzResourceGroupDeploymentaz deployment group create 命令示例,演示如何在资源组范围启动部署。

New-AzResourceGroupDeployment -ResourceGroupName example-group -TemplateFile rbac-test.json -principalId $principalId -principalType "User"
az deployment group create --resource-group example-group --template-file rbac-test.json --parameters principalId=$principalId principalType="User"

后续步骤