Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 133 additions & 0 deletions powershell/tests/functions/Test-MtCaMfaForAllUsers.Tests.ps1
Original file line number Diff line number Diff line change
@@ -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 (<Operator>), 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
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
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 (<Operator>)' -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*'
}
}
}
4 changes: 3 additions & 1 deletion tests/maester/entra/Test.MT.1007.md
Original file line number Diff line number Diff line change
@@ -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)
<!--- Results --->
%TestResult%
37 changes: 28 additions & 9 deletions tests/maester/entra/Test.MT.1007.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -31,7 +36,7 @@
Service = 'Graph',
License = 'AAD_PREMIUM',
Author = 'f-bader',
Contributor = ('brianreidc7', 'SamErde')
Contributor = ('brianreidc7', 'SamErde', 'julien-ly')
)]
[CmdletBinding()]
[OutputType([bool])]
Expand All @@ -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) {
Expand All @@ -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"
}
Expand All @@ -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
Expand Down
Loading