Azure 自訂角色 (機器翻譯)

重要

將管理群組新增至 AssignableScopes 的服務目前為預覽狀態。 此預覽版本是在沒有服務等級協定的情況下提供,不建議用於生產工作負載。 可能不支援特定功能,或可能已經限制功能。 如需詳細資訊,請參閱 Microsoft Azure 預覽版增補使用條款

如果 Azure 內建的角色無法滿足您組織的特定需求,您可以建立自己的自訂角色。 就像內建角色一樣,您可以將自訂角色指派給管理群組的使用者、群組和服務主體, (預覽版中僅) 、訂用帳戶和資源群組範圍。

自訂角色可以在信任相同 Azure AD 租使用者的訂用帳戶之間共用。 每個租使用者有 5,000 個 自訂角色的限制。 (針對 Azure China 21Vianet,限制為 2,000 個自訂角色。) 您可以使用 Azure 入口網站、Azure PowerShell、Azure CLI 或 REST API 來建立自訂角色。

建立自訂角色的步驟

以下是建立自訂角色的基本步驟。

  1. 判斷您需要的許可權。

    當您建立自訂角色時,必須知道可用來定義許可權的動作。 一般而言,您可以從使用現有的內建角色開始,然後針對您的需求進行修改。 您會將動作新增至 Actions角色定義的NotActions 屬性。 如果您有資料動作,您會將這些動作新增至 DataActionsNotDataActions 屬性。

    如需詳細資訊,請參閱下一節 如何判斷您需要的許可權

  2. 決定您要如何建立自訂角色。

    您可以使用Azure 入口網站Azure PowerShellAzure CLIREST API來建立自訂角色。

  3. 建立自訂角色。

    最簡單的方式是使用Azure 入口網站。 如需如何使用 Azure 入口網站建立自訂角色的步驟,請參閱使用 Azure 入口網站建立或更新 Azure 自訂角色

  4. 測試自訂角色。

    一旦具有自訂角色,您必須測試它來驗證是否如預期般運作。 稍後如需進行調整,您可以更新自訂角色。

如何判斷您所需的權限

Azure 有數千個許可權,您可以包含在自訂角色中。 以下是一些方法可協助您判斷您要新增至自訂角色的許可權:

  • 查看現有的 內建角色

    您可能想要修改現有的角色,或合併多個角色中使用的許可權。

  • 列出您想要授與存取權的 Azure 服務。

  • 判斷 對應至 Azure 服務的資源提供者

    Azure 服務會透過 資源提供者公開其功能和許可權。 例如,Microsoft。計算資源提供者會提供虛擬機器資源和Microsoft。計費資源提供者提供訂用帳戶和計費資源。 瞭解資源提供者可協助您縮小範圍,並判斷自訂角色所需的許可權。

    當您使用 Azure 入口網站 建立自訂角色時,也可以藉由搜尋關鍵字來判斷資源提供者。 此搜尋功能會在使用 Azure 入口網站 建立或更新 Azure 自訂角色中所述。

    使用資源提供者新增許可權窗格

  • 搜尋 可用的許可權 ,以尋找您想要包含的許可權。

    當您使用 Azure 入口網站 建立自訂角色時,您可以依關鍵字搜尋許可權。 例如,您可以搜尋 虛擬機器計費 許可權。 您也可以下載擁有權限作為 CSV 檔案,然後搜尋此檔案。 此搜尋功能會在使用 Azure 入口網站 建立或更新 Azure 自訂角色中所述。

    新增許可權清單

自訂角色範例

以下顯示自訂角色的外觀,如使用 JSON 格式的Azure PowerShell所顯示。 此自訂角色可用於監視和重新啟動虛擬機器。

{
  "Name": "Virtual Machine Operator",
  "Id": "88888888-8888-8888-8888-888888888888",
  "IsCustom": true,
  "Description": "Can monitor and restart virtual machines.",
  "Actions": [
    "Microsoft.Storage/*/read",
    "Microsoft.Network/*/read",
    "Microsoft.Compute/*/read",
    "Microsoft.Compute/virtualMachines/start/action",
    "Microsoft.Compute/virtualMachines/restart/action",
    "Microsoft.Authorization/*/read",
    "Microsoft.ResourceHealth/availabilityStatuses/read",
    "Microsoft.Resources/subscriptions/resourceGroups/read",
    "Microsoft.Insights/alertRules/*",
    "Microsoft.Insights/diagnosticSettings/*",
    "Microsoft.Support/*"
  ],
  "NotActions": [],
  "DataActions": [],
  "NotDataActions": [],
  "AssignableScopes": [
    "/subscriptions/{subscriptionId1}",
    "/subscriptions/{subscriptionId2}",
    "/providers/Microsoft.Management/managementGroups/{groupId1}"
  ]
}

以下顯示與使用 Azure CLI 顯示相同的自訂角色。

[
  {
    "assignableScopes": [
      "/subscriptions/{subscriptionId1}",
      "/subscriptions/{subscriptionId2}",
      "/providers/Microsoft.Management/managementGroups/{groupId1}"
    ],
    "description": "Can monitor and restart virtual machines.",
    "id": "/subscriptions/{subscriptionId1}/providers/Microsoft.Authorization/roleDefinitions/88888888-8888-8888-8888-888888888888",
    "name": "88888888-8888-8888-8888-888888888888",
    "permissions": [
      {
        "actions": [
          "Microsoft.Storage/*/read",
          "Microsoft.Network/*/read",
          "Microsoft.Compute/*/read",
          "Microsoft.Compute/virtualMachines/start/action",
          "Microsoft.Compute/virtualMachines/restart/action",
          "Microsoft.Authorization/*/read",
          "Microsoft.ResourceHealth/availabilityStatuses/read",
          "Microsoft.Resources/subscriptions/resourceGroups/read",
          "Microsoft.Insights/alertRules/*",
          "Microsoft.Insights/diagnosticSettings/*",
          "Microsoft.Support/*"
        ],
        "dataActions": [],
        "notActions": [],
        "notDataActions": []
      }
    ],
    "roleName": "Virtual Machine Operator",
    "roleType": "CustomRole",
    "type": "Microsoft.Authorization/roleDefinitions"
  }
]

自訂角色屬性

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

屬性 必要 類型 描述
Name
roleName
String 自訂角色的顯示名稱。 雖然角色定義是管理群組或訂用帳戶層級資源,但角色定義可以在共用相同 Azure AD 租使用者的多個訂用帳戶中使用。 此顯示名稱在 Azure AD 租使用者的範圍內必須是唯一的。 可以包含字母、數字、空格和特殊字元。 字元數上限為 512。
Id
name
String 自訂角色的唯一識別碼。 針對 Azure PowerShell 和 Azure CLI,當您建立新角色時,會自動產生這個識別碼。
IsCustom
roleType
String 表示這是否為自訂角色。 針對自訂角色,將 設定為 trueCustomRole 。 針對內建角色設定為 falseBuiltInRole
Description
description
String 自訂角色的描述。 可以包含字母、數字、空格和特殊字元。 字元數目上限為 2048。
Actions
actions
String[] 字串陣列,指定角色允許執行的控制平面動作。 如需詳細資訊,請參閱 Actions
NotActions
notActions
No String[] 字串陣列,指定從允許 Actions 排除的控制平面動作。 如需詳細資訊,請參閱 NotActions
DataActions
dataActions
No String[] 字串陣列,指定角色允許對該物件內資料執行的資料平面動作。 如果您使用 建立自訂角色 DataActions ,則無法在管理群組範圍指派該角色。 如需詳細資訊,請參閱 DataActions
NotDataActions
notDataActions
No String[] 字串陣列,指定從允許 DataActions 排除的資料平面動作。 如需詳細資訊,請參閱 NotDataActions
AssignableScopes
assignableScopes
String[] 字串陣列,指定自訂角色可用於指派的範圍。 的最大數目 AssignableScopes 為 2,000。 如需詳細資訊,請參閱 AssignableScopes

許可權字串不區分大小寫。 當您建立自訂角色時,慣例是符合您在 Azure 資源提供者作業中看到的許可權案例。

萬用字元權限

ActionsNotActionsDataActionsNotDataActions 支援 () * 萬用字元來定義許可權。 萬用字元 (*) 會將許可權延伸至符合您提供的動作字串的所有專案。 例如,假設您想要新增與 Azure 成本管理和匯出相關的擁有權限。 您可以新增所有這些動作字串:

Microsoft.CostManagement/exports/action
Microsoft.CostManagement/exports/read
Microsoft.CostManagement/exports/write
Microsoft.CostManagement/exports/delete
Microsoft.CostManagement/exports/run/action

您可以只新增萬用字元字串,而不是新增所有這些字串。 例如,下列萬用字元字串相當於前五個字串。 這也包括可能新增的任何未來匯出許可權。

Microsoft.CostManagement/exports/*

建議您明確指定 ActionsDataActions ,而不是使用萬用字元 () * 字元。 未來授與的其他存取權和許可權 Actions ,或使用 DataActions 萬用字元可能不想要的行為。

誰可以建立、刪除、更新或檢視自訂角色

就像內建角色一樣,AssignableScopes 屬性會指定角色可用於指派的範圍。 自訂角色的 AssignableScopes 屬性也會控制誰可以建立、刪除、更新或檢視自訂角色。

Task 動作 描述
建立/刪除自訂角色 Microsoft.Authorization/ roleDefinitions/write 授與所有自訂角色動作 AssignableScopes 的使用者,可以建立 (或刪除) 自訂角色,以供這些範圍使用。 例如,管理群組、訂用帳戶和資源群組的 擁有 者和 使用者存取系統管理員
更新自訂角色 Microsoft.Authorization/ roleDefinitions/write 授與所有自訂角色動作 AssignableScopes 的使用者,可以更新這些範圍內的自訂角色。 例如,管理群組、訂用帳戶和資源群組的 擁有 者和 使用者存取系統管理員
檢視自訂角色 Microsoft.Authorization/ roleDefinitions/read 在範圍中授與此動作的使用者,可以檢視可在該範圍指派的自訂角色。 所有內建角色都允許自訂角色以供指派。

尋找要刪除自訂角色的角色指派

您必須先移除任何使用自訂角色的角色指派,才能刪除自訂角色。 如果您嘗試刪除具有角色指派的自訂角色,您會收到下列訊息: There are existing role assignments referencing role (code: RoleDefinitionHasAssignments)

以下是在刪除自訂角色之前協助尋找角色指派的步驟:

自訂角色限制

下列清單會說明自訂角色的限制。

  • 每個租使用者最多可以有 5000 個 自訂角色。
  • Azure China 21Vianet 為每個租使用者最多可以有 2000 個自訂角色。
  • 您無法設定 AssignableScopes 為根範圍 ("/") 。
  • 您無法在 中使用萬用字元 (*) AssignableScopes 。 此萬用字元限制有助於確保使用者無法藉由更新角色定義來取得範圍的存取權。
  • 您只能在自訂角色中 AssignableScopes 定義一個管理群組。 將管理群組新增至 AssignableScopes 的服務目前為預覽狀態。
  • 動作字串中只能有一個萬用字元。
  • 具有 的 DataActions 自訂角色無法在管理群組範圍指派。
  • Azure Resource Manager不會驗證管理群組是否存在於角色定義的 AssignableScopes 中。

如需自訂角色和管理群組的詳細資訊,請參閱什麼是 Azure 管理群組?

輸入和輸出格式

若要使用命令列建立自訂角色,您通常會使用 JSON 來指定自訂角色所需的屬性。 視您使用的工具而定,輸入和輸出格式看起來會稍有不同。 本節會根據工具列出輸入和輸出格式。

Azure PowerShell

若要使用 Azure PowerShell 建立自訂角色,您必須提供下列輸入。

{
  "Name": "",
  "Description": "",
  "Actions": [],
  "NotActions": [],
  "DataActions": [],
  "NotDataActions": [],
  "AssignableScopes": []
}

若要使用 Azure PowerShell 更新自訂角色,您必須提供下列輸入。 請注意, Id 已新增 屬性。

{
  "Name": "",
  "Id": "",
  "Description": "",
  "Actions": [],
  "NotActions": [],
  "DataActions": [],
  "NotDataActions": [],
  "AssignableScopes": []
}

以下顯示當您使用 Azure PowerShell 和ConvertTo-Json命令列出自訂角色時的輸出範例。

{
  "Name": "",
  "Id": "",
  "IsCustom": true,
  "Description": "",
  "Actions": [],
  "NotActions": [],
  "DataActions": [],
  "NotDataActions": [],
  "AssignableScopes": []
}

Azure CLI

若要使用 Azure CLI 建立或更新自訂角色,您必須提供下列輸入。 當您使用 Azure PowerShell 建立自訂角色時,此格式會是相同的格式。

{
  "Name": "",
  "Description": "",
  "Actions": [],
  "NotActions": [],
  "DataActions": [],
  "NotDataActions": [],
  "AssignableScopes": []
}

以下顯示使用 Azure CLI 列出自訂角色時的輸出範例。

[
  {
    "assignableScopes": [],
    "description": "",
    "id": "",
    "name": "",
    "permissions": [
      {
        "actions": [],
        "dataActions": [],
        "notActions": [],
        "notDataActions": []
      }
    ],
    "roleName": "",
    "roleType": "CustomRole",
    "type": "Microsoft.Authorization/roleDefinitions"
  }
]

REST API

若要使用 REST API 建立或更新自訂角色,您必須提供下列輸入。 此格式與使用 Azure 入口網站 建立自訂角色時所產生的格式相同。

{
  "properties": {
    "roleName": "",
    "description": "",
    "assignableScopes": [],
    "permissions": [
      {
        "actions": [],
        "notActions": [],
        "dataActions": [],
        "notDataActions": []
      }
    ]
  }
}

以下顯示使用 REST API 列出自訂角色時的輸出範例。

{
    "properties": {
        "roleName": "",
        "type": "CustomRole",
        "description": "",
        "assignableScopes": [],
        "permissions": [
            {
                "actions": [],
                "notActions": [],
                "dataActions": [],
                "notDataActions": []
            }
        ],
        "createdOn": "",
        "updatedOn": "",
        "createdBy": "",
        "updatedBy": ""
    },
    "id": "",
    "type": "Microsoft.Authorization/roleDefinitions",
    "name": ""
}

下一步