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
Copy file name to clipboardExpand all lines: articles/app-service/tutorial-python-postgresql-app.md
+59-43Lines changed: 59 additions & 43 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -260,94 +260,110 @@ Sign in to the [Azure portal](https://portal.azure.com/) and follow these steps
260
260
261
261
-----
262
262
263
-
## 2. Verify connection settings
263
+
## 2. Secure connection secrets
264
264
265
-
The creation wizard generated the connectivity variables for you already as [app settings](configure-common.md#configure-app-settings). App settings are one way to keep connection secrets out of your code repository. When you're ready to move your secrets to a more secure location, here's an [article on storing in Azure Key Vault](/azure/key-vault/certificates/quick-create-python).
266
-
267
-
### [Flask](#tab/flask)
265
+
The creation wizard generated the database connectivity string for you already as an [app setting](configure-common.md#configure-app-settings). However, the security best practice is to keep secrets out of App Service completely. You move your secrets to a key vault and change your app setting to a [Key Vault reference](app-service-key-vault-references.md) with the help of Service Connectors.
268
266
269
267
:::row:::
270
268
:::column span="2":::
271
-
**Step 1:** In the App Service page, in the left menu, select **Configuration**.
269
+
**Step 1: Retrieve the existing connection string**
270
+
1. In the left menu of the App Service page, select **Settings > Environment variables**.
271
+
1. Select **AZURE_POSTGRESQL_CONNECTIONSTRING**.
272
+
1. In **Add/Edit application setting**, in the **Value** field, find the *password=* part at the end of the string.
273
+
1. Copy the password string after *Password=* for use later.
274
+
This app setting lets you connect to the Postgres database secured behind a private endpoint. However, the secret is saved directly in the App Service app, which isn't the best. You'll change this.
272
275
:::column-end:::
273
276
:::column:::
274
-
:::image type="content" source="./media/tutorial-python-postgresql-app/azure-portal-get-connection-string-1.png" alt-text="A screenshot showing how to open the configuration page in App Service (Flask)." lightbox="./media/tutorial-python-postgresql-app/azure-portal-get-connection-string-1.png":::
277
+
:::image type="content" source="./media/tutorial-java-tomcat-mysql-app/azure-portal-secure-connection-secrets-1.png" alt-text="A screenshot showing how to see the value of an app setting." lightbox="./media/tutorial-java-tomcat-mysql-app/azure-portal-secure-connection-secrets-1.png":::
275
278
:::column-end:::
276
279
:::row-end:::
277
280
:::row:::
278
281
:::column span="2":::
279
-
**Step 2:** In the **Application settings** tab of the **Configuration** page, verify that `AZURE_POSTGRESQL_CONNECTIONSTRING` is present. That will be injected into the runtime environment as an environment variable.
282
+
**Step 2: Create a key vault for secure management of secrets**
283
+
1. In the top search bar, type "*key vault*", then select **Marketplace** > **Key Vault**.
284
+
1. In **Resource Group**, select **msdocs-python-postgres-tutorial**.
285
+
1. In **Key vault name**, type a name that consists of only letters and numbers.
286
+
1. In **Region**, set it to the same location as the resource group.
280
287
:::column-end:::
281
288
:::column:::
282
-
:::image type="content" source="./media/tutorial-python-postgresql-app/azure-portal-get-connection-string-2.png" alt-text="A screenshot showing how to see the autogenerated connection string (Flask)." lightbox="./media/tutorial-python-postgresql-app/azure-portal-get-connection-string-2.png":::
289
+
:::image type="content" source="./media/tutorial-java-tomcat-mysql-app/azure-portal-secure-connection-secrets-2.png" alt-text="A screenshot showing how to create a key vault." lightbox="./media/tutorial-java-tomcat-mysql-app/azure-portal-secure-connection-secrets-2.png":::
283
290
:::column-end:::
284
291
:::row-end:::
285
292
:::row:::
286
293
:::column span="2":::
287
-
**Step 3:** In a terminal or command prompt, run the following Python script to generate a unique secret: `python -c 'import secrets; print(secrets.token_hex())'`. Copy the output value to use in the next step.
294
+
**Step 3: Secure the key vault with a Private Endpoint**
295
+
1. Select the **Networking** tab.
296
+
1. Unselect **Enable public access**.
297
+
1. Select **Create a private endpoint**.
298
+
1. In **Resource Group**, select **msdocs-python-postgres-tutorial**.
299
+
1. In **Name**, type a name for the private endpoint that consists of only letters and numbers.
300
+
1. In **Region**, set it to the same location as the resource group.
301
+
1. In the dialog, in **Location**, select the same location as your App Service app.
302
+
1. In **Resource Group**, select **msdocs-python-postgres-tutorial**.
303
+
1. In **Name**, type **msdocs-python-myspostgres-XYZVaultEndpoint**.
304
+
1. In **Virtual network**, select **msdocs-python-postgres-XYZVnet**.
305
+
1. In **Subnet**, **msdocs-python-postgres-XYZSubnet**.
306
+
1. Select **OK**.
307
+
1. Select **Review + create**, then select **Create**. Wait for the key vault deployment to finish. You should see "Your deployment is complete."
288
308
:::column-end:::
289
309
:::column:::
310
+
:::image type="content" source="./media/tutorial-java-tomcat-mysql-app/azure-portal-secure-connection-secrets-3.png" alt-text="A screenshot showing how to secure a key vault with a private endpoint." lightbox="./media/tutorial-java-tomcat-mysql-app/azure-portal-secure-connection-secrets-3.png":::
290
311
:::column-end:::
291
312
:::row-end:::
292
313
:::row:::
293
314
:::column span="2":::
294
-
**Step 4:** Back in the **Configuration** page, select **New application setting**. Name the setting `SECRET_KEY`. Paste the value from the previous value. Select **OK**.
315
+
**Step 4: Configure the Service Connector**
316
+
1. In the top search bar, type *msdocs-tomcat-mysql*, then the App Service resource called **msdocs-python-postgres-XYZ**.
317
+
1. In the App Service page, in the left menu, select **Settings > Service Connector**. There's already a connector, which the app creation wizard created for you.
318
+
1. Select checkbox next to the connector, then select **Edit**.
319
+
1. In the **Basics** tab, set **Client type** to **Java**.
320
+
1. Select the **Authentication** tab.
321
+
1. In **Password**, paste the password you copied earlier.
322
+
1. Select **Store Secret in Key Vault**.
323
+
1. Under **Key Vault Connection**, select **Create new**.
324
+
A **Create connection** dialog is opened on top of the edit dialog.
295
325
:::column-end:::
296
326
:::column:::
297
-
:::image type="content" source="./media/tutorial-python-postgresql-app/azure-portal-app-service-app-setting.png" alt-text="A screenshot showing how to set the SECRET_KEY app setting in the Azure portal (Django)." lightbox="./media/tutorial-python-postgresql-app/azure-portal-app-service-app-setting.png":::
327
+
:::image type="content" source="./media/tutorial-java-tomcat-mysql-app/azure-portal-secure-connection-secrets-4.png" alt-text="A screenshot showing how to edit a service connector with a key vault connection." lightbox="./media/tutorial-java-tomcat-mysql-app/azure-portal-secure-connection-secrets-4.png":::
298
328
:::column-end:::
299
329
:::row-end:::
300
330
:::row:::
301
331
:::column span="2":::
302
-
**Step 5:** Select **Save**.
332
+
**Step 5: Establish the Key Vault connection**
333
+
1. In the **Create connection** dialog for the Key Vault connection, in **Key Vault**, select the key vault you created earlier.
334
+
1. Select **Review + Create**. You should see that **System assigned managed identity** is set to **Selected**.
335
+
1. When validation completes, select **Create**.
303
336
:::column-end:::
304
337
:::column:::
305
-
:::image type="content" source="./media/tutorial-python-postgresql-app/azure-portal-app-service-app-setting-save.png" alt-text="A screenshot showing how to save the SECRET_KEY app setting in the Azure portal (Django)." lightbox="./media/tutorial-python-postgresql-app/azure-portal-app-service-app-setting-save.png":::
338
+
:::image type="content" source="./media/tutorial-java-tomcat-mysql-app/azure-portal-secure-connection-secrets-5.png" alt-text="A screenshot showing how to configure a key vault service connector." lightbox="./media/tutorial-java-tomcat-mysql-app/azure-portal-secure-connection-secrets-5.png":::
306
339
:::column-end:::
307
340
:::row-end:::
308
-
309
-
### [Django](#tab/django)
310
-
311
341
:::row:::
312
342
:::column span="2":::
313
-
**Step 1:** In the App Service page, in the left menu, select **Configuration**.
343
+
**Step 6: Finalize the Service Connector configuration**
344
+
1. You're back in the edit dialog for **defaultConnector**. In the **Authentication** tab, wait for the key vault connector to be created. When it's finished, the **Key Vault Connection** dropdown automatically selects it.
345
+
1. Select **Next: Networking**.
346
+
1. Select **Save**. Wait until the **Update succeeded** notification appears.
314
347
:::column-end:::
315
348
:::column:::
316
-
:::image type="content" source="./media/tutorial-python-postgresql-app/azure-portal-get-connection-string-1.png" alt-text="A screenshot showing how to open the configuration page in App Service (Django)." lightbox="./media/tutorial-python-postgresql-app/azure-portal-get-connection-string-1.png":::
349
+
:::image type="content" source="./media/tutorial-java-tomcat-mysql-app/azure-portal-secure-connection-secrets-6.png" alt-text="A screenshot showing the key vault connection selected in the defaultConnector." lightbox="./media/tutorial-java-tomcat-mysql-app/azure-portal-secure-connection-secrets-6.png":::
317
350
:::column-end:::
318
351
:::row-end:::
319
352
:::row:::
320
353
:::column span="2":::
321
-
**Step 2:** In the **Application settings** tab of the **Configuration** page, verify that `AZURE_POSTGRESQL_CONNECTIONSTRING` and `AZURE_REDIS_CONNECTIONSTRING` are present. They will be injected into the runtime environment as an environment variable.
354
+
**Step 7: Verify the Key Vault integration**
355
+
1. From the left menu, select **Settings > Environment variables** again.
356
+
1. Next to **AZURE_POSTGRESQL_CONNECTIONSTRING**, select **Show value**. The value should be `@Microsoft.KeyVault(...)`, which means that it's a [key vault reference](app-service-key-vault-references.md) because the secret is now managed in the key vault.
322
357
:::column-end:::
323
358
:::column:::
324
-
:::image type="content" source="./media/tutorial-python-postgresql-app/azure-portal-get-connection-string-2-django.png" alt-text="A screenshot showing how to see the autogenerated connection string (Django)." lightbox="./media/tutorial-python-postgresql-app/azure-portal-get-connection-string-2.png":::
325
-
:::column-end:::
326
-
:::row-end:::
327
-
:::row:::
328
-
:::column span="2":::
329
-
**Step 3:** In a terminal or command prompt, run the following Python script to generate a unique secret: `python -c 'import secrets; print(secrets.token_hex())'`. Copy the output value to use in the next step.
330
-
:::column-end:::
331
-
:::column:::
332
-
:::column-end:::
333
-
:::row-end:::
334
-
:::row:::
335
-
:::column span="2":::
336
-
**Step 4:** Back in the **Configuration** page, select **New application setting**. Name the setting `SECRET_KEY`. Paste the value from the previous value. Select **OK**.
337
-
:::column-end:::
338
-
:::column:::
339
-
:::image type="content" source="./media/tutorial-python-postgresql-app/azure-portal-app-service-app-setting.png" alt-text="A screenshot showing how to set the SECRET_KEY app setting in the Azure portal (Django)." lightbox="./media/tutorial-python-postgresql-app/azure-portal-app-service-app-setting.png":::
340
-
:::column-end:::
341
-
:::row-end:::
342
-
:::row:::
343
-
:::column span="2":::
344
-
**Step 5:** Select **Save**.
345
-
:::column-end:::
346
-
:::column:::
347
-
:::image type="content" source="./media/tutorial-python-postgresql-app/azure-portal-app-service-app-setting-save.png" alt-text="A screenshot showing how to save the SECRET_KEY app setting in the Azure portal (Django)." lightbox="./media/tutorial-python-postgresql-app/azure-portal-app-service-app-setting-save.png":::
359
+
:::image type="content" source="./media/tutorial-java-tomcat-mysql-app/azure-portal-secure-connection-secrets-7.png" alt-text="A screenshot showing how to see the value of the MySQL environment variable in Azure." lightbox="./media/tutorial-java-tomcat-mysql-app/azure-portal-secure-connection-secrets-7.png":::
348
360
:::column-end:::
349
361
:::row-end:::
350
362
363
+
To summarize, the process involved retrieving the MySQL connection string from the App Service's environment variables, creating an Azure Key Vault for secure secret management with private access, and updating the service connector to store the password in the key vault. A secure connection between the App Service app and key vault was established using a system-assigned managed identity, and the setup was verified by confirming the connection string uses a Key Vault reference.
364
+
365
+
Having issues? Check the [Troubleshooting section](#troubleshooting).
0 commit comments