Tutorial: Use Azure Key Vault with a Windows virtual machine in Python

Azure Key Vault helps you to protect secrets such as API keys, the database connection strings you need to access your applications, services, and IT resources.

In this tutorial, you learn how to get a console application to read information from Azure Key Vault. To do so, you use managed identities for Azure resources.

The tutorial shows you how to:

  • Create a key vault.
  • Add a secret to the key vault.
  • Retrieve a secret from the key vault.
  • Create an Azure virtual machine.
  • Enable a managed identity.
  • Assign permissions to the VM identity.

Before you begin, read Key Vault basic concepts.

If you don’t have an Azure subscription, create a free account.

Prerequisites

For Windows, Mac, and Linux:

  • Git
  • This tutorial requires that you run the Azure CLI locally. You must have the Azure CLI version 2.0.4 or later installed. Run az --version to find the version. If you need to install or upgrade the CLI, see Install Azure CLI 2.0.

About Managed Service Identity

Azure Key Vault stores credentials securely, so they're not displayed in your code. However, you need to authenticate to Azure Key Vault to retrieve your keys. To authenticate to Key Vault, you need a credential. It's a classic bootstrap dilemma. Managed Service Identity (MSI) solves this issue by providing a bootstrap identity that simplifies the process.

When you enable MSI for an Azure service, such as Azure Virtual Machines, Azure App Service, or Azure Functions, Azure creates a service principal. MSI does this for the instance of the service in Azure Active Directory (Azure AD) and injects the service principal credentials into that instance.

MSI

Next, to get an access token, your code calls a local metadata service that's available on the Azure resource. To authenticate to an Azure Key Vault service, your code uses the access token that it gets from the local MSI endpoint.

Log in to Azure

To log in to Azure by using the Azure CLI, enter:

az login

Create a resource group

An Azure resource group is a logical container into which Azure resources are deployed and managed.

Create a resource group by using the az group create command.

Select a resource group name and fill in the placeholder. The following example creates a resource group in the West US location:

# To list locations: az account list-locations --output table
az group create --name "<YourResourceGroupName>" --location "West US"

You use your newly created resource group throughout this tutorial.

Create a key vault

To create a key vault in the resource group that you created in the preceding step, provide the following information:

  • Key vault name: a string of 3 to 24 characters that can contain only numbers (0-9), letters (a-z, A-Z), and hyphens (-)
  • Resource group name
  • Location: West US
az keyvault create --name "<YourKeyVaultName>" --resource-group "<YourResourceGroupName>" --location "West US"

At this point, your Azure account is the only one that's authorized to perform operations on this new key vault.

Add a secret to the key vault

We're adding a secret to help illustrate how this works. The secret might be a SQL connection string or any other information that you need to keep both secure and available to your application.

To create a secret in the key vault called AppSecret, enter the following command:

az keyvault secret set --vault-name "<YourKeyVaultName>" --name "AppSecret" --value "MySecret"

This secret stores the value MySecret.

Create a virtual machine

You can create a virtual machine by using one of the following methods:

Assign an identity to the VM

In this step, you create a system-assigned identity for the virtual machine by running the following command in the Azure CLI:

az vm identity assign --name <NameOfYourVirtualMachine> --resource-group <YourResourceGroupName>

Note the system-assigned identity that's displayed in the following code. The output of the preceding command would be:

{
  "systemAssignedIdentity": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
  "userAssignedIdentities": {}
}

Assign permissions to the VM identity

Now you can assign the previously created identity permissions to your key vault by running the following command:

az keyvault set-policy --name '<YourKeyVaultName>' --object-id <VMSystemAssignedIdentity> --secret-permissions get list

Log on to the virtual machine

To log on to the virtual machine, follow the instructions in Connect and log on to an Azure virtual machine running Windows.

Create and run a sample Python app

In the next section is an example file named Sample.py. It uses a requests library to make HTTP GET calls.

Edit Sample.py

After you create Sample.py, open the file, and then copy the code in this section.

The code presents a two-step process:

  1. Fetch a token from the local MSI endpoint on the VM.
    Doing so also fetches a token from Azure AD.
  2. Pass the token to your key vault, and then fetch your secret.
    # importing the requests library 
    import requests 

    # Step 1: Fetch an access token from a Managed Identity enabled azure resource.      
    # Note that the resource here is https://vault.azure.net for public cloud and api-version is 2018-02-01
    MSI_ENDPOINT = "http://169.254.169.254/metadata/identity/oauth2/token?api-version=2018-02-01&resource=https%3A%2F%2Fvault.azure.net"
    r = requests.get(MSI_ENDPOINT, headers = {"Metadata" : "true"}) 
      
    # extracting data in json format 
    # This request gets an access_token from Azure AD by using the local MSI endpoint.
    data = r.json() 
    
    # Step 2: Pass the access_token received from previous HTTP GET call to your key vault.
    KeyVaultURL = "https://prashanthwinvmvault.vault.azure.net/secrets/RandomSecret?api-version=2016-10-01"
    kvSecret = requests.get(url = KeyVaultURL, headers = {"Authorization": "Bearer " + data["access_token"]})
    
    print(kvSecret.json()["value"])

You can display the secret value by running the following code:

python Sample.py

The preceding code shows you how to do operations with Azure Key Vault in a Windows virtual machine.

Clean up resources

When they are no longer needed, delete the virtual machine and your key vault.

Next steps