From b825718c6538e61c52176b105f5ded55025415f1 Mon Sep 17 00:00:00 2001 From: Julien Ly Date: Fri, 20 Nov 2026 10:40:38 +0100 Subject: [PATCH 1/7] feat(entra): add MT.1286 - user sponsors of Agent ID objects should have a manager When a sponsor leaves or changes roles, the Lifecycle Workflows task "Transfer agent identity sponsorships to manager" moves the sponsorships to the sponsor's manager, and requires a populated manager attribute. MT.1286 reads the sponsors of agent identities, Agent Identity Blueprints and Blueprint Principals, and reports every user sponsor that has no manager or whose manager is a disabled account. Group and service principal sponsors are not evaluated. selection.json regenerated with build/golden/Export-MtGoldenFixture.ps1. --- .../tests/fixtures/golden/selection.json | 6 +- .../Test-MtEntraAgentSponsorManager.Tests.ps1 | 123 +++++++++++++ tests/maester/entra/Test.MT.1286.md | 30 ++++ tests/maester/entra/Test.MT.1286.ps1 | 161 ++++++++++++++++++ 4 files changed, 318 insertions(+), 2 deletions(-) create mode 100644 powershell/tests/functions/Test-MtEntraAgentSponsorManager.Tests.ps1 create mode 100644 tests/maester/entra/Test.MT.1286.md create mode 100644 tests/maester/entra/Test.MT.1286.ps1 diff --git a/powershell/tests/fixtures/golden/selection.json b/powershell/tests/fixtures/golden/selection.json index ae970e7cfd..2fa63533fa 100644 --- a/powershell/tests/fixtures/golden/selection.json +++ b/powershell/tests/fixtures/golden/selection.json @@ -2581,7 +2581,7 @@ "AD" ] }, - "SelectedCount": 639, + "SelectedCount": 640, "SelectedFamilies": [ "MT.1024", "MT.1033", @@ -3162,6 +3162,7 @@ "MT.1259", "MT.1260", "MT.1265", + "MT.1286", "ORCA.100", "ORCA.101", "ORCA.102", @@ -4006,7 +4007,7 @@ "AD" ] }, - "SelectedCount": 595, + "SelectedCount": 596, "SelectedFamilies": [ "MT.1024", "MT.1033", @@ -4543,6 +4544,7 @@ "MT.1259", "MT.1260", "MT.1265", + "MT.1286", "ORCA.100", "ORCA.101", "ORCA.102", diff --git a/powershell/tests/functions/Test-MtEntraAgentSponsorManager.Tests.ps1 b/powershell/tests/functions/Test-MtEntraAgentSponsorManager.Tests.ps1 new file mode 100644 index 0000000000..fdff711d85 --- /dev/null +++ b/powershell/tests/functions/Test-MtEntraAgentSponsorManager.Tests.ps1 @@ -0,0 +1,123 @@ +BeforeAll { + Import-Module $PSScriptRoot/../../Maester.psd1 -Force + . "$PSScriptRoot/../helpers/Use-MtModuleFunction.ps1" + # Check functions that became native tests are not exported. + Use-MtModuleFunction -Name Test-MtEntraAgentSponsorManager +} + +Describe 'MT.1286: Test-MtEntraAgentSponsorManager' -Tag 'Unit' { + BeforeAll { + function Get-HttpError ([int]$StatusCode) { + $response = [System.Net.Http.HttpResponseMessage]::new([System.Net.HttpStatusCode]$StatusCode) + [Microsoft.PowerShell.Commands.HttpResponseException]::new("Response status code does not indicate success: $StatusCode.", $response) + } + } + + BeforeEach { + $script:TestResult = $null + $script:SkippedBecause = $null + $script:SkippedCustomReason = $null + + Mock -ModuleName Maester Add-MtTestResultDetail { + param($Result, $SkippedBecause, $SkippedCustomReason, $SkippedError, $Severity) + + $script:TestResult = $Result + $script:SkippedBecause = $SkippedBecause + $script:SkippedCustomReason = $SkippedCustomReason + } + + # One object of each kind, all sponsored by the same user. + Mock -ModuleName Maester Invoke-MtGraphRequest { + [pscustomobject]@{ id = 'agent-1'; displayName = 'Invoice agent' } + } -ParameterFilter { $RelativeUri -eq 'servicePrincipals/microsoft.graph.agentIdentity' } + Mock -ModuleName Maester Invoke-MtGraphRequest { + [pscustomobject]@{ id = 'principal-1'; displayName = 'Invoice blueprint' } + } -ParameterFilter { $RelativeUri -eq 'servicePrincipals/microsoft.graph.agentIdentityBlueprintPrincipal' } + Mock -ModuleName Maester Invoke-MtGraphRequest { + [pscustomobject]@{ id = 'blueprint-1'; displayName = 'Invoice blueprint' } + } -ParameterFilter { $RelativeUri -eq 'applications/microsoft.graph.agentIdentityBlueprint' } + Mock -ModuleName Maester Invoke-MtGraphRequest { + [pscustomobject]@{ + '@odata.type' = '#microsoft.graph.user' + id = 'sponsor-1' + displayName = 'Ada Sponsor' + userPrincipalName = 'ada@contoso.com' + accountEnabled = $true + } + } -ParameterFilter { $RelativeUri -like '*/sponsors' } + Mock -ModuleName Maester Invoke-MtGraphRequest { + [pscustomobject]@{ id = 'manager-1'; displayName = 'Grace Manager'; accountEnabled = $true } + } -ParameterFilter { $RelativeUri -like 'users/*/manager' } + } + + It 'passes when every user sponsor has an enabled manager, reading each manager once' { + Test-MtEntraAgentSponsorManager | Should -BeTrue + + $script:TestResult | Should -Match 'All 3 user sponsorship' + Should -Invoke Invoke-MtGraphRequest -ModuleName Maester -Exactly 1 -ParameterFilter { $RelativeUri -eq 'users/sponsor-1/manager' } + } + + It 'reports a user sponsor without a manager' { + # Invoke-MtGraphRequest returns nothing when Graph answers 404, which is how a missing manager reads. + Mock -ModuleName Maester Invoke-MtGraphRequest { } -ParameterFilter { $RelativeUri -like 'users/*/manager' } + + Test-MtEntraAgentSponsorManager | Should -BeFalse + + $script:TestResult | Should -Match 'Found 3 of 3 user sponsorship' + # Get-MtSafeMarkdown escapes the parentheses and the @ of the sponsor column. + $script:TestResult | Should -Match '\| Agent identity \| Invoice agent \| Ada Sponsor \\\(ada\\@contoso\.com\\\) \| No manager\. \|' + $script:TestResult | Should -Match '\| Agent blueprint \| Invoice blueprint \|' + } + + It 'reports a user sponsor whose manager is a disabled account' { + Mock -ModuleName Maester Invoke-MtGraphRequest { + [pscustomobject]@{ id = 'manager-1'; displayName = 'Grace Manager'; accountEnabled = $false } + } -ParameterFilter { $RelativeUri -like 'users/*/manager' } + + Test-MtEntraAgentSponsorManager | Should -BeFalse + + $script:TestResult | Should -Match 'Manager Grace Manager is a disabled account\.' + } + + It 'does not evaluate group sponsors, and skips when no object has a user sponsor' { + Mock -ModuleName Maester Invoke-MtGraphRequest { + [pscustomobject]@{ '@odata.type' = '#microsoft.graph.group'; id = 'group-1'; displayName = 'Agent sponsors' } + } -ParameterFilter { $RelativeUri -like '*/sponsors' } + + Test-MtEntraAgentSponsorManager | Should -BeNullOrEmpty + + $script:SkippedBecause | Should -Be 'Custom' + $script:SkippedCustomReason | Should -Match 'user sponsor' + Should -Invoke Invoke-MtGraphRequest -ModuleName Maester -Exactly 0 -ParameterFilter { $RelativeUri -like 'users/*/manager' } + } + + It 'reports only the sponsor without a manager when another sponsor of the same object has one' { + Mock -ModuleName Maester Invoke-MtGraphRequest { + [pscustomobject]@{ '@odata.type' = '#microsoft.graph.user'; id = 'sponsor-1'; displayName = 'Ada Sponsor'; userPrincipalName = 'ada@contoso.com'; accountEnabled = $true } + [pscustomobject]@{ '@odata.type' = '#microsoft.graph.user'; id = 'sponsor-2'; displayName = 'Alan Sponsor'; userPrincipalName = 'alan@contoso.com'; accountEnabled = $true } + } -ParameterFilter { $RelativeUri -like '*/sponsors' } + Mock -ModuleName Maester Invoke-MtGraphRequest { } -ParameterFilter { $RelativeUri -eq 'users/sponsor-2/manager' } + + Test-MtEntraAgentSponsorManager | Should -BeFalse + + $script:TestResult | Should -Match 'Found 3 of 6 user sponsorship' + $script:TestResult | Should -Not -Match 'Ada Sponsor' + $script:TestResult | Should -Match 'Alan Sponsor' + } + + It 'is skipped as not authorized when Graph answers 403' { + Mock -ModuleName Maester Invoke-MtGraphRequest { throw (Get-HttpError 403) } -ParameterFilter { $RelativeUri -eq 'servicePrincipals/microsoft.graph.agentIdentity' } + + Test-MtEntraAgentSponsorManager | Should -BeNullOrEmpty + + $script:SkippedBecause | Should -Be 'NotAuthorized' + } + + It 'is skipped with the error when a manager read fails for another reason' { + Mock -ModuleName Maester Invoke-MtGraphRequest { throw (Get-HttpError 500) } -ParameterFilter { $RelativeUri -like 'users/*/manager' } + + Test-MtEntraAgentSponsorManager | Should -BeNullOrEmpty + + $script:SkippedBecause | Should -Be 'Error' + } +} diff --git a/tests/maester/entra/Test.MT.1286.md b/tests/maester/entra/Test.MT.1286.md new file mode 100644 index 0000000000..9b79490756 --- /dev/null +++ b/tests/maester/entra/Test.MT.1286.md @@ -0,0 +1,30 @@ +User sponsors of Agent ID objects should have a manager. + +Agent identities and Agent Identity Blueprints need an accountable sponsor. When a sponsor leaves or changes roles, the Lifecycle Workflows task **Transfer agent identity sponsorships to manager** adds the sponsor's manager as a sponsor and removes the user. The task requires a populated manager attribute. Without a manager, the sponsorship has nowhere to go when that sponsor leaves. With a disabled manager, it goes to an account nobody uses. + +This check reads the sponsors of agent identities, Agent Identity Blueprints and Blueprint Principals. It reports every user sponsor that has no manager or whose manager is a disabled account. Group and service principal sponsors have no manager and are not evaluated. + +A manager that is set but no longer the right successor still resolves, and the transfer goes through. This check cannot see that case: review who each sponsor's manager is, not only whether one is set. + +### Remediation action: + +1. Sign in to the [Microsoft Entra admin center][entra-admin-center] as at least a User Administrator and browse to **Entra ID** > **Users**. +2. Open each reported sponsor, select **Edit properties**, and set **Manager** on the **Job information** tab. For a user synchronized from Active Directory, set the manager in Active Directory instead and wait for the next synchronization. +3. Where the reported manager is a disabled account, set the sponsor's current manager. +4. To transfer sponsorships automatically, create a workflow from the **Transition agent sponsorships when a sponsor leaves** or **Transition agent sponsorships when a sponsor changes roles** template. + +### Related links + +* [Lifecycle Workflow built-in tasks: Transfer agent identity sponsorships to manager][lcw-task] +* [Agent identity sponsor tasks in Lifecycle Workflows][sponsor-tasks] +* [Governing agent identities][governance] +* [Add or update a user's profile information][user-profile] + +[entra-admin-center]: https://entra.microsoft.com +[lcw-task]: https://learn.microsoft.com/entra/id-governance/lifecycle-workflow-tasks#transfer-agent-identity-sponsorships-to-manager +[sponsor-tasks]: https://learn.microsoft.com/entra/id-governance/agent-sponsor-tasks +[governance]: https://learn.microsoft.com/entra/id-governance/agent-id-governance-overview +[user-profile]: https://learn.microsoft.com/entra/fundamentals/how-to-manage-user-profile-info + + +%TestResult% diff --git a/tests/maester/entra/Test.MT.1286.ps1 b/tests/maester/entra/Test.MT.1286.ps1 new file mode 100644 index 0000000000..6f5bed0b96 --- /dev/null +++ b/tests/maester/entra/Test.MT.1286.ps1 @@ -0,0 +1,161 @@ +function Test-MtEntraAgentSponsorManager { + <# + .SYNOPSIS + Finds users who sponsor Agent ID objects and have no manager, or a disabled one. + + .DESCRIPTION + When a sponsor leaves or changes roles, the Lifecycle Workflows task "Transfer agent identity + sponsorships to manager" adds the sponsor's manager as a sponsor and removes the user. The task + requires a populated manager attribute: without one, the sponsorship has nowhere to go. + + This check reads the sponsors of agent identities, Agent Identity Blueprints and Blueprint + Principals, and reports every user sponsor that has no manager or whose manager is a disabled + account. Group and service principal sponsors have no manager and are not evaluated. + + A manager that is set but no longer the right successor still resolves, so the transfer goes + through. Directory data cannot show that case. + + .EXAMPLE + Test-MtEntraAgentSponsorManager + + .LINK + https://maester.dev/docs/commands/Test-MtEntraAgentSponsorManager + + .LINK + https://learn.microsoft.com/entra/id-governance/lifecycle-workflow-tasks#transfer-agent-identity-sponsorships-to-manager + #> + [MaesterTest( + Id = 'MT.1286', + Title = 'User sponsors of Agent ID objects should have a manager (Preview).', + Product = 'entra', + Feature = 'agent-identities', + Area = 'ai', + ResourceType = 'Agent', + Severity = 'Medium', + UserImpact = 'Low', + ImplementationCost = 'Low', + Mitre = 'None', + Control = 'nist-800-53-r5:AC-2', + Service = 'Graph', + Lifecycle = 'Preview', + LongRunning, + Author = 'julien-ly' + )] + [CmdletBinding()] + [OutputType([bool])] + param() + + try { + $ObjectKinds = @( + @{ + Type = 'Agent identity' + List = 'servicePrincipals/microsoft.graph.agentIdentity' + Sponsors = 'servicePrincipals/{0}/microsoft.graph.agentIdentity/sponsors' + } + @{ + Type = 'Agent blueprint principal' + List = 'servicePrincipals/microsoft.graph.agentIdentityBlueprintPrincipal' + Sponsors = 'servicePrincipals/{0}/microsoft.graph.agentIdentityBlueprintPrincipal/sponsors' + } + @{ + Type = 'Agent blueprint' + List = 'applications/microsoft.graph.agentIdentityBlueprint' + Sponsors = 'applications/{0}/microsoft.graph.agentIdentityBlueprint/sponsors' + } + ) + + # A user often sponsors several objects: read each manager once. + $ManagerBySponsorId = @{} + $UserSponsorships = 0 + $Findings = [System.Collections.Generic.List[pscustomobject]]::new() + + foreach ($Kind in $ObjectKinds) { + $Objects = @( + Invoke-MtGraphRequest -ApiVersion 'v1.0' -RelativeUri $Kind.List -Select @('id', 'displayName') + ) + Write-Verbose "Reading the sponsors of $($Objects.Count) object(s) of type '$($Kind.Type)'." + + foreach ($Object in $Objects) { + $ObjectId = [string]$Object.id + if ([string]::IsNullOrWhiteSpace($ObjectId)) { continue } + + $Sponsors = @( + Invoke-MtGraphRequest -ApiVersion 'v1.0' -RelativeUri ($Kind.Sponsors -f $ObjectId) ` + -Select @('id', 'displayName', 'userPrincipalName', 'accountEnabled') + ) + + foreach ($Sponsor in $Sponsors) { + # Groups and service principals can be sponsors, but have no manager. + if ([string]$Sponsor.'@odata.type' -ne '#microsoft.graph.user') { continue } + $SponsorId = [string]$Sponsor.id + if ([string]::IsNullOrWhiteSpace($SponsorId)) { continue } + $UserSponsorships++ + + if (-not $ManagerBySponsorId.ContainsKey($SponsorId)) { + # Invoke-MtGraphRequest returns nothing for a 404, which is the answer for a user without a manager. + $ManagerBySponsorId[$SponsorId] = Invoke-MtGraphRequest -ApiVersion 'v1.0' ` + -RelativeUri "users/$SponsorId/manager" -Select @('id', 'displayName', 'accountEnabled') + } + $Manager = $ManagerBySponsorId[$SponsorId] + + $Issue = $null + if ($null -eq $Manager -or [string]::IsNullOrWhiteSpace([string]$Manager.id)) { + $Issue = 'No manager.' + } elseif ($Manager.accountEnabled -eq $false) { + $Issue = "Manager $([string]$Manager.displayName) is a disabled account." + } + + if ($Issue) { + $Findings.Add([pscustomobject]@{ + ObjectType = $Kind.Type + ObjectId = $ObjectId + ObjectName = [string]$Object.displayName + Sponsor = [string]$Sponsor.displayName + SponsorUpn = [string]$Sponsor.userPrincipalName + Issue = $Issue + }) + } + } + } + } + + if ($UserSponsorships -eq 0) { + Add-MtTestResultDetail -SkippedBecause Custom -SkippedCustomReason 'No Agent ID object in this tenant has a user sponsor.' + return $null + } + + if ($Findings.Count -eq 0) { + Add-MtTestResultDetail -Result ( + "Well done. All $UserSponsorships user sponsorship(s) of Agent ID objects belong to users with an enabled manager." + ) + return $true + } + + $Result = "Found $($Findings.Count) of $UserSponsorships user sponsorship(s) that cannot be transferred to an enabled manager. " + $Result += 'When these sponsors leave or change roles, the Lifecycle Workflows task that transfers their sponsorships has no manager to transfer them to.' + $Result += "`n`n| Object type | Object | Sponsor | Issue |" + $Result += "`n| --- | --- | --- | --- |" + foreach ($Item in $Findings) { + $ObjectName = $Item.ObjectName + if ([string]::IsNullOrWhiteSpace($ObjectName)) { $ObjectName = "(unnamed) $($Item.ObjectId)" } + $SponsorText = $Item.Sponsor + if (-not [string]::IsNullOrWhiteSpace($Item.SponsorUpn)) { $SponsorText = "$SponsorText ($($Item.SponsorUpn))" } + $ObjectName = Get-MtSafeMarkdown -Text ([System.Net.WebUtility]::HtmlEncode($ObjectName)) + $SponsorText = Get-MtSafeMarkdown -Text ([System.Net.WebUtility]::HtmlEncode($SponsorText)) + $Issue = Get-MtSafeMarkdown -Text ([System.Net.WebUtility]::HtmlEncode($Item.Issue)) + $Result += "`n| $($Item.ObjectType) | $ObjectName | $SponsorText | $Issue |" + } + + Add-MtTestResultDetail -Result $Result -Severity 'Medium' + return $false + } catch { + $ErrorRecord = $_ + } + + if ($ErrorRecord.Exception.Message -match '(?i)403|forbidden|authorization') { + Add-MtTestResultDetail -SkippedBecause NotAuthorized + } else { + Add-MtTestResultDetail -SkippedBecause Error -SkippedError $ErrorRecord + } + return $null +} From 7be548083edd08a7b27610484e2ccf3437ad73b8 Mon Sep 17 00:00:00 2001 From: Julien Ly Date: Fri, 20 Nov 2026 10:50:11 +0100 Subject: [PATCH 2/7] fix(entra): MT.1286 reads a missing manager from the 404 Graph returns Invoke-MtGraphRequest throws the 404 Request_ResourceNotFound that Graph returns for the manager of a user who has none. MT.1286 now reads it as no manager, and still rethrows any other error. Found by running the check against a lab tenant. --- .../Test-MtEntraAgentSponsorManager.Tests.ps1 | 6 +++--- tests/maester/entra/Test.MT.1286.ps1 | 12 +++++++++--- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/powershell/tests/functions/Test-MtEntraAgentSponsorManager.Tests.ps1 b/powershell/tests/functions/Test-MtEntraAgentSponsorManager.Tests.ps1 index fdff711d85..994bcb8415 100644 --- a/powershell/tests/functions/Test-MtEntraAgentSponsorManager.Tests.ps1 +++ b/powershell/tests/functions/Test-MtEntraAgentSponsorManager.Tests.ps1 @@ -58,8 +58,8 @@ Describe 'MT.1286: Test-MtEntraAgentSponsorManager' -Tag 'Unit' { } It 'reports a user sponsor without a manager' { - # Invoke-MtGraphRequest returns nothing when Graph answers 404, which is how a missing manager reads. - Mock -ModuleName Maester Invoke-MtGraphRequest { } -ParameterFilter { $RelativeUri -like 'users/*/manager' } + # Graph answers 404 for the manager of a user who has none, and Invoke-MtGraphRequest throws it. + Mock -ModuleName Maester Invoke-MtGraphRequest { throw (Get-HttpError 404) } -ParameterFilter { $RelativeUri -like 'users/*/manager' } Test-MtEntraAgentSponsorManager | Should -BeFalse @@ -96,7 +96,7 @@ Describe 'MT.1286: Test-MtEntraAgentSponsorManager' -Tag 'Unit' { [pscustomobject]@{ '@odata.type' = '#microsoft.graph.user'; id = 'sponsor-1'; displayName = 'Ada Sponsor'; userPrincipalName = 'ada@contoso.com'; accountEnabled = $true } [pscustomobject]@{ '@odata.type' = '#microsoft.graph.user'; id = 'sponsor-2'; displayName = 'Alan Sponsor'; userPrincipalName = 'alan@contoso.com'; accountEnabled = $true } } -ParameterFilter { $RelativeUri -like '*/sponsors' } - Mock -ModuleName Maester Invoke-MtGraphRequest { } -ParameterFilter { $RelativeUri -eq 'users/sponsor-2/manager' } + Mock -ModuleName Maester Invoke-MtGraphRequest { throw (Get-HttpError 404) } -ParameterFilter { $RelativeUri -eq 'users/sponsor-2/manager' } Test-MtEntraAgentSponsorManager | Should -BeFalse diff --git a/tests/maester/entra/Test.MT.1286.ps1 b/tests/maester/entra/Test.MT.1286.ps1 index 6f5bed0b96..b34cea6a4d 100644 --- a/tests/maester/entra/Test.MT.1286.ps1 +++ b/tests/maester/entra/Test.MT.1286.ps1 @@ -92,9 +92,15 @@ $UserSponsorships++ if (-not $ManagerBySponsorId.ContainsKey($SponsorId)) { - # Invoke-MtGraphRequest returns nothing for a 404, which is the answer for a user without a manager. - $ManagerBySponsorId[$SponsorId] = Invoke-MtGraphRequest -ApiVersion 'v1.0' ` - -RelativeUri "users/$SponsorId/manager" -Select @('id', 'displayName', 'accountEnabled') + $Manager = $null + try { + $Manager = Invoke-MtGraphRequest -ApiVersion 'v1.0' ` + -RelativeUri "users/$SponsorId/manager" -Select @('id', 'displayName', 'accountEnabled') + } catch { + # For a user without a manager, Graph answers 404 Request_ResourceNotFound. Anything else is an error. + if ($_.Exception.Message -notmatch '(?i)\b404\b|Request_ResourceNotFound') { throw } + } + $ManagerBySponsorId[$SponsorId] = $Manager } $Manager = $ManagerBySponsorId[$SponsorId] From d0ee09a8a57714303322fc2a90b912dda82168cb Mon Sep 17 00:00:00 2001 From: Julien Ly Date: Fri, 20 Nov 2026 10:55:32 +0100 Subject: [PATCH 3/7] fix(entra): MT.1286 recognises the 404 by its status code The Graph SDK exception says "NotFound (Not Found)", not 404, so the previous match missed it on a live tenant. The check now reads the response status code, with Request_ResourceNotFound in the error body as a fallback. The unit tests build the same message the SDK does, so they fail on the previous commit. --- .../tests/functions/Test-MtEntraAgentSponsorManager.Tests.ps1 | 3 ++- tests/maester/entra/Test.MT.1286.ps1 | 4 +++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/powershell/tests/functions/Test-MtEntraAgentSponsorManager.Tests.ps1 b/powershell/tests/functions/Test-MtEntraAgentSponsorManager.Tests.ps1 index 994bcb8415..96087bb56b 100644 --- a/powershell/tests/functions/Test-MtEntraAgentSponsorManager.Tests.ps1 +++ b/powershell/tests/functions/Test-MtEntraAgentSponsorManager.Tests.ps1 @@ -7,9 +7,10 @@ Describe 'MT.1286: Test-MtEntraAgentSponsorManager' -Tag 'Unit' { BeforeAll { + # Graph errors name the status, not its number: "Response status code does not indicate success: NotFound (Not Found)." function Get-HttpError ([int]$StatusCode) { $response = [System.Net.Http.HttpResponseMessage]::new([System.Net.HttpStatusCode]$StatusCode) - [Microsoft.PowerShell.Commands.HttpResponseException]::new("Response status code does not indicate success: $StatusCode.", $response) + [Microsoft.PowerShell.Commands.HttpResponseException]::new("Response status code does not indicate success: $([System.Net.HttpStatusCode]$StatusCode) ($($response.ReasonPhrase)).", $response) } } diff --git a/tests/maester/entra/Test.MT.1286.ps1 b/tests/maester/entra/Test.MT.1286.ps1 index b34cea6a4d..062d0f3ce0 100644 --- a/tests/maester/entra/Test.MT.1286.ps1 +++ b/tests/maester/entra/Test.MT.1286.ps1 @@ -98,7 +98,9 @@ -RelativeUri "users/$SponsorId/manager" -Select @('id', 'displayName', 'accountEnabled') } catch { # For a user without a manager, Graph answers 404 Request_ResourceNotFound. Anything else is an error. - if ($_.Exception.Message -notmatch '(?i)\b404\b|Request_ResourceNotFound') { throw } + # The exception message only says "NotFound": read the status code, and the error body as a fallback. + $Status = if ($_.Exception.Response) { [int]$_.Exception.Response.StatusCode } else { 0 } + if ($Status -ne 404 -and "$($_.ErrorDetails.Message)" -notmatch 'Request_ResourceNotFound') { throw } } $ManagerBySponsorId[$SponsorId] = $Manager } From a2ea393603d0b7e8f5a94c92a8d3e18bfc0bf4b5 Mon Sep 17 00:00:00 2001 From: Julien Ly Date: Fri, 20 Nov 2026 11:01:49 +0100 Subject: [PATCH 4/7] refactor(entra): MT.1286 skips as NotApplicable when no object has a user sponsor Follows the repository guidance and the CodeRabbit review: the tenant state makes the check meaningless, so NotApplicable, with the reason kept as result text. Drops the unused Severity and SkippedError parameters that PSScriptAnalyzer flagged in the unit test mock. --- .../Test-MtEntraAgentSponsorManager.Tests.ps1 | 10 ++++------ tests/maester/entra/Test.MT.1286.ps1 | 2 +- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/powershell/tests/functions/Test-MtEntraAgentSponsorManager.Tests.ps1 b/powershell/tests/functions/Test-MtEntraAgentSponsorManager.Tests.ps1 index 96087bb56b..cd31b56a80 100644 --- a/powershell/tests/functions/Test-MtEntraAgentSponsorManager.Tests.ps1 +++ b/powershell/tests/functions/Test-MtEntraAgentSponsorManager.Tests.ps1 @@ -17,14 +17,12 @@ Describe 'MT.1286: Test-MtEntraAgentSponsorManager' -Tag 'Unit' { BeforeEach { $script:TestResult = $null $script:SkippedBecause = $null - $script:SkippedCustomReason = $null Mock -ModuleName Maester Add-MtTestResultDetail { - param($Result, $SkippedBecause, $SkippedCustomReason, $SkippedError, $Severity) + param($Result, $SkippedBecause) $script:TestResult = $Result $script:SkippedBecause = $SkippedBecause - $script:SkippedCustomReason = $SkippedCustomReason } # One object of each kind, all sponsored by the same user. @@ -80,15 +78,15 @@ Describe 'MT.1286: Test-MtEntraAgentSponsorManager' -Tag 'Unit' { $script:TestResult | Should -Match 'Manager Grace Manager is a disabled account\.' } - It 'does not evaluate group sponsors, and skips when no object has a user sponsor' { + It 'does not evaluate group sponsors, and skips as not applicable when no object has a user sponsor' { Mock -ModuleName Maester Invoke-MtGraphRequest { [pscustomobject]@{ '@odata.type' = '#microsoft.graph.group'; id = 'group-1'; displayName = 'Agent sponsors' } } -ParameterFilter { $RelativeUri -like '*/sponsors' } Test-MtEntraAgentSponsorManager | Should -BeNullOrEmpty - $script:SkippedBecause | Should -Be 'Custom' - $script:SkippedCustomReason | Should -Match 'user sponsor' + $script:SkippedBecause | Should -Be 'NotApplicable' + $script:TestResult | Should -Match 'user sponsor' Should -Invoke Invoke-MtGraphRequest -ModuleName Maester -Exactly 0 -ParameterFilter { $RelativeUri -like 'users/*/manager' } } diff --git a/tests/maester/entra/Test.MT.1286.ps1 b/tests/maester/entra/Test.MT.1286.ps1 index 062d0f3ce0..0cdd6f704a 100644 --- a/tests/maester/entra/Test.MT.1286.ps1 +++ b/tests/maester/entra/Test.MT.1286.ps1 @@ -128,7 +128,7 @@ } if ($UserSponsorships -eq 0) { - Add-MtTestResultDetail -SkippedBecause Custom -SkippedCustomReason 'No Agent ID object in this tenant has a user sponsor.' + Add-MtTestResultDetail -SkippedBecause NotApplicable -Result 'No Agent ID object in this tenant has a user sponsor.' return $null } From 9539cd86b1db86aab83fd9e48de965100409762e Mon Sep 17 00:00:00 2001 From: Julien Ly Date: Fri, 20 Nov 2026 11:15:34 +0100 Subject: [PATCH 5/7] refactor(entra): MT.1286 declares Severity Low and leaves other errors to the engine The attribute review rates a user sponsor without a manager as hygiene with a small security effect on its own, so Severity is Low. The result no longer repeats the severity. Only a 403 is caught now, around the Graph reads, and reported as NotAuthorized. Any other error goes to the engine, as the native test guide asks (no outer try/catch). --- .../Test-MtEntraAgentSponsorManager.Tests.ps1 | 7 +- tests/maester/entra/Test.MT.1286.ps1 | 111 +++++++++--------- 2 files changed, 59 insertions(+), 59 deletions(-) diff --git a/powershell/tests/functions/Test-MtEntraAgentSponsorManager.Tests.ps1 b/powershell/tests/functions/Test-MtEntraAgentSponsorManager.Tests.ps1 index cd31b56a80..32c1e41b49 100644 --- a/powershell/tests/functions/Test-MtEntraAgentSponsorManager.Tests.ps1 +++ b/powershell/tests/functions/Test-MtEntraAgentSponsorManager.Tests.ps1 @@ -112,11 +112,12 @@ Describe 'MT.1286: Test-MtEntraAgentSponsorManager' -Tag 'Unit' { $script:SkippedBecause | Should -Be 'NotAuthorized' } - It 'is skipped with the error when a manager read fails for another reason' { + It 'leaves any other error to the engine, without reading it as a missing manager' { Mock -ModuleName Maester Invoke-MtGraphRequest { throw (Get-HttpError 500) } -ParameterFilter { $RelativeUri -like 'users/*/manager' } - Test-MtEntraAgentSponsorManager | Should -BeNullOrEmpty + { Test-MtEntraAgentSponsorManager } | Should -Throw -ExpectedMessage '*InternalServerError*' - $script:SkippedBecause | Should -Be 'Error' + $script:TestResult | Should -BeNullOrEmpty + $script:SkippedBecause | Should -BeNullOrEmpty } } diff --git a/tests/maester/entra/Test.MT.1286.ps1 b/tests/maester/entra/Test.MT.1286.ps1 index 0cdd6f704a..8aa65df96a 100644 --- a/tests/maester/entra/Test.MT.1286.ps1 +++ b/tests/maester/entra/Test.MT.1286.ps1 @@ -31,7 +31,7 @@ Feature = 'agent-identities', Area = 'ai', ResourceType = 'Agent', - Severity = 'Medium', + Severity = 'Low', UserImpact = 'Low', ImplementationCost = 'Low', Mitre = 'None', @@ -45,30 +45,30 @@ [OutputType([bool])] param() - try { - $ObjectKinds = @( - @{ - Type = 'Agent identity' - List = 'servicePrincipals/microsoft.graph.agentIdentity' - Sponsors = 'servicePrincipals/{0}/microsoft.graph.agentIdentity/sponsors' - } - @{ - Type = 'Agent blueprint principal' - List = 'servicePrincipals/microsoft.graph.agentIdentityBlueprintPrincipal' - Sponsors = 'servicePrincipals/{0}/microsoft.graph.agentIdentityBlueprintPrincipal/sponsors' - } - @{ - Type = 'Agent blueprint' - List = 'applications/microsoft.graph.agentIdentityBlueprint' - Sponsors = 'applications/{0}/microsoft.graph.agentIdentityBlueprint/sponsors' - } - ) + $ObjectKinds = @( + @{ + Type = 'Agent identity' + List = 'servicePrincipals/microsoft.graph.agentIdentity' + Sponsors = 'servicePrincipals/{0}/microsoft.graph.agentIdentity/sponsors' + } + @{ + Type = 'Agent blueprint principal' + List = 'servicePrincipals/microsoft.graph.agentIdentityBlueprintPrincipal' + Sponsors = 'servicePrincipals/{0}/microsoft.graph.agentIdentityBlueprintPrincipal/sponsors' + } + @{ + Type = 'Agent blueprint' + List = 'applications/microsoft.graph.agentIdentityBlueprint' + Sponsors = 'applications/{0}/microsoft.graph.agentIdentityBlueprint/sponsors' + } + ) - # A user often sponsors several objects: read each manager once. - $ManagerBySponsorId = @{} - $UserSponsorships = 0 - $Findings = [System.Collections.Generic.List[pscustomobject]]::new() + # A user often sponsors several objects: read each manager once. + $ManagerBySponsorId = @{} + $UserSponsorships = 0 + $Findings = [System.Collections.Generic.List[pscustomobject]]::new() + try { foreach ($Kind in $ObjectKinds) { $Objects = @( Invoke-MtGraphRequest -ApiVersion 'v1.0' -RelativeUri $Kind.List -Select @('id', 'displayName') @@ -126,44 +126,43 @@ } } } - - if ($UserSponsorships -eq 0) { - Add-MtTestResultDetail -SkippedBecause NotApplicable -Result 'No Agent ID object in this tenant has a user sponsor.' + } catch { + # Graph answers 403 when the signed-in account cannot read Agent ID objects, their sponsors or a manager. + # Any other error goes to the engine, which reports it. + if ($_.Exception.Response -and [int]$_.Exception.Response.StatusCode -eq 403) { + Add-MtTestResultDetail -SkippedBecause NotAuthorized return $null } + throw + } - if ($Findings.Count -eq 0) { - Add-MtTestResultDetail -Result ( - "Well done. All $UserSponsorships user sponsorship(s) of Agent ID objects belong to users with an enabled manager." - ) - return $true - } - - $Result = "Found $($Findings.Count) of $UserSponsorships user sponsorship(s) that cannot be transferred to an enabled manager. " - $Result += 'When these sponsors leave or change roles, the Lifecycle Workflows task that transfers their sponsorships has no manager to transfer them to.' - $Result += "`n`n| Object type | Object | Sponsor | Issue |" - $Result += "`n| --- | --- | --- | --- |" - foreach ($Item in $Findings) { - $ObjectName = $Item.ObjectName - if ([string]::IsNullOrWhiteSpace($ObjectName)) { $ObjectName = "(unnamed) $($Item.ObjectId)" } - $SponsorText = $Item.Sponsor - if (-not [string]::IsNullOrWhiteSpace($Item.SponsorUpn)) { $SponsorText = "$SponsorText ($($Item.SponsorUpn))" } - $ObjectName = Get-MtSafeMarkdown -Text ([System.Net.WebUtility]::HtmlEncode($ObjectName)) - $SponsorText = Get-MtSafeMarkdown -Text ([System.Net.WebUtility]::HtmlEncode($SponsorText)) - $Issue = Get-MtSafeMarkdown -Text ([System.Net.WebUtility]::HtmlEncode($Item.Issue)) - $Result += "`n| $($Item.ObjectType) | $ObjectName | $SponsorText | $Issue |" - } + if ($UserSponsorships -eq 0) { + Add-MtTestResultDetail -SkippedBecause NotApplicable -Result 'No Agent ID object in this tenant has a user sponsor.' + return $null + } - Add-MtTestResultDetail -Result $Result -Severity 'Medium' - return $false - } catch { - $ErrorRecord = $_ + if ($Findings.Count -eq 0) { + Add-MtTestResultDetail -Result ( + "Well done. All $UserSponsorships user sponsorship(s) of Agent ID objects belong to users with an enabled manager." + ) + return $true } - if ($ErrorRecord.Exception.Message -match '(?i)403|forbidden|authorization') { - Add-MtTestResultDetail -SkippedBecause NotAuthorized - } else { - Add-MtTestResultDetail -SkippedBecause Error -SkippedError $ErrorRecord + $Result = "Found $($Findings.Count) of $UserSponsorships user sponsorship(s) that cannot be transferred to an enabled manager. " + $Result += 'When these sponsors leave or change roles, the Lifecycle Workflows task that transfers their sponsorships has no manager to transfer them to.' + $Result += "`n`n| Object type | Object | Sponsor | Issue |" + $Result += "`n| --- | --- | --- | --- |" + foreach ($Item in $Findings) { + $ObjectName = $Item.ObjectName + if ([string]::IsNullOrWhiteSpace($ObjectName)) { $ObjectName = "(unnamed) $($Item.ObjectId)" } + $SponsorText = $Item.Sponsor + if (-not [string]::IsNullOrWhiteSpace($Item.SponsorUpn)) { $SponsorText = "$SponsorText ($($Item.SponsorUpn))" } + $ObjectName = Get-MtSafeMarkdown -Text ([System.Net.WebUtility]::HtmlEncode($ObjectName)) + $SponsorText = Get-MtSafeMarkdown -Text ([System.Net.WebUtility]::HtmlEncode($SponsorText)) + $Issue = Get-MtSafeMarkdown -Text ([System.Net.WebUtility]::HtmlEncode($Item.Issue)) + $Result += "`n| $($Item.ObjectType) | $ObjectName | $SponsorText | $Issue |" } - return $null + + Add-MtTestResultDetail -Result $Result + return $false } From 72f0da079588fcfef00e28d27ab9d6ec101d31d3 Mon Sep 17 00:00:00 2001 From: Julien Ly Date: Fri, 20 Nov 2026 11:40:05 +0100 Subject: [PATCH 6/7] fix(entra): MT.1286 requires an enabled manager account before passing A manager with no accountEnabled, such as a contact, counted as enabled. Only an explicit true passes now; anything else is reported as a manager that cannot be confirmed as an enabled account, as the CodeRabbit review asked. --- .../Test-MtEntraAgentSponsorManager.Tests.ps1 | 10 ++++++++++ tests/maester/entra/Test.MT.1286.md | 2 +- tests/maester/entra/Test.MT.1286.ps1 | 8 ++++++-- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/powershell/tests/functions/Test-MtEntraAgentSponsorManager.Tests.ps1 b/powershell/tests/functions/Test-MtEntraAgentSponsorManager.Tests.ps1 index 32c1e41b49..fe021106bb 100644 --- a/powershell/tests/functions/Test-MtEntraAgentSponsorManager.Tests.ps1 +++ b/powershell/tests/functions/Test-MtEntraAgentSponsorManager.Tests.ps1 @@ -78,6 +78,16 @@ Describe 'MT.1286: Test-MtEntraAgentSponsorManager' -Tag 'Unit' { $script:TestResult | Should -Match 'Manager Grace Manager is a disabled account\.' } + It 'reports a user sponsor whose manager has no account state, such as a contact' { + Mock -ModuleName Maester Invoke-MtGraphRequest { + [pscustomobject]@{ '@odata.type' = '#microsoft.graph.orgContact'; id = 'contact-1'; displayName = 'Grace Contact' } + } -ParameterFilter { $RelativeUri -like 'users/*/manager' } + + Test-MtEntraAgentSponsorManager | Should -BeFalse + + $script:TestResult | Should -Match 'Cannot confirm that manager Grace Contact is an enabled account\.' + } + It 'does not evaluate group sponsors, and skips as not applicable when no object has a user sponsor' { Mock -ModuleName Maester Invoke-MtGraphRequest { [pscustomobject]@{ '@odata.type' = '#microsoft.graph.group'; id = 'group-1'; displayName = 'Agent sponsors' } diff --git a/tests/maester/entra/Test.MT.1286.md b/tests/maester/entra/Test.MT.1286.md index 9b79490756..18a1bd2ccc 100644 --- a/tests/maester/entra/Test.MT.1286.md +++ b/tests/maester/entra/Test.MT.1286.md @@ -2,7 +2,7 @@ User sponsors of Agent ID objects should have a manager. Agent identities and Agent Identity Blueprints need an accountable sponsor. When a sponsor leaves or changes roles, the Lifecycle Workflows task **Transfer agent identity sponsorships to manager** adds the sponsor's manager as a sponsor and removes the user. The task requires a populated manager attribute. Without a manager, the sponsorship has nowhere to go when that sponsor leaves. With a disabled manager, it goes to an account nobody uses. -This check reads the sponsors of agent identities, Agent Identity Blueprints and Blueprint Principals. It reports every user sponsor that has no manager or whose manager is a disabled account. Group and service principal sponsors have no manager and are not evaluated. +This check reads the sponsors of agent identities, Agent Identity Blueprints and Blueprint Principals. It reports every user sponsor that has no manager, or whose manager is not confirmed as an enabled account: a disabled account, or a manager with no account state, such as a contact. Group and service principal sponsors have no manager and are not evaluated. A manager that is set but no longer the right successor still resolves, and the transfer goes through. This check cannot see that case: review who each sponsor's manager is, not only whether one is set. diff --git a/tests/maester/entra/Test.MT.1286.ps1 b/tests/maester/entra/Test.MT.1286.ps1 index 8aa65df96a..443f155f6d 100644 --- a/tests/maester/entra/Test.MT.1286.ps1 +++ b/tests/maester/entra/Test.MT.1286.ps1 @@ -9,8 +9,9 @@ requires a populated manager attribute: without one, the sponsorship has nowhere to go. This check reads the sponsors of agent identities, Agent Identity Blueprints and Blueprint - Principals, and reports every user sponsor that has no manager or whose manager is a disabled - account. Group and service principal sponsors have no manager and are not evaluated. + Principals, and reports every user sponsor that has no manager, or whose manager is not + confirmed as an enabled account. Group and service principal sponsors have no manager and + are not evaluated. A manager that is set but no longer the right successor still resolves, so the transfer goes through. Directory data cannot show that case. @@ -111,6 +112,9 @@ $Issue = 'No manager.' } elseif ($Manager.accountEnabled -eq $false) { $Issue = "Manager $([string]$Manager.displayName) is a disabled account." + } elseif ($Manager.accountEnabled -ne $true) { + # A contact has no accountEnabled, and Graph can leave the property out of a user it returns. + $Issue = "Cannot confirm that manager $([string]$Manager.displayName) is an enabled account." } if ($Issue) { From d48c56042aacfea34a59ed3f32d1138e0d3e30b2 Mon Sep 17 00:00:00 2001 From: Julien Ly Date: Mon, 23 Nov 2026 13:26:49 +0100 Subject: [PATCH 7/7] MT.1286: describe non-user sponsors without naming service principals Graph returns a 400 when a service principal is added as a sponsor of a Blueprint Principal, and the Add sponsors pages for agent identities and blueprints list users or groups. The check already evaluates user sponsors only; this aligns the test doc, the help and the code comment. --- tests/maester/entra/Test.MT.1286.md | 2 +- tests/maester/entra/Test.MT.1286.ps1 | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/maester/entra/Test.MT.1286.md b/tests/maester/entra/Test.MT.1286.md index 18a1bd2ccc..35015f30bd 100644 --- a/tests/maester/entra/Test.MT.1286.md +++ b/tests/maester/entra/Test.MT.1286.md @@ -2,7 +2,7 @@ User sponsors of Agent ID objects should have a manager. Agent identities and Agent Identity Blueprints need an accountable sponsor. When a sponsor leaves or changes roles, the Lifecycle Workflows task **Transfer agent identity sponsorships to manager** adds the sponsor's manager as a sponsor and removes the user. The task requires a populated manager attribute. Without a manager, the sponsorship has nowhere to go when that sponsor leaves. With a disabled manager, it goes to an account nobody uses. -This check reads the sponsors of agent identities, Agent Identity Blueprints and Blueprint Principals. It reports every user sponsor that has no manager, or whose manager is not confirmed as an enabled account: a disabled account, or a manager with no account state, such as a contact. Group and service principal sponsors have no manager and are not evaluated. +This check reads the sponsors of agent identities, Agent Identity Blueprints and Blueprint Principals. It reports every user sponsor that has no manager, or whose manager is not confirmed as an enabled account: a disabled account, or a manager with no account state, such as a contact. Sponsors that are not users, such as groups, have no manager and are not evaluated. A manager that is set but no longer the right successor still resolves, and the transfer goes through. This check cannot see that case: review who each sponsor's manager is, not only whether one is set. diff --git a/tests/maester/entra/Test.MT.1286.ps1 b/tests/maester/entra/Test.MT.1286.ps1 index 443f155f6d..68d8e2f770 100644 --- a/tests/maester/entra/Test.MT.1286.ps1 +++ b/tests/maester/entra/Test.MT.1286.ps1 @@ -10,7 +10,7 @@ This check reads the sponsors of agent identities, Agent Identity Blueprints and Blueprint Principals, and reports every user sponsor that has no manager, or whose manager is not - confirmed as an enabled account. Group and service principal sponsors have no manager and + confirmed as an enabled account. Sponsors that are not users, such as groups, have no manager and are not evaluated. A manager that is set but no longer the right successor still resolves, so the transfer goes @@ -86,7 +86,7 @@ ) foreach ($Sponsor in $Sponsors) { - # Groups and service principals can be sponsors, but have no manager. + # Only user sponsors have a manager; groups and any other sponsor type are skipped. if ([string]$Sponsor.'@odata.type' -ne '#microsoft.graph.user') { continue } $SponsorId = [string]$Sponsor.id if ([string]::IsNullOrWhiteSpace($SponsorId)) { continue }