azure - 通过服务主体访问 Azure 管道中的 Azure PIM api
问题描述
我正在尝试https://api.azrbac.mspim.azure.com/api/v2/privilegedAccess
在 azure 管道中调用 azure 特权身份管理 api ()。我有以下代码来调用该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 组的功能,所以我使用它。这完美地工作。我从这篇文章中获得的服务主体获取令牌的方法。
现在要对其启用特权访问,我需要直接调用 API,因为它似乎没有任何 powershell 命令。这就是事情变得棘手的地方。API 调用返回 500 内部服务器错误,正文中的唯一错误是an error has occurred
. 所以这并不能告诉我任何事情。于是我开始调查:
- 当我不传递令牌时,我得到一个未经授权的异常
- 当我将废话作为令牌传递时,我收到内部服务器错误。所以这为我指明了令牌有问题的方向。我认为令牌中的观众标签有问题。
- 我在令牌中尝试了各种 URL,它们都显示 500 作为响应。
- 在执行相同调用时,我记录了浏览器在 azure 门户中执行的 api 调用。我从这个请求中读取了令牌,它给了我一些指导作为 aud。当我使用这个指南时,它给了我一个
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
解决方案
guid
您从 azure 门户中的浏览器中的令牌获得的应该是正确的。
但是这个 API 端点似乎没有公开给外部使用。
我们目前无法为此资源生成应用程序令牌。
如您所见,有几个用户语音帖子需要此功能。
继续对帖子进行投票会有所帮助。
推荐阅读
- oracle - Can not install Oracle database client 18c on win 10 32 bit
- arduino - 我的代码没有打开继电器模块中的所有四个通道
- java - Java:将参数传递给有界参数函数
- python - TF2.0 中的 saved_model.prune()
- c# - WPF ClickOnce 安装程序不包含来自 nuget 包的非托管 dll
- serverless - 比较 Apache Velocity 模板中的字符串 (AWS AppSync)
- android - 带有 Google 地图的 Android 版 Facebook Messenger
- javascript - 带有复选框的控件元素显示
- javascript - 检查登录状态,Google oAuth,没有呈现按钮
- python - Python 中基于 yield 的协程与带有 @asyncio.coroutine 和 @types.coroutine 装饰器的协程有何不同?