Skip to content

Commit 9dab0f8

Browse files
Merge pull request #211569 from Blackmist/v2-secrets
v2 version
2 parents f07f566 + 4fcb4bf commit 9dab0f8

File tree

4 files changed

+134
-44
lines changed

4 files changed

+134
-44
lines changed
Lines changed: 51 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,84 @@
11
---
2-
title: Authentication secrets in training
2+
title: Authentication secrets
33
titleSuffix: Azure Machine Learning
4-
description: Learn how to pass secrets to training jobs in secure fashion using the Azure Key Vault for your workspace.
4+
description: Learn how to pass secrets to training jobs in secure fashion using Azure Key Vault.
55
services: machine-learning
66
author: rastala
77
ms.author: roastala
88
ms.reviewer: larryfr
99
ms.service: machine-learning
1010
ms.subservice: enterprise-readiness
11-
ms.date: 10/21/2021
11+
ms.date: 09/16/2022
1212
ms.topic: how-to
13-
ms.custom: sdkv1, event-tier1-build-2022
13+
ms.custom: sdkv2
1414
---
1515

16-
# Use authentication credential secrets in Azure Machine Learning training jobs
16+
# Use authentication credential secrets in Azure Machine Learning jobs
1717

18-
[!INCLUDE [sdk v1](../../includes/machine-learning-sdk-v1.md)]
18+
[!INCLUDE [sdk v2](../../includes/machine-learning-sdk-v2.md)]
19+
> [!div class="op_single_selector" title1="Select the version of the Azure Machine Learning Python SDK you are using:"]
20+
> * [v1](v1/how-to-use-secrets-in-runs.md)
21+
> * [v2 (current version)](how-to-use-secrets-in-runs.md)
1922
20-
In this article, you learn how to use secrets in training jobs securely. Authentication information such as your user name and password are secrets. For example, if you connect to an external database in order to query training data, you would need to pass your username and password to the remote job context. Coding such values into training scripts in cleartext is insecure as it would expose the secret.
23+
Authentication information such as your user name and password are secrets. For example, if you connect to an external database in order to query training data, you would need to pass your username and password to the remote job context. Coding such values into training scripts in clear text is insecure as it would potentially expose the secret.
2124

22-
Instead, your Azure Machine Learning workspace has an associated resource called a [Azure Key Vault](../key-vault/general/overview.md). Use this Key Vault to pass secrets to remote jobs securely through a set of APIs in the Azure Machine Learning Python SDK.
25+
The Azure Key Vault allows you to securely store and retrieve secrets. In this article, learn how you can retrieve secrets stored in a key vault from a training job running on a compute cluster.
2326

24-
The standard flow for using secrets is:
25-
1. On local computer, log in to Azure and connect to your workspace.
26-
2. On local computer, set a secret in Workspace Key Vault.
27-
3. Submit a remote job.
28-
4. Within the remote job, get the secret from Key Vault and use it.
27+
> [!IMPORTANT]
28+
> The Azure Machine Learning Python SDK v2 and Azure CLI extension v2 for machine learning do not provide the capability to set or get secrets. Instead, the information in this article uses the [Azure Key Vault Secrets client library for Python](/python/api/overview/azure/keyvault-secrets-readme).
2929
30-
## Set secrets
30+
## Prerequisites
3131

32-
In the Azure Machine Learning, the [Keyvault](/python/api/azureml-core/azureml.core.keyvault.keyvault) class contains methods for setting secrets. In your local Python session, first obtain a reference to your workspace Key Vault, and then use the [`set_secret()`](/python/api/azureml-core/azureml.core.keyvault.keyvault#set-secret-name--value-) method to set a secret by name and value. The __set_secret__ method updates the secret value if the name already exists.
32+
Before following the steps in this article, make sure you have the following prerequisites:
3333

34-
```python
35-
from azureml.core import Workspace
36-
from azureml.core import Keyvault
37-
import os
34+
> [!TIP]
35+
> Many of the prerequisites in this section require __Contributor__, __Owner__, or equivalent access to your Azure subscription, or the Azure Resource Group that contains the resources. You may need to contact your Azure administrator and have them perform these actions.
3836
37+
* An Azure subscription. If you don't have an Azure subscription, create a free account before you begin. Try the [free or paid version of Azure Machine Learning](https://azure.microsoft.com/free/).
38+
39+
* An Azure Machine Learning workspace. If you don't have one, use the steps in the [Quickstart: Create workspace resources](quickstart-create-resources.md) article to create one.
3940

40-
ws = Workspace.from_config()
41-
my_secret = os.environ.get("MY_SECRET")
42-
keyvault = ws.get_default_keyvault()
43-
keyvault.set_secret(name="mysecret", value = my_secret)
44-
```
41+
* An Azure Key Vault. If you used the [Quickstart: Create workspace resources](quickstart-create-resources.md) article to create your workspace, a key vault was created for you. You can also create a separate key vault instance using the information in the [Quickstart: Create a key vault](/azure/key-vault/general/quick-create-portal) article.
4542

46-
Do not put the secret value in your Python code as it is insecure to store it in file as cleartext. Instead, obtain the secret value from an environment variable, for example Azure DevOps build secret, or from interactive user input.
43+
> [!TIP]
44+
> You do not have to use same key vault as the workspace.
4745
48-
You can list secret names using the [`list_secrets()`](/python/api/azureml-core/azureml.core.keyvault.keyvault#list-secrets--) method and there is also a batch version,[set_secrets()](/python/api/azureml-core/azureml.core.keyvault.keyvault#set-secrets-secrets-batch-) that allows you to set multiple secrets at a time.
46+
* 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.
4947

50-
> [!IMPORTANT]
51-
> Using `list_secrets()` will only list secrets created through `set_secret()` or `set_secrets()` using the Azure ML SDK. It will not list secrets created by something other than the SDK. For example, a secret created using the Azure portal or Azure PowerShell will not be listed.
52-
>
53-
> You can use [`get_secret()`](#get-secrets) to get a secret value from the key vault, regardless of how it was created. So you can retrieve secrets that are not listed by `list_secrets()`.
48+
* Grant the managed identity for the compute cluster access to the secrets stored in key vault. The method used to grant access depends on how your key vault is configured:
49+
50+
* [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.
51+
* [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+
53+
* A stored secret value in the key vault. This value can then be retrieved using a key. For more information, see [Quickstart: Set and retrieve a secret from Azure Key Vault](/azure/key-vault/secrets/quick-create-python).
54+
55+
> [!TIP]
56+
> 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.
57+
58+
## Getting secrets
59+
60+
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.
5461

55-
## Get secrets
62+
The environment is used to build the Docker image that the training job runs in on the compute cluster.
5663

57-
In your local code, you can use the [`get_secret()`](/python/api/azureml-core/azureml.core.keyvault.keyvault#get-secret-name-) method to get the secret value by name.
64+
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:
5865

59-
For jobs submitted the [`Experiment.submit`](/python/api/azureml-core/azureml.core.experiment.experiment#submit-config--tags-none----kwargs-) , use the [`get_secret()`](/python/api/azureml-core/azureml.core.run.run#get-secret-name-) method with the [`Run`](/python/api/azureml-core/azureml.core.run%28class%29) class. Because a submitted run is aware of its workspace, this method shortcuts the Workspace instantiation and returns the secret value directly.
66+
```python
67+
from azure.identity import DefaultAzureCredential
68+
from azure.keyvault.secret import SecretClient
6069

61-
```python
62-
# Code in submitted job
63-
from azureml.core import Experiment, Run
70+
credential = DefaultAzureCredential()
6471

65-
run = Run.get_context()
66-
secret_value = run.get_secret(name="mysecret")
67-
```
72+
secret_client = SecretClient(vault_url="https://my-key-vault.vault.azure.net/", credential=credential)
73+
```
6874

69-
Be careful not to expose the secret value by writing or printing it out.
75+
1. After authenticating, use the Key Vault client library to retrieve a secret by providing the associated key:
7076

71-
There is also a batch version, [get_secrets()](/python/api/azureml-core/azureml.core.run.run#get-secrets-secrets-) for accessing multiple secrets at once.
77+
```python
78+
secret = secret_client.get_secret("secret-name")
79+
print(secret.value)
80+
```
7281

7382
## Next steps
7483

75-
* [View example notebook](https://github.com/Azure/MachineLearningNotebooks/blob/master/how-to-use-azureml/manage-azureml-service/authentication-in-azureml/authentication-in-azureml.ipynb)
76-
* [Learn about enterprise security with Azure Machine Learning](concept-enterprise-security.md)
84+
For an example of submitting a training job using the Azure Machine Learning Python SDK v2 (preview), see [Train models with the Python SDK v2](how-to-train-sdk.md).

articles/machine-learning/toc.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@
405405
- name: Train with custom Docker image
406406
href: how-to-train-with-custom-image.md
407407
- name: Use Key Vault when training
408-
displayName: secrets keyvault
408+
displayName: secrets keyvault job
409409
href: how-to-use-secrets-in-runs.md
410410
- name: Train with the CLI v2
411411
href: how-to-train-cli.md
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
---
2+
title: Authentication secrets in training
3+
titleSuffix: Azure Machine Learning
4+
description: Learn how to pass secrets to training jobs in secure fashion using the Azure Key Vault for your workspace.
5+
services: machine-learning
6+
author: rastala
7+
ms.author: roastala
8+
ms.reviewer: larryfr
9+
ms.service: machine-learning
10+
ms.subservice: enterprise-readiness
11+
ms.date: 10/21/2021
12+
ms.topic: how-to
13+
ms.custom: sdkv1, event-tier1-build-2022
14+
---
15+
16+
# Use authentication credential secrets in Azure Machine Learning training jobs
17+
18+
[!INCLUDE [sdk v1](../../../includes/machine-learning-sdk-v1.md)]
19+
> [!div class="op_single_selector" title1="Select the version of the Azure Machine Learning Python SDK you are using:"]
20+
> * [v1](how-to-use-secrets-in-runs.md)
21+
> * [v2 (current version)](../how-to-use-secrets-in-runs.md)
22+
23+
In this article, you learn how to use secrets in training jobs securely. Authentication information such as your user name and password are secrets. For example, if you connect to an external database in order to query training data, you would need to pass your username and password to the remote job context. Coding such values into training scripts in cleartext is insecure as it would expose the secret.
24+
25+
Instead, your Azure Machine Learning workspace has an associated resource called a [Azure Key Vault](/azure/key-vault/general/overview). Use this Key Vault to pass secrets to remote jobs securely through a set of APIs in the Azure Machine Learning Python SDK.
26+
27+
The standard flow for using secrets is:
28+
1. On local computer, log in to Azure and connect to your workspace.
29+
2. On local computer, set a secret in Workspace Key Vault.
30+
3. Submit a remote job.
31+
4. Within the remote job, get the secret from Key Vault and use it.
32+
33+
## Set secrets
34+
35+
In the Azure Machine Learning, the [Keyvault](/python/api/azureml-core/azureml.core.keyvault.keyvault) class contains methods for setting secrets. In your local Python session, first obtain a reference to your workspace Key Vault, and then use the [`set_secret()`](/python/api/azureml-core/azureml.core.keyvault.keyvault#set-secret-name--value-) method to set a secret by name and value. The __set_secret__ method updates the secret value if the name already exists.
36+
37+
```python
38+
from azureml.core import Workspace
39+
from azureml.core import Keyvault
40+
import os
41+
42+
43+
ws = Workspace.from_config()
44+
my_secret = os.environ.get("MY_SECRET")
45+
keyvault = ws.get_default_keyvault()
46+
keyvault.set_secret(name="mysecret", value = my_secret)
47+
```
48+
49+
Do not put the secret value in your Python code as it is insecure to store it in file as cleartext. Instead, obtain the secret value from an environment variable, for example Azure DevOps build secret, or from interactive user input.
50+
51+
You can list secret names using the [`list_secrets()`](/python/api/azureml-core/azureml.core.keyvault.keyvault#list-secrets--) method and there is also a batch version,[set_secrets()](/python/api/azureml-core/azureml.core.keyvault.keyvault#set-secrets-secrets-batch-) that allows you to set multiple secrets at a time.
52+
53+
> [!IMPORTANT]
54+
> Using `list_secrets()` will only list secrets created through `set_secret()` or `set_secrets()` using the Azure ML SDK. It will not list secrets created by something other than the SDK. For example, a secret created using the Azure portal or Azure PowerShell will not be listed.
55+
>
56+
> You can use [`get_secret()`](#get-secrets) to get a secret value from the key vault, regardless of how it was created. So you can retrieve secrets that are not listed by `list_secrets()`.
57+
58+
## Get secrets
59+
60+
In your local code, you can use the [`get_secret()`](/python/api/azureml-core/azureml.core.keyvault.keyvault#get-secret-name-) method to get the secret value by name.
61+
62+
For jobs submitted the [`Experiment.submit`](/python/api/azureml-core/azureml.core.experiment.experiment#submit-config--tags-none----kwargs-) , use the [`get_secret()`](/python/api/azureml-core/azureml.core.run.run#get-secret-name-) method with the [`Run`](/python/api/azureml-core/azureml.core.run%28class%29) class. Because a submitted run is aware of its workspace, this method shortcuts the Workspace instantiation and returns the secret value directly.
63+
64+
```python
65+
# Code in submitted job
66+
from azureml.core import Experiment, Run
67+
68+
run = Run.get_context()
69+
secret_value = run.get_secret(name="mysecret")
70+
```
71+
72+
Be careful not to expose the secret value by writing or printing it out.
73+
74+
There is also a batch version, [get_secrets()](/python/api/azureml-core/azureml.core.run.run#get-secrets-secrets-) for accessing multiple secrets at once.
75+
76+
## Next steps
77+
78+
* [View example notebook](https://github.com/Azure/MachineLearningNotebooks/blob/master/how-to-use-azureml/manage-azureml-service/authentication-in-azureml/authentication-in-azureml.ipynb)
79+
* [Learn about enterprise security with Azure Machine Learning](../concept-enterprise-security.md)

articles/machine-learning/v1/toc.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,9 @@
190190
href: how-to-train-pytorch.md
191191
- name: Migrate from Estimators to ScriptRunConfig
192192
href: how-to-migrate-from-estimators-to-scriptrunconfig.md
193+
- name: Use Key Vault when training
194+
displayName: secrets keyvault
195+
href: how-to-use-secrets-in-runs.md
193196
- name: Automated machine learning
194197
displayName: automl, auto ml
195198
items:

0 commit comments

Comments
 (0)