共用方式為


了解 Azure 角色定義

如果您想嘗試了解 Azure 角色的運作方式或要建立自己的 Azure 自訂角色,最好先了解如何定義角色。 本文詳細描述角色定義並提供一些範例。

角色定義

角色定義是一個權限集合。 有時簡稱為「角色」。 角色定義會列出可執行的動作,例如讀取、寫入和刪除。 它也可以列出從允許的動作中排除的動作或與基礎資料相關的動作。

下列範例顯示使用 Azure PowerShell 顯示角色定義中的屬性:

Name
Id
IsCustom
Description
Actions []
NotActions []
DataActions []
NotDataActions []
AssignableScopes []
Condition
ConditionVersion

下列範例顯示如何使用 Azure CLIREST API 顯示角色定義中的屬性:

roleName
name
id
roleType
type
description
actions []
notActions []
dataActions []
notDataActions []
assignableScopes []
condition
conditionVersion
createdOn
updatedOn
createdBy
updatedBy

下表描述角色屬性的意義。

屬性 說明
Name
roleName
角色的顯示名稱。
Id
name
角色的唯一識別碼。 內建角色在不同雲端中會具有相同的角色識別碼。
id 角色的完整唯一標識碼。 即使角色已重新命名,角色識別碼也不會變更。 最佳做法是在指令碼中使用角色識別碼。
IsCustom
roleType
指出此角色是否為自訂角色。 若為自訂角色,請設為 trueCustomRole。 若為內建角色,請設為 falseBuiltInRole
type 物件的類型。 設定為 Microsoft.Authorization/roleDefinitions
Description
description
角色描述。
Actions
actions
字串陣列,用來指定角色允許執行的控制平面動作。
NotActions
notActions
字串陣列,用來指定從允許的 Actions 中排除的控制平面動作。
DataActions
dataActions
字串陣列,用來指定角色允許對物件內資料執行的資料平面動作。
NotDataActions
notDataActions
字串陣列,用來指定從允許的 DataActions 中排除的資料平面動作。
AssignableScopes
assignableScopes
字串陣列,用來指定角色可用於指派的範圍。
Condition
condition
以角色定義中的一或多個動作為依據的條件陳述式 (適用於內建角色)。
ConditionVersion
conditionVersion
條件版本號碼。 預設為 2.0,且是唯一支援的版本。
createdOn 建立角色的日期和時間。
updatedOn 上次更新角色的日期和時間。
createdBy 建立角色的主體 (適用於自訂角色)。
updatedBy 更新角色的主體 (適用於自訂角色)。

動作格式

動作是用具有下列格式的字串來指定:

  • {Company}.{ProviderName}/{resourceType}/{action}

動作字串的 {action} 部分指定您可以對資源類型執行的動作類型。 例如,您將會在 {action} 中看到下列子字串:

動作子字串 描述
* 此萬用字元會授與所有符合字串之動作的存取權。
read 啟用讀取動作 (GET)。
write 啟用寫入動作 (PUT 或 PATCH)。
action 啟用如重新啟動虛擬機器的自訂動作 (POST)。
delete 啟用刪除動作 (DELETE)。

角色定義範例

以下是 Azure PowerShell 和 Azure CLI 中顯示的參與者角色定義。 Actions 下的萬用字元 (*) 動作表示指派給這個角色的主體可以執行所有動作;換句話說,它可以管理所有項目。 這包括未來 Azure 新增資源類型時所定義的動作。 NotActions 下的動作會從 Actions 扣除。 如果是參與者角色,NotActions 會移除此角色管理資源存取權及管理 Azure 藍圖指派的能力。

Azure PowerShell 中顯示的參與者角色:

{
  "Name": "Contributor",
  "Id": "b24988ac-6180-42a0-ab88-20f7382dd24c",
  "IsCustom": false,
  "Description": "Grants full access to manage all resources, but does not allow you to assign roles in Azure RBAC, manage assignments in Azure Blueprints, or share image galleries.",
  "Actions": [
    "*"
  ],
  "NotActions": [
    "Microsoft.Authorization/*/Delete",
    "Microsoft.Authorization/*/Write",
    "Microsoft.Authorization/elevateAccess/Action",
    "Microsoft.Blueprint/blueprintAssignments/write",
    "Microsoft.Blueprint/blueprintAssignments/delete",
    "Microsoft.Compute/galleries/share/action",
    "Microsoft.Purview/consents/write",
    "Microsoft.Purview/consents/delete"
  ],
  "DataActions": [],
  "NotDataActions": [],
  "AssignableScopes": [
    "/"
  ],
  "Condition": null,
  "ConditionVersion": null
}

Azure CLI 中顯示的參與者角色:

[
  {
    "assignableScopes": [
      "/"
    ],
    "createdBy": null,
    "createdOn": "2015-02-02T21:55:09.880642+00:00",
    "description": "Grants full access to manage all resources, but does not allow you to assign roles in Azure RBAC, manage assignments in Azure Blueprints, or share image galleries.",
    "id": "/subscriptions/{subscriptionId}/providers/Microsoft.Authorization/roleDefinitions/b24988ac-6180-42a0-ab88-20f7382dd24c",
    "name": "b24988ac-6180-42a0-ab88-20f7382dd24c",
    "permissions": [
      {
        "actions": [
          "*"
        ],
        "condition": null,
        "conditionVersion": null,
        "dataActions": [],
        "notActions": [
          "Microsoft.Authorization/*/Delete",
          "Microsoft.Authorization/*/Write",
          "Microsoft.Authorization/elevateAccess/Action",
          "Microsoft.Blueprint/blueprintAssignments/write",
          "Microsoft.Blueprint/blueprintAssignments/delete",
          "Microsoft.Compute/galleries/share/action",
          "Microsoft.Purview/consents/write",
          "Microsoft.Purview/consents/delete"
        ],
        "notDataActions": []
      }
    ],
    "roleName": "Contributor",
    "roleType": "BuiltInRole",
    "type": "Microsoft.Authorization/roleDefinitions",
    "updatedBy": null,
    "updatedOn": "2023-07-10T15:10:53.947865+00:00"
  }
]

控制項和資料動作

控制平面動作的角色型存取控制是在角色定義的 ActionsNotActions 屬性中指定。 以下是 Azure 中控制平面動作的幾個範例:

  • 管理儲存體帳戶的存取權
  • 建立、更新或刪除 Blob 容器
  • 刪除資源群組及其所有資源

只要容器驗證方法設為 [Azure AD 使用者帳戶],而不是 [存取金鑰],您的資料平面就不會繼承控制平面存取權。 此隔離可防止具有萬用字元 (*) 的角色不受限地存取您的資料。 例如,如果使用者具有訂用帳戶的讀取者角色,則可以檢視儲存體帳戶,但預設為無法檢視基礎資料。

在此之前,角色型存取控制並未用於資料動作。 資料動作的授權會因為資源提供者不同而有差異。 用於控制平面動作的相同角色型存取控制授權模型已延伸至資料平面動作。

為支援資料平面動作,新的資料屬性已新增至角色定義。 資料平面動作是在 DataActionsNotDataActions 屬性中指定。 藉由新增這些資料屬性,控制平面和資料平面之間的分隔得以保持。 這可避免目前具有萬用字元 (*) 的角色指派突然存取資料。 以下是可在 DataActionsNotDataActions 中指定的幾項資料平面動作:

  • 讀取容器中的 Blob 清單
  • 在容器中寫入儲存體 Blob
  • 刪除佇列中的訊息

以下是儲存體 Blob 資料讀取器的角色定義,其中包含 ActionsDataActions 屬性中的動作。 此角色可讓您讀取 Blob 容器和基礎 Blob 資料。

Azure PowerShell 中顯示的儲存體 Blob 資料讀取者角色:

{
  "Name": "Storage Blob Data Reader",
  "Id": "2a2b9908-6ea1-4ae2-8e65-a410df84e7d1",
  "IsCustom": false,
  "Description": "Allows for read access to Azure Storage blob containers and data",
  "Actions": [
    "Microsoft.Storage/storageAccounts/blobServices/containers/read",
    "Microsoft.Storage/storageAccounts/blobServices/generateUserDelegationKey/action"
  ],
  "NotActions": [],
  "DataActions": [
    "Microsoft.Storage/storageAccounts/blobServices/containers/blobs/read"
  ],
  "NotDataActions": [],
  "AssignableScopes": [
    "/"
  ],
  "Condition": null,
  "ConditionVersion": null
}

Azure CLI 中顯示的儲存體 Blob 資料讀取者角色:

[
  {
    "assignableScopes": [
      "/"
    ],
    "createdBy": null,
    "createdOn": "2017-12-21T00:01:24.797231+00:00",
    "description": "Allows for read access to Azure Storage blob containers and data",
    "id": "/subscriptions/{subscriptionId}/providers/Microsoft.Authorization/roleDefinitions/2a2b9908-6ea1-4ae2-8e65-a410df84e7d1",
    "name": "2a2b9908-6ea1-4ae2-8e65-a410df84e7d1",
    "permissions": [
      {
        "actions": [
          "Microsoft.Storage/storageAccounts/blobServices/containers/read",
          "Microsoft.Storage/storageAccounts/blobServices/generateUserDelegationKey/action"
        ],
        "condition": null,
        "conditionVersion": null,
        "dataActions": [
          "Microsoft.Storage/storageAccounts/blobServices/containers/blobs/read"
        ],
        "notActions": [],
        "notDataActions": []
      }
    ],
    "roleName": "Storage Blob Data Reader",
    "roleType": "BuiltInRole",
    "type": "Microsoft.Authorization/roleDefinitions",
    "updatedBy": null,
    "updatedOn": "2021-11-11T20:13:55.297507+00:00"
  }
]

只有資料平面動作可新增至 DataActionsNotDataActions 屬性。 資源提供者會藉由將 isDataAction 屬性設為 true,來識別哪些動作是資料動作。 若要查看 isDataActiontrue 的動作清單,請參閱資源提供者作業。 沒有資料動作的角色在角色定義中不需要具備 DataActionsNotDataActions 屬性。

所有控制平面 API 呼叫的授權都會由 Azure Resource Manager 處理。 資料平面 API 呼叫的授權由資源提供者或 Azure Resource Manager 處理。

資料動作範例

為了進一步了解控制平面和資料平面動作如何運作,我們來看看特定範例。 Alice 已在訂用帳戶範圍上獲得擁有者角色的指派。 Bob 已在儲存體帳戶範圍內獲得儲存體 Blob 資料參與者角色的指派。 此範例如下圖所示。

角色型存取控制已擴充為支援控制平面和資料平面動作

Alice 的擁有者角色和 Bob 的儲存體 Blob 資料參與者角色具有下列動作:

擁有者

    動作
    *

儲存體 Blob 資料參與者

    動作
    Microsoft.Storage/storageAccounts/blobServices/containers/delete
    Microsoft.Storage/storageAccounts/blobServices/containers/read
    Microsoft.Storage/storageAccounts/blobServices/containers/write
    Microsoft.Storage/storageAccounts/blobServices/generateUserDelegationKey/action
    DataActions
    Microsoft.Storage/storageAccounts/blobServices/containers/blobs/delete
    Microsoft.Storage/storageAccounts/blobServices/containers/blobs/read
    Microsoft.Storage/storageAccounts/blobServices/containers/blobs/write
    Microsoft.Storage/storageAccounts/blobServices/containers/blobs/move/action
    Microsoft.Storage/storageAccounts/blobServices/containers/blobs/add/action

由於 Alice 在訂閱範圍內有萬用字元 (*) 動作,她的權限可向下繼承,因此她可執行所有的控制平面動作。 Alice 可以讀取、寫入和刪除容器。 不過,Alice 無法在未採取額外步驟的情況下執行資料平面動作。 例如,根據預設,Alice 無法讀取容器內的 Blob。 若要讀取 Blob,Alice 必須擷取儲存體存取金鑰,並使用它們來存取 Blob。

Bob 的權限則僅限於儲存體 Blob 資料參與者角色中指定的 ActionsDataActions。 根據角色,Bob 可以執行控制平面和資料平面動作。 例如,Bob 可以讀取、寫入和刪除指定儲存體帳戶中的容器,也可以讀取、寫入和刪除 Blob。

如需適用於儲存體之控制及資料平面安全性的詳細資訊,請參閱 Azure 儲存體安全性指南

哪些工具支援使用 Azure 角色進行資料動作?

若要檢視及使用資料動作,您必須有正確版本的工具或 SDK:

工具 版本
Azure PowerShell 1.1.0 或更新版本
Azure CLI 2.0.30 或更新版本
Azure for .NET 2.8.0-預覽或更新版本
Azure SDK for Go 15.0.0 或更新版本
適用於 Java 的 Azure 1.9.0 或更新版本
Azure for Python 0.40.0 或更新版本
Azure SDK for Ruby 0.17.1 或更新版本

若要在 REST API 中檢視及使用資料動作,您必須將 api-version 參數設為下列版本或更新版本:

  • 2018-07-01

動作

Actions 權限會指定角色所允許執行的控制平面動作。 它是識別 Azure 資源提供者的安全性實體動作的字串集合。 以下是可用於 Actions 的幾個控制平面動作範例。

動作字串 描述
*/read 授與所有 Azure 資源提供者的所有資源類型之讀取動作的存取權。
Microsoft.Compute/* 授與對 Microsoft.Compute 資源提供者中所有資源類型之所有動作的存取權。
Microsoft.Network/*/read 授與 Microsoft.Network 資源提供者中所有資源類型之讀取動作的存取權。
Microsoft.Compute/virtualMachines/* 授與虛擬機器及其子資源類型之所有動作的存取權。
microsoft.web/sites/restart/Action 授與重新啟動 Web 應用程式的存取權。

NotActions

NotActions 權限會指定從具有萬用字元 (*) 之允許的 Actions 中減去或排除的控制平面動作。 如果您想允許的動作集透過減去具有萬用字元 (*) 的 Actions 行動會更容易定義,請使用 NotActions 權限。 由角色 (有效權限) 所授與的存取權是透過將 Actions 動作減去 NotActions 的動作來計算。

Actions - NotActions = Effective control plane permissions

下表顯示兩個 Microsoft.CostManagement 萬用字元動作的有效控制平面權限範例:

動作 NotActions 有效的控制平面權限
Microsoft.CostManagement/exports/* none Microsoft.CostManagement/exports/action
Microsoft.CostManagement/exports/read
Microsoft.CostManagement/exports/write
Microsoft.CostManagement/exports/delete
Microsoft.CostManagement/exports/run/action
Microsoft.CostManagement/exports/* Microsoft.CostManagement/exports/delete Microsoft.CostManagement/exports/action
Microsoft.CostManagement/exports/read
Microsoft.CostManagement/exports/write
Microsoft.CostManagement/exports/run/action

注意

如果一名使用者受指派了一個角色,而這個角色在 NotActions 中被排除了一個動作,然後又受指派了第二個角色,這個角色獲得同個動作的存取權,則允許使用者執行該動作。 NotActions 不是一個拒絕規則,它只是一個便利的方式,可以在需要排除特定動作時建立允許的動作集合。

NotAction 和否定性指派之間的差異

NotActions 和否定性指派不同,且用途不同。 NotActions 是從萬用字元 (*) 動作中減去特定動作的便利方式。

拒絕指派會封鎖使用者執行指定的動作,即使角色指派授與他們存取權也一樣。 如需詳細資訊,請參閱了解 Azure 拒絕指派

DataActions

DataActions 權限會指定允許在該物件內對資料執行之角色的資料平面動作。 例如,如果使用者有儲存體帳戶的讀取 Blob 資料存取權,則他們可讀取該儲存體帳戶中的 Blob。 以下是可用於 DataActions 中的一些資料動作範例。

資料動作字串 描述
Microsoft.Storage/storageAccounts/blobServices/containers/blobs/read 傳回 Blob 或 Blob 清單。
Microsoft.Storage/storageAccounts/blobServices/containers/blobs/write 傳回寫入 Blob 的結果。
Microsoft.Storage/storageAccounts/queueServices/queues/messages/read 傳回訊息。
Microsoft.Storage/storageAccounts/queueServices/queues/messages/* 傳回訊息或寫入或刪除訊息的結果。

NotDataActions

NotDataActions 權限會指定從具有萬用字元 (*) 之允許的 DataActions 中減去或排除的資料平面動作。 如果您想允許的動作集透過減去具有萬用字元 (*) 的 DataActions 行動會更容易定義,請使用 NotDataActions 權限。 由角色 (有效權限) 所授與的存取權透過將 DataActions 動作減去 NotDataActions 的動作來計算。 每個資源提供者會提供個別的一組 API 來完成資料動作。

DataActions - NotDataActions = Effective data plane permissions

下表顯示兩個 Microsoft.Storage 萬用字元動作的有效資料平面權限範例:

DataActions NotDataActions 有效的資料平面權限
Microsoft.Storage/storageAccounts/queueServices/queues/messages/* none Microsoft.Storage/storageAccounts/queueServices/queues/messages/read
Microsoft.Storage/storageAccounts/queueServices/queues/messages/write
Microsoft.Storage/storageAccounts/queueServices/queues/messages/delete
Microsoft.Storage/storageAccounts/queueServices/queues/messages/add/action
Microsoft.Storage/storageAccounts/queueServices/queues/messages/process/action
Microsoft.Storage/storageAccounts/queueServices/queues/messages/* Microsoft.Storage/storageAccounts/queueServices/queues/messages/delete
Microsoft.Storage/storageAccounts/queueServices/queues/messages/read
Microsoft.Storage/storageAccounts/queueServices/queues/messages/write
Microsoft.Storage/storageAccounts/queueServices/queues/messages/add/action
Microsoft.Storage/storageAccounts/queueServices/queues/messages/process/action

注意

如果為使用者指派的角色已在 NotDataActions 中排除一個資料動作,並且指派授與了相同資料作業存取權的第二個角色給使用者,則允許使用者執行該資料動作。 NotDataActions 不是拒絕規則 - 它只是一個便利的方式,可以在需要排除特定資料動作時建立允許的資料動作集合。

AssignableScopes

AssignableScopes 屬性會指定可以指派角色定義的範圍 (根、管理群組、訂閱,或資源群組)。 您可以指派自訂角色在只需要該角色的管理群組、訂閱,或資源群組。 您至少必須使用一個管理群組、訂閱,或資源群組。

例如,如果 AssignableScopes 設至一個訂閱,這表示自訂角色可在指定訂閱的訂閱範圍、訂閱中任何資源群組的資源群組範圍,或訂閱中任何資源的資源範圍進行指派。

內建角色的 AssignableScopes 設定為根目錄範圍 ("/")。 根目錄範圍表示角色可指派給所有範圍。

有效的可指派範圍範例包括:

角色可供指派 範例
一個訂用帳戶 "/subscriptions/{subscriptionId1}"
兩個訂閱 "/subscriptions/{subscriptionId1}", "/subscriptions/{subscriptionId2}"
網路資源群組 "/subscriptions/{subscriptionId1}/resourceGroups/Network"
一個管理群組 "/providers/Microsoft.Management/managementGroups/{groupId1}"
管理群組和一個訂閱 "/providers/Microsoft.Management/managementGroups/{groupId1}", "/subscriptions/{subscriptionId1}",
所有範圍 (僅適用於內建角色) "/"

您只能在自訂角色的 AssignableScopes 中定義一個管理群組。

雖然您可以使用命令列在 AssignableScopes 中建立具有資源執行個體的自訂角色,但不建議這麼做。 每個租用戶最多支援 5,000 個自訂角色。 使用此策略可能會耗盡您可用的自訂角色。 最後,存取層級取決於自訂角色指派 (範圍 + 角色權限 + 安全性主體),而不是列在自訂角色中的 AssignableScopes。 因此,以管理群組、訂閱,或資源群組的 AssignableScopes 來建立自訂角色,但指派給自訂角色的範圍需要精簡 (例如資源或資源群組)。

如需自訂角色之 AssignableScopes 的詳細資訊,請參閱 Azure 自訂角色

特殊權限管理員角色定義

特殊權限系統管理員角色是授與特殊權限系統管理員存取權的角色,例如其可管理 Azure 資源或指派角色給其他使用者。 如果內建或自訂角色包含下列任何動作,則會將其視為特殊權限角色。 如需詳細資訊,請參閱列出或管理特殊權限管理員角色指派 (部份機器翻譯)。

動作字串 描述
* 建立和管理所有類型的資源。
*/delete 刪除所有類型的資源。
*/write 寫入所有類型的資源。
Microsoft.Authorization/denyAssignments/delete 在指定範圍刪除拒絕指派。
Microsoft.Authorization/denyAssignments/write 在指定範圍建立拒絕指派。
Microsoft.Authorization/roleAssignments/delete 刪除指定範圍內的角色指派。
Microsoft.Authorization/roleAssignments/write 建立指定範圍的角色指派。
Microsoft.Authorization/roleDefinitions/delete 刪除指定的自訂角色定義。
Microsoft.Authorization/roleDefinitions/write 以指定權限和可指派的範圍來建立或更新自訂角色定義。

下一步