Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
In this tutorial, you learn how to implement Key Vault references in a Python application using App Configuration. It builds on the web app introduced in the quickstart. Before you continue, complete Create a Python app with App Configuration first.
In this tutorial, you learn how to:
- Create an App Configuration key that references a value stored in Key Vault.
- Access the value of this key from a Python application.
Prerequisites
- Azure subscription - create one for free
- Python 3.8 or later - for information on setting up Python on Windows, see the Python on Windows documentation
- Finish the Create a Python app with App Configuration quickstart.
Create a key vault
Sign in to the Azure portal, and then select Create a resource.
In the search box, enter Key Vault. In the result list, select Key Vault.
On the Key Vault page, select Create.
On the Create a key vault page, enter the following information:
- For Subscription: Select a subscription.
- For Resource group: Enter the name of an existing resource group or select Create new and enter a resource group name.
- For Key vault name: Enter a unique name.
- For Region: Select a location.
For the other options, use the default values.
Select Review + create.
After the system validates and displays your inputs, select Create.
At this point, your Azure account is the only one authorized to access this new vault.
Add a secret to Key Vault
Add a secret to the vault to test Key Vault retrieval. The secret is called Message, and its value is "Hello from Key Vault."
On the Key Vault resource menu, select Objects > Secrets.
Select Generate/Import.
In the Create a secret dialog, enter the following values:
- For Upload options: Enter Manual.
- For Name: Enter Message.
- For Secret value: Enter Hello from Key Vault.
For the other options, use the default values.
Select Create.
Add a Key Vault reference to App Configuration
Sign in to the Azure portal. Select All resources, and then select your App Configuration store.
Select Configuration Explorer.
Select + Create > Key vault reference, and then specify the following values:
- Key: Enter TestApp:Settings:KeyVaultMessage.
- Label: Leave this value blank.
- Subscription, Resource group, and Key vault: Enter the values corresponding to the key vault you created in the previous section.
- Secret: Select the secret named Message that you created in the previous section.
Grant your app access to Key Vault
Your application uses DefaultAzureCredential to authenticate to both App Configuration and Key Vault. This credential automatically works with managed identities in Azure, and with your developer credentials locally.
Grant your identity access to Key Vault. Assign the Key Vault Secrets User role to your user account or managed identity:
az role assignment create --role "Key Vault Secrets User" --scope /subscriptions/<SubscriptionId>/resourceGroups/<ResourceGroupName>/providers/Microsoft.KeyVault/vaults/<KeyVaultName> --assignee <AzureAdUserOrManagedIdentity>Grant your identity access to App Configuration. Assign the App Configuration Data Reader role:
az role assignment create --role "App Configuration Data Reader" --scope /subscriptions/<SubscriptionId>/resourceGroups/<ResourceGroupName>/providers/Microsoft.AppConfiguration/configurationStores/<AppConfigurationStoreName> --assignee <AzureAdUserOrManagedIdentity>
Update your code to use a Key Vault reference
Install the required packages by running the following command:
pip install azure-appconfiguration-provider azure-identityCreate an environment variable called AZURE_APPCONFIG_ENDPOINT. Set its value to the endpoint of your App Configuration store. You can find the endpoint on the Overview blade in the Azure portal.
setx AZURE_APPCONFIG_ENDPOINT "endpoint-of-your-app-configuration-store"Restart the command prompt to allow the change to take effect.
Update your Python application file to load Key Vault references. Create or update a file called app.py:
from azure.appconfiguration.provider import load from azure.identity import DefaultAzureCredential import os endpoint = os.environ.get("AZURE_APPCONFIG_ENDPOINT") credential = DefaultAzureCredential() # Connect to Azure App Configuration and resolve Key Vault references. config = load( endpoint=endpoint, credential=credential, keyvault_credential=credential, ) # Access configuration values, including resolved Key Vault references. print(config["TestApp:Settings:KeyVaultMessage"])The
keyvault_credentialparameter tells the provider to use the given credential when resolving Key Vault references. The sameDefaultAzureCredentialinstance is used for both App Configuration and Key Vault authentication.Note
If your Key Vault references point to multiple key vaults that require different credentials, you can use the
keyvault_client_configsparameter instead to provide per-vault credentials. For more information, see the Python provider reference.Run the application:
python app.pyYou see the message that you entered in App Configuration. You also see the message that you entered in Key Vault, resolved through the Key Vault reference.
Clean up resources
If you don't want to continue using the resources created in this article, delete the resource group you created here to avoid charges.
Important
Deleting a resource group is irreversible. The resource group and all the resources in it are permanently deleted. Ensure that you don't accidentally delete the wrong resource group or resources. If you created the resources for this article inside a resource group that contains other resources you want to keep, delete each resource individually from its respective pane instead of deleting the resource group.
- Sign in to the Azure portal, and select Resource groups.
- In the Filter by name box, enter the name of your resource group.
- In the result list, select the resource group name to see an overview.
- Select Delete resource group.
- You're asked to confirm the deletion of the resource group. Enter the name of your resource group to confirm, and select Delete.
After a few moments, the resource group and all its resources are deleted.
Next steps
In this tutorial, you created an App Configuration key that references a value stored in Key Vault. To learn more about the Python provider for Azure App Configuration, see the Python provider reference documentation.