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
In this article, you learn how to use secrets in training runs securely. For example, to connect to an external database to query training data, you would need to pass your username and password to the remote run context. Coding such values into training scripts in cleartext is insecure as it would expose the secret.
19
+
In this article, you learn how to use secrets in training runs 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 run context. Coding such values into training scripts in cleartext is insecure as it would expose the secret.
21
20
22
-
Instead, your Azure Machine Learning Workspace has [Azure Key Vault](https://docs.microsoft.com/azure/key-vault/key-vault-overview) as associated resource. This Key Vault can be used for passing secrets to remote runs securely through a set of APIs in Azure Machine Learning Python SDK.
21
+
Instead, your Azure Machine Learning workspace has an associated resource called a [Azure Key Vault](https://docs.microsoft.com/azure/key-vault/key-vault-overview). Use this Key Vault to pass secrets to remote runs securely through a set of APIs in the Azure Machine Learning Python SDK.
23
22
24
23
The basic flow for using secrets is:
25
-
1. On local computer, log in to Azure and connect to your Workspace.
24
+
1. On local computer, log in to Azure and connect to your workspace.
26
25
2. On local computer, set a secret in Workspace Key Vault.
27
26
3. Submit a remote run.
28
-
4. Within the remote run, get the secret from Key Value and use it.
27
+
4. Within the remote run, get the secret from Key Vault and use it.
29
28
30
29
## Set secrets
31
30
32
-
In the Azure Machine Learning Python SDK, the [Keyvault](https://docs.microsoft.com/python/api/azureml-core/azureml.core.keyvault.keyvault?view=azure-ml-py) class contains methods for setting secrets. In your local Python session, first obtain a reference to Workspace Key Vault, and then use [set_secret](https://docs.microsoft.com/python/api/azureml-core/azureml.core.keyvault.keyvault?view=azure-ml-py#set-secret-name--value-) method to set a secret by name and value.
31
+
In the Azure Machine Learning, the [Keyvault](https://docs.microsoft.com/python/api/azureml-core/azureml.core.keyvault.keyvault?view=azure-ml-py) 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()`](https://docs.microsoft.com/python/api/azureml-core/azureml.core.keyvault.keyvault?view=azure-ml-py#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.
33
32
34
33
```python
35
34
from azureml.core import Workspace
35
+
from azureml.core import Keyvault
36
36
import os
37
37
38
+
38
39
ws = Workspace.from_config()
39
40
my_secret = os.environ.get("MY_SECRET")
40
41
keyvault = ws.get_default_keyvault()
@@ -43,25 +44,25 @@ keyvault.set_secret(name="mysecret", value = my_secret)
43
44
44
45
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.
45
46
46
-
You can list secret names using the [list_secrets](https://docs.microsoft.com/python/api/azureml-core/azureml.core.keyvault.keyvault?view=azure-ml-py#list-secrets--) method. The __set_secret__ method updates the secret value if the name already exists.
47
+
You can list secret names using the [`list_secrets()`](https://docs.microsoft.com/python/api/azureml-core/azureml.core.keyvault.keyvault?view=azure-ml-py#list-secrets--) method and there is also a batch version,[set_secrets()](https://docs.microsoft.com/python/api/azureml-core/azureml.core.keyvault.keyvault?view=azure-ml-py#set-secrets-secrets-batch-) that allows you to set multiple secrets at a time.
47
48
48
49
## Get secrets
49
50
50
-
In your local code, you can use the[Keyvault.get_secret](https://docs.microsoft.com/python/api/azureml-core/azureml.core.keyvault.keyvault?view=azure-ml-py#get-secret-name-) method to get the secret value by name.
51
+
In your local code, you can use the[`get_secret()`](https://docs.microsoft.com/python/api/azureml-core/azureml.core.keyvault.keyvault?view=azure-ml-py#get-secret-name-) method to get the secret value by name.
51
52
52
-
In runs submitted using [Experiment.submit](https://docs.microsoft.com/python/api/azureml-core/azureml.core.experiment.experiment?view=azure-ml-py#submit-config--tags-none----kwargs-), use the [Run.get_secret](https://docs.microsoft.com/python/api/azureml-core/azureml.core.run.run?view=azure-ml-py#get-secret-name-) method. Because a submitted run is aware of its Workspace, this method shortcuts the Workspace instantiation and returns the secret value directly.
53
+
For runs submitted the [`Experiment.submit`](https://docs.microsoft.com/python/api/azureml-core/azureml.core.experiment.experiment?view=azure-ml-py#submit-config--tags-none----kwargs-), use the [`get_secret()`](https://docs.microsoft.com/python/api/azureml-core/azureml.core.run.run?view=azure-ml-py#get-secret-name-) method with the [`Run`](https://docs.microsoft.com/python/api/azureml-core/azureml.core.run(class)?view=azure-ml-py) class. Because a submitted run is aware of its workspace, this method shortcuts the Workspace instantiation and returns the secret value directly.
53
54
54
55
```python
55
56
# Code in submitted run
56
-
from azureml.core import Run
57
+
from azureml.core importExperiment, Run
57
58
58
59
run = Run.get_context()
59
60
secret_value = run.get_secret(name="mysecret")
60
61
```
61
62
62
63
Be careful not to expose the secret value by writing or printing it out.
63
64
64
-
The set and get methods also have batch versions [set_secrets](https://docs.microsoft.com/python/api/azureml-core/azureml.core.keyvault.keyvault?view=azure-ml-py#set-secrets-secrets-batch-) and [get_secrets](https://docs.microsoft.com/python/api/azureml-core/azureml.core.run.run?view=azure-ml-py#get-secrets-secrets-) for accessing multiple secrets at once.
65
+
There is also a batch version, [get_secrets()](https://docs.microsoft.com/python/api/azureml-core/azureml.core.run.run?view=azure-ml-py#get-secrets-secrets-) for accessing multiple secrets at once.
0 commit comments