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
title: Tutorial - Use Azure Key Vault with an Azure virtual machine in Python | Microsoft Docs
3
-
description: In this tutorial, you configure a Python application to read a secret from a key vault
2
+
title: Tutorial - Use a Linux virtual machine and a Python application to store secrets in Azure Key Vault | Microsoft Docs
3
+
description: In this tutorial, you learn how to configure a Python application to read a secret from Azure Key Vault.
4
4
services: key-vault
5
5
documentationcenter:
6
6
author: prashanthyv
@@ -13,93 +13,93 @@ ms.topic: tutorial
13
13
ms.date: 09/05/2018
14
14
ms.author: pryerram
15
15
ms.custom: mvc
16
-
#Customer intent: As a developer, I want to use Azure Key Vault to store secrets for my app, so that they are kept secure.
16
+
#Customer intent: As a developer, I want to use Azure Key Vault to store secrets for my app so that they are kept secure.
17
17
---
18
-
# Tutorial: Use Azure Key Vault with an Azure virtual machine in Python
18
+
19
+
# Tutorial: Use a Linux VM and a Python app to store secrets in Azure Key Vault
19
20
20
21
Azure Key Vault helps you protect secrets such as the API keys and database connection strings needed to access your applications, services, and IT resources.
21
22
22
-
In this tutorial, you follow the steps to get an Azure web application to read information from Azure Key Vault by using managed identities for Azure resources. You learn how to:
23
+
In this tutorial, you set up an Azure web application to read information from Azure Key Vault by using managed identities for Azure resources. You learn how to:
23
24
24
25
> [!div class="checklist"]
25
-
> * Create a key vault.
26
-
> * Store a secret in the key vault.
27
-
> * Create an Azure virtual machine.
28
-
> * Enable a [managed identity](../active-directory/managed-identities-azure-resources/overview.md) for the virtual machine.
29
-
> * Grant the required permissions for the console application to read data from the key vault.
30
-
> * Retrieve a secret from the key vault.
26
+
> * Create a key vault
27
+
> * Store a secret in your key vault
28
+
> * Create a Linux virtual machine
29
+
> * Enable a [managed identity](../active-directory/managed-identities-azure-resources/overview.md) for the virtual machine
30
+
> * Grant the required permissions for the console application to read data from the key vault
31
+
> * Retrieve a secret from your key vault
31
32
32
-
Before you go any further, please read the [basic concepts about Key Vault](key-vault-whatis.md#basic-concepts).
33
+
Before you go any further, make sure you understand the [basic concepts about Key Vault](key-vault-whatis.md#basic-concepts).
33
34
34
35
## Prerequisites
35
-
For all platforms, you need:
36
36
37
-
*Git ([download](https://git-scm.com/downloads)).
37
+
*[Git](https://git-scm.com/downloads).
38
38
* An Azure subscription. If you don't have an Azure subscription, create a [free account](https://azure.microsoft.com/free/?WT.mc_id=A261C142F) before you begin.
39
-
*[Azure CLI](https://docs.microsoft.com/cli/azure/install-azure-cli?view=azure-cli-latest) version 2.0.4 or later. It's available for Windows, Mac, and Linux.
39
+
*[Azure CLI version 2.0.4 or later](https://docs.microsoft.com/cli/azure/install-azure-cli?view=azure-cli-latest) or Azure Cloud Shell.
This tutorial makes use of Managed Service Identity (MSI).
43
+
## Understand Managed Service Identity
43
44
44
-
Azure Key Vault can store credentials securely so they aren’t in your code. To retrieve them, you need to authenticate to Key Vault. To authenticate to Key Vault, you need a credential. That's a classic bootstrap problem. Through Azure and Azure Active Directory (Azure AD), MSI provides a “bootstrap identity” that makes it simpler to get things started.
45
+
Azure Key Vault can store credentials securely so they aren’t in your code. To retrieve them, you need to authenticate to Azure Key Vault. However, to authenticate to Key Vault, you need a credential. It's a classic bootstrap problem. Through Azure and Azure Active Directory (Azure AD), Managed Service Identity (MSI) provides a bootstrap identity that makes it simpler to get things started.
45
46
46
-
When you enable MSI for an Azure service such as Virtual Machines, App Service, or Functions, Azure creates a [service principal](key-vault-whatis.md#basic-concepts) for the instance of the service in Azure AD. Azure injects the credentials for the service principal into the instance of the service.
47
+
When you enable MSI for an Azure service like Virtual Machines, App Service, or Functions, Azure creates a service principal for the instance of the service in Azure AD. It injects the credentials for the service principal into the instance of the service.
47
48
48
49

49
50
50
-
Next, Your code calls a local metadata service that's available on the Azure resource to get an access token.
51
-
Your code uses the access token that it gets from the local MSI endpoint to authenticate to an Azure Key Vault service.
51
+
Next, your code calls a local metadata service available on the Azure resource to get an access token. Your code uses the access token that it gets from the local MSI endpoint to authenticate to an Azure Key Vault service.
52
52
53
-
## Log in to Azure
53
+
## Sign in to Azure
54
54
55
-
To log in to Azure by using the Azure CLI, enter:
55
+
To sign in to Azure by using the Azure CLI, enter:
56
56
57
-
```azurecli
57
+
```azurecli-interactive
58
58
az login
59
59
```
60
60
61
61
## Create a resource group
62
62
63
-
Create a resource group by using the [az group create](/cli/azure/group#az-group-create) command. An Azure resource group is a logical container into which Azure resources are deployed and managed.
63
+
An Azure resource group is a logical container into which Azure resources are deployed and managed.
64
64
65
-
Select a resource group name and fill in the placeholder.
66
-
The following example creates a resource group in the West US location:
65
+
Create a resource group by using the `az group create` command in the West US location with the following code. Replace `YourResourceGroupName` with a name of your choice.
67
66
68
-
```azurecli
67
+
```azurecli-interactive
69
68
# To list locations: az account list-locations --output table
70
69
az group create --name "<YourResourceGroupName>" --location "West US"
71
70
```
72
71
73
-
The resource group that you just created is used throughout this article.
72
+
You use this resource group throughout the tutorial.
74
73
75
74
## Create a key vault
76
75
77
-
Next you create a key vault in the resource group that you created in the previous step. Provide the following information:
76
+
Next, you create a key vault in the resource group that you created in the previous step. Provide the following information:
78
77
79
78
* Key vault name: The name must be a string of 3-24 characters and must contain only 0-9, a-z, A-Z, and hyphens (-).
80
79
* Resource group name.
81
80
* Location: **West US**.
82
81
83
-
```azurecli
82
+
```azurecli-interactive
84
83
az keyvault create --name "<YourKeyVaultName>" --resource-group "<YourResourceGroupName>" --location "West US"
85
84
```
85
+
86
86
At this point, your Azure account is the only one that's authorized to perform any operations on this new vault.
87
87
88
88
## Add a secret to the key vault
89
89
90
-
We're adding a secret to help illustrate how this works. You might be storing a SQL connection string or any other information that you need to keep securely but make available to your application.
90
+
We're adding a secret to help illustrate how this works. You might want to store a SQL connection string or any other information that needs to be both kept secure and available to your application.
91
91
92
92
Type the following commands to create a secret in the key vault called *AppSecret*. This secret will store the value **MySecret**.
93
93
94
-
```azurecli
94
+
```azurecli-interactive
95
95
az keyvault secret set --vault-name "<YourKeyVaultName>" --name "AppSecret" --value "MySecret"
96
96
```
97
97
98
-
## Create a virtual machine
98
+
## Create a Linux virtual machine
99
99
100
-
Create a VM by using the [az vm create](/cli/azure/vm) command.
100
+
Create a VM by using the `az vm create` command.
101
101
102
-
The following example creates a VM named *myVM* and adds a user account named *azureuser*. The `--generate-ssh-keys` parameter automatically generates an SSH key and puts it in the default key location (*~/.ssh*). To use a specific set of keys instead, use the `--ssh-key-value` option.
102
+
The following example creates a VM named **myVM** and adds a user account named **azureuser**. The `--generate-ssh-keys` parameter automatically generates an SSH key and puts it in the default key location (**~/.ssh**). To create a specific set of keys instead, use the `--ssh-key-value` option.
103
103
104
104
```azurecli-interactive
105
105
az vm create \
@@ -112,7 +112,7 @@ az vm create \
112
112
113
113
It takes a few minutes to create the VM and supporting resources. The following example output shows that the VM creation was successful:
1. Fetches a token from the local MSI endpoint on the VM. The endpoint then fetches a token from Azure Active Directory.
198
+
1. Passes the token to the key vault and fetches your secret.
199
+
200
+
Run the following command. You should see the secret value.
201
+
202
+
```console
190
203
python Sample.py
191
204
```
192
205
193
-
The preceding code shows you how to do operations with Azure Key Vault in a Windows virtual machine.
206
+
In this tutorial, you learned how to use Azure Key Vault with a Python app running on a Linux virtual machine.
207
+
208
+
## Clean up resources
209
+
210
+
Delete the resource group, virtual machine, and all related resources when you no longer need them. To do so, select the resource group for the VM and select **Delete**.
211
+
212
+
Delete the key vault by using the `az keyvault delete` command:
0 commit comments