@@ -10,7 +10,113 @@ ms.date: 09/29/2021
10
10
11
11
## Sample script - stable
12
12
13
- [ !code-powershell[ main] ( ../../../powershell_scripts/event-grid/secure-webhook-delivery/secure-webhook-azure-ad-app.ps1 " Register Azure AD App ")]
13
+ ``` azurepowershell
14
+ # NOTE: Before run this script ensure you are logged in Azure by using "az login" command.
15
+
16
+ $webhookAppObjectId = "[REPLACE_WITH_YOUR_ID]"
17
+ $eventSubscriptionWriterAppId = "[REPLACE_WITH_YOUR_ID]"
18
+
19
+ # Start execution
20
+ try {
21
+
22
+ # Creates an application role of given name and description
23
+
24
+ Function CreateAppRole([string] $Name, [string] $Description)
25
+ {
26
+ $appRole = New-Object Microsoft.Open.AzureAD.Model.AppRole
27
+ $appRole.AllowedMemberTypes = New-Object System.Collections.Generic.List[string]
28
+ $appRole.AllowedMemberTypes.Add("Application");
29
+ $appRole.AllowedMemberTypes.Add("User");
30
+ $appRole.DisplayName = $Name
31
+ $appRole.Id = New-Guid
32
+ $appRole.IsEnabled = $true
33
+ $appRole.Description = $Description
34
+ $appRole.Value = $Name;
35
+
36
+ return $appRole
37
+ }
38
+
39
+ # Creates Azure Event Grid Azure AD Application if not exists
40
+
41
+ $eventGridAppId = "4962773b-9cdb-44cf-a8bf-237846a00ab7" # You don't need to modify this id
42
+ $eventGridRoleName = "AzureEventGridSecureWebhookSubscriber" # You don't need to modify this role name
43
+ $eventGridSP = Get-AzureADServicePrincipal -Filter ("appId eq '" + $eventGridAppId + "'")
44
+ if ($eventGridSP -match "Microsoft.EventGrid")
45
+ {
46
+ Write-Host "The Azure AD Application is already defined.`n"
47
+ } else {
48
+ Write-Host "Creating the Azure Event Grid Azure AD Application"
49
+ $eventGridSP = New-AzureADServicePrincipal -AppId $eventGridAppId
50
+ }
51
+
52
+ # Creates the Azure app role for the webhook Azure AD application
53
+
54
+ $app = Get-AzureADApplication -ObjectId $webhookAppObjectId
55
+ $appRoles = $app.AppRoles
56
+
57
+ Write-Host "Azure AD App roles before addition of the new role..."
58
+ Write-Host $appRoles
59
+
60
+ if ($appRoles -match $eventGridRoleName)
61
+ {
62
+ Write-Host "The Azure Event Grid role is already defined.`n"
63
+ } else {
64
+ Write-Host "Creating the Azure Event Grid role in Azure AD Application: " $webhookAppObjectId
65
+ $newRole = CreateAppRole -Name $eventGridRoleName -Description "Azure Event Grid Role"
66
+ $appRoles.Add($newRole)
67
+ Set-AzureADApplication -ObjectId $app.ObjectId -AppRoles $appRoles
68
+ }
69
+
70
+ Write-Host "Azure AD App roles after addition of the new role..."
71
+ Write-Host $appRoles
72
+
73
+ # Creates the user role assignment for the app that will create event subscription
74
+
75
+ $servicePrincipal = Get-AzureADServicePrincipal -Filter ("appId eq '" + $app.AppId + "'")
76
+ $eventSubscriptionWriterSP = Get-AzureADServicePrincipal -Filter ("appId eq '" + $eventSubscriptionWriterAppId + "'")
77
+
78
+ if ($null -eq $eventSubscriptionWriterSP)
79
+ {
80
+ Write-Host "Create new Azure AD Application"
81
+ $eventSubscriptionWriterSP = New-AzureADServicePrincipal -AppId $eventSubscriptionWriterAppId
82
+ }
83
+
84
+ try
85
+ {
86
+ Write-Host "Creating the Azure AD Application role assignment: " $eventSubscriptionWriterAppId
87
+ $eventGridAppRole = $app.AppRoles | Where-Object -Property "DisplayName" -eq -Value $eventGridRoleName
88
+ New-AzureADServiceAppRoleAssignment -Id $eventGridAppRole.Id -ResourceId $servicePrincipal.ObjectId -ObjectId $eventSubscriptionWriterSP.ObjectId -PrincipalId $eventSubscriptionWriterSP.ObjectId
89
+ }
90
+ catch
91
+ {
92
+ if( $_.Exception.Message -like '*Permission being assigned already exists on the object*')
93
+ {
94
+ Write-Host "The Azure AD Application role is already defined.`n"
95
+ }
96
+ else
97
+ {
98
+ Write-Error $_.Exception.Message
99
+ }
100
+ Break
101
+ }
102
+
103
+ # Creates the service app role assignment for Event Grid Azure AD Application
104
+
105
+ $eventGridAppRole = $app.AppRoles | Where-Object -Property "DisplayName" -eq -Value $eventGridRoleName
106
+ New-AzureADServiceAppRoleAssignment -Id $eventGridAppRole.Id -ResourceId $servicePrincipal.ObjectId -ObjectId $eventGridSP.ObjectId -PrincipalId $eventGridSP.ObjectId
107
+
108
+ # Print output references for backup
109
+
110
+ Write-Host ">> Webhook's Azure AD Application Id: $($app.AppId)"
111
+ Write-Host ">> Webhook's Azure AD Application ObjectId Id: $($app.ObjectId)"
112
+ }
113
+ catch {
114
+ Write-Host ">> Exception:"
115
+ Write-Host $_
116
+ Write-Host ">> StackTrace:"
117
+ Write-Host $_.ScriptStackTrace
118
+ }
119
+ ```
14
120
15
121
## Script explanation
16
122
0 commit comments