Skip to content

Commit 81ea0ed

Browse files
committed
Ready for review
1 parent 5f97947 commit 81ea0ed

14 files changed

+54
-27
lines changed

articles/cosmos-db/access-secrets-from-keyvault.md

Lines changed: 54 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ ms.date: 11/07/2022
2020
> [!IMPORTANT]
2121
> It's recommended to access Azure Cosmos DB is to use a [system-assigned managed identity](managed-identity-based-authentication.md). If your service cannot take advantage of managed identities then use the [certificate-based authentication](certificate-based-authentication.md). If both the managed identity solution and cert based solution do not meet your needs, please use the Azure Key vault solution in this article.
2222
23-
If you are using Azure Cosmos DB as your database, you connect to databases, container, and items by using an SDK, the API endpoint, and either the primary or secondary key.
23+
If you're using Azure Cosmos DB as your database, you connect to databases, container, and items by using an SDK, the API endpoint, and either the primary or secondary key.
2424

2525
It's not a good practice to store the endpoint URI and sensitive read-write keys directly within application code or configuration file. Ideally, this data is read from environment variables within the host. In Azure App Service, [app settings](/azure/app-service/configure-common#configure-app-settings) allow you to inject runtime credentials for your Azure Cosmos DB account without the need for developers to store these credentials in an insecure clear text manner.
2626

2727
Azure Key Vault iterates on this best practice further by allowing you to store these credentials securely while giving services like Azure App Service managed access to the credentials. Azure App Service will securely read your credentials from Azure Key Vault and inject those credentials into your running application.
2828

29-
With this best practice, developers can store the credentials for features/tools like the [Azure Cosmos DB emulator](local-emulator.md) or [Try Azure Cosmos DB free](try-free.md) during development and the operations team can ensure that the correct production settings are injected at runtime.
29+
With this best practice, developers can store the credentials for tools like the [Azure Cosmos DB emulator](local-emulator.md) or [Try Azure Cosmos DB free](try-free.md) during development. Then, the operations team can ensure that the correct production settings are injected at runtime.
3030

3131
In this tutorial, you learn how to:
3232

@@ -89,7 +89,7 @@ First, create a new key vault to store your API for NoSQL credentials.
8989

9090
## Add Azure Cosmos DB access keys to the Key Vault
9191

92-
Now, store the your Azure Cosmos DB credentials as secrets in the key vault.
92+
Now, store your Azure Cosmos DB credentials as secrets in the key vault.
9393

9494
1. Select **Go to resource** to go to the Azure Key Vault resource page.
9595

@@ -173,52 +173,79 @@ In this section, create a new Azure Web App, deploy a sample application, and th
173173

174174
1. Review the settings you provide, and then select **Create**. It takes a few minutes to create the account. Wait for the portal page to display **Your deployment is complete** before moving on.
175175

176-
1.
176+
1. You may need to wait a few extra minutes for the web application to be initially deployed to the web app. From the Azure Web App resource page, select **Browse** to see the default state of the app.
177+
178+
:::image type="content" source="media/access-secrets-from-keyvault/sample-web-app-empty.png" lightbox="media/access-secrets-from-keyvault/sample-web-app-empty.png" alt-text="Screenshot of the web application in it's default state without credentials.":::
179+
180+
1. Select the **Identity** navigation menu option.
181+
182+
1. On the **Identity** page, select **On** for **System-assigned** managed identity, and then select **Save**.
183+
184+
:::image type="content" source="media/access-secrets-from-keyvault/enable-managed-identity.png" alt-text="Screenshot of system-assigned managed identity being enabled from the Identity page.":::
177185

178186
## Inject Azure Key Vault secrets as Azure Web App app settings
179187

180-
Finally, inject the secrets stored in your key vault as app settings within the web app. This will, in turn, inject the credentials into the application at runtime without storing the credentials in clear text.
188+
Finally, inject the secrets stored in your key vault as app settings within the web app. The app settings will, in turn, inject the credentials into the application at runtime without storing the credentials in clear text.
181189

182-
1.
190+
1. Return to the key vault page in the Azure portal. Select **Access policies** from the navigation menu.
183191

192+
1. On the **Access policies** page, select **Create** from the menu.
184193

185-
## Next steps
194+
:::image type="content" source="media/access-secrets-from-keyvault/create-access-policy.png" alt-text="Screenshot of the Create option in the Access policies menu.":::
186195

187-
- To configure a firewall for Azure Cosmos DB, see [firewall support](how-to-configure-firewall.md) article.
188-
- To configure virtual network service endpoint, see [secure access by using VNet service endpoint](how-to-configure-vnet-service-endpoint.md) article.
196+
1. On the **Permissions** tab of the **Create an access policy** page, select the **Get** option in the **Secret permissions** section. Select **Next**.
189197

190-
---
198+
:::image type="content" source="media/access-secrets-from-keyvault/get-secrets-permission.png" alt-text="Screenshot of the Get permission enabled for Secret permissions.":::
191199

192-
Old stuff
200+
1. On the **Principal** tab, select the name of the web app you created earlier in this tutorial. Select **Next**.
193201

202+
:::image type="content" source="media/access-secrets-from-keyvault/assign-principal.png" alt-text="Screenshot of a web app managed identity assigned to a permission.":::
194203

195-
1. Create an Azure web application or you can download the app from the [GitHub repository](https://github.com/Azure/azure-cosmos-dotnet-v2/tree/master/Demo/keyvaultdemo). It's a simple MVC application.
204+
> [!NOTE]
205+
> In this example screenshot, the web app is named **msdocs-dotnet-web**.
196206
197-
2. Unzip the downloaded application and open the **HomeController.cs** file. Update the secret ID in the following line:
207+
1. Select **Next** again to skip the **Application** tab. On the **Review + create** tab, review the settings you provide, and then select **Create**.
198208

199-
`var secret = await keyVaultClient.GetSecretAsync("<Your Key Vault’s secret identifier>")`
209+
1. Return to the web app page in the Azure portal. Select **Configuration** from the navigation menu.
200210

201-
3. **Save** the file, **Build** the solution.
202-
4. Next deploy the application to Azure. Open the context menu for the project and choose **publish**. Create a new app service profile (you can name the app WebAppKeyVault1) and select **Publish**.
211+
1. On the **Configuration** page, select **New application setting**. In the **Add/Edit application setting** dialog, enter the following information:
203212

204-
5. Once the application is deployed from the Azure portal, navigate to web app that you deployed, and turn on the **Managed service identity** of this application.
213+
| Setting | Description |
214+
| --- | --- |
215+
| **Name** | `CREDENTIALS__ENDPOINT` |
216+
| **Key** | Get the **secret identifier** for the **cosmos-endpoint** secret in your key vault that you created earlier in this tutorial. Enter the identifier in the following format: `@Microsoft.KeyVault(SecretUri=<secret-identifier>)`. |
205217

206-
:::image type="content" source="media/access-secrets-from-keyvault/turn-on-managed-service-identity.png" alt-text="Screenshot of the Managed service identity page in the Azure portal.":::
218+
> [!TIP]
219+
> Ensure that the environment variable has a double underscore (`__`) value instead of a single underscore. The double-underscore is a key delimeter supported by .NET on all platforms. For more information, see [environment variables configuration](/dotnet/core/extensions/configuration-providers#environment-variable-configuration-provider).
207220
208-
If you run the application now, you'll see the following error, as you have not given any permission to this application in Key Vault.
221+
> [!NOTE]
222+
> For example, if the secret identifier is `https://msdocs-key-vault.vault.azure.net/secrets/cosmos-endpoint/69621c59ef5b4b7294b5def118921b07`, then the reference would be `@Microsoft.KeyVault(SecretUri=https://msdocs-key-vault.vault.azure.net/secrets/cosmos-endpoint/69621c59ef5b4b7294b5def118921b07)`.
223+
>
224+
> :::image type="content" source="media/access-secrets-from-keyvault/create-app-setting.png" alt-text="Screenshot of the Add/Edit application setting dialog with a new app setting referencing a key vault secret.":::
225+
>
226+
227+
1. Select **OK** to persist the new app setting
228+
229+
1. Select **New application setting** again. In the **Add/Edit application setting** dialog, enter the following information and then select **OK**:
230+
231+
| Setting | Description |
232+
| --- | --- |
233+
| **Name** | `CREDENTIALS__KEY` |
234+
| **Key** | Get the **secret identifier** for the **cosmos-readwrite-key** secret in your key vault that you created earlier in this tutorial. Enter the identifier in the following format: `@Microsoft.KeyVault(SecretUri=<secret-identifier>)`. |
209235

210-
:::image type="content" source="media/access-secrets-from-keyvault/app-deployed-without-access.png" alt-text="Screenshot of the error message displayed by an app deployed without access.":::
236+
1. Back on the **Configuration** page, select **Save** to update the app settings for the web app.
211237

212-
In this section, you register the application with Azure Active Directory and give permissions for the application to read the Key Vault.
238+
:::image type="content" source="media/access-secrets-from-keyvault/save-app-settings.png" alt-text="Screenshot of the Save option in the Configuration page's menu.":::
213239

214-
1. Navigate to the Azure portal, open the **Key Vault** you created in the previous section.
240+
1. Wait a few minutes for the web app to restart with the new app settings. At this point, the new app settings should indicate that they're a **Key vault Reference**.
215241

216-
2. Open **Access policies**, select **+Add New** find the web app you deployed, select permissions and select **OK**.
242+
:::image type="content" source="media/access-secrets-from-keyvault/app-settings-reference.png" lightbox="media/access-secrets-from-keyvault/app-settings-reference.png" alt-text="Screenshot of the Key vault Reference designation on two app settings in a web app.":::
217243

218-
:::image type="content" source="media/access-secrets-from-keyvault/add-access-policy.png" alt-text="Add access policy":::
244+
1. Select **Overview** from the navigation menu. Select **Browse** to see the app with populated credentials.
219245

220-
Now, if you run the application, you can read the secret from Key Vault.
246+
:::image type="content" source="media/access-secrets-from-keyvault/sample-web-app-populated.png" lightbox="media/access-secrets-from-keyvault/sample-web-app-populated.png" alt-text="Screenshot of the web application with valid Azure Cosmos DB for NoSQL account credentials.":::
221247

222-
:::image type="content" source="media/access-secrets-from-keyvault/app-deployed-with-access.png" alt-text="App deployed with secret":::
248+
## Next steps
223249

224-
Similarly, you can add a user to access the key Vault. You need to add yourself to the Key Vault by selecting **Access Policies** and then grant all the permissions you need to run the application from Visual studio. When this application is running from your desktop, it takes your identity.
250+
- To configure a firewall for Azure Cosmos DB, see [firewall support](how-to-configure-firewall.md) article.
251+
- To configure virtual network service endpoint, see [secure access by using VNet service endpoint](how-to-configure-vnet-service-endpoint.md) article.
Binary file not shown.
Binary file not shown.
14.2 KB
Loading
20.5 KB
Loading
3.14 KB
Loading
23.6 KB
Loading
19.1 KB
Loading
23.1 KB
Loading

0 commit comments

Comments
 (0)