Skip to content

Find-MgGraphPermission #809

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Aug 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,8 @@ _pkginfo.txt
ClientBin/
~$*
*~
.#*
*#
*.dbmdl
*.dbproj.schemaview
*.jfm
Expand Down
6 changes: 6 additions & 0 deletions samples/2-ConnectToGraph.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ Connect-Graph
# Try to Get-User
Get-MgUser

# Search for delegated permissions related to sites
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@peombwa goal here is to show you can use the new command to find permissions, and also to get to the permissions reference help.

Find-MgGraphPermission sites -PermissionType Delegated

# Grant more permissions
Connect-Graph -Scopes "User.Read","User.ReadWrite.All","Mail.ReadWrite",`
"Directory.Read.All","Chat.ReadWrite", "People.Read", `
Expand All @@ -17,3 +20,6 @@ Connect-Graph -Scopes "User.Read","User.ReadWrite.All","Mail.ReadWrite",`

# Forget all access tokens
Disconnect-Graph

# Launch detailed permissions documentation
Get-Help Find-MgGraphPermission -Online
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@msewaweru , this works because the .LINKS field in the comment help has exactly one entry, and it's the URI to the permissions docs. That means we can't provide references to other commands in .LINKS though -- hope that's ok.

Copy link
Contributor

@georgend georgend Aug 10, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@maisarissi given the pointer by @adamedx it means we cannot use related links as we had intended to store Survey Links.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You may want to sync with someone from the https://github.com/powershell/powershell or other experts on dcs + PowerShell project to confirm the behavior I observed.

Copy link

@maisarissi maisarissi Aug 11, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there is such a limitation they should call Related Link instead of Related Links (plural) LOL
I will try to sync with someone to confirm this behavior.

35 changes: 27 additions & 8 deletions samples/9-Applications.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ $app3 = New-MgApplication -displayName "ImplicitWebApp" `
}

# Create an registration for an ASP.NET Web App
$scopeId_UserRead = Find-MgGraphPermission User.Read -ExactMatch -PermissionType Delegated | Select-Object -ExpandProperty Id
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@peombwa you'll want to sign off on having the sample changed to use the new command.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good! This is a good example of how Find-MgGraphPermission can be used to aid in scope discovery.

$app = New-MgApplication -displayName "AspNetWebApp" `
-Web @{
RedirectUris = "https://localhost:5001/signin-oidc"; `
Expand All @@ -36,7 +37,7 @@ $app = New-MgApplication -displayName "AspNetWebApp" `
-RequiredResourceAccess @{ ResourceAppId = "00000003-0000-0000-c000-000000000000"
ResourceAccess = @(
@{
Id = "e1fe6dd8-ba31-4d61-89e7-88639da4683d"
Id = $scopeId_UserRead
Type = "Scope"
}
)
Expand All @@ -56,7 +57,7 @@ $createAppParams = @{
ResourceAppId = "00000003-0000-0000-c000-000000000000"
ResourceAccess = @(
@{
Id = "e1fe6dd8-ba31-4d61-89e7-88639da4683d"
Id = $scopeId_UserRead
Type = "Scope"
}
)
Expand All @@ -76,18 +77,36 @@ $Certificate = Get-ChildItem -Path "Cert:\CurrentUser\My\$CertificateThumbprint"
# Graph resource Id
$GraphResourceId = "00000003-0000-0000-c000-000000000000"

# Graph permissions constants
$UserReadAll = @{ Id = "df021288-bdef-4463-88db-98f22de89214"; Type = "Role" }
$GroupReadAll = @{ Id = "5b567255-7703-4780-807c-7be8301ae99b"; Type = "Role" }
$MailboxSettingsRead = @{ Id = "40f97065-369a-49f4-947c-6a255697ae91"; Type = "Role" }
$MailSend = @{ Id = "b633e1c5-b582-4048-a93e-9f11b44c7e96"; Type = "Role" }
# Show friendly Graph permission names given their unique identifiers
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another big change to the sample @peombwa

Find-MgGraphPermission | Where-Object Id -in @(
'df021288-bdef-4463-88db-98f22de89214'
'5b567255-7703-4780-807c-7be8301ae99b'
'40f97065-369a-49f4-947c-6a255697ae91'
'b633e1c5-b582-4048-a93e-9f11b44c7e96'
)

# Create an application registration.
$requiredPermissions = 'Group.Read.All', 'Mail.Send', 'MailboxSettings.Read', 'User.Read.All' |
Find-MgGraphPermission -ExactMatch -PermissionType Application

$resourceAccess = foreach ( $permission in $requiredPermissions ) {
@{ Id = $permission.Id; Type = 'Role' }
}

$AppName = "ScriptedGraphPSApp"
$app4 = New-MgApplication -"ClientCredentialApp" $AppName `
-SignInAudience "AzureADMyOrg" `
-RequiredResourceAccess @{ ResourceAppId = $graphResourceId; ResourceAccess = $UserReadAll, $GroupReadAll, $MailboxSettingsRead, $MailSend } `
-RequiredResourceAccess @{ ResourceAppId = $graphResourceId; ResourceAccess = $resourceAccess } `
-KeyCredentials @(@{ Type = "AsymmetricX509Cert"; Usage = "Verify"; Key= $Certificate.RawData })

# Create corresponding service principal.
New-MgServicePrincipal -AppId $app4.AppId

# Show permissions assigned to the application in the organization
# using friendly permission names instead of just the unique identifiers
$servicePrincipal4 = Get-MgServicePrincipal -Filter "appId eq '$($app4.AppId)'"
Get-MgServicePrincipalAppRoleAssignment -ServicePrincipalId $servicePrincipal4.id |
Select-Object appRoleId |
Find-MgGraphPermission


Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Version>1.6.0</Version>
<Version>1.7.0</Version>
<LangVersion>7.1</LangVersion>
<TargetFramework>netstandard2.0</TargetFramework>
<OutputType>Library</OutputType>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,50 @@
</TableRowEntries>
</TableControl>
</View>

<View>
<Name>Permission</Name>
<ViewSelectedBy>
<TypeName>Microsoft.Graph.Custom.Permission</TypeName>
</ViewSelectedBy>
<GroupBy>
<PropertyName>PermissionType</PropertyName>
<Label>PermissionType</Label>
</GroupBy>
<TableControl>
<TableHeaders>
<TableColumnHeader>
<Label>Id</Label>
</TableColumnHeader>
<TableColumnHeader>
<Label>Consent</Label>
</TableColumnHeader>
<TableColumnHeader>
<Label>Name</Label>
</TableColumnHeader>
<TableColumnHeader>
<Label>Description</Label>
</TableColumnHeader>
</TableHeaders>
<TableRowEntries>
<TableRowEntry>
<TableColumnItems>
<TableColumnItem>
<PropertyName>Id</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>Consent</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>Name</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>Description</PropertyName>
</TableColumnItem>
</TableColumnItems>
</TableRowEntry>
</TableRowEntries>
</TableControl>
</View>
</ViewDefinitions>
</Configuration>
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0"?>
<package>
<metadata>
<version>1.4.2</version>
<version>1.7.0</version>
<id>Microsoft.Graph.Authentication</id>
<description>Microsoft Graph PowerShell authentication module</description>
<authors>Microsoft</authors>
Expand All @@ -24,6 +24,7 @@
<file src="artifacts\Microsoft.Graph.Authentication.Core.dll" />
<file src="artifacts\Microsoft.Graph.Core.dll" />
<file src="artifacts\StartupScripts\*" target="StartupScripts" />
<file src="artifacts\custom\" target="custom" />
<file src="artifacts\Dependencies\Newtonsoft.Json.dll" target="Dependencies" />
<file src="artifacts\Dependencies\Microsoft.Graph.Auth.dll" target="Dependencies" />
<file src="artifacts\Dependencies\Microsoft.IdentityModel.JsonWebTokens.dll" target="Dependencies" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#
# Generated by: Microsoft
#
# Generated on: 3/12/2021
# Generated on: 8/4/2021
#

@{
Expand All @@ -12,7 +12,7 @@
RootModule = './Microsoft.Graph.Authentication.psm1'

# Version number of this module.
ModuleVersion = '1.6.0'
ModuleVersion = '1.7.0'

# Supported PSEditions
CompatiblePSEditions = 'Core', 'Desktop'
Expand Down Expand Up @@ -63,13 +63,13 @@ DotNetFrameworkVersion = '4.7.2'
# TypesToProcess = @()

# Format files (.ps1xml) to be loaded when importing this module
FormatsToProcess = './Microsoft.Graph.Authentication.format.ps1xml'
FormatsToProcess = 'Microsoft.Graph.Authentication.format.ps1xml'

# Modules to import as nested modules of the module specified in RootModule/ModuleToProcess
# NestedModules = @()

# Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export.
FunctionsToExport = @()
FunctionsToExport = 'Find-MgGraphPermission'

# Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export.
CmdletsToExport = 'Connect-MgGraph', 'Disconnect-MgGraph', 'Get-MgContext',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ $null = Import-Module -Name (Join-Path $PSScriptRoot 'Microsoft.Graph.Authentica

if (Test-Path -Path "$PSScriptRoot\StartupScripts" -ErrorAction Ignore)
{
Get-ChildItem "$PSScriptRoot\StartupScripts" -ErrorAction Stop | ForEach-Object {
Get-ChildItem "$PSScriptRoot\StartupScripts" -Filter *.ps1 -ErrorAction Stop | ForEach-Object {
. $_.FullName
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# ------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. See License in the project root for license information.
# ------------------------------------------------------------------------------

# Load custom commands
$customScriptCommandDirItem = Get-Item $PSScriptRoot -ErrorAction Ignore
if ( $customScriptCommandDirItem ) {
$customScriptCommandDir = join-path $customScriptCommandDirItem.FullName ../custom

Get-ChildItem $customScriptCommandDir -Filter *.ps1 -ErrorAction Stop | ForEach-Object {
. $_.FullName
}
}

# Export custom script commands without removing the
# binary cmdlets. Custom script commands are functions,
# the cmdlets are.. cmdlets. We must explicitly specify
# both functions and cmdlets at export; if only one of
# these classes is specified, nothing of the other
# class will be exported.
Export-ModuleMember -Function * -Cmdlet *
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just an FYI Exporting module members like this will suppress currently exported cmdlet aliases such as Connect-Graph. The Pester tests to confirm this can be found here. I've fixed this in my PR by adding Get-ModuleCmdlet and Get-ScriptCmdlet helper cmdlets to aid in exporting functions, cmdlets, and aliases.

This will be fixed #816 when we merge the 2 PRs.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other than the issue highlighted here (which has been fixed by PR #816), everything else looks good to me!

Awesome, thank you @peombwa! What are the next steps? It sounds like this is in your queue to merge and you'll be the person to handle merging this (along with the related PR's)?

Copy link
Member

@peombwa peombwa Aug 12, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@adamedx, That's right. I'll merge the PR later today (to give others time to review it) then schedule everything for a 1.7.0 release.

@FehintolaObafemi, thank you for the excellent contribution!! I'm quite sure our customers will find this command valuable for their scripts.


17 changes: 11 additions & 6 deletions src/Authentication/Authentication/build-module.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,21 @@ if ($LastExitCode -ne 0) {

# Ensure out directory exists and is clean.
Remove-Item -Path $outDir -Recurse -ErrorAction Ignore
New-Item -Path $outDir -ItemType Directory
New-Item -Path $outDeps -ItemType Directory
New-Item -Path $outCore -ItemType Directory
New-Item -Path $outDesktop -ItemType Directory
New-Item -Path $outDir -ItemType Directory | out-null
New-Item -Path $outDeps -ItemType Directory | out-null
New-Item -Path $outCore -ItemType Directory | out-null
New-Item -Path $outDesktop -ItemType Directory | out-null

# Copy manifest.
Copy-Item -Path "$cmdletsSrc/$ModulePrefix.$ModuleName.format.ps1xml" -Destination $outDir
Copy-Item -Path "$cmdletsSrc/$ModulePrefix.$ModuleName.psm1" -Destination $outDir
Copy-Item -Path "$cmdletsSrc/$ModulePrefix.$ModuleName.psd1" -Destination $outDir
Copy-Item -Path "$cmdletsSrc/StartupScripts" -Recurse -Destination $outDir
Copy-Item -Path "$cmdletsSrc/StartupScripts" -Filter *.ps1 -Recurse -Destination $outDir

# Copy custom commands

Copy-Item -Path "$cmdletsSrc/custom" -Filter *.ps1 -Recurse -Destination $outDir
Copy-Item -Path "$cmdletsSrc/custom" -Filter *.json -Recurse -Destination $outDir -Force

# Core assemblies to include with cmdlets (Let PowerShell load them).
$CoreAssemblies = @('Microsoft.Graph.Authentication.Core', 'Microsoft.Graph.Core')
Expand All @@ -114,4 +119,4 @@ Get-ChildItem -Path "$cmdletsSrc/bin/$Configuration/$netStandard/publish/" |
Where-Object { -not $Deps.Contains($_.Name) -and $_.Extension -in $copyExtensions } |
ForEach-Object { Copy-Item -Path $_.FullName -Destination $outDir }

Write-Host -ForegroundColor Green '-------------Done-------------'
Write-Host -ForegroundColor Green '-------------Done-------------'
Loading