【问题标题】:Access Azure PIM api in azure pipelines via service principal通过服务主体访问 Azure 管道中的 Azure PIM api
【发布时间】: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 中创建证书并使用它进行身份验证的答案。

编辑: 一些背景资料:

【问题讨论】:

    标签: azure azure-pipelines azure-authentication


    【解决方案1】:

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

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

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

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

    继续为帖子投票会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-21
      • 1970-01-01
      • 2022-01-03
      • 1970-01-01
      • 2014-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多