如何在 Linux 上运行的 Azure Devops Pipelines 中使用 azcopy CLI 使用托管标识对 azure 文件共享进行身份验证,而无需 SAS 令牌?

问题描述 投票:0回答:1

现在我们有这个 azure 管道 AzureCLI 任务,我们使用预安装的 azcopy cli 将文件从管道工作区/git 复制到我们的 azure 文件共享中。我们之前在 URL 中使用 SAS 令牌,但是,这些令牌最终会过期,这是另一个需要管理的秘密。我们知道有一种方法可以在没有 SAS 令牌的情况下将文件上传到 azure 文件共享,并且文档说它应该与像这样的简单 cli 任务一起使用

 - task: AzureCLI@2
      inputs:
        azureSubscription: your-azure-service-connection-name
        scriptType: 'bash'
        scriptLocation: 'inlineScript'
        inlineScript: |                    
              export AZCOPY_AUTO_LOGIN_TYPE=AZCLI
              azcopy sync directory/MyFilesToCopy https://mystorageaccount.file.core.windows.net --recursive --put-md5
        addSpnToEnvironment: true
        failOnStandardError: true

我们得到的错误是:

Failed to perform Auto-login: AzureCLICredential: WARNING: Could not retrieve credential from local cache for service principal *** under tenant common. Trying credential under tenant xxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx, assuming that is an app credential.
ERROR: AADSTS700016: Application with identifier '***' was not found in the directory 'Microsoft'. This can happen if the application has not been installed by the administrator of the tenant or consented to by any user in the tenant. You may have sent your authentication request to the wrong tenant. Trace ID: b0c23ef4-f557-4151-8a1a-375d236b4700 Correlation ID: 2066f167-dae6-4dd1-9031-7b0a1136ac4b Timestamp: 2024-04-08 14:34:32Z
Interactive authentication is needed. Please run:
az login

这个很能说明问题,因为在其正上方,我可以看到成功的 azure cli 登录信息

/usr/bin/az login --service-principal -u *** --tenant xxxxx-xxxx-xxxx-xxxx-xxxxxxxx --allow-no-subscriptions --federated-token ***
[
  {
    "cloudName": "AzureCloud",
    .....

azure-devops azure-cli azure-pipelines-yaml azure-service-principal azure-cli2
1个回答
0
投票

我们的解决方案: 您需要将 azuretenantId 添加为环境变量,以便 azcopy 自动登录知道要使用哪个租户,尽管我们的错误消息显示它试图使用我们的租户进行连接,但指定它会强制它不使用 Microsoft“通用”租户。

此外,您需要确保您的服务连接对您的资源组具有贡献者权限(图 1),并且服务主体对您的存储帐户具有存储文件数据特权贡献者 权限。

此处的文档中对此进行了说明:https://learn.microsoft.com/en-us/azure/storage/common/storage-use-azcopy-authorize-azure-active-directory。没有提到自动登录部分需要包含tenantId。

 - task: AzureCLI@2
      inputs:
        azureSubscription: your-azure-service-connection-name
        scriptType: 'bash'
        scriptLocation: 'inlineScript'
        inlineScript: |                    
              export AZCOPY_AUTO_LOGIN_TYPE=AZCLI
              export AZCOPY_TENANT_ID=$tenantId
              azcopy sync directory/MyFilesToCopy https://mystorageaccount.file.core.windows.net --recursive --put-md5
        addSpnToEnvironment: true
        failOnStandardError: true
© www.soinside.com 2019 - 2024. All rights reserved.