问题描述
我正在尝试在 azure 管道中调用 azure 特权身份管理 api (https://api.azrbac.mspim.azure.com/api/v2/privilegedAccess
)。我有以下代码来调用 register
方法,但它不起作用,但我无法弄清楚出了什么问题。我先展示一下代码:
install-module azureadpreview -Force
import-module azureadpreview
$context = [Microsoft.Azure.Commands.Common.Authentication.Abstractions.AzureRmProfileProvider]::Instance.Profile.DefaultContext
$graphToken = [Microsoft.Azure.Commands.Common.Authentication.AzureSession]::Instance.AuthenticationFactory.Authenticate($context.Account,$context.Environment,$context.Tenant.Id.ToString(),$null,[Microsoft.Azure.Commands.Common.Authentication.ShowDialog]::Never,"https://graph.microsoft.com").Accesstoken
$pimToken = [Microsoft.Azure.Commands.Common.Authentication.AzureSession]::Instance.AuthenticationFactory.Authenticate($context.Account,"<what do i enter here?>").Accesstoken
$aadToken = [Microsoft.Azure.Commands.Common.Authentication.AzureSession]::Instance.AuthenticationFactory.Authenticate($context.Account,"https://graph.windows.net").Accesstoken
Connect-AzureAD -AadAccesstoken $aadToken -AccountId $context.Account.Id -TenantId $context.tenant.id -MsAccesstoken $graphToken
Write-Output "Create PIM role"
$Group = New-AzureADMSGroup -displayName "TestPIMGroup" -Description "TestForPim" -mailenabled $false -SecurityEnabled $true -MailNickName "NOTUSED" -IsAssignabletoRole $true
Write-Output "Test api call"
$Headers = @{
"Accept" = "*/*"
"Accept-Language" = "en"
"Authorization" = "Bearer {0}" -f $pimToken
"Content-Type" = "application/json"
}
$Body = @{
externalId = $Group.Id
} | ConvertTo-Json
$URL = 'https://api.azrbac.mspim.azure.com/api/v2/privilegedAccess/aadGroups/resources/register'
Write-Output "Body: $Body"
$Headerjson = $Headers | ConvertTo-Json
Write-Output "Headers: $Headerjson"
try {
$QueryResponse = Invoke-RestMethod -Uri $URL -Headers $Headers -Method POST -Body $Body
}
catch {
$_.Exception.Response
$result = $_.Exception.Response.GetResponseStream()
$reader = New-Object System.IO.StreamReader($result)
$reader.BaseStream.Position = 0
$reader.discardBufferedData()
$responseBody = $reader.ReadToEnd();
$responseBody
exit 1
}
$QueryResponse.value
所以我想要完成的是,它创建一个 PIM 组,并在其上启用特权访问。 azureadpreview
模块具有创建 PIM 组的功能,所以我使用它。这完美地工作。获取我从 this post 获得的服务主体的令牌的方法。
现在要启用特权访问,我需要直接调用 API,因为似乎没有任何 powershell 命令可以使用它。这是事情变得棘手的地方。 API 调用返回 500 内部服务器错误,正文中唯一的错误是 an error has occurred
。所以这并没有真正告诉我任何事情。于是我开始调查:
- 当我不传递令牌时,我得到一个未经授权的异常
- 当我将无意义作为令牌传递时,我收到内部服务器错误。所以这给我指明了令牌有问题的方向。我认为令牌中的受众标签有问题。
- 我尝试了令牌中的各种 URL,但都显示 500 作为响应。
- 我在执行相同调用时记录了浏览器在 azure 门户中执行的 api 调用。我从这个请求中读取了令牌,它为我提供了一些 aud 的 guid。当我使用这个 guid 时,它给了我一个
Unauthorized
响应
有人能帮我解决我的情况吗?
回答限制:
我需要能够通过我的服务连接重用我已经在我的 azure 管道中使用的服务主体来访问 pim api。所以我不是在寻找有关在 azure 中创建证书并使用它进行身份验证的答案。
编辑: 一些背景信息:
- https://feedback.azure.com/forums/169401-azure-active-directory/suggestions/42203638-allow-privileged-access-for-groups-to-be-enabled-f
- https://feedback.azure.com/forums/34192--general-feedback/suggestions/42644962-possibility-for-azure-ad-privileged-access-groups
- https://github.com/MicrosoftDocs/azure-docs/issues/70296#issuecomment-776069283
解决方法
您从 azure 门户浏览器中的令牌中获得的 guid
应该是正确的。
但是这个 API 端点似乎没有公开给外部使用。
我们目前无法为此资源生成应用程序令牌。
如您所见,有多个用户语音帖子需要此功能。
继续为帖子投票会有帮助。