diff --git a/powershell/tests/functions/Test-MtCaMfaForAllUsers.Tests.ps1 b/powershell/tests/functions/Test-MtCaMfaForAllUsers.Tests.ps1 new file mode 100644 index 0000000000..6cadf3a3b6 --- /dev/null +++ b/powershell/tests/functions/Test-MtCaMfaForAllUsers.Tests.ps1 @@ -0,0 +1,133 @@ +Describe 'Test-MtCaMfaForAllUsers' { + 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-MtCaMfaForAllUsers + + # An enabled policy that targets all users and all resources. + function Get-TestCaPolicy { + param( + [string] $DisplayName, + [string[]] $BuiltInControls = @(), + [string[]] $CustomAuthenticationFactors = @(), + [string] $StrengthRequirementsSatisfied, + [ValidateSet('OR', 'AND')] + [string] $Operator = 'OR', + [switch] $NoGrantControls + ) + $grantControls = $null + if (-not $NoGrantControls) { + $authenticationStrength = $null + if ($StrengthRequirementsSatisfied) { + $authenticationStrength = [pscustomobject]@{ requirementsSatisfied = $StrengthRequirementsSatisfied } + } + $grantControls = [pscustomobject]@{ + operator = $Operator + builtInControls = $BuiltInControls + customAuthenticationFactors = $CustomAuthenticationFactors + authenticationStrength = $authenticationStrength + } + } + [pscustomobject]@{ + displayName = $DisplayName + state = 'enabled' + conditions = [pscustomobject]@{ + users = [pscustomobject]@{ includeUsers = @('All') } + applications = [pscustomobject]@{ includeApplications = @('All') } + } + grantControls = $grantControls + } + } + } + + BeforeEach { + Mock Add-MtTestResultDetail -ModuleName Maester { } + } + + It 'Passes for a policy that requires built-in MFA' { + $policies = @(Get-TestCaPolicy -DisplayName 'Require MFA' -BuiltInControls 'mfa') + Mock Get-MtConditionalAccessPolicy -ModuleName Maester { $policies } + + Test-MtCaMfaForAllUsers | Should -BeTrue + } + + It 'Passes for a policy that requires an authentication strength that satisfies MFA' { + $policies = @(Get-TestCaPolicy -DisplayName 'Require phishing-resistant MFA' -StrengthRequirementsSatisfied 'mfa') + Mock Get-MtConditionalAccessPolicy -ModuleName Maester { $policies } + + Test-MtCaMfaForAllUsers | Should -BeTrue + } + + It 'Fails for a policy that relies on a custom control (), and lists the policy' -ForEach @( + @{ Operator = 'OR' } + @{ Operator = 'AND' } + ) { + $policies = @(Get-TestCaPolicy -DisplayName 'Legacy MFA provider' -CustomAuthenticationFactors 'ContosoMFA' -Operator $Operator) + Mock Get-MtConditionalAccessPolicy -ModuleName Maester { $policies } + + Test-MtCaMfaForAllUsers | Should -BeFalse + Should -Invoke Add-MtTestResultDetail -ModuleName Maester -Times 1 -Exactly -ParameterFilter { + $Result -like 'No Conditional Access policy requires*' -and + $Result -like '*custom control*' -and + $Result -like '*- Legacy MFA provider*' + } + } + + It 'Fails for a policy that a custom control satisfies instead of built-in MFA (OR), and lists the policy' { + $policies = @(Get-TestCaPolicy -DisplayName 'MFA or legacy provider' -BuiltInControls 'mfa' -CustomAuthenticationFactors 'ContosoMFA') + Mock Get-MtConditionalAccessPolicy -ModuleName Maester { $policies } + + Test-MtCaMfaForAllUsers | Should -BeFalse + Should -Invoke Add-MtTestResultDetail -ModuleName Maester -Times 1 -Exactly -ParameterFilter { + $Result -like '*- MFA or legacy provider*' + } + } + + It 'Passes for a policy that requires built-in MFA and a custom control (AND)' { + $policies = @(Get-TestCaPolicy -DisplayName 'MFA and legacy provider' -BuiltInControls 'mfa' -CustomAuthenticationFactors 'ContosoMFA' -Operator 'AND') + Mock Get-MtConditionalAccessPolicy -ModuleName Maester { $policies } + + Test-MtCaMfaForAllUsers | Should -BeTrue + Should -Invoke Add-MtTestResultDetail -ModuleName Maester -Times 1 -Exactly -ParameterFilter { + $Result -notlike '*custom control*' + } + } + + It 'Fails for a policy that requires a compliant device and a custom control (AND), and lists the policy' { + $policies = @(Get-TestCaPolicy -DisplayName 'Compliant device and legacy provider' -BuiltInControls 'compliantDevice' -CustomAuthenticationFactors 'ContosoMFA' -Operator 'AND') + Mock Get-MtConditionalAccessPolicy -ModuleName Maester { $policies } + + Test-MtCaMfaForAllUsers | Should -BeFalse + Should -Invoke Add-MtTestResultDetail -ModuleName Maester -Times 1 -Exactly -ParameterFilter { + $Result -like '*- Compliant device and legacy provider*' + } + } + + It 'Passes for a policy that requires MFA, and still lists a separate custom control policy ()' -ForEach @( + @{ Operator = 'OR' } + @{ Operator = 'AND' } + ) { + $policies = @( + Get-TestCaPolicy -DisplayName 'Require MFA' -BuiltInControls 'mfa' + Get-TestCaPolicy -DisplayName 'Legacy MFA provider' -CustomAuthenticationFactors 'ContosoMFA' -Operator $Operator + ) + Mock Get-MtConditionalAccessPolicy -ModuleName Maester { $policies } + + Test-MtCaMfaForAllUsers | Should -BeTrue + Should -Invoke Add-MtTestResultDetail -ModuleName Maester -Times 1 -Exactly -ParameterFilter { + $Result -like 'The following Conditional Access policies require*' -and + $Result -like '*- Legacy MFA provider*' + } + } + + It 'Fails for a policy with session controls only' { + $policies = @(Get-TestCaPolicy -DisplayName 'Sign-in frequency' -NoGrantControls) + Mock Get-MtConditionalAccessPolicy -ModuleName Maester { $policies } + + Test-MtCaMfaForAllUsers | Should -BeFalse + Should -Invoke Add-MtTestResultDetail -ModuleName Maester -Times 1 -Exactly -ParameterFilter { + $Result -notlike '*custom control*' + } + } +} diff --git a/tests/maester/entra/Test.MT.1007.md b/tests/maester/entra/Test.MT.1007.md index cb3c2fb301..8c953f8c65 100644 --- a/tests/maester/entra/Test.MT.1007.md +++ b/tests/maester/entra/Test.MT.1007.md @@ -1,5 +1,7 @@ This test checks if the tenant has at least one Conditional Access policy requiring MFA for all users. -See [Require MFA for all users - Microsoft Learn](https://learn.microsoft.com/entra/identity/conditional-access/howto-conditional-access-policy-all-users-mfa) +A policy that accepts a custom control instead of MFA doesn't count: custom controls don't satisfy the multifactor authentication requirement, and Microsoft deprecated them in favor of external MFA. The test result lists these policies. A policy that requires MFA and a custom control (AND) still counts. + +See [Require MFA for all users - Microsoft Learn](https://learn.microsoft.com/entra/identity/conditional-access/howto-conditional-access-policy-all-users-mfa) and [Migrate from custom controls to external multifactor authentication - Microsoft Learn](https://learn.microsoft.com/entra/identity/conditional-access/how-to-migrate-custom-controls-external-mfa) %TestResult% \ No newline at end of file diff --git a/tests/maester/entra/Test.MT.1007.ps1 b/tests/maester/entra/Test.MT.1007.ps1 index 7af400df45..8d4b998469 100644 --- a/tests/maester/entra/Test.MT.1007.ps1 +++ b/tests/maester/entra/Test.MT.1007.ps1 @@ -6,8 +6,13 @@ .Description MFA for all users Conditional Access policy can be used to require MFA for all users in the tenant. + A policy that accepts a custom control instead of MFA doesn't count: custom controls don't satisfy the + multifactor authentication requirement, and Microsoft deprecated them in favor of external MFA. A policy + that requires MFA and a custom control (AND) still counts. + Learn more: https://learn.microsoft.com/entra/identity/conditional-access/howto-conditional-access-policy-all-users-mfa + https://learn.microsoft.com/entra/identity/conditional-access/how-to-migrate-custom-controls-external-mfa .Example Test-MtCaMfaForAllUsers @@ -31,7 +36,7 @@ Service = 'Graph', License = 'AAD_PREMIUM', Author = 'f-bader', - Contributor = ('brianreidc7', 'SamErde') + Contributor = ('brianreidc7', 'SamErde', 'julien-ly') )] [CmdletBinding()] [OutputType([bool])] @@ -49,6 +54,9 @@ -not $_.conditions.signInRiskLevels } $policiesResult = New-Object System.Collections.ArrayList + # Policies that target all users and all resources and accept a custom control instead of MFA. Custom + # controls don't satisfy the multifactor authentication requirement, so these policies don't count. + $customControlPolicies = New-Object System.Collections.ArrayList $result = $false foreach ($policy in $policies) { @@ -60,20 +68,23 @@ ).Count -gt 0 } - if ( - ( - $policy.grantControls.builtInControls -contains 'mfa' -or - $policy.grantControls.authenticationStrength.requirementsSatisfied -contains 'mfa' -or - $hasCustomAuthenticationFactor - ) -and - $policy.conditions.users.includeUsers -eq 'All' -and + $targetsAllUsersAndResources = $policy.conditions.users.includeUsers -eq 'All' -and $policy.conditions.applications.includeApplications -eq 'All' - ) { + $requiresMfa = $policy.grantControls.builtInControls -contains 'mfa' -or + $policy.grantControls.authenticationStrength.requirementsSatisfied -contains 'mfa' + # A custom control takes the place of MFA, unless the grant requires MFA as well (AND). + $customControlReplacesMfa = $hasCustomAuthenticationFactor -and + (-not $requiresMfa -or $policy.grantControls.operator -ne 'AND') + + if ($requiresMfa -and -not $customControlReplacesMfa -and $targetsAllUsersAndResources) { $result = $true $CurrentResult = $true $policiesResult.Add($policy) | Out-Null } else { $CurrentResult = $false + if ($customControlReplacesMfa -and $targetsAllUsersAndResources) { + $customControlPolicies.Add($policy) | Out-Null + } } Write-Verbose "$($policy.displayName) - $CurrentResult" } @@ -84,6 +95,14 @@ $testResult = 'No Conditional Access policy requires multi-factor authentication for all users.' } + if ($customControlPolicies.Count -gt 0) { + $customControlList = ($customControlPolicies | ForEach-Object { "- $(Get-MtSafeMarkdown $_.displayName)" }) -join "`n" + $migrationLink = 'https://learn.microsoft.com/entra/identity/conditional-access/how-to-migrate-custom-controls-external-mfa' + $testResult += "`n`nThe following policies target all users and accept a custom control instead of multifactor authentication. " + + "A custom control doesn't satisfy the multifactor authentication requirement, so they don't count. Custom controls are deprecated: " + + "[migrate them to external MFA]($migrationLink).`n`n$customControlList" + } + Add-MtTestResultDetail -Result $testResult -GraphObjects $policiesResult -GraphObjectType ConditionalAccess return $result