Skip to content

Commit 40be661

Browse files
authored
Merge pull request #231772 from diberry/diberry/0322-app-service-fix-leak
App Service - PY tutorial - secret leak fix
2 parents 789c7d7 + 5deae59 commit 40be661

File tree

3 files changed

+36
-7
lines changed

3 files changed

+36
-7
lines changed
25 KB
Loading
12.2 KB
Loading

articles/app-service/tutorial-python-postgresql-app.md

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,14 @@ DBUSER=<db-user-name>
6666
DBPASS=<db-password>
6767
```
6868

69+
Create a SECRET_KEY value for your app by running the following command at a terminal prompt: `python -c 'import secrets; print(secrets.token_hex())'`.
70+
71+
Set the returned value as the value of `SECRET_KEY` in the .env file.
72+
73+
```
74+
SECRET_KEY=<secret-key>
75+
```
76+
6977
Create a virtual environment for the app:
7078

7179
[!INCLUDE [Virtual environment setup](<./includes/quickstart-python/virtual-environment-setup.md>)]
@@ -128,7 +136,7 @@ Sign in to the [Azure portal](https://portal.azure.com/) and follow these steps
128136
1. *Region* &rarr; Any Azure region near you.
129137
1. *Name* &rarr; **msdocs-python-postgres-XYZ** where *XYZ* is any three random characters. This name must be unique across Azure.
130138
1. *Runtime stack* &rarr; **Python 3.10**.
131-
1. *Database* &rarr; **PostgreSQL - Flexible Server** is selected by default as the database engine. The server name and database name is also set by default to appropriate values.
139+
1. *Database* &rarr; **PostgreSQL - Flexible Server** is selected by default as the database engine. The server name and database name are also set by default to appropriate values.
132140
1. *Hosting plan* &rarr; **Basic**. When you're ready, you can [scale up](manage-scale-up.md) to a production pricing tier later.
133141
1. Select **Review + create**.
134142
1. After validation completes, select **Create**.
@@ -154,7 +162,7 @@ Sign in to the [Azure portal](https://portal.azure.com/) and follow these steps
154162

155163
## 2. Verify connection settings
156164

157-
The creation wizard generated the connectivity variables for you already as [app settings](configure-common.md#configure-app-settings).
165+
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](../key-vault/certificates/quick-create-python.md).
158166

159167
:::row:::
160168
:::column span="2":::
@@ -167,14 +175,35 @@ The creation wizard generated the connectivity variables for you already as [app
167175
:::row:::
168176
:::column span="2":::
169177
**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.
170-
App settings are one way to keep connection secrets out of your code repository.
171-
When you're ready to move your secrets to a more secure location,
172-
here's an [article on storing in Azure Key Vault](../key-vault/certificates/quick-create-python.md).
173178
:::column-end:::
174179
:::column:::
175180
:::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." lightbox="./media/tutorial-python-postgresql-app/azure-portal-get-connection-string-2.png":::
176181
:::column-end:::
177182
:::row-end:::
183+
:::row:::
184+
:::column span="2":::
185+
**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.
186+
:::column-end:::
187+
:::column:::
188+
:::column-end:::
189+
:::row-end:::
190+
:::row:::
191+
:::column span="2":::
192+
**Step 4.** In the **Application settings** tab of the **Configuration** page, select **New application setting**. Name the setting `SECRET_KEY`. Paste the value from the previous value. Select **OK**.
193+
:::column-end:::
194+
:::column:::
195+
:::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." lightbox="./media/tutorial-python-postgresql-app/azure-portal-app-service-app-setting.png":::
196+
:::column-end:::
197+
:::row-end:::
198+
:::row:::
199+
:::column span="2":::
200+
**Step 5.** Select **Save**.
201+
:::column-end:::
202+
:::column:::
203+
:::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." lightbox="./media/tutorial-python-postgresql-app/azure-portal-app-service-app-setting-save.png":::
204+
:::column-end:::
205+
:::row-end:::
206+
178207

179208
Having issues? Check the [Troubleshooting guide](configure-language-python.md#troubleshooting).
180209

@@ -552,7 +581,7 @@ The `azd up` command cloned the sample app project template to your machine. The
552581
553582
* **Source code**: The code and assets for a Flask or Django web app that can be used for local development or deployed to Azure.
554583
* **Bicep files**: Infrastructure as code (IaC) files that are used by `azd` to create the necessary resources in Azure.
555-
* **Configuration files**: Essential configuration files such as `azure.yaml` that are used by `azd` to provision, deploy and wire resources together to produce a fully-fledged application.
584+
* **Configuration files**: Essential configuration files such as `azure.yaml` that are used by `azd` to provision, deploy and wire resources together to produce a fully fledged application.
556585
557586
### 2. Provisioned the Azure resources
558587
@@ -563,7 +592,7 @@ The `azd up` command created all of the resources for the sample application in
563592
* **Azure App Service plan**: An App Service plan was created to host App Service instances. App Service plans define what compute resources are available for one or more web apps.
564593
* **Azure App Service**: An App Service instance was created in the new App Service plan to host and run the deployed application. In this case a Linux instance was created and configured to run Python apps. Additional configurations were also applied to the app service, such as setting the Postgres connection string and secret keys.
565594
* **Azure Database for PostgresSQL**: A Postgres database and server were created for the app hosted on App Service to connect to. The required admin user, network and connection settings were also configured.
566-
* **Azure Application Insights**: Application insights was setup and configured for the app hosted on the App Service. This service enables detailed telemetry and monitoring for your application.
595+
* **Azure Application Insights**: Application insights was set up and configured for the app hosted on the App Service. This service enables detailed telemetry and monitoring for your application.
567596
568597
You can inspect the Bicep files in the [`infra`](https://github.com/Azure-Samples/msdocs-flask-postgresql-sample-app/tree/main/infra) folder of the project to understand how each of these resources were provisioned in more detail. The `resources.bicep` file defines most of the different services created in Azure. For example, the App Service plan and App Service web app instance were created and connected using the following Bicep code:
569598

0 commit comments

Comments
 (0)