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..fe021106bb --- /dev/null +++ b/powershell/tests/functions/Test-MtEntraAgentSponsorManager.Tests.ps1 @@ -0,0 +1,133 @@ +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 { + # 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: $([System.Net.HttpStatusCode]$StatusCode) ($($response.ReasonPhrase)).", $response) + } + } + + BeforeEach { + $script:TestResult = $null + $script:SkippedBecause = $null + + Mock -ModuleName Maester Add-MtTestResultDetail { + param($Result, $SkippedBecause) + + $script:TestResult = $Result + $script:SkippedBecause = $SkippedBecause + } + + # 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' { + # 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 + + $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 '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' } + } -ParameterFilter { $RelativeUri -like '*/sponsors' } + + Test-MtEntraAgentSponsorManager | Should -BeNullOrEmpty + + $script:SkippedBecause | Should -Be 'NotApplicable' + $script:TestResult | 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 { throw (Get-HttpError 404) } -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 '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 -Throw -ExpectedMessage '*InternalServerError*' + + $script:TestResult | Should -BeNullOrEmpty + $script:SkippedBecause | Should -BeNullOrEmpty + } +} diff --git a/tests/maester/entra/Test.MT.1286.md b/tests/maester/entra/Test.MT.1286.md new file mode 100644 index 0000000000..35015f30bd --- /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 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. + +### 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..68d8e2f770 --- /dev/null +++ b/tests/maester/entra/Test.MT.1286.ps1 @@ -0,0 +1,172 @@ +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 not + 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 + 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 = 'Low', + 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() + + $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() + + try { + 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) { + # 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 } + $UserSponsorships++ + + if (-not $ManagerBySponsorId.ContainsKey($SponsorId)) { + $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. + # 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 + } + $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." + } 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) { + $Findings.Add([pscustomobject]@{ + ObjectType = $Kind.Type + ObjectId = $ObjectId + ObjectName = [string]$Object.displayName + Sponsor = [string]$Sponsor.displayName + SponsorUpn = [string]$Sponsor.userPrincipalName + Issue = $Issue + }) + } + } + } + } + } 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 ($UserSponsorships -eq 0) { + Add-MtTestResultDetail -SkippedBecause NotApplicable -Result '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 + return $false +}