-
Notifications
You must be signed in to change notification settings - Fork 2
/
Grant-MIGraphPermission.ps1
287 lines (235 loc) · 12.3 KB
/
Grant-MIGraphPermission.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
<#PSScriptInfo
.VERSION 1.0.6
.GUID 0b801f6d-e4f2-4968-a5d2-d959dc0dd7c5
.AUTHOR Elliot Huffman
.COMPANYNAME
.COPYRIGHT Elliot Huffman 2022
.TAGS Azure AD AAD IAM Identity_And_Access_Management MI Managed_Identity
.LICENSEURI https://github.com/elliot-huffman/Powershell-Doodads/blob/main/LICENSE
.PROJECTURI
.ICONURI
.EXTERNALMODULEDEPENDENCIES Microsoft.Graph.Authentication,Microsoft.Graph.Applications
.REQUIREDSCRIPTS
.EXTERNALSCRIPTDEPENDENCIES
.RELEASENOTES
1.0.6: Change to Microsoft.Graph.Beta to support new features.
1.0.5: Disable access token validation since it is not working with Install-PSM. Will be re-enabled later.
1.0.4: Fix a bug where the Get-MgServicePrincipal changed requiring the use of a switch type for the 'All' parameter.
.PRIVATEDATA
#>
#Requires -Module Microsoft.Graph.Authentication
#Requires -Module Microsoft.Graph.Beta.Applications
# Ensure the appropriate pre-requirements for the script
<#
.SYNOPSIS
Grants Graph API Permissions to Managed Identity Service Principals
.DESCRIPTION
Grants API permissions to managed identities. The default setting is the MS Graph API but other APIs registered in Azure AD are supported. Such as MDE, EXO, etc.
Returns false on failure
.EXAMPLE
PS C:\> Grant-MIGraphPermission
Launches the GUI to walk the user through picking out the API permissions to grant to a Managed Identity
.PARAMETER CLIMode
This switch forces the suppression of GUI elements and forces headless mode.
This is useful if used in an automation system where a GUI will gum up the works.
Errors will be thrown if a required param is not specified in CLI only mode.
.PARAMETER ObjectID
This is the identity that is going to have the permission granted to it.
By default the GUI returns a list of managed identities however this can be any service principal.
.PARAMETER PermissionName
This is the display name of the permission the be granted to the target ObjectID.
.PARAMETER GraphServicePrincipalID
This is the GUID of the service principal that is the permissions provider.
By default the service provider is the Microsoft Graph API, however you can grant permissions on other providers too by providing their GUID.
Common providers are as below:
- Microsoft Graph API: 00000003-0000-0000-c000-000000000000
- Defender for Endpoint: fc780465-2017-40d4-a0c5-307022471b92
- Defender for Cloud Apps: 05a65629-4c1b-48c1-a78b-804c4abdd4af
- Exchange Online: 00000002-0000-0ff1-ce00-000000000000
- SharePoint Online: 00000003-0000-0ff1-ce00-000000000000
- Yammer: 00000005-0000-0ff1-ce00-000000000000
- Universal Print: da9b70f6-5323-4ce6-ae5c-88dcc5082966
.PARAMETER AccessToken
This parameter is used to pass an access token into this script rather than going through the authentication process via GUI.
This is useful for fully automated processes that are able to provide their own access tokens and run this script without human interaction.
The audience should be "https://graph.microsoft.com" for the token, otherwise it won't work.
There is input validation to help catch access tokens that don't have the correct audience.
The access token should minimally have the scopes (permissions) of 'Directory.Read.All', 'AppRoleAssignment.ReadWrite.All', and 'Application.ReadWrite.All'.
Scopes that have these basic set of permissions or greater will work (e.g. Global Admin).
Example use cases:
- CI/CD Pipeline
- Dot Sourced Loading this script from another
.INPUTS
Switch
System.GUID
System.GUID[]
System.String
System.String[]
.OUTPUTS
System.Boolean
.NOTES
This script requires the Microsoft.Graph.Authentication and Microsoft.Graph.Applications modules to be installed before execution.
If the GUI mode is to be used, ISE must be installed as some Windows Forms components are included in ISE that this script uses (Out-GridView).
This script requires the minimum Azure AD permissions of:
- Directory.Read.All
- AppRoleAssignment.ReadWrite.All
- Application.ReadWrite.All
These permission may be granted by a more widely scoped permission such as "Global admin" or "Directory.ReadWrite.All", etc..
#>
# Cmdlet bind script so that it can perform advanced operations
[CmdletBinding(
DefaultParameterSetName = 'GUI Mode',
SupportsShouldProcess = $true
)]
# Initialize the parameters for the script
param(
[Parameter(
Mandatory = $true,
Position = 0,
ParameterSetName = 'CLI Mode'
)]
[Switch]$CLIMode,
[Parameter(
Mandatory = $true,
Position = 1,
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true,
ParameterSetName = 'CLI Mode'
)]
[Parameter(ParameterSetName = 'GUI Mode')]
[ValidateNotNullOrEmpty()]
[System.GUID[]]$ObjectID,
[Parameter(
Mandatory = $true,
Position = 2,
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true,
ParameterSetName = 'CLI Mode'
)]
[Parameter(ParameterSetName = 'GUI Mode')]
[ValidateNotNullOrEmpty()]
[System.String[]]$PermissionName,
[ValidateNotNullOrEmpty()]
[System.GUID]$GraphServicePrincipalID = '00000003-0000-0000-c000-000000000000',
# [ValidateScript({ ([System.Text.Encoding]::ASCII.GetString([System.Convert]::FromBase64String(($_ -split '\.')[1])) | ConvertFrom-Json).aud -eq 'https://graph.microsoft.com' })]
[System.Security.SecureString]$AccessToken
)
# Initialize processing
begin {
# Stop execution on error
$ErrorActionPreference = 'Stop'
# Write Verbose info
Write-Verbose -Message 'Logging into Azure AD'
# Check if an access token has been provided
if ($null -ne $AccessToken) {
# Log into the Graph API using the provided access token
Connect-MgGraph -AccessToken $AccessToken
} else {
# Log into the Graph API using Azure AD authentication
Connect-MgGraph -Scopes 'Directory.Read.All', 'AppRoleAssignment.ReadWrite.All', 'Application.ReadWrite.All'
}
# Write Verbose info
Write-Verbose -Message 'Calculating GUI or CLI mode'
# Check to see if GUI mode is forced or necessary
if ($CLIMode) {
# If CLI mode is $true, the operator is forcing the execution of no GUI in headless only
[System.Boolean]$GUIMode = $false
} elseif (!$ObjectID -or !$PermissionName) {
# If the ObjectID or PermissionName parameters are not specified and the script is not forced CLI only, GUI is implied.
[System.Boolean]$GUIMode = $true
} else {
# If the parameters are present as expected, set the GUI mode to be off as no user selection is necessary.
[System.Boolean]$GUIMode = $false
}
}
# Process each object passed on the command line
process {
# Write Verbose info
Write-Verbose -Message 'Getting an instance of the Graph API App Service Principal'
# Get the GraphAPI instance
[Microsoft.Graph.Beta.PowerShell.Models.MicrosoftGraphServicePrincipal]$GraphAppSP = Get-MgBetaServicePrincipal -Filter "AppID eq '$GraphServicePrincipalID'"
# Write debug info
Write-Debug -Message "$(Get-Date -Format 'HH:mm:ss') - Graph API SP Info:"
Write-Debug -Message "$(Get-Date -Format 'HH:mm:ss') - `$GraphAppSP: $GraphAppSP"
# If in GUI mode
if ($GUIMode) {
# Write Verbose info
Write-Verbose -Message 'Getting a list of all managed identities and render it in a picker dialog for the end user to select one.'
# Get a list of Managed Identities and make the user select one of them
[Microsoft.Graph.Beta.PowerShell.Models.MicrosoftGraphServicePrincipal[]]$SelectedPrincipalList = Get-MgBetaServicePrincipal -Filter "ServicePrincipalType eq 'ManagedIdentity'" -All | Out-GridView -Title 'Select the Managed Identity to Assign Permission' -OutputMode 'Multiple'
} else {
# Write Verbose info
Write-Verbose -Message 'Getting the specified service principal.'
# Loop through each ID specified and save the results into a new list.
foreach ($GUID in $ObjectID) {
# Pull the specified Object ID's graph object
[Microsoft.Graph.Beta.PowerShell.Models.MicrosoftGraphServicePrincipal[]]$SelectedPrincipalList += Get-MgBetaServicePrincipal -ServicePrincipalId $GUID
}
}
# Write debug info
Write-Debug -Message "$(Get-Date -Format 'HH:mm:ss') - Selected Principal List:"
Write-Debug -Message "$(Get-Date -Format 'HH:mm:ss') - `$SelectedPrincipal: $SelectedPrincipalList"
# Write Verbose info
Write-Verbose -Message 'Validating principal selection was successful'
# Throw an error and end execution if the end user doesn't select an object
if (($null -eq $SelectedPrincipalList) -or ($SelectedPrincipalList.Count -eq 0)) {
# Write an error to the console
Write-Error -Message 'No principals were selected successfully.
If GUI was used, this usually indicates that the end user closed the dialog.
If CLI was used, ths was most likely due to an incorrect GUID.'
# Return false to the caller to indicate failure
return $false
}
# Route execution based on GUI mode
if ($GUIMode) {
# Write Verbose info
Write-Verbose -Message 'Getting a list of all app roles/permissions and render it in a picker dialog for the end user to select one.'
# Get the specified permission that needs to be assigned
[Microsoft.Graph.Beta.PowerShell.Models.MicrosoftGraphAppRole[]]$AppRoleList = $GraphAppSP.AppRoles | Out-GridView -Title 'Select the Permission to Assign' -OutputMode 'Multiple'
} else {
# Write Verbose info
Write-Verbose -Message 'Getting the specified app role/permission'
# Loop through each permission requested and enrich the permission provided with system context
foreach ($RoleName in $PermissionName) {
# Add each context instance to the app role list
[Microsoft.Graph.Beta.PowerShell.Models.MicrosoftGraphAppRole[]]$AppRoleList += $GraphAppSP.AppRoles | Where-Object -FilterScript { $_.Value -eq $RoleName }
}
}
# Write debug info
Write-Debug -Message "$(Get-Date -Format 'HH:mm:ss') - List of selected app roles/permissions:"
Write-Debug -Message "$(Get-Date -Format 'HH:mm:ss') - `$AppRoleList: $AppRoleList"
# Write Verbose info
Write-Verbose -Message 'Validating role selection was successful'
# Throw an error and end execution if the end user doesn't select an object
if (($null -eq $AppRoleList) -or ($AppRoleList.Count -eq 0)) {
# Write an error to the console
Write-Error -Message 'No API roles were selected successfully.
If GUI was used, this usually indicates that the end user closed the dialog.
If CLI was used, ths was most likely due to an incorrect permission name being specified.'
# Return false to the caller to indicate failure
return $false
}
# Write Verbose info
Write-Verbose -Message 'Assigning the specified permission to the specified principal'
# Loop through each principal specified and perform the specified role assignment
foreach ($Principal in $SelectedPrincipalList) {
# Loop through each of the selected app roles and assign the role to the specified principal
foreach ($Role in $AppRoleList) {
# Simulate the result if asked to simulate
if ($PSCmdlet.ShouldProcess('Selected Service Principal', "Grant $($Role.Value)")) {
# Write debug info
Write-Debug -Message "$(Get-Date -Format 'HH:mm:ss') - Pre-Assignment Variable Dump:"
Write-Debug -Message "$(Get-Date -Format 'HH:mm:ss') - `$Principal.ObjectId: $($Principal.Id)"
Write-Debug -Message "$(Get-Date -Format 'HH:mm:ss') - `$GraphAppSP.ObjectId: $($GraphAppSP.Id)"
Write-Debug -Message "$(Get-Date -Format 'HH:mm:ss') - `$Role.Id: $($Role.Id)"
# Assign the Graph API permission to the specified service principal
New-MgBetaServicePrincipalAppRoleAssignment -PrincipalId $Principal.Id -ServicePrincipalId $Principal.Id -AppRoleId $Role.Id -ResourceId $GraphAppSP.Id
}
}
}
}
# Run the necessary cleanup commands after processing is complete
end {
# Log out of the session
Disconnect-MgGraph
}