简体   繁体   English

无法在 Azure Devops 中使用 PowerShell 任务获取令牌

[英]Unable to acquire token using PowerShell task in Azure Devops

I have developed a PowerShell script which runs as expected when running on my laptop directly.我开发了一个 PowerShell 脚本,当直接在我的笔记本电脑上运行时,它会按预期运行。 However when running the script through Azure Devops it is failing to acquire the token and throws an error saying the client secret is incorrect.但是,当通过 Azure Devops 运行脚本时,它无法获取令牌并引发错误,提示客户端密码不正确。

The script is called as a PowerShell task from the pipeline, it runs on an ubuntu ADO agent.该脚本被称为管道中的 PowerShell 任务,它在 ubuntu ADO 代理上运行。

The script creates an AAD App Registration, sets some API permissions up, and then uses this registration to create some other elements in the AAD.该脚本创建一个 AAD 应用注册,设置一些 API 权限,然后使用此注册在 AAD 中创建一些其他元素。

Once the app registration is created, the script generates a Client Secret using创建应用注册后,脚本会使用

$clientSecret = (az ad app credential reset --id $appId --append  | ConvertFrom-Json).password

I can do a Write-Host $clientSecret in the script and the job shows the correct secret that was generated.我可以在脚本中执行Write-Host $clientSecret并且该作业显示生成的正确密钥。

The section that fails is during the Invoke-WebRequest command used to get the token.失败的部分是在用于获取令牌的Invoke-WebRequest命令期间。

# connect to graph using the application registration
$url = "https://login.microsoftonline.com/$b2cTenantId/oauth2/token"
$resource = "https://graph.microsoft.com/"
$restbody = @{
     grant_type    = 'client_credentials'
     client_id     = $appID
     client_secret = $clientSecret
     resource      = $resource
}

Write-Host "Rest Body = "
Write-Host ($restbody | out-string)
# Get the return Auth Token
$token = Invoke-WebRequest -Method POST -Uri $url -Body $restbody 
     

The error thrown is抛出的错误是

{"error":"invalid_client","error_description":"AADSTS7000215: Invalid client secret provided. Ensure the secret being sent in the request is the client secret value, not the client secret ID, for a secret added to app {"error":"invalid_client","error_description":"AADSTS7000215: 提供的客户端密码无效。确保请求中发送的密码是添加到应用程序的密码的客户端密码值,而不是客户端密码 ID

As I said, exactly the same script, using the same credentials runs locally fine.正如我所说,完全相同的脚本,使用相同的凭据在本地运行良好。 The script does an AZ Login and Connect-AZAccount first.该脚本首先执行 AZ Login 和Connect-AZAccount

To protect the secrets (password, access token, etc..) and prevent them from being leaked, Azure Pipelines does not recommend direct plaintext of secrets in the scripts.为了保护机密(密码、访问令牌等)并防止它们被泄露,Azure Pipelines 不建议在脚本中直接使用明文机密。

You need to set the secrets as secret variables through the logging command " SetVariable ".您需要通过日志记录命令“ SetVariable ”将秘密设置为秘密变量。

- pwsh: |
    Write-Host "##vso[task.setvariable variable=clientSecret;issecret=true]{Secret Value}"
  name: 'Set Secret'

Then in the subsequently PowerShell tasks, you can map the secret variables as environment variables for use in the PowerShell scripts.然后在随后的 PowerShell 任务中,您可以将机密变量映射为环境变量以在 PowerShell 脚本中使用。

  - powershell: | 
      Write-Host "This is a script that could use $CLIENT_SECRET"
      Write-Host "$CLIENT_SECRET= $(clientSecret)"
    env:
      CLIENT_SECRET: $(clientSecret)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 azure 中运行 powershell 脚本时无法获取租户组织的令牌 - unable to acquire token for tenant organization when running powershell scripts in azure 使用 Microsoft Identity 为 Graph 和 Azure DevOps API 获取唯一令牌 - Acquire a unique token for both Graph and Azure DevOps APIs using Microsoft Identity Azure powershell 任务中的 AzureRM 命令:Azure DevOps - AzureRM commands in Azure powershell Task: Azure DevOps Azure DevOps-自定义任务-具有Azure身份验证的PowerShell - Azure DevOps - Custom Task - PowerShell with Azure Authentification Azure DevOps Azure PowerShell 任务 output 变量 - 无法将值传递给第三阶段 - Azure DevOps Azure PowerShell task output variable - Unable to pass value to third stage PowerShell任务脚本路径Azure DevOps发布 - Script path of PowerShell task Azure DevOps release 从PowerShell获取Azure Function App的授权令牌 - Acquire Authorization Token for Azure Function App from PowerShell 无法在 Azure DevOPS 中资源组部署任务的覆盖参数中使用来自 PowerShell 脚本的输出变量 - Unable to use output variables from a PowerShell script in the Override Parameters of a Resource Group deployment task in Azure DevOPS 如何在 Azure DevOps Azure Powershell 任务中启用 PowerShell 交互模式? - How to enable PowerShell Interactive mode in Azure DevOps Azure Powershell Task? 在 Azure powershell 任务中添加/获取 Azure Keyvault 的秘密 - Add/get secret to Azure Keyvault with Azure powershell task in Azure devops
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM