【发布时间】:2021-02-10 12:56:22
【问题描述】:
我正在尝试在 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, $null, "https://graph.microsoft.com").AccessToken
$pimToken = [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, $null, "<what do i enter here?>").AccessToken
$aadToken = [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, $null, "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得到的服务主体获取token的方法。
现在要启用特权访问,我需要直接调用 API,因为它似乎没有任何 powershell 命令。这就是事情变得棘手的地方。 API 调用返回 500 内部服务器错误,正文中的唯一错误是 an error has occurred。所以这并不能告诉我任何事情。于是我开始调查:
- 当我不传递令牌时,我得到一个未经授权的异常
- 当我将废话作为令牌传递时,我收到内部服务器错误。所以这为我指明了令牌有问题的方向。我认为令牌中的受众标签有问题。
- 我尝试了令牌中的各种 URL,都显示 500 作为响应。
- 在执行相同调用时,我记录了浏览器在 Azure 门户中执行的 api 调用。我从这个请求中读取了令牌,它给了我一些指导作为 aud。当我使用这个 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 azure-pipelines azure-authentication