首页 > 解决方案 > 通过服务主体访问 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. 所以这并不能告诉我任何事情。于是我开始调查:

谁能帮我解决我的情况?

回答限制:

我需要能够通过我的服务连接重用我已经在我的 azure 管道中使用的服务主体来访问 pim api。因此,我不是在寻找有关在 azure 中创建证书并使用它进行身份验证的答案。

编辑:一些背景信息:

标签: azureazure-pipelinesazure-authentication

解决方案


guid您从 azure 门户中的浏览器中的令牌获得的应该是正确的。

但是这个 API 端点似乎没有公开给外部使用。

我们目前无法为此资源生成应用程序令牌。

如您所见,有几个用户语音帖子需要此功能。

继续对帖子进行投票会有所帮助。


推荐阅读