Credentials not being passed from Azure Keyvault correctly during docker image build

I have a python script that uses the DefaultAzureCredential() function provided by the azure.identity library. This works fine locally when I’m logged into my Azure account that has the correct permissions and the script runs.

I’m now wanting to run the script from inside a docker container by using the environment variables stored in my key vault. As far as I can see, this should work. The image is built and pushed to my Azure container registry through a managed identity. However, when I pull the image and try to run it, I receive the following error:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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
</code>
<code>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 </code>
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

My yaml file looks like this:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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
</code>
<code>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 </code>
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

Is there a glaring error in there or how can it be improved? The service connections are all added correctly. I have tried hardcoding the exact credentials from the vault into a local dockerfile that ran locally and it also worked there, suggesting the problem is really due to the 3 secrets not being handled correctly.

Any suggestions or help is massively appreciated.

As far as I have tested based on your pipeline workflow, the issue does not seem to stem from the pipeline itself. I recommend you double-check how your Python script reads the environment variables defined in your Dockerfile.

For your reference, I have provided my sample files, which successfully printed the expected environment variable values when I ran the container.

azure-pipelines.yml

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>trigger: none
parameters:
- name: KVConnection
type: string
default: 'ARMSvcCnnWIFAutoSub1'
- name: keyVault
type: string
default: 'azkeyvaultxxxxxx '
- name: containerRegistry
type: string
default: 'DockerHubSvcCnn'
# - 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: azdockerhubxxx/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: azdockerhubxxx/planview
containerRegistry: ${{ parameters.containerRegistry }}
# identity: ${{ parameters.CRconnection }}
tags: |
planview
</code>
<code>trigger: none parameters: - name: KVConnection type: string default: 'ARMSvcCnnWIFAutoSub1' - name: keyVault type: string default: 'azkeyvaultxxxxxx ' - name: containerRegistry type: string default: 'DockerHubSvcCnn' # - 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: azdockerhubxxx/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: azdockerhubxxx/planview containerRegistry: ${{ parameters.containerRegistry }} # identity: ${{ parameters.CRconnection }} tags: | planview </code>
trigger: none

parameters:
- name: KVConnection
  type: string
  default: 'ARMSvcCnnWIFAutoSub1'
- name: keyVault
  type: string
  default: 'azkeyvaultxxxxxx '
- name: containerRegistry
  type: string
  default: 'DockerHubSvcCnn'
# - 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: azdockerhubxxx/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: azdockerhubxxx/planview
    containerRegistry: ${{ parameters.containerRegistry }}
    # identity: ${{ parameters.CRconnection }}
    tags: |
      planview


print_env.py

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import os
def print_env_variables():
client_id = os.getenv('AZURE_CLIENT_ID')
tenant_id = os.getenv('AZURE_TENANT_ID')
client_cred = os.getenv('AZURE_CLIENT_SECRET')
print(f"Client ID: {client_id}")
print(f"Tenant ID: {tenant_id}")
print(f"Client Secret: {client_cred}")
if __name__ == "__main__":
print_env_variables()
</code>
<code>import os def print_env_variables(): client_id = os.getenv('AZURE_CLIENT_ID') tenant_id = os.getenv('AZURE_TENANT_ID') client_cred = os.getenv('AZURE_CLIENT_SECRET') print(f"Client ID: {client_id}") print(f"Tenant ID: {tenant_id}") print(f"Client Secret: {client_cred}") if __name__ == "__main__": print_env_variables() </code>
import os

def print_env_variables():
    client_id = os.getenv('AZURE_CLIENT_ID')
    tenant_id = os.getenv('AZURE_TENANT_ID')
    client_cred = os.getenv('AZURE_CLIENT_SECRET')

    print(f"Client ID: {client_id}")
    print(f"Tenant ID: {tenant_id}")
    print(f"Client Secret: {client_cred}")

if __name__ == "__main__":
    print_env_variables()

Dockerfile

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code># Base image with Python
FROM python:3.9-slim
# Set environment variables (will be replaced by build arguments)
ARG AZURE_CLIENT_ID
ARG AZURE_TENANT_ID
ARG AZURE_CLIENT_SECRET
# Set them as environment variables inside the container
ENV AZURE_CLIENT_ID=$AZURE_CLIENT_ID
ENV AZURE_TENANT_ID=$AZURE_TENANT_ID
ENV AZURE_CLIENT_SECRET=$AZURE_CLIENT_SECRET
# Copy the Python script into the container
COPY print_env.py /app/print_env.py
# Set the working directory
WORKDIR /app
# Run the Python script to verify environment variables
RUN python print_env.py
# Default command for the image
CMD ["python", "print_env.py"]
</code>
<code># Base image with Python FROM python:3.9-slim # Set environment variables (will be replaced by build arguments) ARG AZURE_CLIENT_ID ARG AZURE_TENANT_ID ARG AZURE_CLIENT_SECRET # Set them as environment variables inside the container ENV AZURE_CLIENT_ID=$AZURE_CLIENT_ID ENV AZURE_TENANT_ID=$AZURE_TENANT_ID ENV AZURE_CLIENT_SECRET=$AZURE_CLIENT_SECRET # Copy the Python script into the container COPY print_env.py /app/print_env.py # Set the working directory WORKDIR /app # Run the Python script to verify environment variables RUN python print_env.py # Default command for the image CMD ["python", "print_env.py"] </code>
# Base image with Python
FROM python:3.9-slim

# Set environment variables (will be replaced by build arguments)
ARG AZURE_CLIENT_ID
ARG AZURE_TENANT_ID
ARG AZURE_CLIENT_SECRET

# Set them as environment variables inside the container
ENV AZURE_CLIENT_ID=$AZURE_CLIENT_ID
ENV AZURE_TENANT_ID=$AZURE_TENANT_ID
ENV AZURE_CLIENT_SECRET=$AZURE_CLIENT_SECRET

# Copy the Python script into the container
COPY print_env.py /app/print_env.py

# Set the working directory
WORKDIR /app

# Run the Python script to verify environment variables
RUN python print_env.py

# Default command for the image
CMD ["python", "print_env.py"]

3

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật