azure-sdk-for-python: `AzureMLOnBehalfOfCredential` does not work with keyvault

  • Package Name: azure-ai-ml
  • Package Version: 1.25.0
  • Operating System: Linux
  • Python Version: 3.12

Describe the bug

Azure AI ML describes in its documentation that AzureMLOnBehalfOfCredential can be used to access for example keyvault. Due to what seems to be a programming error (TypeError: Session.request() got an unexpected keyword argument 'claims'), that is however not possible.

To Reproduce

Here’s a minimal reproducer:

# hello.py
import sys
from os import environ

from azure.ai.ml import MLClient, command
from azure.ai.ml.identity import AzureMLOnBehalfOfCredential
from azure.ai.ml.entities import JobResourceConfiguration, UserIdentityConfiguration, Environment, BuildContext
from azure.identity import DefaultAzureCredential
from azure.keyvault.secrets import SecretClient


def do_submit() -> None:
    client = MLClient(
        credential=DefaultAzureCredential(),
        subscription_id=environ.get("AZURE_ML_SUBSCRIPTION_ID"),
        workspace_name=environ.get("AZURE_ML_WORKSPACE"),
        resource_group_name=environ.get("AZURE_ML_RESOURCE_GROUP"),
    )
    job = command(
        code=".",
        command="python hello.py kv --obo",
        environment=Environment(
            name="OboAuth-MRE-Env",
            version="1",
            build=BuildContext(
                path=".",
                dockerfile_path="Dockerfile",
            ),
        ),
        display_name="mre-run",
        experiment_name="az-auth-issue-mre",
        identity=UserIdentityConfiguration(),
        resources=JobResourceConfiguration(
            instance_type="Standard_E4_v3",
            instance_count=1,
        ),
        queue_settings={"job_tier": "Standard"},
    )
    res = client.jobs.create_or_update(job)
    print("Job submitted:", res)


def do_access_kv(use_obo: bool = False) -> None:
    if use_obo:
        credential = AzureMLOnBehalfOfCredential()
    else:
        credential = DefaultAzureCredential()
    client = SecretClient(
        vault_url="https://MY-KV-URL-HERE.vault.azure.net/",
        credential=credential,
    )
    value = client.get_secret("MY-SECRET-HERE").value
    assert value is not None, "Failed to access key vault"
    print("Successfully accessed key vault")


if __name__ == "__main__":
    if "submit" in sys.argv:
        do_submit()
    elif "kv" in sys.argv:
        do_access_kv(use_obo="--obo" in sys.argv)
    else:
        print("Usage: python hello.py [submit|kv] [--obo]")
        sys.exit(1)

And

FROM python:3.12
RUN pip install azure-ai-ml azure-identity azure-keyvault
  • First, verify the output from running hello.py kv locally is “Successfully accessed key vault”.
  • Second, run hello.py submit to submit to Azure ML
  • Third, wait and observe that the Azure ML job fails with the exception TypeError: Session.request() got an unexpected keyword argument 'claims'

Expected behavior

I expected the “on behalf of” credential to work essentially the same as the default credential, with my identity.

This is also how documentation in the examples repository thinks it should work.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 23 (6 by maintainers)

Commits related to this issue

Most upvoted comments

Hello @achauhan-scc,

We are having a similar issue than @jonaslb: We would need to be able to impersonate the caller and run the workload with its identity without mounting the identities on the compute cluster (the compute cluster could be used by different group of people with diverging interrests). The OnBehalfOf solution as described in the official documentation seems like a solution that would resolve our issue.

As the OP, we faced this solution doesn’t work with the Python Azure Key Vault client library (but surprisingly seems to work with the Python Azure Blob Storage client library). We are running the code on an AML compute cluster. Could we get some clarity around whether this capacity is planned to be fixed and usable in production?

Here is our stacktrace:

_AzureMLOnBehalfOfCredential.get_token failed: Session.request() got an unexpected keyword argument 'claims'
Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "/mnt/azureml/cr/j/d41b0d89c33c4e73834f6a2f85779cd4/exe/wd/main.py", line 28, in <module>
    main()
  File "/mnt/azureml/cr/j/d41b0d89c33c4e73834f6a2f85779cd4/exe/wd/main.py", line 23, in main
    print(list(secret_client.list_properties_of_secrets()))
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/azure/core/paging.py", line 123, in __next__
    return next(self._page_iterator)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/azure/core/paging.py", line 75, in __next__
    self._response = self._get_next(self.continuation_token)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/azure/keyvault/secrets/_generated/_operations/_operations.py", line 850, in get_next
    pipeline_response: PipelineResponse = self._client._pipeline.run(  # pylint: disable=protected-access
                                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/azure/core/pipeline/_base.py", line 240, in run
    return first_node.send(pipeline_request)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/azure/core/pipeline/_base.py", line 96, in send
    response = self.next.send(request)
               ^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/azure/core/pipeline/_base.py", line 96, in send
    response = self.next.send(request)
               ^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/azure/core/pipeline/_base.py", line 96, in send
    response = self.next.send(request)
               ^^^^^^^^^^^^^^^^^^^^^^^
  [Previous line repeated 2 more times]
  File "/usr/local/lib/python3.12/site-packages/azure/core/pipeline/policies/_redirect.py", line 204, in send
    response = self.next.send(request)
               ^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/azure/core/pipeline/policies/_retry.py", line 551, in send
    response = self.next.send(request)
               ^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/azure/keyvault/secrets/_shared/challenge_auth_policy.py", line 120, in send
    return self.handle_challenge_flow(request, response)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/azure/keyvault/secrets/_shared/challenge_auth_policy.py", line 149, in handle_challenge_flow
    request_authorized = self.on_challenge(request, response)
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/azure/keyvault/secrets/_shared/challenge_auth_policy.py", line 239, in on_challenge
    self.authorize_request(request, scope, claims=challenge.claims, tenant_id=challenge.tenant_id)
  File "/usr/local/lib/python3.12/site-packages/azure/core/pipeline/policies/_authentication.py", line 145, in authorize_request
    self._request_token(*scopes, **kwargs)
  File "/usr/local/lib/python3.12/site-packages/azure/core/pipeline/policies/_authentication.py", line 108, in _request_token
    self._token = self._get_token(*scopes, **kwargs)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/azure/core/pipeline/policies/_authentication.py", line 99, in _get_token
    return cast(TokenCredential, self._credential).get_token(*scopes, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/azure/ai/ml/identity/_credentials/aml_on_behalf_of.py", line 48, in get_token
    return self._credential.get_token(*scopes, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/azure/ai/ml/identity/_internal/managed_identity_base.py", line 45, in get_token
    return super(ManagedIdentityBase, self).get_token(*scopes, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/azure/ai/ml/identity/_internal/get_token_mixin.py", line 76, in get_token
    token = self._request_token(*scopes, **kwargs)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/azure/ai/ml/identity/_internal/managed_identity_base.py", line 53, in _request_token
    return cast(ManagedIdentityClient, self._client).request_token(*scopes, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/azure/ai/ml/identity/_internal/managed_identity_client.py", line 122, in request_token
    response = self._pipeline.run(request, retry_on_methods=[request.method], **kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/azure/core/pipeline/_base.py", line 240, in run
    return first_node.send(pipeline_request)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/azure/core/pipeline/_base.py", line 96, in send
    response = self.next.send(request)
               ^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/azure/core/pipeline/_base.py", line 96, in send
    response = self.next.send(request)
               ^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/azure/core/pipeline/_base.py", line 96, in send
    response = self.next.send(request)
               ^^^^^^^^^^^^^^^^^^^^^^^
  [Previous line repeated 1 more time]
  File "/usr/local/lib/python3.12/site-packages/azure/core/pipeline/policies/_retry.py", line 551, in send
    response = self.next.send(request)
               ^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/azure/core/pipeline/_base.py", line 96, in send
    response = self.next.send(request)
               ^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/azure/core/pipeline/_base.py", line 96, in send
    response = self.next.send(request)
               ^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/azure/core/pipeline/_base.py", line 96, in send
    response = self.next.send(request)
               ^^^^^^^^^^^^^^^^^^^^^^^
  [Previous line repeated 1 more time]
  File "/usr/local/lib/python3.12/site-packages/azure/core/pipeline/_base.py", line 128, in send
    self._sender.send(request.http_request, **request.context.options),
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/azure/core/pipeline/transport/_requests_basic.py", line 363, in send
    response = self.session.request(  # type: ignore
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: Session.request() got an unexpected keyword argument 'claims'

The scenario is almost as simple as the reproducer snippet in the issue. We use the jobs to perform some ad-hoc “data engineering”/transforms/reduction tasks in preparation for other tasks. It’s often submitted from a laptop, although it can also be from an app that uses a managed identity. But anyway, one of the things these jobs do, is access some external APIs. For this, secrets are needed, and they are in an Azure Vault.

We use environment variables currently, but these are not actually considered secret, and hence viewable on e.g. ml studio, so we want to stop doing that.

I should say that we might be in a situation where Azure ML serverless is “overkill” for some of the jobs. So we are considering other products, such as Azure Batch or other orchestration tools. But for now, things are on Azure ML.