You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: articles/machine-learning/how-to-use-secrets-in-runs.md
+31-20Lines changed: 31 additions & 20 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -54,17 +54,18 @@ Before following the steps in this article, make sure you have the following pre
54
54
55
55
## Getting secrets
56
56
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:
58
58
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.
60
61
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)
62
63
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.
64
65
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.
66
67
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:
68
69
69
70
```python
70
71
from azure.identity import DefaultAzureCredential
@@ -75,9 +76,20 @@ Before following the steps in this article, make sure you have the following pre
1. After authenticating, use the Key Vault client library to retrieve a secret by providing the associated key:
79
80
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`classis used to authenticate on behalf of your user identity:
81
93
82
94
```python
83
95
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
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
+
92
111
```python
93
112
from azure.ai.ml import Input, command
94
113
from azure.ai.ml.constants import AssetTypes
95
114
from azure.ai.ml.entities import UserIdentityConfiguration
@@ -105,16 +124,8 @@ Before following the steps in this article, make sure you have the following pre
105
124
```
106
125
107
126
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:
0 commit comments