Skip to content

MT.1005 reports a raw %TestResult% when no user-targeted Conditional Access policy exists #2814

Open

Description

@julien-ly

What happens

In auto-detection mode (no EmergencyAccessAccounts configured), when no Conditional Access policy is left after the authentication context, service principal and agent identity filters, MT.1005 returns False and its result reads:

These Conditional Access policies don't have the emergency access group excluded:

%TestResult%
  • The sentence announces a list of policies, and there is none.
  • group is the default value of $EmergencyAccessUUIDType (L68), not a detected type.
  • The placeholder stays raw: Add-MtTestResultDetail only replaces it when -GraphObjects is not empty (L120, L191-L194), and the list of policies without the exclusion is empty (L92-L93).

@earbona23 measured it while reviewing #2811 (comment), with the same output on main and on that branch, so #2811 does not cause it.

Where it shows

The False verdict looks intended: the two unit tests under "Agent Identity and Service Principal policies" expect it (L462-L484, "When all policies are filtered out, there are no policies to check, so no emergency access is detected"). For comparison, the same tenant passes when an account is configured in EmergencyAccessAccounts ("All Conditional Access policies exclude the configured emergency access accounts or groups:"). This issue is only about the message.

Suggested change

In the auto-detection branch, when $PolicyCount is 0, return False with a message of its own and no placeholder, for example "No user-targeted Conditional Access policy was found, so no emergency access account or group could be detected." Whether this case should be skipped instead is the maintainers' call.

Reproduction

Code level, no tenant change. On main (2d659e8) and on the head of #2811 (f0d1dce), PowerShell 7 and Pester 5.7.1: 4 passed on both. The tests pin the current behaviour.

Pester
# $env:MAESTER_WT = '<path to a Maester clone>'; Invoke-Pester -Path ./Test.MT.1005.NoPolicy.Repro.Tests.ps1 -Output Detailed
Describe 'MT.1005 with no user-targeted Conditional Access policy' {
    BeforeAll {
        Import-Module (Join-Path $env:MAESTER_WT 'powershell/Maester.psd1') -Force
        . (Join-Path $env:MAESTER_WT 'powershell/tests/helpers/Use-MtModuleFunction.ps1')
        Use-MtModuleFunction -Name Test-MtCaEmergencyAccessExists
        Mock -ModuleName Maester Get-MtLicenseInformation { 'P1' }
        Mock -ModuleName Maester Add-MtTestResultDetail { $global:MtRepro = @{ Result = $Result; Objects = @($GraphObjects | Where-Object { $_ }) } }
        $script:userId = '11111111-1111-1111-1111-111111111111'
    }
    BeforeEach { $global:MtRepro = $null }

    It 'Control: auto-detection, one policy that excludes a user, passes' {
        Mock -ModuleName Maester Get-MtConditionalAccessPolicy {
            [pscustomobject]@{ id = 'p1'; displayName = 'Policy 1'; state = 'enabled'; conditions = [pscustomobject]@{ applications = [pscustomobject]@{ includeApplications = @('All') }; users = [pscustomobject]@{ includeUsers = @('All'); excludeUsers = @('11111111-1111-1111-1111-111111111111'); excludeGroups = @() } } }
        }
        Mock -ModuleName Maester Invoke-MtGraphRequest { [pscustomobject]@{ id = '11111111-1111-1111-1111-111111111111'; displayName = 'Break glass 1' } }
        Test-MtCaEmergencyAccessExists | Should -BeTrue
    }

    It 'Auto-detection, no Conditional Access policy at all: fails with a sentence about policies and a raw placeholder' {
        Mock -ModuleName Maester Get-MtConditionalAccessPolicy { @() }
        Test-MtCaEmergencyAccessExists | Should -BeFalse
        $global:MtRepro.Result | Should -BeExactly "These Conditional Access policies don't have the emergency access group excluded:`n`n%TestResult%"
        $global:MtRepro.Objects.Count | Should -Be 0
    }

    It 'Auto-detection, only an agent identity policy (filtered out): same output' {
        Mock -ModuleName Maester Get-MtConditionalAccessPolicy {
            [pscustomobject]@{ id = 'p2'; displayName = 'Agents'; state = 'enabled'; conditions = [pscustomobject]@{ applications = [pscustomobject]@{ includeApplications = @('All') }; clientApplications = [pscustomobject]@{ includeAgentIdServicePrincipals = @('All') }; users = [pscustomobject]@{ includeUsers = @(); excludeUsers = @(); excludeGroups = @() } } }
        }
        Test-MtCaEmergencyAccessExists | Should -BeFalse
        $global:MtRepro.Result | Should -BeExactly "These Conditional Access policies don't have the emergency access group excluded:`n`n%TestResult%"
        $global:MtRepro.Objects.Count | Should -Be 0
    }

    It 'Configured account, same tenant with no policy: passes' {
        Mock -ModuleName Maester Get-MtConditionalAccessPolicy { @() }
        Mock -ModuleName Maester Invoke-MtGraphRequest { [pscustomobject]@{ id = '11111111-1111-1111-1111-111111111111'; displayName = 'Break glass 1' } }
        Test-MtCaEmergencyAccessExists -EmergencyAccessAccounts @(@{ Id = $script:userId; Type = 'user' }) | Should -BeTrue
        $global:MtRepro.Result | Should -BeLike 'All Conditional Access policies exclude the configured emergency access accounts or groups:*'
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions