Skip to content

Commit 2b3c080

Browse files
authored
Merge pull request MicrosoftDocs#65631 from Jak-MS/tutorial-python-linux-virtual-machine
edit pass: developmental edit - tutorial-python-linux-virtual-machine
2 parents cd9bace + 3ef5abe commit 2b3c080

File tree

1 file changed

+101
-76
lines changed

1 file changed

+101
-76
lines changed

articles/key-vault/tutorial-python-linux-virtual-machine.md

Lines changed: 101 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
2-
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.
44
services: key-vault
55
documentationcenter:
66
author: prashanthyv
@@ -13,93 +13,93 @@ ms.topic: tutorial
1313
ms.date: 09/05/2018
1414
ms.author: pryerram
1515
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.
1717
---
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
1920

2021
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.
2122

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:
2324

2425
> [!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
3132
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).
3334

3435
## Prerequisites
35-
For all platforms, you need:
3636

37-
* Git ([download](https://git-scm.com/downloads)).
37+
* [Git](https://git-scm.com/downloads).
3838
* 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.
40+
41+
[!INCLUDE [Azure Cloud Shell](../../includes/cloud-shell-try-it.md)]
4042

41-
### Managed Service Identity and how it works
42-
This tutorial makes use of Managed Service Identity (MSI).
43+
## Understand Managed Service Identity
4344

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.
4546

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.
4748

4849
![MSI](media/MSI.png)
4950

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.
5252

53-
## Log in to Azure
53+
## Sign in to Azure
5454

55-
To log in to Azure by using the Azure CLI, enter:
55+
To sign in to Azure by using the Azure CLI, enter:
5656

57-
```azurecli
57+
```azurecli-interactive
5858
az login
5959
```
6060

6161
## Create a resource group
6262

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.
6464

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.
6766

68-
```azurecli
67+
```azurecli-interactive
6968
# To list locations: az account list-locations --output table
7069
az group create --name "<YourResourceGroupName>" --location "West US"
7170
```
7271

73-
The resource group that you just created is used throughout this article.
72+
You use this resource group throughout the tutorial.
7473

7574
## Create a key vault
7675

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:
7877

7978
* 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 (-).
8079
* Resource group name.
8180
* Location: **West US**.
8281

83-
```azurecli
82+
```azurecli-interactive
8483
az keyvault create --name "<YourKeyVaultName>" --resource-group "<YourResourceGroupName>" --location "West US"
8584
```
85+
8686
At this point, your Azure account is the only one that's authorized to perform any operations on this new vault.
8787

8888
## Add a secret to the key vault
8989

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.
9191

9292
Type the following commands to create a secret in the key vault called *AppSecret*. This secret will store the value **MySecret**.
9393

94-
```azurecli
94+
```azurecli-interactive
9595
az keyvault secret set --vault-name "<YourKeyVaultName>" --name "AppSecret" --value "MySecret"
9696
```
9797

98-
## Create a virtual machine
98+
## Create a Linux virtual machine
9999

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.
101101

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.
103103

104104
```azurecli-interactive
105105
az vm create \
@@ -112,7 +112,7 @@ az vm create \
112112

113113
It takes a few minutes to create the VM and supporting resources. The following example output shows that the VM creation was successful:
114114

115-
```
115+
```azurecli
116116
{
117117
"fqdns": "",
118118
"id": "/subscriptions/<guid>/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachines/myVM",
@@ -125,72 +125,97 @@ It takes a few minutes to create the VM and supporting resources. The following
125125
}
126126
```
127127

128-
Note your own `publicIpAddress` value in the output from your VM. This address is used to access the VM in the next steps.
128+
Make a note of your own `publicIpAddress` in the output from your VM. You'll use this address to access the VM in later steps.
129129

130-
## Assign an identity to the virtual machine
131-
In this step, we're creating a system-assigned identity for the virtual machine. Run the following command in the Azure CLI:
130+
## Assign an identity to the VM
132131

133-
```
132+
Create a system-assigned identity to the virtual machine by running the following command:
133+
134+
```azurecli-interactive
134135
az vm identity assign --name <NameOfYourVirtualMachine> --resource-group <YourResourceGroupName>
135136
```
136137

137-
The output of the command is as follows. Note the value of **systemAssignedIdentity**.
138+
The output of the command is as follows.
138139

139-
```
140+
```azurecli
140141
{
141142
"systemAssignedIdentity": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
142143
"userAssignedIdentities": {}
143144
}
144145
```
145146

146-
## Give the virtual machine identity permission to the key vault
147-
Now we can give the identity permission to the key vault. Run the following command:
147+
Make a note of the `systemAssignedIdentity`. You use it the next step.
148148

149-
```
149+
## Give the VM identity permission to Key Vault
150+
151+
Now you can give Key Vault permission to the identity you created. Run the following command:
152+
153+
```azurecli-interactive
150154
az keyvault set-policy --name '<YourKeyVaultName>' --object-id <VMSystemAssignedIdentity> --secret-permissions get list
151155
```
152156

153-
## Log in to the virtual machine
157+
## Log in to the VM
154158

155-
Log in to the virtual machine by following [this tutorial](https://docs.microsoft.com/azure/virtual-machines/windows/connect-logon).
159+
Log in to the virtual machine by using a terminal.
156160

157-
## Create and run the sample Python app
161+
```terminal
162+
ssh azureuser@<PublicIpAddress>
163+
```
158164

159-
The following example file is named *Sample.py*.
160-
It uses the [requests](https://pypi.org/project/requests/2.7.0/) library to make HTTP GET calls.
165+
## Install Python library on the VM
161166

162-
## Edit Sample.py
163-
After you create Sample.py, open the file and copy the following code. The code is a two-step process:
164-
1. Fetch a token from the local MSI endpoint on the VM. The endpoint then fetches a token from Azure Active Directory.
165-
2. Pass the token to the key vault and fetch your secret.
167+
Download and install the [requests](https://pypi.org/project/requests/2.7.0/) Python library to make HTTP GET calls.
166168

167-
```
168-
# importing the requests library
169-
import requests
170-
171-
# Step 1: Fetch an access token from an MSI-enabled Azure resource
172-
# Note that the resource here is https://vault.azure.net for the public cloud, and api-version is 2018-02-01
173-
MSI_ENDPOINT = "http://169.254.169.254/metadata/identity/oauth2/token?api-version=2018-02-01&resource=https%3A%2F%2Fvault.azure.net"
174-
r = requests.get(MSI_ENDPOINT, headers = {"Metadata" : "true"})
175-
176-
# Extracting data in JSON format
177-
# This request gets an access token from Azure Active Directory by using the local MSI endpoint
178-
data = r.json()
179-
180-
# Step 2: Pass the access token received from the previous HTTP GET call to the key vault
181-
KeyVaultURL = "https://prashanthwinvmvault.vault.azure.net/secrets/RandomSecret?api-version=2016-10-01"
182-
kvSecret = requests.get(url = KeyVaultURL, headers = {"Authorization": "Bearer " + data["access_token"]})
183-
184-
print(kvSecret.json()["value"])
185-
```
169+
## Create, edit, and run the sample Python app
186170

187-
By running the following command, you should see the secret value:
171+
Create a Python file called **Sample.py**.
188172

173+
Open Sample.py and edit it to contain the following code:
174+
175+
```python
176+
# importing the requests library
177+
import requests
178+
179+
# Step 1: Fetch an access token from an MSI-enabled Azure resource
180+
# Note that the resource here is https://vault.azure.net for the public cloud, and api-version is 2018-02-01
181+
MSI_ENDPOINT = "http://169.254.169.254/metadata/identity/oauth2/token?api-version=2018-02-01&resource=https%3A%2F%2Fvault.azure.net"
182+
r = requests.get(MSI_ENDPOINT, headers = {"Metadata" : "true"})
183+
184+
# Extracting data in JSON format
185+
# This request gets an access token from Azure Active Directory by using the local MSI endpoint
186+
data = r.json()
187+
188+
# Step 2: Pass the access token received from the previous HTTP GET call to the key vault
189+
KeyVaultURL = "https://prashanthwinvmvault.vault.azure.net/secrets/RandomSecret?api-version=2016-10-01"
190+
kvSecret = requests.get(url = KeyVaultURL, headers = {"Authorization": "Bearer " + data["access_token"]})
191+
192+
print(kvSecret.json()["value"])
189193
```
194+
195+
The preceding code performs a two-step process:
196+
197+
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
190203
python Sample.py
191204
```
192205

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:
213+
214+
```azurecli-interactive
215+
az keyvault delete --name
216+
[--resource-group]
217+
[--subscription]
218+
```
194219

195220
## Next steps
196221

0 commit comments

Comments
 (0)