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 kvlocally is “Successfully accessed key vault”. - Second, run
hello.py submitto 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
- #39793 — committed to achauhan-scc/azure-sdk-for-python by achauhan-scc 2 years ago
- #39830, #39793 (#39838) * #39744 * fixing some syntax warnings #39830 * #39793 — committed to Azure/azure-sdk-for-python by achauhan-scc 2 years ago
- #39830, #39793 (#39838) * #39744 * fixing some syntax warnings #39830 * #39793 — committed to allenkim0129/azure-sdk-for-python by achauhan-scc 2 years ago
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
OnBehalfOfsolution 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:
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.