使用服务连接获取 OAuth 令牌

Obtain OAuth token using Service Connection

我正在使用 Azure DevOps 部署 Web 应用程序并为 SQL 数据库执行数据库迁移。我有一个服务连接设置,并且能够使用 Azure CLI 部署 Azure 资源,例如

- task: AzureCLI@2
  displayName: 'Deploy Azure resources'
  inputs:
    azureSubscription: 'My Service Connection'
    scriptType: 'bash'
    scriptLocation: inlineScript
    inlineScript: ./deploy.sh

现在,我想进行数据库迁移,我想使用Service Connection 进行身份验证(App Registration 已被授权访问SQL 数据库)。我的理解是我可以使用 OAuth 令牌登录到 SQL 数据库 - 我的问题是如何使用服务连接获取该令牌。

请注意,我无法将客户端机密添加到 Azure 中的服务主体,因此无法使用 client_secret/oauth2/token 进行 REST 调用(我在企业工作space,事情都被锁定了)。

鉴于管道可以访问 Azure DevOps 服务连接,我的感觉是必须有一些方法可以使用它来获取对 https://database.windows.net/ 资源有效的 OAuth 令牌 - 但是如何?

您可以尝试以下情况的解决方案:

Adding an Azure CLI task which retrieved the bearer token. Then passed this to Azure Powershell task which used the token.

$token= & az account get-access-token --resource=https://database.windows.net --query accessToken
Write-Host("##vso[task.setvariable variable=sqlToken]$token")