Skip to content

Commit b30e9fe

Browse files
committed
adding info about 'on-behalf-of' feature
1 parent 8069148 commit b30e9fe

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

articles/machine-learning/how-to-migrate-from-v1.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ Environments created from v1 can be used in v2. In v2, environments have new fea
172172

173173
## Managing secrets
174174

175-
The management of Key Vault secrets differs significantly in V2 compared to V1. The V1 set_secret and get_secret SDK methods are not available in V2. Instead, direct access using Key Vault client libraries should be used.
175+
The management of Key Vault secrets differs significantly in V2 compared to V1. The V1 set_secret and get_secret SDK methods are not available in V2. Instead, direct access using Key Vault client libraries should be used. When accessing secrets from a training script, you can use either the managed identity of the compute or your identity.
176176

177177
For details about Key Vault, see [Use authentication credential secrets in Azure Machine Learning training jobs](how-to-use-secrets-in-runs.md?view=azureml-api-2&preserve-view=true).
178178

articles/machine-learning/how-to-use-secrets-in-runs.md

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ ms.author: larryfr
88
ms.reviewer: roastala
99
ms.service: azure-machine-learning
1010
ms.subservice: enterprise-readiness
11-
ms.date: 01/19/2024
11+
ms.date: 08/20/2024
1212
ms.topic: how-to
1313
ms.custom: sdkv2
1414
---
@@ -42,7 +42,7 @@ Before following the steps in this article, make sure you have the following pre
4242
4343
* (Optional) An Azure Machine Learning compute cluster configured to use a [managed identity](how-to-create-attach-compute-cluster.md?tabs=azure-studio#set-up-managed-identity). The cluster can be configured for either a system-assigned or user-assigned managed identity.
4444

45-
* If your job will run on a compute cluster, grant the managed identity for the compute cluster access to the secrets stored in key vault. Or, if the job will run on serverless compute, grant the managed identity specified for the job access to the secrets. The method used to grant access depends on how your key vault is configured:
45+
* If your job runs on a compute cluster, grant the managed identity for the compute cluster access to the secrets stored in key vault. Or, if the job will run on serverless compute, grant the managed identity specified for the job access to the secrets. The method used to grant access depends on how your key vault is configured:
4646

4747
* [Azure role-based access control (Azure RBAC)](/azure/key-vault/general/rbac-guide): When configured for Azure RBAC, add the managed identity to the __Key Vault Secrets User__ role on your key vault.
4848
* [Azure Key Vault access policy](/azure/key-vault/general/assign-access-policy): When configured to use access policies, add a new policy that grants the __get__ operation for secrets and assign it to the managed identity.
@@ -60,6 +60,10 @@ Before following the steps in this article, make sure you have the following pre
6060

6161
1. From your training code, use the [Azure Identity SDK](/python/api/overview/azure/identity-readme) and [Key Vault client library](/python/api/overview/azure/keyvault-secrets-readme) to get the managed identity credentials and authenticate to key vault:
6262

63+
# [Managed identity](#tab/managed)
64+
65+
To use the managed identity of the compute to access the key vault, use `DefaultAzureCredential` to get the compute's identity.
66+
6367
```python
6468
from azure.identity import DefaultAzureCredential
6569
from azure.keyvault.secrets import SecretClient
@@ -69,13 +73,47 @@ Before following the steps in this article, make sure you have the following pre
6973
secret_client = SecretClient(vault_url="https://my-key-vault.vault.azure.net/", credential=credential)
7074
```
7175

76+
# [Your identity](#tab/user)
77+
78+
To use your identity (the identity of the person that submits the job), use `AzureMLOnBehalfOfCredential` in the training script to get the identity.
79+
80+
```python
81+
from azure.ai.ml.identity import AzureMLOnBehalfOfCredential
82+
from azure.keyvault.secrets import SecretClient
83+
84+
credential = AzureMLOnBehalfOfCredential()
85+
secret_client = SecretClient(vault_url="https://my-key-vault.vault.azure.net/", credential=credential)
86+
```
87+
88+
When you submit the training job, you must specify that it runs in the context of your identity by using `identity=UserIdentityConfiguration()`. The following example submits a job using this parameter:
89+
90+
```python
91+
from azure.ai.ml import Input, command
92+
from azure.ai.ml.constants import AssetTypes
93+
from azure.ai.ml.entities import UserIdentityConfiguration
94+
95+
job = command(
96+
code="./sdk/ml/azure-ai-ml/samples/src",
97+
command="python read_data.py --input_data ${{inputs.input_data}}",
98+
inputs={"input_data": Input(type=AssetTypes.MLTABLE, path="./sample_data")},
99+
environment="AzureML-sklearn-1.0-ubuntu20.04-py38-cpu:1",
100+
compute="cpu-cluster",
101+
identity=UserIdentityConfiguration(),
102+
)
103+
```
104+
105+
For an example of using the Azure CLI to submit a job that uses your identity, visit [Https://github.com/Azure/azureml-examples/blob/d4c90eead3c1fd97393d0657f7a78831490adf1c/cli/jobs/single-step/on-behalf-of/README.md](https://github.com/Azure/azureml-examples/blob/d4c90eead3c1fd97393d0657f7a78831490adf1c/cli/jobs/single-step/on-behalf-of/README.md).
106+
107+
---
108+
72109
1. After authenticating, use the Key Vault client library to retrieve a secret by providing the associated key:
73110

74111
```python
75112
secret = secret_client.get_secret("secret-name")
76113
print(secret.value)
77114
```
78115

116+
79117
## Next steps
80118

81119
For an example of submitting a training job using the Azure Machine Learning Python SDK v2, see [Train models with the Python SDK v2](how-to-train-sdk.md).

0 commit comments

Comments
 (0)