了解 Azure RBAC 的範圍

「範圍」是要套用存取權的一組資源。 當您指派角色時,請務必了解範圍,這樣才能只授與安全性主體真正需要的存取權。 藉由限制範圍,萬一安全性主體遭到入侵時,承受風險的資源仍可得到控制。

範圍層級

Azure 有下列四個層級可讓您指定範圍:管理群組、訂閱、資源群組和資源。 範圍會採用父子式關聯性的結構。 階層的每個層級都會讓範圍更為明確。 您可以在這裡的任何層級範圍當中指派角色。 角色套用範圍依您選取的層級而定。 較低層級會繼承較高層級的角色權限。

Scope for a role assignment

管理群組是高於訂用帳戶的範圍層級,但管理群組支援更複雜的階層。 下圖顯示您可以定義的管理群組和訂用帳戶階層範例。 如需管理群組的詳細資訊,請參閱什麼是 Azure 管理群組?

Management group and subscription hierarchy

範圍格式

如果您使用命令列指派角色,則必須指定範圍。 在命令列工具中,範圍有可能是較長的字串,可識別角色指派的確切範圍。 在 Azure 入口網站中,此範圍通常會列為資源識別碼

範圍是由一連串以正斜線 (/) 字元分隔的識別碼所組成。 您可以將此字串視為下列階層的表示法,其中,不含預留位置的文字 ({}) 是固定識別碼:

/subscriptions
    /{subscriptionId}
        /resourcegroups
            /{resourceGroupName}
                /providers
                    /{providerName}
                        /{resourceType}
                            /{resourceSubType1}
                                /{resourceSubType2}
                                    /{resourceName}
  • {subscriptionId} 是要使用之訂用帳戶的識別碼 (GUID)。
  • {resourceGroupName} 是包含資源群組的名稱。
  • {providerName} 是處理資源的資源提供者名稱,而 {resourceType}{resourceSubType*} 則會識別該資源提供者之中的後續層級。
  • {resourceName} 是識別特定資源之字串的最後一個部分。

管理群組是高於訂用帳戶的層級,且具有最廣泛的 (最少特定) 範圍。 此層級的角色指派會套用至管理群組之中的訂用帳戶。 管理群組的範圍有下列格式:

/providers
    /Microsoft.Management
        /managementGroups
            /{managmentGroupName}

範圍範例

範圍 範例
管理群組 /providers/Microsoft.Management/managementGroups/marketing-group
訂用帳戶 /subscriptions/00000000-0000-0000-0000-000000000000
資源群組 /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/Example-Storage-rg
/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/pharma-sales
資源 /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/Example-Storage-rg/providers/Microsoft.Storage/storageAccounts/azurestorage12345/blobServices/default/containers/blob-container-01
/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/MyVirtualNetworkResourceGroup/providers/Microsoft.Network/virtualNetworks/MyVirtualNetwork12345

如何判斷資源的範圍

判斷管理群組、訂用帳戶或資源群組的範圍相當簡單。 您只需要知道名稱和訂用帳戶識別碼即可。 不過,判斷資源範圍還需要執行一些工作。 以下是您可以判斷資源範圍的幾個方式。

  • 在 Azure 入口網站中,開啟資源,然後查看屬性。 資源應該會列出您可以用來判斷範圍的資源識別碼。 例如,以下是儲存體帳戶的資源識別碼。

    Screenshot that shows resource IDs for a storage account in Azure portal.

  • 另一種方式是使用 Azure 入口網站暫時在資源範圍指派角色,然後使用 Azure PowerShellAzure CLI 列出角色指派。 在輸出中,範圍將會列為屬性。

    RoleAssignmentId   : /subscriptions/<subscriptionId>/resourceGroups/test-rg/providers/Microsoft.Storage/storageAccounts/azurestorage12345/blobServices/default/containers/blob-container-01/pro
                         viders/Microsoft.Authorization/roleAssignments/<roleAssignmentId>
    Scope              : /subscriptions/<subscriptionId>/resourceGroups/test-rg/providers/Microsoft.Storage/storageAccounts/azurestorage12345/blobServices/default/containers/blob-container-01
    DisplayName        : User
    SignInName         : user@contoso.com
    RoleDefinitionName : Storage Blob Data Reader
    RoleDefinitionId   : 2a2b9908-6ea1-4ae2-8e65-a410df84e7d1
    ObjectId           : <principalId>
    ObjectType         : User
    CanDelegate        : False
    Description        :
    ConditionVersion   :
    Condition          :
    
    {
        "canDelegate": null,
        "condition": null,
        "conditionVersion": null,
        "description": null,
        "id": "/subscriptions/{subscriptionId}/resourceGroups/Example-Storage-rg/providers/Microsoft.Storage/storageAccounts/azurestorage12345/blobServices/default/containers/blob-container-01/providers/Microsoft.Authorization/roleAssignments/{roleAssignmentId}",
        "name": "{roleAssignmentId}",
        "principalId": "{principalId}",
        "principalName": "user@contoso.com",
        "principalType": "User",
        "resourceGroup": "test-rg",
        "roleDefinitionId": "/subscriptions/{subscriptionId}/providers/Microsoft.Authorization/roleDefinitions/2a2b9908-6ea1-4ae2-8e65-a410df84e7d1",
        "roleDefinitionName": "Storage Blob Data Reader",
        "scope": "/subscriptions/{subscriptionId}/resourceGroups/Example-Storage-rg/providers/Microsoft.Storage/storageAccounts/azurestorage12345/blobServices/default/containers/blob-container-01",
        "type": "Microsoft.Authorization/roleAssignments"
      }
    

範圍和 ARM 範本

角色指派是 Azure Resource Manager 中稱為延伸模組資源的特殊類型。 延伸模組資源是會增加另一個資源之功能的資源。 它們一律以另一個資源的延伸模組形式存在 (例如,子項目)。 例如,訂用帳戶範圍的角色指派是訂用帳戶的延伸模組資源。 角色指派的名稱一律是您要延伸的資源名稱加上 /Microsoft.Authorization/roleAssignments/{roleAssignmentId}。 使用 Azure Resource Manager 範本 (ARM 範本) 指派角色時,您通常不需要提供範圍。 原因是範圍欄位一律以您要延伸的資源所使用的識別碼結尾。 您可以從角色指派本身的識別碼來判斷範圍。 下表顯示角色指派識別碼和對應範圍的範例:

角色指派識別碼 範圍
/subscriptions/{subscriptionId}/providers/Microsoft.Authorization/roleAssignments/{roleAssignmentId} /subscriptions/{subscriptionId}
/subscriptions/{subscriptionId}/resourceGroups/Example-Storage-rg/providers/Microsoft.Authorization/roleAssignments/{roleAssignmentId} /subscriptions/{subscriptionId}/resourceGroups/Example-Storage-rg

如需範圍和 ARM 範本的詳細資訊,請參閱使用 Azure Resource Manager 範本指派 Azure 角色。 如需完整的延伸模組資源類型清單,請參閱擴充其他資源功能的資源類型 (部分機器翻譯)。

下一步