Repository navigation
feat(entra): add MT.1276 hard deletion protection check #2782
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
base: main
Are you sure you want to change the base?
Changes from all commits
988062a
84ac1c8
a6b8068
1877065
7039aee
26bfd19
ba5cb4e
f2a3b8b
9f76ca3
369e32f
4245060
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| function Format-MtEntraHardDeletionTable { | ||
| <# | ||
| .SYNOPSIS | ||
| Formats Hard Deletion Protection objects as a Markdown table, capped at a number of rows. | ||
|
|
||
| .DESCRIPTION | ||
| Builds a table with the columns Type, Object ID, Display name and one more column taken from | ||
| the StateProperty. The display name comes from the tenant, so it goes through | ||
| Get-MtSafeMarkdown: a name that looks like a link or an image is shown as text, and a pipe or | ||
| a line break does not break the row. | ||
|
|
||
| When there are more rows than Limit, the table shows the first Limit rows and a line gives | ||
| the total. | ||
|
|
||
| .PARAMETER InputObject | ||
| The objects to list. Each has Type, Id and DisplayName. | ||
|
|
||
| .PARAMETER StateProperty | ||
| The property shown in the last column, for example Status or State. | ||
|
|
||
| .PARAMETER StateHeading | ||
| The heading of the last column. | ||
|
|
||
| .PARAMETER Limit | ||
| The most rows to show. The default is 25. | ||
|
|
||
| .EXAMPLE | ||
| Format-MtEntraHardDeletionTable -InputObject $Rows -StateProperty Status -StateHeading Status | ||
| #> | ||
| [CmdletBinding()] | ||
| [OutputType([string])] | ||
| param( | ||
| [Parameter(Mandatory)] | ||
| [object[]] $InputObject, | ||
|
|
||
| [Parameter(Mandatory)] | ||
| [string] $StateProperty, | ||
|
|
||
| [Parameter(Mandatory)] | ||
| [string] $StateHeading, | ||
|
|
||
| [Parameter()] | ||
| [ValidateRange(1, 1000)] | ||
| [int] $Limit = 25 | ||
| ) | ||
|
|
||
| $Table = "| Type | Object ID | Display name | $StateHeading |`n| --- | --- | --- | --- |" | ||
| foreach ($Row in @($InputObject | Select-Object -First $Limit)) { | ||
| $DisplayName = Get-MtSafeMarkdown -Text ([string]$Row.DisplayName) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Preserve literal HTML entities in display names. A display name of Escape literal ampersands before inserting the name into the row. Keep the existing escapes for links, pipes, and line breaks. Check that
|
||
| $Table += "`n| $($Row.Type) | $($Row.Id) | $DisplayName | $($Row.$StateProperty) |" | ||
| } | ||
| if (@($InputObject).Count -gt $Limit) { | ||
| $Table += "`n`nShowing the first $Limit of $(@($InputObject).Count) objects." | ||
| } | ||
| $Table | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| function Get-MtEntraHardDeletionObjectStatus { | ||
| <# | ||
| .SYNOPSIS | ||
| Returns the Hard Deletion Protection status of one object listed in the Maester config. | ||
|
|
||
| .DESCRIPTION | ||
| Reads the object from Microsoft Graph beta. Status is 'Protected', 'Not protected', | ||
| 'Soft deleted' or 'Not found'. | ||
|
|
||
| When the active lookup returns HTTP 404, the function reads the object through the | ||
| documented single-item route, directory/deletedItems/{id}, and checks that its @odata.type is | ||
| the configured type or one of its Agent ID subtypes: agentUser for User, | ||
| agentIdentityBlueprint for Application, and agentIdentity or | ||
| agentIdentityBlueprintPrincipal for ServicePrincipal. | ||
|
|
||
| It reports 'Soft deleted' when it finds the object there, whether or not the object has | ||
| protection. An object of another type, a response without an @odata.type, or no object | ||
| gives 'Not found'. Any Graph error other than a 404 is thrown. | ||
|
|
||
| The Id must be the object ID, a GUID. The function uses its canonical lower-case form in both | ||
| requests and in the result, so the result matches the objects that | ||
| Get-MtEntraHardDeletionProtectedObject returns. | ||
|
|
||
| .PARAMETER Configured | ||
| A HardDeletionProtectedObjects entry with Type and Id. | ||
|
|
||
| .EXAMPLE | ||
| Get-MtEntraHardDeletionObjectStatus -Configured $Entry | ||
| #> | ||
| [CmdletBinding()] | ||
| [OutputType([pscustomobject])] | ||
| param( | ||
| [Parameter(Mandatory)] | ||
| [object] $Configured | ||
| ) | ||
|
|
||
| # GraphTypes are the @odata.type values a deleted object of this type can have. Agent ID objects | ||
| # are covered through their underlying user, application and service principal types. | ||
| $EndpointByType = @{ | ||
| user = @{ | ||
| Name = 'User'; Path = 'users'; GraphTypes = @('user', 'agentUser') | ||
| } | ||
| group = @{ Name = 'Group'; Path = 'groups'; GraphTypes = @('group') } | ||
| application = @{ | ||
| Name = 'Application'; Path = 'applications' | ||
| GraphTypes = @('application', 'agentIdentityBlueprint') | ||
| } | ||
| serviceprincipal = @{ | ||
| Name = 'ServicePrincipal'; Path = 'servicePrincipals' | ||
| GraphTypes = @( | ||
| 'servicePrincipal', 'agentIdentity', 'agentIdentityBlueprintPrincipal' | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| $Type = ([string]$Configured.Type).ToLowerInvariant() | ||
| $ParsedId = [guid]::Empty | ||
| $IdIsGuid = [guid]::TryParse([string]$Configured.Id, [ref]$ParsedId) | ||
| if (!$EndpointByType.ContainsKey($Type) -or !$IdIsGuid) { | ||
| throw ( | ||
| "Invalid HardDeletionProtectedObjects entry (Type '$($Configured.Type)', " + | ||
| "Id '$($Configured.Id)'). Type must be User, Group, Application or " + | ||
| 'ServicePrincipal and Id must be the object ID (a GUID).' | ||
| ) | ||
| } | ||
| $Endpoint = $EndpointByType[$Type] | ||
| $AcceptedTypes = @($Endpoint.GraphTypes | ForEach-Object { "#microsoft.graph.$_" }) | ||
| $Id = $ParsedId.ToString() | ||
|
|
||
| $Object = $null | ||
| $Status = 'Not found' | ||
| try { | ||
| $Object = Invoke-MtGraphRequest -ApiVersion 'beta' -RelativeUri "$($Endpoint.Path)/$Id" ` | ||
| -Select @('id', 'displayName', 'isHardDeleteProtectionEnabled') -ErrorAction Stop | ||
| $Status = if ($Object.isHardDeleteProtectionEnabled -eq $true) { | ||
| 'Protected' | ||
| } else { | ||
| 'Not protected' | ||
| } | ||
| } catch { | ||
| if (!(Test-MtGraphNotFoundError -ErrorRecord $_)) { throw } | ||
| } | ||
|
|
||
| if (!$Object) { | ||
| try { | ||
| $Deleted = Invoke-MtGraphRequest -ApiVersion 'beta' ` | ||
| -RelativeUri "directory/deletedItems/$Id" ` | ||
| -Select @('id', 'displayName') -ErrorAction Stop | ||
| $DeletedType = [string]$Deleted.'@odata.type' | ||
| if ($DeletedType -in $AcceptedTypes) { | ||
| $Object = $Deleted | ||
| $Status = 'Soft deleted' | ||
| } | ||
| } catch { | ||
| if (!(Test-MtGraphNotFoundError -ErrorRecord $_)) { throw } | ||
| } | ||
| } | ||
|
|
||
| [pscustomobject]@{ | ||
| Type = $Endpoint.Name | ||
| Id = $Id | ||
| DisplayName = if ($Object) { [string]$Object.displayName } else { '' } | ||
| Status = $Status | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| function Get-MtEntraHardDeletionProtectedObject { | ||
| <# | ||
| .SYNOPSIS | ||
| Returns the users, groups, applications and service principals that have Hard Deletion | ||
| Protection. | ||
|
|
||
| .DESCRIPTION | ||
| Queries Microsoft Graph beta for objects whose isHardDeleteProtectionEnabled property is | ||
| true, both among active objects and among soft-deleted ones. Each result has Type, Id, | ||
| DisplayName and State ('Active' or 'Soft deleted'). | ||
|
|
||
| The reads happen one after the other, so the scan is not a snapshot. An object that is soft | ||
| deleted or restored while it runs can show up in both reads, or in neither. The function | ||
| returns an object seen in both reads once, with the State 'Active or soft deleted', and does | ||
| not claim that either read is newer. An object seen in neither read is missed. | ||
|
|
||
| .EXAMPLE | ||
| Get-MtEntraHardDeletionProtectedObject | ||
| #> | ||
| [CmdletBinding()] | ||
| [OutputType([pscustomobject])] | ||
| param() | ||
|
|
||
| $DeletedRoot = 'directory/deletedItems/microsoft.graph' | ||
| $Sources = @( | ||
| @{ Type = 'User'; Path = 'users'; Deleted = "$DeletedRoot.user" } | ||
| @{ Type = 'Group'; Path = 'groups'; Deleted = "$DeletedRoot.group" } | ||
| @{ Type = 'Application'; Path = 'applications'; Deleted = "$DeletedRoot.application" } | ||
| @{ | ||
| Type = 'ServicePrincipal' | ||
| Path = 'servicePrincipals' | ||
| Deleted = "$DeletedRoot.servicePrincipal" | ||
| } | ||
| ) | ||
|
|
||
| $Observed = [System.Collections.Generic.List[object]]::new() | ||
| foreach ($Source in $Sources) { | ||
| foreach ($State in 'Active', 'Soft deleted') { | ||
| $Path = if ($State -eq 'Active') { $Source.Path } else { $Source.Deleted } | ||
| $Objects = @( | ||
| Invoke-MtGraphRequest -ApiVersion 'beta' -RelativeUri $Path ` | ||
| -Filter 'isHardDeleteProtectionEnabled eq true' ` | ||
| -Select @('id', 'displayName') -ErrorAction Stop | ||
| ) | ||
| foreach ($Object in $Objects) { | ||
| $Observed.Add([pscustomobject]@{ | ||
| Type = $Source.Type | ||
| Id = [string]$Object.id | ||
| DisplayName = [string]$Object.displayName | ||
| State = $State | ||
| }) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| foreach ($Group in @($Observed | Group-Object -Property Type, Id)) { | ||
| $States = @($Group.Group.State | Select-Object -Unique) | ||
| [pscustomobject]@{ | ||
| Type = $Group.Group[0].Type | ||
| Id = $Group.Group[0].Id | ||
| DisplayName = $Group.Group[0].DisplayName | ||
| State = if ($States.Count -eq 1) { $States[0] } else { 'Active or soft deleted' } | ||
| } | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Require both
TypeandIdduring configuration validation.An entry with a valid
Idand noTypepasses the item validator. The validator checks allowed values only for fields that are present and non-null. MT.1276 then rejects the entry at runtime, after it starts reading Graph. Add a required-all item rule or an equivalent validation step for this setting. Reject missing and nullTypevalues, and preserve valid entries with mixed-case type names. (raw.githubusercontent.com)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not changing the validator in this PR.
RequireAnyonly says that at least one of the listed fields must be present, soItemscan't also requireType. Requiring every field needs a new rule inTest-MtGlobalSetting, which every registry setting goes through, so it should be its own change with its own tests and not part of a PR that adds one check.An entry without a
Typeis not silent.Get-MtEntraHardDeletionObjectStatusthrows for it, so MT.1276 reports an Error that names the entry (Invalid HardDeletionProtectedObjects entry (Type '', Id '…')). The unit tests cover an invalidTypeand a non-GUIDIdthrough the same check. The docs page now says that an entry without aTypepasses the pre-run check and then fails the test, so it no longer promises more than the validator does.