Skip to content

Commit 0300de6

Browse files
authored
Merge pull request #285068 from Blackmist/280344-secrets
adding info about 'on-behalf-of' feature
2 parents bab12fe + c3ea9d0 commit 0300de6

File tree

2 files changed

+59
-7
lines changed

2 files changed

+59
-7
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: 58 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
---
22
title: Authentication secrets
33
titleSuffix: Azure Machine Learning
4-
description: Learn how to pass secrets to training jobs in secure fashion using Azure Key Vault.
4+
description: Learn how to securely get secrets from Azure Key Vault in your training jobs by using the Key Vault Secrets client library.
55
services: machine-learning
66
author: Blackmist
77
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
13-
ms.custom: sdkv2
13+
ms.custom: sdkv2, FY25Q1-Linter
14+
# Customer intent: As a data scientist, I want to securely access secrets from Azure Key Vault in my training jobs so that I can use them in my training scripts.
1415
---
1516

1617
# Use authentication credential secrets in Azure Machine Learning jobs
@@ -42,7 +43,7 @@ Before following the steps in this article, make sure you have the following pre
4243
4344
* (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.
4445

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:
46+
* 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 runs 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:
4647

4748
* [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.
4849
* [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.
@@ -52,7 +53,14 @@ Before following the steps in this article, make sure you have the following pre
5253
> [!TIP]
5354
> The quickstart link is to the steps for using the Azure Key Vault Python SDK. In the table of contents in the left navigation area are links to other ways to set a key.
5455
55-
## Getting secrets
56+
## Get secrets
57+
58+
There are two ways to get secrets during training:
59+
60+
- Using a managed identity associated with the compute resource the training job runs on.
61+
- Using your identity by having the compute run the job on your behalf.
62+
63+
# [Managed identity](#tab/managed)
5664

5765
1. Add the `azure-keyvault-secrets` and `azure-identity` packages to the [Azure Machine Learning environment](concept-environments.md) used when training the model. For example, by adding them to the conda file used to build the environment.
5866

@@ -76,6 +84,50 @@ Before following the steps in this article, make sure you have the following pre
7684
print(secret.value)
7785
```
7886

79-
## Next steps
87+
# [Your identity](#tab/user)
88+
89+
1. Add the `azure-keyvault-secrets`, `azure-identity`, and `azure-ai-ml` packages to the [Azure Machine Learning environment](concept-environments.md) used when training the model. For example, by adding them to the conda file used to build the environment.
90+
91+
The environment is used to build the Docker image that the training job runs in on the compute cluster.
92+
93+
1. From your training code, use the [Azure Machine Learning SDK](/python/api/overview/azure/ai-ml-readme) and [Key Vault client library](/python/api/overview/azure/keyvault-secrets-readme) to get the managed identity credentials and authenticate to key vault. The `AzureMLOnBehalfOfCredential` class is used to authenticate on behalf of your user identity:
94+
95+
```python
96+
from azure.ai.ml.identity import AzureMLOnBehalfOfCredential
97+
from azure.keyvault.secrets import SecretClient
98+
99+
credential = AzureMLOnBehalfOfCredential()
100+
secret_client = SecretClient(vault_url="https://my-key-vault.vault.azure.net/", credential=credential)
101+
```
102+
103+
After authenticating, use the Key Vault client library to retrieve a secret by providing the associated key:
104+
105+
```python
106+
secret = secret_client.get_secret("secret-name")
107+
print(secret.value)
108+
```
109+
110+
1. When you submit the training job, you must specify that it runs on behalf of your identity by using `identity=UserIdentityConfiguration()`. The following example submits a job using this parameter:
111+
112+
```python
113+
from azure.ai.ml import Input, command
114+
from azure.ai.ml.constants import AssetTypes
115+
from azure.ai.ml.entities import UserIdentityConfiguration
116+
117+
job = command(
118+
code="./sdk/ml/azure-ai-ml/samples/src",
119+
command="python read_data.py --input_data ${{inputs.input_data}}",
120+
inputs={"input_data": Input(type=AssetTypes.MLTABLE, path="./sample_data")},
121+
environment="AzureML-sklearn-1.0-ubuntu20.04-py38-cpu:1",
122+
compute="cpu-cluster",
123+
identity=UserIdentityConfiguration(),
124+
)
125+
```
126+
127+
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).
128+
129+
---
130+
131+
## Related content
80132

81133
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)