2
2
title : Azure PowerShell - Secure WebHook delivery with Microsoft Entra user in Azure Event Grid
3
3
description : Describes how to deliver events to HTTPS endpoints protected by Microsoft Entra user using Azure Event Grid
4
4
ms.devlang : powershell
5
- ms.custom : devx-track-azurepowershell, has-azure-ad-ps-ref
5
+ ms.custom : has-azure-ad-ps-ref, azure-ad-ref-level-one-done
6
6
ms.topic : sample
7
- ms.date : 09/29/2021
7
+ ms.date : 02/02/2024
8
8
---
9
9
10
10
# Secure WebHook delivery with Microsoft Entra user in Azure Event Grid
@@ -15,15 +15,15 @@ Here are the high level steps from the script:
15
15
16
16
1 . Create a service principal for ** Microsoft.EventGrid** if it doesn't already exist.
17
17
1 . Create a role named ** AzureEventGridSecureWebhookSubscriber** in the ** Microsoft Entra app for your Webhook** .
18
- 1 . Add service principal of user who will be creating the subscription to the AzureEventGridSecureWebhookSubscriber role.
18
+ 1 . Add service principal of user who is creating the subscription to the AzureEventGridSecureWebhookSubscriber role.
19
19
1 . Add service principal of Microsoft.EventGrid to the AzureEventGridSecureWebhookSubscriber.
20
20
21
- ## Sample script - stable
21
+ ## Sample script
22
22
23
23
``` azurepowershell
24
24
# NOTE: Before run this script ensure you are logged in Azure by using "az login" command.
25
25
26
- $webhookAppObjectId = "[REPLACE_WITH_YOUR_ID]"
26
+ $webhookAppId = "[REPLACE_WITH_YOUR_ID]"
27
27
$eventSubscriptionWriterUserPrincipalName = "[REPLACE_WITH_USER_PRINCIPAL_NAME_OF_THE_USER_WHO_WILL_CREATE_THE_SUBSCRIPTION]"
28
28
29
29
# Start execution
@@ -33,10 +33,10 @@ try {
33
33
34
34
Function CreateAppRole([string] $Name, [string] $Description)
35
35
{
36
- $appRole = New-Object Microsoft.Open.AzureAD.Model.AppRole
36
+ $appRole = New-Object Microsoft.Graph.PowerShell.Models.MicrosoftGraphAppRole
37
37
$appRole.AllowedMemberTypes = New-Object System.Collections.Generic.List[string]
38
- $appRole.AllowedMemberTypes.Add( "Application") ;
39
- $appRole.AllowedMemberTypes.Add( "User") ;
38
+ $appRole.AllowedMemberTypes += "Application";
39
+ $appRole.AllowedMemberTypes += "User";
40
40
$appRole.DisplayName = $Name
41
41
$appRole.Id = New-Guid
42
42
$appRole.IsEnabled = $true
@@ -46,59 +46,59 @@ try {
46
46
return $appRole
47
47
}
48
48
49
- # Creates Azure Event Grid Azure AD Application if not exists
49
+ # Creates Azure Event Grid Microsoft Entra Application if not exists
50
50
# You don't need to modify this id
51
- # But Azure Event Grid Azure AD Application Id is different for different clouds
51
+ # But Azure Event Grid Microsoft Entra Application Id is different for different clouds
52
52
53
53
$eventGridAppId = "4962773b-9cdb-44cf-a8bf-237846a00ab7" # Azure Public Cloud
54
54
# $eventGridAppId = "54316b56-3481-47f9-8f30-0300f5542a7b" # Azure Government Cloud
55
- $eventGridRoleName = "AzureEventGridSecureWebhookSubscriber" # You don't need to modify this role name
56
- $eventGridSP = Get-AzureADServicePrincipal -Filter ("appId eq '" + $eventGridAppId + "'")
57
- if ($eventGridSP -match "Microsoft.EventGrid")
55
+ $eventGridSP = Get-MgServicePrincipal -Filter ("appId eq '" + $eventGridAppId + "'")
56
+ if ($eventGridSP.DisplayName -match "Microsoft.EventGrid")
58
57
{
59
- Write-Host "The Azure AD Application is already defined.`n"
58
+ Write-Host "The Event Grid Microsoft Entra Application is already defined.`n"
60
59
} else {
61
- Write-Host "Creating the Azure Event Grid Azure AD Application"
62
- $eventGridSP = New-AzureADServicePrincipal -AppId $eventGridAppId
60
+ Write-Host "Creating the Azure Event Grid Microsoft Entra Application"
61
+ $eventGridSP = New-MgServicePrincipal -AppId $eventGridAppId
63
62
}
64
63
65
- # Creates the Azure app role for the webhook Azure AD application
64
+ # Creates the Azure app role for the webhook Microsoft Entra application
65
+ $eventGridRoleName = "AzureEventGridSecureWebhookSubscriber" # You don't need to modify this role name
66
66
67
- $app = Get-AzureADApplication -ObjectId $webhookAppObjectId
67
+ $app = Get-MgApplication -ApplicationId $webhookAppObjectId
68
68
$appRoles = $app.AppRoles
69
69
70
- Write-Host "Azure AD App roles before addition of the new role..."
71
- Write-Host $appRoles
70
+ Write-Host "Microsoft Entra App roles before addition of the new role..."
71
+ Write-Host $appRoles.DisplayName
72
72
73
- if ($appRoles -match $eventGridRoleName)
73
+ if ($appRoles.DisplayName -match $eventGridRoleName)
74
74
{
75
75
Write-Host "The Azure Event Grid role is already defined.`n"
76
76
} else {
77
- Write-Host "Creating the Azure Event Grid role in Azure AD Application: " $webhookAppObjectId
77
+ Write-Host "Creating the Azure Event Grid role in Microsoft Entra Application: " $webhookAppObjectId
78
78
$newRole = CreateAppRole -Name $eventGridRoleName -Description "Azure Event Grid Role"
79
- $appRoles.Add( $newRole)
80
- Set-AzureADApplication -ObjectId $app.ObjectId -AppRoles $appRoles
79
+ $appRoles += $newRole
80
+ Update-MgApplication -ApplicationId $webhookAppObjectId -AppRoles $appRoles
81
81
}
82
82
83
- Write-Host "Azure AD App roles after addition of the new role..."
84
- Write-Host $appRoles
83
+ Write-Host "Microsoft Entra App roles after addition of the new role..."
84
+ Write-Host $appRoles.DisplayName
85
85
86
86
# Creates the user role assignment for the user who will create event subscription
87
87
88
- $servicePrincipal = Get-AzureADServicePrincipal -Filter ("appId eq '" + $app.AppId + "'")
88
+ $servicePrincipal = Get-MgServicePrincipal -Filter ("appId eq '" + $app.AppId + "'")
89
89
90
90
try
91
91
{
92
- Write-Host "Creating the Azure Ad App Role assignment for user: " $eventSubscriptionWriterUserPrincipalName
93
- $eventSubscriptionWriterUser = Get-AzureAdUser -ObjectId $eventSubscriptionWriterUserPrincipalName
92
+ Write-Host "Creating the Microsoft Entra App Role assignment for user: " $eventSubscriptionWriterUserPrincipalName
93
+ $eventSubscriptionWriterUser = Get-MgUser -UserId $eventSubscriptionWriterUserPrincipalName
94
94
$eventGridAppRole = $app.AppRoles | Where-Object -Property "DisplayName" -eq -Value $eventGridRoleName
95
- New-AzureADUserAppRoleAssignment -Id $eventGridAppRole .Id -ResourceId $servicePrincipal.ObjectId -ObjectId $eventSubscriptionWriterUser.ObjectId -PrincipalId $eventSubscriptionWriterUser.ObjectId
95
+ New-MgUserAppRoleAssignment -UserId $eventSubscriptionWriterUser .Id -PrincipalId $eventSubscriptionWriterUser.Id -ResourceId $servicePrincipal.Id -AppRoleId $eventGridAppRole.Id
96
96
}
97
97
catch
98
98
{
99
99
if( $_.Exception.Message -like '*Permission being assigned already exists on the object*')
100
100
{
101
- Write-Host "The Azure AD User Application role is already defined.`n"
101
+ Write-Host "The Microsoft Entra User Application role is already defined.`n"
102
102
}
103
103
else
104
104
{
@@ -107,15 +107,15 @@ try {
107
107
Break
108
108
}
109
109
110
- # Creates the service app role assignment for Event Grid Azure AD Application
110
+ # Creates the service app role assignment for Event Grid Microsoft Entra Application
111
111
112
112
$eventGridAppRole = $app.AppRoles | Where-Object -Property "DisplayName" -eq -Value $eventGridRoleName
113
- New-AzureADServiceAppRoleAssignment -Id $eventGridAppRole .Id -ResourceId $servicePrincipal.ObjectId -ObjectId $eventGridSP.ObjectId -PrincipalId $eventGridSP.ObjectId
113
+ New-MgServicePrincipalAppRoleAssignment -ServicePrincipalId $eventGridSP .Id -PrincipalId $eventGridSP.Id -ResourceId $servicePrincipal.Id -AppRoleId $eventGridAppRole.Id
114
114
115
115
# Print output references for backup
116
116
117
- Write-Host ">> Webhook's Azure AD Application Id: $($app.AppId)"
118
- Write-Host ">> Webhook's Azure AD Application ObjectId Id: $($app.ObjectId )"
117
+ Write-Host ">> Webhook's Microsoft Entra Application Id: $($app.AppId)"
118
+ Write-Host ">> Webhook's Microsoft Entra Application Object Id: $($app.Id )"
119
119
}
120
120
catch {
121
121
Write-Host ">> Exception:"
@@ -127,4 +127,4 @@ catch {
127
127
128
128
## Script explanation
129
129
130
- For more details refer to [ Secure WebHook delivery with Microsoft Entra ID in Azure Event Grid] ( ../secure-webhook-delivery.md )
130
+ For more information, see [ Secure WebHook delivery with Microsoft Entra ID in Azure Event Grid] ( ../secure-webhook-delivery.md ) .
0 commit comments