Skip to content

Commit b54c355

Browse files
authored
Merge pull request #291340 from mrm9084/PythonAAD
App Config Python AAD
2 parents ee04910 + 78173c2 commit b54c355

File tree

2 files changed

+111
-53
lines changed

2 files changed

+111
-53
lines changed

articles/azure-app-configuration/enable-dynamic-configuration-python.md

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ author: mrm9084
77
ms.service: azure-app-configuration
88
ms.devlang: python
99
ms.topic: tutorial
10-
ms.date: 01/29/2024
10+
ms.date: 12/03/2024
1111
ms.custom: devx-track-python, devx-track-extended-python
1212
ms.author: mametcal
1313
#Customer intent: As a Python developer, I want to dynamically update my app to use the latest configuration data in Azure App Configuration.
@@ -38,6 +38,44 @@ Add the following key-value to your Azure App Configuration store. For more info
3838

3939
1. Create a new Python file named *app.py* and add the following code:
4040

41+
### [Microsoft Entra ID (recommended)](#tab/entra-id)
42+
43+
You use the `DefaultAzureCredential` to authenticate to your App Configuration store. Follow the [instructions](./concept-enable-rbac.md#authentication-with-token-credentials) to assign your credential the **App Configuration Data Reader** role. Be sure to allow sufficient time for the permission to propagate before running your application.
44+
45+
```python
46+
from azure.appconfiguration.provider import load, WatchKey
47+
from azure.identity import DefaultAzureCredential
48+
import os
49+
import time
50+
51+
endpoint = os.environ.get("APPCONFIGURATION_ENDPOINT")
52+
53+
# Connecting to Azure App Configuration using connection string
54+
# Setting up to refresh when the Sentinel key is changed.
55+
config = load(
56+
endpoint=endpoint,
57+
credential=DefaultAzureCredential(),
58+
refresh_on=[WatchKey("sentinel")],
59+
refresh_interval=10, # Default value is 30 seconds, shorted for this sample
60+
)
61+
62+
print("Update the `message` in your Azure App Configuration store using Azure portal or CLI.")
63+
print("First, update the `message` value, and then update the `sentinel` key value.")
64+
65+
while (True):
66+
# Refreshing the configuration setting
67+
config.refresh()
68+
69+
# Current value of message
70+
print(config["message"])
71+
72+
# Waiting before the next refresh
73+
time.sleep(5)
74+
```
75+
76+
77+
### [Connection string](#tab/connection-string)
78+
4179
```python
4280
from azure.appconfiguration.provider import load, WatchKey
4381
import os
@@ -66,6 +104,7 @@ Add the following key-value to your Azure App Configuration store. For more info
66104
# Waiting before the next refresh
67105
time.sleep(5)
68106
```
107+
---
69108

70109
1. Run your script:
71110

@@ -104,6 +143,7 @@ In `app.py`, set up Azure App Configuration to load your configuration values. T
104143

105144
```python
106145
from azure.appconfiguration.provider import load, WatchKey
146+
from azure.identity import DefaultAzureCredential
107147

108148
azure_app_config = None # declare azure_app_config as a global variable
109149

@@ -112,7 +152,8 @@ def on_refresh_success():
112152

113153

114154
global azure_app_config
115-
azure_app_config = load(connection_string=os.environ.get("AZURE_APPCONFIG_CONNECTION_STRING")
155+
azure_app_config = load(endpoint=os.environ.get("AZURE_APPCONFIG_ENDPOINT"),
156+
credential=DefaultAzureCredential(),
116157
refresh_on=[WatchKey("sentinel")],
117158
on_refresh_success=on_refresh_success,
118159
refresh_interval=10, # Default value is 30 seconds, shortened for this sample
@@ -164,7 +205,8 @@ def on_refresh_success():
164205
app.config.update(azure_app_config)
165206

166207
AZURE_APPCONFIGURATION = load(
167-
connection_string=os.environ.get("AZURE_APPCONFIG_CONNECTION_STRING"),
208+
connection_string=os.environ.get(endpoint=os.environ.get("AZURE_APPCONFIG_ENDPOINT"),
209+
credential=DefaultAzureCredential(),
168210
refresh_on=[WatchKey("sentinel")],
169211
on_refresh_success=on_refresh_success,
170212
refresh_interval=10, # Default value is 30 seconds, shortened for this sample

articles/azure-app-configuration/quickstart-python-provider.md

Lines changed: 66 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
title: Quickstart for using Azure App Configuration with Python apps | Microsoft Learn
33
description: In this quickstart, create a Python app with the Azure App Configuration to centralize storage and management of application settings separate from your code.
44
services: azure-app-configuration
5-
author: maud-lv
5+
author: mrm9084
66
ms.service: azure-app-configuration
77
ms.devlang: python
88
ms.topic: quickstart
99
ms.custom: devx-track-python, mode-other, engagement-fy23
10-
ms.date: 11/20/2023
11-
ms.author: malev
10+
ms.date: 12/03/2024
11+
ms.author: mametcal
1212
#Customer intent: As a Python developer, I want to manage all my app settings in one place.
1313
---
1414
# Quickstart: Create a Python app with Azure App Configuration
@@ -57,17 +57,22 @@ In this section, you will create a console application and load data from your A
5757

5858
1. Create a new file called *app-configuration-quickstart.py* in the *app-configuration-quickstart* directory and add the following code:
5959

60+
### [Microsoft Entra ID (recommended)](#tab/entra-id)
61+
You use the `DefaultAzureCredential` to authenticate to your App Configuration store. Follow the [instructions](./concept-enable-rbac.md#authentication-with-token-credentials) to assign your credential the **App Configuration Data Reader** role. Be sure to allow sufficient time for the permission to propagate before running your application.
62+
6063
```python
6164
from azure.appconfiguration.provider import (
6265
load,
6366
SettingSelector
6467
)
68+
from azure.identity import DefaultAzureCredential
6569
import os
6670

67-
connection_string = os.environ.get("AZURE_APPCONFIG_CONNECTION_STRING")
71+
endpoint = os.environ.get("AZURE_APPCONFIG_ENDPOINT")
6872

6973
# Connect to Azure App Configuration using a connection string.
70-
config = load(connection_string=connection_string)
74+
config = load(endpoint=endpoint, credential=credential)
75+
credential = DefaultAzureCredential()
7176

7277
# Find the key "message" and print its value.
7378
print(config["message"])
@@ -76,88 +81,99 @@ In this section, you will create a console application and load data from your A
7681

7782
# Connect to Azure App Configuration using a connection string and trimmed key prefixes.
7883
trimmed = {"test."}
79-
config = load(connection_string=connection_string, trim_prefixes=trimmed)
84+
config = load(endpoint=endpoint, credential=credential, trim_prefixes=trimmed)
8085
# From the keys with trimmed prefixes, find a key with "message" and print its value.
8186
print(config["message"])
8287

8388
# Connect to Azure App Configuration using SettingSelector.
8489
selects = {SettingSelector(key_filter="message*", label_filter="\0")}
85-
config = load(connection_string=connection_string, selects=selects)
90+
config = load(endpoint=endpoint, credential=credential, selects=selects)
8691

8792
# Print True or False to indicate if "message" is found in Azure App Configuration.
8893
print("message found: " + str("message" in config))
8994
print("test.message found: " + str("test.message" in config))
9095
```
9196

92-
### Run the application
93-
94-
1. Set an environment variable named **AZURE_APPCONFIG_CONNECTION_STRING**, and set it to the connection string of your App Configuration store. At the command line, run the following command:
97+
### [Connection string](#tab/connection-string)
98+
```python
99+
from azure.appconfiguration.provider import (
100+
load,
101+
SettingSelector
102+
)
103+
import os
95104

96-
#### [Windows command prompt](#tab/windowscommandprompt)
105+
connection_string = os.environ.get("AZURE_APPCONFIG_CONNECTION_STRING")
97106

98-
To run the app locally using the Windows command prompt, run the following command and replace `<app-configuration-store-connection-string>` with the connection string of your app configuration store:
107+
# Connect to Azure App Configuration using a connection string.
108+
config = load(connection_string=connection_string)
99109

100-
```cmd
101-
setx AZURE_APPCONFIG_CONNECTION_STRING "connection-string-of-your-app-configuration-store"
102-
```
110+
# Find the key "message" and print its value.
111+
print(config["message"])
112+
# Find the key "my_json" and print the value for "key" from the dictionary.
113+
print(config["my_json"]["key"])
103114

104-
#### [PowerShell](#tab/powershell)
115+
# Connect to Azure App Configuration using a connection string and trimmed key prefixes.
116+
trimmed = {"test."}
117+
config = load(connection_string=connection_string, trim_prefixes=trimmed)
118+
# From the keys with trimmed prefixes, find a key with "message" and print its value.
119+
print(config["message"])
105120

106-
If you use Windows PowerShell, run the following command and replace `<app-configuration-store-connection-string>` with the connection string of your app configuration store:
121+
# Connect to Azure App Configuration using SettingSelector.
122+
selects = {SettingSelector(key_filter="message*", label_filter="\0")}
123+
config = load(connection_string=connection_string, selects=selects)
107124

108-
```azurepowershell
109-
$Env:AZURE_APPCONFIG_CONNECTION_STRING = "<app-configuration-store-connection-string>"
125+
# Print True or False to indicate if "message" is found in Azure App Configuration.
126+
print("message found: " + str("message" in config))
127+
print("test.message found: " + str("test.message" in config))
110128
```
129+
---
111130

112-
#### [macOS](#tab/unix)
113-
114-
If you use macOS, run the following command and replace `<app-configuration-store-connection-string>` with the connection string of your app configuration store:
131+
### Run the application
115132

116-
```console
117-
export AZURE_APPCONFIG_CONNECTION_STRING='<app-configuration-store-connection-string>'
118-
```
133+
1. Set an environment variable.
119134

120-
#### [Linux](#tab/linux)
135+
### [Microsoft Entra ID (recommended)](#tab/entra-id)
136+
Set the environment variable named **AZURE_APPCONFIG_ENDPOINT** to the endpoint of your App Configuration store found under the *Overview* of your store in the Azure portal.
121137

122-
If you use Linux, run the following command and replace `<app-configuration-store-connection-string>` with the connection string of your app configuration store:
138+
If you use the Windows command prompt, run the following command and restart the command prompt to allow the change to take effect:
123139

124-
```console
125-
export AZURE_APPCONFIG_CONNECTION_STRING='<app-configuration-store-connection-string>'
140+
```cmd
141+
setx AZURE_APPCONFIG_ENDPOINT "endpoint-of-your-app-configuration-store"
126142
```
127143

128-
1. Print out the value of the environment variable to validate that it is set properly with the command below.
144+
If you use PowerShell, run the following command:
129145

130-
#### [Windows command prompt](#tab/windowscommandprompt)
146+
```powershell
147+
$Env:AZURE_APPCONFIG_ENDPOINT = "endpoint-of-your-app-configuration-store"
148+
```
131149

132-
Using the Windows command prompt, restart the command prompt to allow the change to take effect and run the following command:
150+
If you use macOS or Linux, run the following command:
133151

134-
```cmd
135-
echo %AZURE_APPCONFIG_CONNECTION_STRING%
152+
```bash
153+
export AZURE_APPCONFIG_ENDPOINT='endpoint-of-your-app-configuration-store'
136154
```
137155

138-
#### [PowerShell](#tab/powershell)
156+
### [Connection string](#tab/connection-string)
157+
Set the environment variable named **AZURE_APPCONFIG_CONNECTION_STRING** to the read-only connection string of your App Configuration store found under *Access keys* of your store in the Azure portal.
139158

140-
If you use Windows PowerShell, run the following command:
159+
If you use the Windows command prompt, run the following command and restart the command prompt to allow the change to take effect:
141160

142-
```azurepowershell
143-
$Env:AZURE_APPCONFIG_CONNECTION_STRING
161+
```cmd
162+
setx AZURE_APPCONFIG_CONNECTION_STRING "connection-string-of-your-app-configuration-store"
144163
```
145164

146-
#### [macOS](#tab/unix)
147-
148-
If you use macOS, run the following command:
165+
If you use PowerShell, run the following command:
149166

150-
```console
151-
echo "$AZURE_APPCONFIG_CONNECTION_STRING"
167+
```powershell
168+
$Env:AZURE_APPCONFIG_CONNECTION_STRING = "connection-string-of-your-app-configuration-store"
152169
```
153170

154-
#### [Linux](#tab/linux)
155-
156-
If you use Linux, run the following command:
171+
If you use macOS or Linux, run the following command:
157172

158-
```console
159-
echo "$AZURE_APPCONFIG_CONNECTION_STRING"
173+
```bash
174+
export AZURE_APPCONFIG_CONNECTION_STRING='connection-string-of-your-app-configuration-store'
160175
```
176+
---
161177

162178
1. After the environment variable is properly set, run the following command to run the app locally:
163179

@@ -182,7 +198,7 @@ The App Configuration provider loads data into a `Mapping` object, accessible as
182198
You can use Azure App Configuration in your existing Flask web apps by updating its in-built configuration. You can do this by passing your App Configuration provider object to the `update` function of your Flask app instance in `app.py`:
183199

184200
```python
185-
azure_app_config = load(connection_string=os.environ.get("AZURE_APPCONFIG_CONNECTION_STRING"))
201+
azure_app_config = load(endpoint=os.environ.get("AZURE_APPCONFIG_ENDPOINT"), credential=credential)
186202

187203
# NOTE: This will override all existing configuration settings with the same key name.
188204
app.config.update(azure_app_config)
@@ -195,7 +211,7 @@ message = app.config.get("message")
195211
You can use Azure App Configuration in your existing Django web apps by adding the following lines of code into your `settings.py` file
196212

197213
```python
198-
AZURE_APPCONFIGURATION = load(connection_string=os.environ.get("AZURE_APPCONFIG_CONNECTION_STRING"))
214+
AZURE_APPCONFIGURATION = load(endpoint=os.environ.get("AZURE_APPCONFIG_ENDPOINT"), credential=credential)
199215
```
200216

201217
To access individual configuration settings in the Django views, you can reference them from the provider object created in Django settings. For example, in `views.py`:

0 commit comments

Comments
 (0)