将 Microsoft Graph 权限分配给 Azure 托管服务标识
Assigning Microsoft Graph permissions to Azure Managed Service Identity
我正在尝试为我的 Azure 逻辑应用程序向 Azure 托管服务标识分配权限,但是 运行 出错了。
我使用的是 2.0.1.16 版本的 AzureAD powershell 模块。
我所做的如下:
- 创建逻辑应用
- 在该逻辑应用的工作流设置中生成 Azure 托管服务标识。
- 执行下面的 PowerShell 以将 group.readwrite.all 角色分配给托管服务身份。
它首先抛出拒绝访问错误,随后的尝试将抛出 "InvalidRequest" 错误。我是全局管理员,拥有资源组的完全访问权限(这是我拥有完全权限的基本开发租户)
我已经在不同的租户身上试过了。有人能让这个工作吗?
PS C:\Users\markp> $msi = Get-AzureADServicePrincipal | ?{$_.DisplayName -ieq "test"}
PS C:\Users\markp> $mai
PS C:\Users\markp> $msi
ObjectId AppId DisplayName
-------- ----- -----------
8d87a1f9-fab0-45a9-a6f3-1e93b2d99b52 032c3c1c-2530-4eae-b390-153e3b0eb3c6 TEST
PS C:\Users\markp> $graph = Get-AzureADServicePrincipal -Filter "AppId eq '00000003-0000-0000-c000-000000000000'"
PS C:\Users\markp> $graph
ObjectId AppId DisplayName
-------- ----- -----------
5a634981-6bbe-4c81-9aef-185a71df56f7 00000003-0000-0000-c000-000000000000 Microsoft Graph
PS C:\Users\markp> $role = $graph.AppRoles | ?{$_.Value -imatch "group.read.all" }
PS C:\Users\markp> $role
AllowedMemberTypes : {Application}
Description : Allows the app to read group properties and memberships, and read the calendar and conversations f
or all groups, without a signed-in user.
DisplayName : Read all groups
Id : 5b567255-7703-4780-807c-7be8301ae99b
IsEnabled : True
Value : Group.Read.All
PS C:\Users\markp> New-AzureADServiceAppRoleAssignment -ObjectId $msi.ObjectId -PrincipalId $msi.ObjectId -Id $role.Id -ResourceId $graph.ObjectId
New-AzureADServiceAppRoleAssignment : Error occurred while executing NewServicePrincipalAppRoleAssignment
Code: Authorization_RequestDenied
Message: Insufficient privileges to complete the operation.
HttpStatusCode: Forbidden
HttpStatusDescription: Forbidden
HttpResponseStatus: Completed
At line:1 char:1
+ New-AzureADServiceAppRoleAssignment -ObjectId $msi.ObjectId -Principa ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [New-AzureADServiceAppRoleAssignment], ApiException
+ FullyQualifiedErrorId : Microsoft.Open.AzureAD16.Client.ApiException,Microsoft.Open.AzureAD16.PowerShell.NewServ
icePrincipalAppRoleAssignment
PS C:\Users\markp> New-AzureADServiceAppRoleAssignment -ObjectId $msi.ObjectId -PrincipalId $msi.ObjectId -Id $role.Id -ResourceId $graph.ObjectId
New-AzureADServiceAppRoleAssignment : Error occurred while executing NewServicePrincipalAppRoleAssignment
Code: Request_BadRequest
Message: One or more properties are invalid.
RequestId: 57ef7908-dd8b-4a21-b48e-f2692d324264
DateTimeStamp: Fri, 28 Sep 2018 14:36:55 GMT
HttpStatusCode: BadRequest
HttpStatusDescription: Bad Request
HttpResponseStatus: Completed
At line:1 char:1
+ New-AzureADServiceAppRoleAssignment -ObjectId $msi.ObjectId -Principa ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [New-AzureADServiceAppRoleAssignment], ApiException
+ FullyQualifiedErrorId : Microsoft.Open.AzureAD16.Client.ApiException,Microsoft.Open.AzureAD16.PowerShell.NewServ
icePrincipalAppRoleAssignment
初始设置
我在我的试用订阅中设置了一个名为“test”的新逻辑应用程序,就像您在上面解释的那样,并通过工作流设置为其启用了 MSI。然后我使用了与上面完全相同的 PowerShell 命令。
解释正在发生的两件最重要的事情
首先运行 - 即使您遇到代码错误:Authorization_RequestDenied,添加新角色分配的操作实际上已经完成了它的工作。添加了新权限。 (我将在下面的部分对此提供一些证据)
第一个 运行 之后的后续 运行 秒 - 您收到代码错误:Request_BadRequest,因为 permissions/role 您正在尝试添加已在第一个 运行.
中添加
证据
完全来自您的 post 的 PowerShell 脚本。
这些查询仅在 Azure AD Graph Explorer(而不是新的 Microsoft Graph Explorer)中对我有用。此外,查询区分大小写,因此即使是很小的差异也会产生影响。
您基本上可以查看服务主体的角色分配,在第一次 运行 启动您的 PowerShell 脚本之前和之后,您可以看到所需的权限实际上已添加(我创建了一个新的逻辑应用程序里面什么都没有,只是启用了 MSI 来测试这部分)
Azure AD 图形查询:
Azure AD Graph Query: https://graph.windows.net/{tenant Id}/servicePrincipals/{MSI Service Principal ID}/appRoleAssignments
{MSI Service Principal ID} is 8d87a1f9-fab0-45a9-a6f3-1e93b2d99b52 in your case.
MSI 服务主体的角色分配在使用上述查询的第一个 运行 New-AzureADServiceAppRoleAssignment 之前。
MSI 服务主体的角色分配在使用上述查询的第一个 运行 New-AzureADServiceAppRoleAssignment 之后。
另一个相关但略有不同的 SO Post
提到的 PowerShell 脚本几乎与您正在使用的相同。有趣的是,对于许多人来说,脚本看起来也没有错误。
但请注意用户 Olandese
的最后评论
目前该行为尚未更改。
- 第一次伪造认证错误
- 第二次错误请求
但是,在执行 Powershell 命令后,您现在还可以通过 MS Graph 的新 beta 端点确认正在应用的更改。
导航至 https://developer.microsoft.com/en-us/graph/graph-explorer 并向您的用户进行身份验证(应在左侧栏中指明)
您可以通过以下方式查询申请的角色:
https://graph.microsoft.com/beta/servicePrincipals/{msi.ObjId}/appRoleAssignedTo
我正在尝试为我的 Azure 逻辑应用程序向 Azure 托管服务标识分配权限,但是 运行 出错了。
我使用的是 2.0.1.16 版本的 AzureAD powershell 模块。
我所做的如下:
- 创建逻辑应用
- 在该逻辑应用的工作流设置中生成 Azure 托管服务标识。
- 执行下面的 PowerShell 以将 group.readwrite.all 角色分配给托管服务身份。
它首先抛出拒绝访问错误,随后的尝试将抛出 "InvalidRequest" 错误。我是全局管理员,拥有资源组的完全访问权限(这是我拥有完全权限的基本开发租户)
我已经在不同的租户身上试过了。有人能让这个工作吗?
PS C:\Users\markp> $msi = Get-AzureADServicePrincipal | ?{$_.DisplayName -ieq "test"}
PS C:\Users\markp> $mai
PS C:\Users\markp> $msi
ObjectId AppId DisplayName
-------- ----- -----------
8d87a1f9-fab0-45a9-a6f3-1e93b2d99b52 032c3c1c-2530-4eae-b390-153e3b0eb3c6 TEST
PS C:\Users\markp> $graph = Get-AzureADServicePrincipal -Filter "AppId eq '00000003-0000-0000-c000-000000000000'"
PS C:\Users\markp> $graph
ObjectId AppId DisplayName
-------- ----- -----------
5a634981-6bbe-4c81-9aef-185a71df56f7 00000003-0000-0000-c000-000000000000 Microsoft Graph
PS C:\Users\markp> $role = $graph.AppRoles | ?{$_.Value -imatch "group.read.all" }
PS C:\Users\markp> $role
AllowedMemberTypes : {Application}
Description : Allows the app to read group properties and memberships, and read the calendar and conversations f
or all groups, without a signed-in user.
DisplayName : Read all groups
Id : 5b567255-7703-4780-807c-7be8301ae99b
IsEnabled : True
Value : Group.Read.All
PS C:\Users\markp> New-AzureADServiceAppRoleAssignment -ObjectId $msi.ObjectId -PrincipalId $msi.ObjectId -Id $role.Id -ResourceId $graph.ObjectId
New-AzureADServiceAppRoleAssignment : Error occurred while executing NewServicePrincipalAppRoleAssignment
Code: Authorization_RequestDenied
Message: Insufficient privileges to complete the operation.
HttpStatusCode: Forbidden
HttpStatusDescription: Forbidden
HttpResponseStatus: Completed
At line:1 char:1
+ New-AzureADServiceAppRoleAssignment -ObjectId $msi.ObjectId -Principa ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [New-AzureADServiceAppRoleAssignment], ApiException
+ FullyQualifiedErrorId : Microsoft.Open.AzureAD16.Client.ApiException,Microsoft.Open.AzureAD16.PowerShell.NewServ
icePrincipalAppRoleAssignment
PS C:\Users\markp> New-AzureADServiceAppRoleAssignment -ObjectId $msi.ObjectId -PrincipalId $msi.ObjectId -Id $role.Id -ResourceId $graph.ObjectId
New-AzureADServiceAppRoleAssignment : Error occurred while executing NewServicePrincipalAppRoleAssignment
Code: Request_BadRequest
Message: One or more properties are invalid.
RequestId: 57ef7908-dd8b-4a21-b48e-f2692d324264
DateTimeStamp: Fri, 28 Sep 2018 14:36:55 GMT
HttpStatusCode: BadRequest
HttpStatusDescription: Bad Request
HttpResponseStatus: Completed
At line:1 char:1
+ New-AzureADServiceAppRoleAssignment -ObjectId $msi.ObjectId -Principa ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [New-AzureADServiceAppRoleAssignment], ApiException
+ FullyQualifiedErrorId : Microsoft.Open.AzureAD16.Client.ApiException,Microsoft.Open.AzureAD16.PowerShell.NewServ
icePrincipalAppRoleAssignment
初始设置
我在我的试用订阅中设置了一个名为“test”的新逻辑应用程序,就像您在上面解释的那样,并通过工作流设置为其启用了 MSI。然后我使用了与上面完全相同的 PowerShell 命令。
解释正在发生的两件最重要的事情
首先运行 - 即使您遇到代码错误:Authorization_RequestDenied,添加新角色分配的操作实际上已经完成了它的工作。添加了新权限。 (我将在下面的部分对此提供一些证据)
第一个 运行 之后的后续 运行 秒 - 您收到代码错误:Request_BadRequest,因为 permissions/role 您正在尝试添加已在第一个 运行.
中添加
证据
完全来自您的 post 的 PowerShell 脚本。
这些查询仅在 Azure AD Graph Explorer(而不是新的 Microsoft Graph Explorer)中对我有用。此外,查询区分大小写,因此即使是很小的差异也会产生影响。
您基本上可以查看服务主体的角色分配,在第一次 运行 启动您的 PowerShell 脚本之前和之后,您可以看到所需的权限实际上已添加(我创建了一个新的逻辑应用程序里面什么都没有,只是启用了 MSI 来测试这部分)
Azure AD 图形查询:
Azure AD Graph Query: https://graph.windows.net/{tenant Id}/servicePrincipals/{MSI Service Principal ID}/appRoleAssignments
{MSI Service Principal ID} is 8d87a1f9-fab0-45a9-a6f3-1e93b2d99b52 in your case.
MSI 服务主体的角色分配在使用上述查询的第一个 运行 New-AzureADServiceAppRoleAssignment 之前。
MSI 服务主体的角色分配在使用上述查询的第一个 运行 New-AzureADServiceAppRoleAssignment 之后。
另一个相关但略有不同的 SO Post
目前该行为尚未更改。
- 第一次伪造认证错误
- 第二次错误请求
但是,在执行 Powershell 命令后,您现在还可以通过 MS Graph 的新 beta 端点确认正在应用的更改。
导航至 https://developer.microsoft.com/en-us/graph/graph-explorer 并向您的用户进行身份验证(应在左侧栏中指明)
您可以通过以下方式查询申请的角色: https://graph.microsoft.com/beta/servicePrincipals/{msi.ObjId}/appRoleAssignedTo