我有一个使用 azure.identity 库提供的 DefaultAzureCredential() 函数的 python 脚本。当我登录到具有正确权限的 Azure 帐户并运行脚本时,此脚本在本地运行良好。
我现在想使用存储在密钥保管库中的环境变量从 docker 容器内部运行脚本。据我所知,这应该可行。该映像是通过托管标识构建并推送到我的 Azure 容器注册表的。但是,当我拉取映像并尝试运行它时,我收到以下错误:
default_credential = DefaultAzureCredential()
^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/site-packages/azure/identity/_credentials/default.py", line 149, in __init__
credentials.append(EnvironmentCredential(authority=authority, _within_dac=True, **kwargs))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/site-packages/azure/identity/_credentials/environment.py", line 70, in __init__
self._credential = ClientSecretCredential(
^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/site-packages/azure/identity/_credentials/client_secret.py", line 45, in __init__
raise ValueError("client_id should be the id of a Microsoft Entra application")
ValueError: client_id should be the id of a Microsoft Entra application
我的 yaml 文件如下所示:
parameters:
- name: KVConnection
type: string
default: 'read-keyvault'
- name: keyVault
type: string
default: 'bi-pv-kv'
- name: containerRegistry
type: string
default: 'dcr'
- name: CRconnection
type: string
default: 'buildandpushdocker'
steps:
- task: AzureKeyVault@2
displayName: "Get KV secrets"
inputs:
azureSubscription: ${{ parameters.KVConnection }}
KeyVaultName: ${{ parameters.keyVault }}
SecretsFilter: '*'
RunAsPreJob: true
- bash: |
echo "##vso[task.setvariable variable=clientid]$(sp-client-id)"
echo "##vso[task.setvariable variable=clientcred]$(sp-az-secret)"
echo "##vso[task.setvariable variable=tenantid]$(sp-tenant-id)"
displayName: Set environment variables
- task: Docker@2
displayName: Build Planview image
inputs:
command: build
repository: planview
containerRegistry: ${{ parameters.containerRegistry }}
identity: ${{ parameters.CRconnection }}
arguments: '--build-arg AZURE_CLIENT_ID=$(clientid) --build-arg AZURE_TENANT_ID=$(tenantid)
--build-arg AZURE_CLIENT_SECRET=$(clientcred)'
tags: |
planview
- task: Docker@2
displayName: Push Planview image
inputs:
command: push
repository: planview
containerRegistry: ${{ parameters.containerRegistry }}
identity: ${{ parameters.CRconnection }}
tags: |
planview
其中是否存在明显的错误或如何改进?服务连接均已正确添加。我尝试将保险库中的确切凭据硬编码到本地运行的本地 dockerfile 中,它也在那里工作,这表明问题实际上是由于未正确处理 3 个机密造成的。
非常感谢任何建议或帮助。
据我根据您的管道工作流程进行的测试,问题似乎并非源于管道本身。我建议您仔细检查 Python 脚本如何读取 Dockerfile 中定义的环境变量。
为了您的参考,我提供了我的示例文件,当我运行容器时,这些文件成功打印了预期的环境变量值。
azure-pipelines.yml
打印环境.py
Dockerfile