|
1 | 1 | ---
|
2 |
| -title: Authentication secrets in training |
| 2 | +title: Authentication secrets |
3 | 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. |
| 4 | +description: Learn how to pass secrets to training jobs in secure fashion using Azure Key Vault. |
5 | 5 | services: machine-learning
|
6 | 6 | author: rastala
|
7 | 7 | ms.author: roastala
|
8 | 8 | ms.reviewer: larryfr
|
9 | 9 | ms.service: machine-learning
|
10 | 10 | ms.subservice: enterprise-readiness
|
11 |
| -ms.date: 10/21/2021 |
| 11 | +ms.date: 09/16/2022 |
12 | 12 | ms.topic: how-to
|
13 |
| -ms.custom: sdkv1, event-tier1-build-2022 |
| 13 | +ms.custom: sdkv2 |
14 | 14 | ---
|
15 | 15 |
|
16 |
| -# Use authentication credential secrets in Azure Machine Learning training jobs |
| 16 | +# Use authentication credential secrets in Azure Machine Learning jobs |
17 | 17 |
|
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) |
19 | 22 |
|
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. |
21 | 24 |
|
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. |
23 | 26 |
|
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). |
29 | 29 |
|
30 |
| -## Set secrets |
| 30 | +## Prerequisites |
31 | 31 |
|
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: |
33 | 33 |
|
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. |
38 | 36 |
|
| 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. |
39 | 40 |
|
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. |
45 | 42 |
|
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. |
47 | 45 |
|
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. |
49 | 47 |
|
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. |
54 | 61 |
|
55 |
| -## Get secrets |
| 62 | + The environment is used to build the Docker image that the training job runs in on the compute cluster. |
56 | 63 |
|
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: |
58 | 65 |
|
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 |
60 | 69 |
|
61 |
| -```python |
62 |
| -# Code in submitted job |
63 |
| -from azureml.core import Experiment, Run |
| 70 | + credential = DefaultAzureCredential() |
64 | 71 |
|
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 | + ``` |
68 | 74 |
|
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: |
70 | 76 |
|
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 | + ``` |
72 | 81 |
|
73 | 82 | ## Next steps
|
74 | 83 |
|
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). |
0 commit comments