簡體   English   中英

Azure AD 組和用戶分配的托管身份

[英]Azure AD Groups and User Assigned Managed Identities

您如何允許用戶分配的托管身份 (UAMI) 到 R/W Azure AD 組? 我有一個作為 UMAI 運行的.Net Core 3.1 Azure Function 應用程序。 該應用程序需要能夠 R/W Azure AD 組。 我的代碼通過筆記本電腦上的應用注冊服務主體運行。 在 Azure 中,UAMI 是訂閱貢獻者,並與 FunctionApp 關聯。

這適用於本地和 Azure:

        var azureFluentClient = AzureAuthenticator.AzureFluentClient(context._ILogger, context.ExecutionContext,context.Settings);
        var resourceGroups = await azureFluentClient.ResourceGroups.ListAsync();

在本地工作,在 Azure 上失敗:

        var azureFluentClient = AzureAuthenticator.AzureFluentClient(context._ILogger, context.ExecutionContext, context.Settings);
        var groups = await azureFluentClient.AccessManagement.ActiveDirectoryGroups.GetByNameAsync("AAA1");

錯誤:

{"odata.error":{"code":"Authorization_RequestDenied","message":{"lang":"en","value":"Insufficient privileges to complete the operation."},"requestId":"f797cb42-a75d-48d9-a902-c580955800cd","date":"2020-07-10T05:41:59"}}

ex.Response.Content: "Insufficient privileges to complete the operation."

Microsoft.Azure.Management.Graph.RBAC.Fluent.Models.GraphErrorException: Operation returned an invalid status code 'BadRequest'
   at Microsoft.Azure.Management.Graph.RBAC.Fluent.GroupsOperations.ListWithHttpMessagesAsync(ODataQuery`1 odataQuery, Dictionary`2 customHeaders, CancellationToken cancellationToken)
   at Microsoft.Azure.Management.Graph.RBAC.Fluent.GroupsOperationsExtensions.ListAsync(IGroupsOperations operations, ODataQuery`1 odataQuery, CancellationToken cancellationToken)
   at Microsoft.Azure.Management.Graph.RBAC.Fluent.ActiveDirectoryGroupsImpl.<ListAsync>b__7_0(CancellationToken cancellation)
   at Microsoft.Azure.Management.ResourceManager.Fluent.Core.PagedCollection`2.LoadPageWithWrapModelAsync(Func`2 listInnerAsync, Func`3 listInnerNext, Func`3 wrapModelAsync, Boolean loadAllPages, CancellationToken cancellationToken)
   at Microsoft.Azure.Management.ResourceManager.Fluent.Core.PagedCollection`2.LoadPage(Func`2 listInnerAsync, Func`3 listInnerNext, Func`2 wrapModel, Boolean loadAllPages, CancellationToken cancellationToken)
   at Microsoft.Azure.Management.Graph.RBAC.Fluent.ActiveDirectoryGroupsImpl.ListAsync(Boolean loadAllPages, CancellationToken cancellationToken)
   at HDIManagement.FunctionApp.API.TestServiceConfiguration.TestADGroupAccess(OperationsContext context) in C:\Users\josep\source\repos\HDI\BareMetal\HDI-Logging\HDIManagement.FunctionApp\API\TestConfiguration.cs:line 67

不同之處在於有一個用於本地運行的應用程序注冊,它具有API 權限: Azure Active Directory Graph/Directory.ReadWrite.All 我認為問題在於沒有與 UAMI 關聯的應用程序注冊,因此無法授予權限。

是的,用戶分配的身份是不與 AD 應用關聯的服務主體。

在這種情況下,如果您想授予 Azure AD 組的 R/W 權限,您可以直接將管理員角色Groups administrator授予它。

在門戶中導航到Azure Active Directory -> Roles and administrators -> select Groups administrator -> Add assignments -> 搜索身份名稱並添加。

解決方案:FunctionApp 的用戶分配的托管標識(UAMI)需要

  • 分配 UAMI 訂閱貢獻者角色(我的代碼不僅僅是查找 AAD 組)
  • 分配 UAMI AD Directory Readers 角色(在生產環境中可接受)
  • 將 UAMI 作為所有者添加到要操作的 AD 組(添加/刪除成員)

請注意,UAMI 將無法創建、分配、刪除或管理任何未通過所有權明確授予權限的 AD 組。

由於 AD 延遲/緩存對托管身份所做的更改,我花費了大量時間(更改可能需要一段時間才能出現,讓我陷入困境)。 我編寫了這個腳本來幫助確定我的 FunctionApp 的實際需求。 運行此腳本后,我運行我的 FunctionApp 測試方法。 由於 Identity 每次都是新的,因此沒有緩存或延遲實現。

Connect-AzureRmAccount -TenantId $tenantId
Connect-AzureAD -TenantId $tenantId

Set-AzureRmContext  -TenantId $tenantId -SubscriptionId $subscriptionId

#Drops/recreates manged Identity
Set-AzureRmWebApp -AssignIdentity $false -ResourceGroupName "rg01" -Name "fa01" 
Set-AzureRmWebApp -AssignIdentity $true -ResourceGroupName "rg01" -Name "fa01" 

$current = Get-AzureRmWebApp -ResourceGroupName "rg01" -Name "fa01"
$objectId = $current.Identity.PrincipalId

#Add functionApp Identity to Contributor
$role = Get-AzureRmRoleDefinition -Name "Contributor"
New-AzureRMRoleAssignment -ObjectId $objectId -RoleDefinitionName "Contributor"  -Scope "/subscriptions/{subscription guid}"

#Assign functionApp Identity to AD Role 'Directory Readers'
$roleDefinition = Get-AzureADMSRoleDefinition -Filter "displayName eq 'Directory Readers'"
$roleAssignment = New-AzureADMSRoleAssignment  -RoleDefinitionId $roleDefinition.Id -PrincipalId $objectId  -ResourceScope "/"

#Assign functionApp Identity to AD Group
$group = Get-AzureADGroup  -Filter "DisplayName eq  'adgroup01'"
Add-AzureADGroupOwner -ObjectId $group.ObjectId -RefObjectId $objectId

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM