Skip to content

Commit 77e688b

Browse files
committed
refactor
1 parent 65938ff commit 77e688b

File tree

1 file changed

+31
-20
lines changed

1 file changed

+31
-20
lines changed

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

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,18 @@ Before following the steps in this article, make sure you have the following pre
5454
5555
## Getting secrets
5656

57-
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.
57+
There are two ways to get secrets during training:
5858

59-
The environment is used to build the Docker image that the training job runs in on the compute cluster.
59+
- Using a managed identity associated with the compute resource the training job runs on.
60+
- Using your identity by having the compute run the job on your behalf.
6061

61-
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.
62+
# [Managed identity](#tab/managed)
6263

63-
If you want to use your credentials instead of a managed identity to get the secrets, add the [Azure Machine Learning SDK](/python/api/overview/azure/ai-ml-readme) to your training code:
64+
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.
6465

65-
# [Managed identity](#tab/managed)
66+
The environment is used to build the Docker image that the training job runs in on the compute cluster.
6667

67-
To use the managed identity of the compute to access the key vault, use `DefaultAzureCredential` to get the compute's identity.
68+
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:
6869

6970
```python
7071
from azure.identity import DefaultAzureCredential
@@ -75,9 +76,20 @@ Before following the steps in this article, make sure you have the following pre
7576
secret_client = SecretClient(vault_url="https://my-key-vault.vault.azure.net/", credential=credential)
7677
```
7778

78-
# [Your identity](#tab/user)
79+
1. After authenticating, use the Key Vault client library to retrieve a secret by providing the associated key:
7980

80-
To use your identity (the identity of the person that submits the job), use `AzureMLOnBehalfOfCredential` in the training script to get the identity.
81+
```python
82+
secret = secret_client.get_secret("secret-name")
83+
print(secret.value)
84+
```
85+
86+
# [Your identity](#tab/user)
87+
88+
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.
89+
90+
The environment is used to build the Docker image that the training job runs in on the compute cluster.
91+
92+
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:
8193

8294
```python
8395
from azure.ai.ml.identity import AzureMLOnBehalfOfCredential
@@ -87,13 +99,20 @@ Before following the steps in this article, make sure you have the following pre
8799
secret_client = SecretClient(vault_url="https://my-key-vault.vault.azure.net/", credential=credential)
88100
```
89101

90-
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:
91-
102+
After authenticating, use the Key Vault client library to retrieve a secret by providing the associated key:
103+
104+
```python
105+
secret = secret_client.get_secret("secret-name")
106+
print(secret.value)
107+
```
108+
109+
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:
110+
92111
```python
93112
from azure.ai.ml import Input, command
94113
from azure.ai.ml.constants import AssetTypes
95114
from azure.ai.ml.entities import UserIdentityConfiguration
96-
115+
97116
job = command(
98117
code="./sdk/ml/azure-ai-ml/samples/src",
99118
command="python read_data.py --input_data ${{inputs.input_data}}",
@@ -105,16 +124,8 @@ Before following the steps in this article, make sure you have the following pre
105124
```
106125

107126
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).
108-
109-
---
110-
111-
1. After authenticating, use the Key Vault client library to retrieve a secret by providing the associated key:
112-
113-
```python
114-
secret = secret_client.get_secret("secret-name")
115-
print(secret.value)
116-
```
117127

128+
---
118129

119130
## Next steps
120131

0 commit comments

Comments
 (0)