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/cosmos-db/access-secrets-from-keyvault.md
+54-27Lines changed: 54 additions & 27 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -20,13 +20,13 @@ ms.date: 11/07/2022
20
20
> [!IMPORTANT]
21
21
> 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.
22
22
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.
24
24
25
25
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.
26
26
27
27
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.
28
28
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.
30
30
31
31
In this tutorial, you learn how to:
32
32
@@ -89,7 +89,7 @@ First, create a new key vault to store your API for NoSQL credentials.
89
89
90
90
## Add Azure Cosmos DB access keys to the Key Vault
91
91
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.
93
93
94
94
1. Select **Go to resource** to go to the Azure Key Vault resource page.
95
95
@@ -173,52 +173,79 @@ In this section, create a new Azure Web App, deploy a sample application, and th
173
173
174
174
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.
175
175
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.":::
177
185
178
186
## Inject Azure Key Vault secrets as Azure Web App app settings
179
187
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.
181
189
182
-
1.
190
+
1. Return to the key vault page in the Azure portal. Select **Access policies** from the navigation menu.
183
191
192
+
1. On the **Access policies** page, select **Create** from the menu.
184
193
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.":::
186
195
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**.
189
197
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.":::
191
199
192
-
Old stuff
200
+
1. On the **Principal** tab, select the name of the web app you created earlier in this tutorial. Select **Next**.
193
201
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.":::
194
203
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**.
196
206
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**.
1. Return to the web app page in the Azure portal. Select **Configuration** from the navigation menu.
200
210
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:
203
212
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>)`. |
205
217
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).
207
220
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>)`. |
209
235
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.
211
237
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.":::
213
239
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**.
215
241
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.":::
1. Select **Overview** from the navigation menu. Select **Browse** to see the app with populated credentials.
219
245
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.":::
221
247
222
-
:::image type="content" source="media/access-secrets-from-keyvault/app-deployed-with-access.png" alt-text="App deployed with secret":::
248
+
## Next steps
223
249
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.
0 commit comments