Search code examples
azure-container-registry

Not able to get the list of azure repository using python


I’m newbie to azure, trying to list the repositories in azure container registry using client id, tenant id, secret using below code.

from azure.containerregistry import ContainerRegistryClient
from azure.identity import ClientSecretCredential


registry_name = "test1"
resource_group = "donieee"
app_id="xxxxx"
secret="xxxxxxx"
tenantid="xxxxxx"

credential = ClientSecretCredential(client_id=app_id,client_secret=secret,tenant_id=tenantid)

# Create a client object for the ACR
client = ContainerRegistryClient(
    endpoint=f"{registry_name}.azurecr.io",
    credential=credential
)

repositories = client.list_repository_names()
for repo in repositories:
    print(repo)

getting below error azure.core.exceptions.ClientAuthenticationError: Operation returned an invalid status ‘Unauthorised’

Kindly suggest me answer.


Solution

  • I tried in my environment and got the same error:

    enter image description here

    The above error occurs when your application doesn't have the proper role to list the repositories.

    Note: According to MS-DOCS to list the respositories you need either ACR pull role or Contributor role required.

    On my side, I have assigned the role to the application and run the same code it is listing the repositories.

    Code:

    from azure.containerregistry import ContainerRegistryClient
    from azure.identity import ClientSecretCredential
    
    
    registry_name = "test1"
    resource_group = "donieee"
    app_id="xxxxx"
    secret="xxxxxxx"
    tenantid="xxxxxx"
    
    credential = ClientSecretCredential(client_id=app_id,client_secret=secret,tenant_id=tenantid)
    
    # Create a client object for the ACR
    client = ContainerRegistryClient(
        endpoint=f"{registry_name}.azurecr.io",
        credential=credential
    )
    
    repositories = client.list_repository_names()
    for repo in repositories:
        print(repo)
    
    

    In my portal, I have some repositories in my Azure container registry like as below.

    Portal:

    enter image description here

    Output:

    enter image description here