Skip to content

Commit e084842

Browse files
authored
Merge pull request #188849 from diberry/diberry/0216-app-service-mi
App service - auth - as app
2 parents 1175b39 + 0ffd530 commit e084842

17 files changed

+804
-600
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
---
2+
services: storage, app-service-web
3+
author: rwike77
4+
manager: CelesteDG
5+
ms.service: app-service-web
6+
ms.topic: include
7+
ms.workload: identity
8+
ms.date: 02/16/2022
9+
ms.author: ryanwi
10+
ms.reviewer: stsoneff
11+
ms.devlang: csharp azurecli
12+
ms.custom: azureday1
13+
---
14+
15+
## Clean up resources
16+
17+
If you're finished with this tutorial and no longer need the web app or associated resources, clean up the resources you created.
18+
19+
### Delete the resource group
20+
21+
In the [Azure portal](https://portal.azure.com), select **Resource groups** from the portal menu and select the resource group that contains your app service and app service plan.
22+
23+
Select **Delete resource group** to delete the resource group and all the resources.
24+
25+
:::image type="content" alt-text="Screenshot that shows deleting the resource group." source="../media/scenario-secure-app-clean-up-resources/delete-resource-group.png":::
26+
27+
This command might take several minutes to run.
28+
29+
### Delete the app registration
30+
31+
From the portal menu, select **Azure Active Directory** > **App registrations**. Then select the application you created.
32+
:::image type="content" alt-text="Screenshot that shows selecting app registration." source="../media/scenario-secure-app-clean-up-resources/select-app-registration.png":::
33+
34+
In the app registration overview, select **Delete**.
35+
:::image type="content" alt-text="Screenshot that shows deleting the app registration." source="../media/scenario-secure-app-clean-up-resources/delete-app-registration.png":::
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
---
2+
ms.topic: include
3+
ms.date: 10/26/2021
4+
5+
ms.reviewer: madsd
6+
ms.custom: devx-track-azurecli
7+
---
8+
9+
1. Configure the Cognitive Services secrets as app settings `CS_ACCOUNT_NAME` and `CS_ACCOUNT_KEY`.
10+
11+
```azurecli-interactive
12+
# Get subscription key for Cognitive Services resource
13+
csKey1=$(az cognitiveservices account keys list --resource-group $groupName --name $csResourceName --query key1 --output tsv)
14+
15+
az webapp config appsettings set --resource-group $groupName --name $appName --settings CS_ACCOUNT_NAME="$csResourceName" CS_ACCOUNT_KEY="$csKey1"
16+
````
17+
18+
1. In the browser, navigate to your deploy app at `<app-name>.azurewebsites.net` and try out the language detector with strings in various languages.
19+
20+
![Screenshot that shows deployed language detector app in App Service.](../../media/tutorial-connect-msi-key-vault/deployed-app.png)
21+
22+
If you look at the application code, you may notice the debug output for the detection results in the same font color as the background. You can see it by trying to highlight the white space directly below the result.
23+
24+
## Secure back-end connectivity
25+
26+
At the moment, connection secrets are stored as app settings in your App Service app. This approach is already securing connection secrets from your application codebase. However, any contributor who can manage your app can also see the app settings. In this step, you move the connection secrets to a key vault, and lock down access so that only you can manage it and only the App Service app can read it using its managed identity.
27+
28+
1. Create a key vault. Replace *\<vault-name>* with a unique name.
29+
30+
```azurecli-interactive
31+
# Save app name as variable for convenience
32+
vaultName=<vault-name>
33+
34+
az keyvault create --resource-group $groupName --name $vaultName --location $region --sku standard --enable-rbac-authorization
35+
```
36+
37+
The `--enable-rbac-authorization` parameter [sets Azure role-based access control (RBAC) as the permission model](../../../key-vault/general/rbac-guide.md#using-azure-rbac-secret-key-and-certificate-permissions-with-key-vault). This setting by default invalidates all access policies permissions.
38+
39+
1. Give yourself the *Key Vault Secrets Officer* RBAC role for the vault.
40+
41+
```azurecli-interactive
42+
vaultResourceId=$(az keyvault show --name $vaultName --query id --output tsv)
43+
myId=$(az ad signed-in-user show --query objectId --output tsv)
44+
az role assignment create --role "Key Vault Secrets Officer" --assignee-object-id $myId --assignee-principal-type User --scope $vaultResourceId
45+
```
46+
47+
1. Enable the system-assigned managed identity for your app, and give it the *Key Vault Secrets User* RBAC role for the vault.
48+
49+
```azurecli-interactive
50+
az webapp identity assign --resource-group $groupName --name $appName --scope $vaultResourceId --role "Key Vault Secrets User"
51+
```
52+
53+
1. Add the Cognitive Services resource name and subscription key as secrets to the vault, and save their IDs as environment variables for the next step.
54+
55+
```azurecli-interactive
56+
csResourceKVUri=$(az keyvault secret set --vault-name $vaultName --name csresource --value $csResourceName --query id --output tsv)
57+
csKeyKVUri=$(az keyvault secret set --vault-name $vaultName --name cskey --value $csKey1 --query id --output tsv)
58+
```
59+
60+
1. Previously, you set the secrets as app settings `CS_ACCOUNT_NAME` and `CS_ACCOUNT_KEY` in your app. Now, set them as [key vault references](../../app-service-key-vault-references.md) instead.
61+
62+
```azurecli-interactive
63+
az webapp config appsettings set --resource-group $groupName --name $appName --settings CS_ACCOUNT_NAME="@Microsoft.KeyVault(SecretUri=$csResourceKVUri)" CS_ACCOUNT_KEY="@Microsoft.KeyVault(SecretUri=$csKeyKVUri)"
64+
```
65+
66+
1. In the browser, navigate to `<app-name>.azurewebsites.net` again. If you get detection results back, then you're connecting to the Cognitive Services endpoint with key vault references.
67+
68+
Congratulations, your app is now connecting to Cognitive Services using secrets kept in your key vault, without any changes to your application code.
69+
70+
## Clean up resources
71+
72+
In the preceding steps, you created Azure resources in a resource group. If you don't expect to need these resources in the future, delete the resource group by running the following command in the Cloud Shell:
73+
74+
```azurecli-interactive
75+
az group delete --name $groupName
76+
```
77+
78+
This command may take a minute to run.
79+
80+
## Next steps
81+
82+
- [Tutorial: Isolate back-end communication with Virtual Network integration](../../tutorial-networking-isolate-vnet.md)
83+
- [Integrate your app with an Azure virtual network](../../overview-vnet-integration.md)
84+
- [App Service networking features](../../networking-features.md)
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
---
2+
ms.topic: include
3+
ms.date: 10/26/2021
4+
5+
ms.reviewer: madsd
6+
ms.custom: devx-track-azurecli
7+
---
8+
9+
[Azure App Service](../../overview.md) can use [managed identities](../../overview-managed-identity.md) to connect to back-end services without a connection string, which eliminates connection secrets to manage and keeps your back-end connectivity secure in a production environment. For back-end services that don't support managed identities and still requires connection secrets, you can use Key Vault to manage connection secrets. This tutorial uses Cognitive Services as an example to show you how it's done in practice. When you're finished, you have an app that makes programmatic calls to Cognitive Services, without storing any connection secrets inside App Service.
10+
11+
* [Sample application](https://github.com/Azure-Samples/app-service-language-detector)
12+
13+
> [!TIP]
14+
> Azure Cognitive Services do [support authentication through managed identities](../../../cognitive-services/authentication.md#authorize-access-to-managed-identities), but this tutorial uses the [subscription key authentication](../../../cognitive-services/authentication.md#authenticate-with-a-single-service-subscription-key) to demonstrate how you could connect to an Azure service that doesn't support managed identities from App Services.
15+
16+
![Architecture diagram for tutorial scenario.](../../media/tutorial-connect-msi-key-vault/architecture.png)
17+
18+
With this architecture:
19+
20+
- Connectivity to Key Vault is secured by managed identities
21+
- App Service accesses the secrets using [Key Vault references](../../app-service-key-vault-references.md) as app settings.
22+
- Access to the key vault is restricted to the app. App contributors, such as administrators, may have complete control of the App Service resources, and at the same time have no access to the Key Vault secrets.
23+
- If your application code already accesses connection secrets with app settings, no change is required.
24+
25+
What you will learn:
26+
27+
> [!div class="checklist"]
28+
> * Enable managed identities
29+
> * Use managed identities to connect to Key Vault
30+
> * Use Key Vault references
31+
> * Access Cognitive Services
32+
33+
## Prerequisites
34+
35+
Prepare your environment for the Azure CLI.
36+
37+
[!INCLUDE [azure-cli-prepare-your-environment-no-header.md](../../../../includes/azure-cli-prepare-your-environment-no-header.md)]
38+
39+
## Create app with connectivity to Cognitive Services
40+
41+
1. Create a resource group to contain all of your resources:
42+
43+
```azurecli-interactive
44+
# Save resource group name as variable for convenience
45+
groupName=myKVResourceGroup
46+
region=westeurope
47+
48+
az group create --name $groupName --location $region
49+
```
50+
51+
1. Create a Cognitive Services resource. Replace *\<cs-resource-name>* with a unique name of your choice.
52+
53+
```azurecli-interactive
54+
# Save resource name as variable for convenience.
55+
csResourceName=<cs-resource-name>
56+
57+
az cognitiveservices account create --resource-group $groupName --name $csResourceName --location $region --kind TextAnalytics --sku F0 --custom-domain $csResourceName
58+
```
59+
60+
> [!NOTE]
61+
> `--sku F0` creates a free tier Cognitive Services resource. Each subscription is limited to a quota of one free-tier `TextAnalytics` resource. If you're already over the quota, use `--sku S` instead.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
---
2+
services: microsoft-graph, app-service-web
3+
author: rwike77
4+
manager: CelesteDG
5+
6+
ms.service: app-service-web
7+
ms.topic: include
8+
ms.workload: identity
9+
ms.date: 01/21/2022
10+
ms.author: ryanwi
11+
ms.reviewer: stsoneff
12+
ms.custom: azureday1, devx-track-azurepowershell
13+
#Customer intent: As an application developer, I want to learn how to access data in Microsoft Graph by using managed identities.
14+
---
15+
16+
## Next steps
17+
18+
In this tutorial, you learned how to:
19+
20+
> [!div class="checklist"]
21+
>
22+
> * Create a system-assigned managed identity.
23+
> * Create a storage account and Blob Storage container.
24+
> * Access storage from a web app by using managed identities.
25+
26+
> [!div class="nextstepaction"]
27+
> [Tutorial: Isolate back-end communication with Virtual Network integration](../../tutorial-networking-isolate-vnet.md)
28+
29+
> [!div class="nextstepaction"]
30+
> [App Service accesses Microsoft Graph on behalf of the user](../../scenario-secure-app-access-microsoft-graph-as-user.md)
31+
32+
> [!div class="nextstepaction"]
33+
> [Map an existing custom DNS name to Azure App Service](../../app-service-web-tutorial-custom-domain.md)

0 commit comments

Comments
 (0)