azure-sdk-for-python: Unable to retrieve ManagedIdentity credentials from within a serverless job in Azure ML Workspace

Package Name: azure.identity-1.14.1, azure-keyvault-secrets-4.7.0 Operating System: Linux Python Version: 3.8

Describe the bug I am trying to get the ManagedIdentity credentials in order to access some secrets within a KeyVault in a serverless jobs in the Azure ML workspace

Here is the full trace error:

2023/11/02 15:03:34 INFO ManagedIdentityCredential will use Azure ML managed identity
2023-11-02 15:03:34.367 | WARNING  | trainer.run_manager:_get_token:60 - <azure.identity._credentials.managed_identity.ManagedIdentityCredential object at 0x15032c7a4c10>
2023/11/02 15:03:34 WARNING AzureMLCredential.get_token failed: Unexpected content type "text/plain; charset=utf-8"
Content: InternalError - :{
Info: Request failure status code: 404

}
2023/11/02 15:03:34 WARNING ManagedIdentityCredential.get_token failed: Unexpected content type "text/plain; charset=utf-8"
Content: InternalError - :{
Info: Request failure status code: 404

}
Traceback (most recent call last):
  File "/opt/conda/envs/ptca/lib/python3.8/site-packages/azure/core/pipeline/policies/_universal.py", line 614, in deserialize_from_text
    return json.loads(data_as_str)
  File "/opt/conda/envs/ptca/lib/python3.8/json/__init__.py", line 357, in loads
    return _default_decoder.decode(s)
  File "/opt/conda/envs/ptca/lib/python3.8/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/opt/conda/envs/ptca/lib/python3.8/json/decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/opt/conda/envs/ptca/lib/python3.8/site-packages/azure/identity/_internal/managed_identity_client.py", line 41, in _process_response
    content = ContentDecodePolicy.deserialize_from_text(
  File "/opt/conda/envs/ptca/lib/python3.8/site-packages/azure/core/pipeline/policies/_universal.py", line 616, in deserialize_from_text
    raise DecodeError(
azure.core.exceptions.DecodeError: JSON is invalid: Expecting value: line 1 column 1 (char 0)

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/opt/conda/envs/ptca/lib/python3.8/runpy.py", line 194, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/opt/conda/envs/ptca/lib/python3.8/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/mnt/azureml/cr/j/60811a0967a64dae83ca1768599fd992/exe/wd/trainer/run_executor.py", line 25, in <module>
    run_manager: RunManager = RunOptionManager.from_dictionary(vars(run_args))
  File "/mnt/azureml/cr/j/60811a0967a64dae83ca1768599fd992/exe/wd/trainer/option_manager.py", line 60, in from_dictionary
    return cls.managed_class(**args)
  File "/mnt/azureml/cr/j/60811a0967a64dae83ca1768599fd992/exe/wd/trainer/run_manager.py", line 31, in __init__
    access_token = self._get_token()
  File "/mnt/azureml/cr/j/60811a0967a64dae83ca1768599fd992/exe/wd/trainer/run_manager.py", line 63, in _get_token
    app_id = client.get_secret("sp-client-id-dev")
  File "/opt/conda/envs/ptca/lib/python3.8/site-packages/azure/core/tracing/decorator.py", line 78, in wrapper_use_tracer
    return func(*args, **kwargs)
  File "/opt/conda/envs/ptca/lib/python3.8/site-packages/azure/keyvault/secrets/_client.py", line 72, in get_secret
    bundle = self._client.get_secret(
  File "/opt/conda/envs/ptca/lib/python3.8/site-packages/azure/keyvault/secrets/_generated/_operations_mixin.py", line 1640, in get_secret
    return mixin_instance.get_secret(vault_base_url, secret_name, secret_version, **kwargs)
  File "/opt/conda/envs/ptca/lib/python3.8/site-packages/azure/core/tracing/decorator.py", line 78, in wrapper_use_tracer
    return func(*args, **kwargs)
  File "/opt/conda/envs/ptca/lib/python3.8/site-packages/azure/keyvault/secrets/_generated/v7_4/operations/_key_vault_client_operations.py", line 760, in get_secret
    pipeline_response: PipelineResponse = self._client._pipeline.run(  # pylint: disable=protected-access
  File "/opt/conda/envs/ptca/lib/python3.8/site-packages/azure/core/pipeline/_base.py", line 230, in run
    return first_node.send(pipeline_request)
  File "/opt/conda/envs/ptca/lib/python3.8/site-packages/azure/core/pipeline/_base.py", line 86, in send
    response = self.next.send(request)
  File "/opt/conda/envs/ptca/lib/python3.8/site-packages/azure/core/pipeline/_base.py", line 86, in send
    response = self.next.send(request)
  File "/opt/conda/envs/ptca/lib/python3.8/site-packages/azure/core/pipeline/_base.py", line 86, in send
    response = self.next.send(request)
  [Previous line repeated 2 more times]
  File "/opt/conda/envs/ptca/lib/python3.8/site-packages/azure/core/pipeline/policies/_redirect.py", line 197, in send
    response = self.next.send(request)
  File "/opt/conda/envs/ptca/lib/python3.8/site-packages/azure/core/pipeline/policies/_retry.py", line 531, in send
    response = self.next.send(request)
  File "/opt/conda/envs/ptca/lib/python3.8/site-packages/azure/core/pipeline/policies/_authentication.py", line 135, in send
    request_authorized = self.on_challenge(request, response)
  File "/opt/conda/envs/ptca/lib/python3.8/site-packages/azure/keyvault/secrets/_shared/challenge_auth_policy.py", line 112, in on_challenge
    self.authorize_request(request, scope, tenant_id=challenge.tenant_id)
  File "/opt/conda/envs/ptca/lib/python3.8/site-packages/azure/core/pipeline/policies/_authentication.py", line 113, in authorize_request
    self._token = self._credential.get_token(*scopes, **kwargs)
  File "/opt/conda/envs/ptca/lib/python3.8/site-packages/azure/identity/_internal/decorators.py", line 33, in wrapper
    token = fn(*args, **kwargs)
  File "/opt/conda/envs/ptca/lib/python3.8/site-packages/azure/identity/_credentials/managed_identity.py", line 137, in get_token
    return self._credential.get_token(*scopes, claims=claims, tenant_id=tenant_id, **kwargs)
  File "/opt/conda/envs/ptca/lib/python3.8/site-packages/azure/identity/_internal/managed_identity_base.py", line 47, in get_token
    return super(ManagedIdentityBase, self).get_token(*scopes, claims=claims, tenant_id=tenant_id, **kwargs)
  File "/opt/conda/envs/ptca/lib/python3.8/site-packages/azure/identity/_internal/get_token_mixin.py", line 86, in get_token
    token = self._request_token(*scopes, claims=claims, tenant_id=tenant_id, **kwargs)
  File "/opt/conda/envs/ptca/lib/python3.8/site-packages/azure/identity/_internal/managed_identity_base.py", line 55, in _request_token
    return cast(ManagedIdentityClient, self._client).request_token(*scopes, **kwargs)
  File "/opt/conda/envs/ptca/lib/python3.8/site-packages/azure/identity/_internal/managed_identity_client.py", line 113, in request_token
    token = self._process_response(response, request_time)
  File "/opt/conda/envs/ptca/lib/python3.8/site-packages/azure/identity/_internal/managed_identity_client.py", line 49, in _process_response
    raise ClientAuthenticationError(message=message, response=response.http_response) from ex
azure.core.exceptions.ClientAuthenticationError: Unexpected content type "text/plain; charset=utf-8"
Content: InternalError - :{
Info: Request failure status code: 404

}

To Reproduce I am creating the job using the Python sdk as described in the documentation here
The workspace don’t have a user assigned identity therefore I have used UserIdentityConfiguration(), as ManagedIdentityConfiguration() would prevent me from creating the job. The job then fails at the following:

key_vault = "key_vault_id"
kv_uri = f"https://{key_vault}.vault.azure.net"

credential = ManagedIdentityCredential()
client = SecretClient(vault_url=kv_uri, credential=credential)

secret = client.get_secret("secret_id")

The same code to retrieve a secret works correctly on a VM inside the Azure ML Workspace

Expected behavior I would expect to be able to use the system managed identity of the ML workspace to retrieve secrets in a serverless job

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 17 (4 by maintainers)

Most upvoted comments

@mauibar - ManagedIdentityCredential needs client_id passed to it, which you need to verify is available on your cluster

Try this:

key_vault = "key_vault_id"
kv_uri = f"https://{key_vault}.vault.azure.net"

credential = ManagedIdentityCredential(client_id=os.environ.get('DEFAULT_IDENTITY_CLIENT_ID'))
client = SecretClient(vault_url=kv_uri, credential=credential)

secret = client.get_secret("secret_id")

This resolved the issue for me (thank you @xiangyan99 for helping me identify it)