From 20d7683356e828ce3c48c5ef28f9b32e733cb04e Mon Sep 17 00:00:00 2001 From: Jeremy Finney Date: Wed, 11 Nov 2026 11:56:55 +1300 Subject: [PATCH 01/10] feat(cisa): align Teams checks with pinned baseline Add 14 CISA Teams checks and framework mappings, with unified app settings retrieval and lifecycle handling. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- powershell/Maester.psm1 | 4 + .../checks/cisa/teams/TeamsChecks.ps1 | 153 ++++++++++++++++ .../Get-MtTeamsUnifiedTenantSettings.ps1 | 133 ++++++++++++++ .../internal/session/Clear-ModuleVariable.ps1 | 1 + powershell/public/connect/Connect-Maester.ps1 | 2 + .../public/connect/Disconnect-Maester.ps1 | 5 + .../tests/functions/CisaTeamsChecks.Tests.ps1 | 164 ++++++++++++++++++ .../functions/Clear-ModuleVariable.Tests.ps1 | 17 +- .../tests/functions/Connect-Maester.Tests.ps1 | 6 +- .../functions/Disconnect-Maester.Tests.ps1 | 26 ++- .../functions/TeamsUnifiedSettings.Tests.ps1 | 110 ++++++++++++ .../functions/engine/Frameworks.Tests.ps1 | 10 +- .../general/Build-MaesterModule.Tests.ps1 | 8 +- tests/cisa/frameworks/cisa-scuba.json | 16 +- tests/cisa/teams/Test.CISA.MS.TEAMS.1.1.md | 20 +++ tests/cisa/teams/Test.CISA.MS.TEAMS.1.1.ps1 | 32 ++++ tests/cisa/teams/Test.CISA.MS.TEAMS.1.2.md | 20 +++ tests/cisa/teams/Test.CISA.MS.TEAMS.1.2.ps1 | 32 ++++ tests/cisa/teams/Test.CISA.MS.TEAMS.1.3.md | 20 +++ tests/cisa/teams/Test.CISA.MS.TEAMS.1.3.ps1 | 49 ++++++ tests/cisa/teams/Test.CISA.MS.TEAMS.1.4.md | 19 ++ tests/cisa/teams/Test.CISA.MS.TEAMS.1.4.ps1 | 45 +++++ tests/cisa/teams/Test.CISA.MS.TEAMS.1.5.md | 20 +++ tests/cisa/teams/Test.CISA.MS.TEAMS.1.5.ps1 | 32 ++++ tests/cisa/teams/Test.CISA.MS.TEAMS.1.6.md | 20 +++ tests/cisa/teams/Test.CISA.MS.TEAMS.1.6.ps1 | 29 ++++ tests/cisa/teams/Test.CISA.MS.TEAMS.1.7.md | 19 ++ tests/cisa/teams/Test.CISA.MS.TEAMS.1.7.ps1 | 47 +++++ tests/cisa/teams/Test.CISA.MS.TEAMS.2.1.md | 16 ++ tests/cisa/teams/Test.CISA.MS.TEAMS.2.1.ps1 | 65 +++++++ tests/cisa/teams/Test.CISA.MS.TEAMS.2.2.md | 18 ++ tests/cisa/teams/Test.CISA.MS.TEAMS.2.2.ps1 | 58 +++++++ tests/cisa/teams/Test.CISA.MS.TEAMS.2.3.md | 18 ++ tests/cisa/teams/Test.CISA.MS.TEAMS.2.3.ps1 | 51 ++++++ tests/cisa/teams/Test.CISA.MS.TEAMS.4.1.md | 21 +++ tests/cisa/teams/Test.CISA.MS.TEAMS.4.1.ps1 | 30 ++++ tests/cisa/teams/Test.CISA.MS.TEAMS.5.1.md | 18 ++ tests/cisa/teams/Test.CISA.MS.TEAMS.5.1.ps1 | 31 ++++ tests/cisa/teams/Test.CISA.MS.TEAMS.5.2.md | 18 ++ tests/cisa/teams/Test.CISA.MS.TEAMS.5.2.ps1 | 31 ++++ tests/cisa/teams/Test.CISA.MS.TEAMS.5.3.md | 18 ++ tests/cisa/teams/Test.CISA.MS.TEAMS.5.3.ps1 | 31 ++++ 42 files changed, 1472 insertions(+), 11 deletions(-) create mode 100644 powershell/internal/checks/cisa/teams/TeamsChecks.ps1 create mode 100644 powershell/internal/services/teams/Get-MtTeamsUnifiedTenantSettings.ps1 create mode 100644 powershell/tests/functions/CisaTeamsChecks.Tests.ps1 create mode 100644 powershell/tests/functions/TeamsUnifiedSettings.Tests.ps1 create mode 100644 tests/cisa/teams/Test.CISA.MS.TEAMS.1.1.md create mode 100644 tests/cisa/teams/Test.CISA.MS.TEAMS.1.1.ps1 create mode 100644 tests/cisa/teams/Test.CISA.MS.TEAMS.1.2.md create mode 100644 tests/cisa/teams/Test.CISA.MS.TEAMS.1.2.ps1 create mode 100644 tests/cisa/teams/Test.CISA.MS.TEAMS.1.3.md create mode 100644 tests/cisa/teams/Test.CISA.MS.TEAMS.1.3.ps1 create mode 100644 tests/cisa/teams/Test.CISA.MS.TEAMS.1.4.md create mode 100644 tests/cisa/teams/Test.CISA.MS.TEAMS.1.4.ps1 create mode 100644 tests/cisa/teams/Test.CISA.MS.TEAMS.1.5.md create mode 100644 tests/cisa/teams/Test.CISA.MS.TEAMS.1.5.ps1 create mode 100644 tests/cisa/teams/Test.CISA.MS.TEAMS.1.6.md create mode 100644 tests/cisa/teams/Test.CISA.MS.TEAMS.1.6.ps1 create mode 100644 tests/cisa/teams/Test.CISA.MS.TEAMS.1.7.md create mode 100644 tests/cisa/teams/Test.CISA.MS.TEAMS.1.7.ps1 create mode 100644 tests/cisa/teams/Test.CISA.MS.TEAMS.2.1.md create mode 100644 tests/cisa/teams/Test.CISA.MS.TEAMS.2.1.ps1 create mode 100644 tests/cisa/teams/Test.CISA.MS.TEAMS.2.2.md create mode 100644 tests/cisa/teams/Test.CISA.MS.TEAMS.2.2.ps1 create mode 100644 tests/cisa/teams/Test.CISA.MS.TEAMS.2.3.md create mode 100644 tests/cisa/teams/Test.CISA.MS.TEAMS.2.3.ps1 create mode 100644 tests/cisa/teams/Test.CISA.MS.TEAMS.4.1.md create mode 100644 tests/cisa/teams/Test.CISA.MS.TEAMS.4.1.ps1 create mode 100644 tests/cisa/teams/Test.CISA.MS.TEAMS.5.1.md create mode 100644 tests/cisa/teams/Test.CISA.MS.TEAMS.5.1.ps1 create mode 100644 tests/cisa/teams/Test.CISA.MS.TEAMS.5.2.md create mode 100644 tests/cisa/teams/Test.CISA.MS.TEAMS.5.2.ps1 create mode 100644 tests/cisa/teams/Test.CISA.MS.TEAMS.5.3.md create mode 100644 tests/cisa/teams/Test.CISA.MS.TEAMS.5.3.ps1 diff --git a/powershell/Maester.psm1 b/powershell/Maester.psm1 index f5b0d45188..8995f3a98e 100644 --- a/powershell/Maester.psm1 +++ b/powershell/Maester.psm1 @@ -32,6 +32,10 @@ $__MtSession = @{ SpoCache = @{} # Cache for SharePoint Online tenant settings retrieved via PnP FabricCache = @{} # Fabric admin API data cached for the current test run GitHubCache = @{} # Per-session REST response cache; cleared each Invoke-Maester run + TeamsCache = @{} # Teams admin API data cached for the current test run + TeamsAuthCache = @{} # Teams unified settings MSAL client; retained until disconnect + TeamsUseDeviceCode = $false # Whether Connect-Maester requested device-code authentication + TeamsEnvironmentName = $null # Explicit Teams PowerShell environment, when provided ADCache = @{} # Active Directory data cache ADConnection = $null # Active Directory connection state ADCredential = $null # Active Directory credential retained only for the connected session diff --git a/powershell/internal/checks/cisa/teams/TeamsChecks.ps1 b/powershell/internal/checks/cisa/teams/TeamsChecks.ps1 new file mode 100644 index 0000000000..a4c71576e2 --- /dev/null +++ b/powershell/internal/checks/cisa/teams/TeamsChecks.ps1 @@ -0,0 +1,153 @@ +function Get-MtCisaTeamsMeetingPolicies { + [CmdletBinding()] + [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseSingularNouns', '', Justification = 'The Teams cmdlet returns a collection of meeting policies.')] + param( + [switch]$GlobalOnly + ) + + $policies = if ($GlobalOnly) { + @(Get-CsTeamsMeetingPolicy -Identity Global -ErrorAction Stop) + } else { + @(Get-CsTeamsMeetingPolicy -ErrorAction Stop) + } + + if ($policies.Count -eq 0) { + throw 'Teams returned no meeting policies.' + } + if ($GlobalOnly -and ($policies.Count -ne 1 -or $policies[0].Identity -ne 'Global')) { + throw 'Teams did not return exactly one Global meeting policy.' + } + + return $policies +} + +function Test-MtCisaTeamsMeetingPolicyBoolean { + [CmdletBinding()] + param( + [Parameter(Mandatory)] + [string]$SettingName, + [Parameter(Mandatory)] + [bool]$ExpectedValue, + [Parameter(Mandatory)] + [string]$PolicyDescription + ) + + $policies = Get-MtCisaTeamsMeetingPolicies + $results = foreach ($policy in $policies) { + $setting = $policy.PSObject.Properties[$SettingName] + if ($null -eq $setting -or $setting.Value -isnot [bool]) { + throw "Teams meeting policy '$($policy.Identity)' did not return a Boolean value for $SettingName." + } + + [pscustomobject]@{ + Policy = $policy.Identity + Value = $setting.Value + Pass = $setting.Value -eq $ExpectedValue + } + } + + $passed = @($results | Where-Object { -not $_.Pass }).Count -eq 0 + $markdown = if ($passed) { + "Well done. $PolicyDescription is configured for every meeting policy.`n`n" + } else { + "$PolicyDescription is not configured for every meeting policy.`n`n" + } + $markdown += "| Policy | $SettingName | Result |`n| --- | --- | --- |`n" + foreach ($row in $results) { + $markdown += "| $($row.Policy) | $($row.Value) | $(if ($row.Pass) { 'Pass' } else { 'Fail' }) |`n" + } + Add-MtTestResultDetail -Result $markdown.TrimEnd() + return $passed +} + +function Get-MtCisaTeamsGlobalMeetingPolicy { + [CmdletBinding()] + param() + + $policies = Get-MtCisaTeamsMeetingPolicies -GlobalOnly + return $policies[0] +} + +function Get-MtCisaTeamsFederationConfiguration { + [CmdletBinding()] + param() + + $configurations = @(Get-CsTenantFederationConfiguration -ErrorAction Stop) + if ($configurations.Count -ne 1) { + throw 'Teams did not return exactly one tenant federation configuration.' + } + return $configurations[0] +} + +function Test-MtCisaTeamsIsUSGovernment { + [CmdletBinding()] + param() + + $context = Get-MgContext -ErrorAction Stop + if (-not $context) { + throw 'A Microsoft Graph context is required to determine the Teams environment.' + } + return ($context.Environment -in @('USGov', 'USGovDoD') -or + $__MtSession.TeamsEnvironmentName -in @('TeamsGCCH', 'TeamsDOD')) +} + +function Test-MtCisaTeamsAppControl { + [CmdletBinding()] + param( + [Parameter(Mandatory)] + [ValidateSet('DefaultApp', 'GlobalApp', 'PrivateApp')] + [string]$SettingName, + [Parameter(Mandatory)] + [string]$AppType + ) + + $settings = @(Get-MtTeamsUnifiedTenantSettings) + $setting = $settings | Where-Object { $_.SettingName -eq $SettingName } | Select-Object -First 1 + $policyProperty = switch ($SettingName) { + 'DefaultApp' { 'DefaultCatalogAppsType' } + 'GlobalApp' { 'GlobalCatalogAppsType' } + 'PrivateApp' { 'PrivateCatalogAppsType' } + } + + $rows = @() + if ($setting) { + $value = [string]$setting.SettingValue + $passed = $value -eq 'None' + $rows += [pscustomobject]@{ + Policy = 'Org-wide setting' + Value = $value + Pass = $passed + } + } else { + $policies = @(Get-CsTeamsAppPermissionPolicy -ErrorAction Stop) + if ($policies.Count -eq 0) { + throw 'Teams returned no app permission policies.' + } + + $rows = foreach ($policy in $policies) { + $type = $policy.PSObject.Properties[$policyProperty] + if ($null -eq $type -or $type.Value -notin @('AllowedAppList', 'BlockedAppList')) { + throw "Teams app permission policy '$($policy.Identity)' did not return a supported value for $policyProperty." + } + + [pscustomobject]@{ + Policy = $policy.Identity + Value = $type.Value + Pass = $type.Value -eq 'AllowedAppList' + } + } + $passed = @($rows | Where-Object { -not $_.Pass }).Count -eq 0 + } + + $markdown = if ($passed) { + "Well done. $AppType apps are restricted to the approved set.`n`n" + } else { + "$AppType apps are not restricted to the approved set.`n`n" + } + $markdown += "| Policy or setting | Value | Result |`n| --- | --- | --- |`n" + foreach ($row in $rows) { + $markdown += "| $($row.Policy) | $($row.Value) | $(if ($row.Pass) { 'Pass' } else { 'Fail' }) |`n" + } + Add-MtTestResultDetail -Result $markdown.TrimEnd() + return $passed +} diff --git a/powershell/internal/services/teams/Get-MtTeamsUnifiedTenantSettings.ps1 b/powershell/internal/services/teams/Get-MtTeamsUnifiedTenantSettings.ps1 new file mode 100644 index 0000000000..19ae99a930 --- /dev/null +++ b/powershell/internal/services/teams/Get-MtTeamsUnifiedTenantSettings.ps1 @@ -0,0 +1,133 @@ +function Get-MtTeamsUnifiedTenantSettings { + [CmdletBinding()] + [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseSingularNouns', '', Justification = 'The Teams API returns a collection of settings.')] + param() + + $context = Get-MgContext -ErrorAction Stop + if (-not $context) { + throw 'A Microsoft Graph context is required to retrieve Teams unified app settings.' + } + + $tenantId = [string]$context.TenantId + if ([string]::IsNullOrWhiteSpace($tenantId)) { + throw 'The Microsoft Graph context does not include a tenant ID.' + } + + if (($context.Environment -and $context.Environment -ne 'Global') -or + $__MtSession.TeamsEnvironmentName -in @('TeamsChina', 'TeamsGCCH', 'TeamsDOD')) { + return $null + } + if ($context.AuthType -ne 'Delegated') { + return $null + } + + $cacheKey = "UnifiedAppSettings:$tenantId" + if ($__MtSession.TeamsCache.ContainsKey($cacheKey)) { + return $__MtSession.TeamsCache[$cacheKey] + } + + $accessToken = Get-MtTeamsUnifiedSettingsAccessToken -Context $context + $response = Invoke-RestMethod ` + -Uri 'https://substrate.office.com/AdminAppCatalog/ps/v2/admin/unifiedApp/settings' ` + -Method Get ` + -Headers @{ Authorization = "Bearer $accessToken" } ` + -ErrorAction Stop + + if ($null -eq $response) { + throw 'The Teams unified app settings API returned an empty response.' + } + + $settings = if ($response.PSObject.Properties['value']) { + @($response.value) + } else { + @($response) + } + + foreach ($setting in $settings) { + if ($null -eq $setting -or + $null -eq $setting.PSObject.Properties['SettingName'] -or + $null -eq $setting.PSObject.Properties['SettingValue']) { + throw 'The Teams unified app settings API returned a setting without SettingName or SettingValue.' + } + } + + $__MtSession.TeamsCache[$cacheKey] = $settings + return $settings +} + +function Get-MtTeamsUnifiedSettingsAccessToken { + [CmdletBinding()] + param( + [Parameter(Mandatory)] + [object]$Context + ) + + $tenantId = [string]$Context.TenantId + $clientId = '12128f48-ec9e-42f0-b203-ea49fb6af367' + $scope = @('08ff1ce2-4973-4b08-86a3-ebed13badc7f/.default') + $authority = "https://login.microsoftonline.com/$tenantId" + $cacheKey = "$tenantId|$($Context.Environment)" + + if (-not ('Microsoft.Identity.Client.PublicClientApplicationBuilder' -as [type])) { + $authenticationModule = Get-Module Microsoft.Graph.Authentication | Sort-Object Version -Descending | Select-Object -First 1 + if (-not $authenticationModule) { + throw 'Microsoft.Graph.Authentication must be loaded before acquiring a Teams unified settings token.' + } + + $dependencyFolder = if ($PSVersionTable.PSEdition -eq 'Core') { 'Core' } else { 'Desktop' } + $msalPath = Join-Path $authenticationModule.ModuleBase "Dependencies\$dependencyFolder\Microsoft.Identity.Client.dll" + if (-not (Test-Path -LiteralPath $msalPath)) { + throw "Microsoft.Identity.Client.dll was not found at '$msalPath'." + } + $null = Add-Type -Path $msalPath -ErrorAction Stop + } + + if (-not $__MtSession.TeamsAuthCache.ContainsKey('Clients')) { + $__MtSession.TeamsAuthCache['Clients'] = @{} + } + + $clients = $__MtSession.TeamsAuthCache['Clients'] + if (-not $clients.ContainsKey($cacheKey)) { + $clients[$cacheKey] = [Microsoft.Identity.Client.PublicClientApplicationBuilder]::Create($clientId). + WithAuthority($authority). + WithRedirectUri('http://localhost'). + Build() + } + $application = $clients[$cacheKey] + + $accounts = @($application.GetAccountsAsync().GetAwaiter().GetResult()) + $account = $null + if ($Context.Account) { + $account = $accounts | Where-Object { $_.Username -eq [string]$Context.Account } | Select-Object -First 1 + } + + $result = $null + if ($account) { + try { + $result = $application.AcquireTokenSilent($scope, $account).ExecuteAsync().GetAwaiter().GetResult() + } catch [Microsoft.Identity.Client.MsalUiRequiredException] { + Write-Verbose 'Silent token acquisition requires user interaction.' + } + } + + if (-not $result -and $__MtSession.TeamsUseDeviceCode) { + $deviceCodeCallback = [System.Func[Microsoft.Identity.Client.DeviceCodeResult, System.Threading.Tasks.Task]] { + param($deviceCode) + Write-Information $deviceCode.Message -InformationAction Continue + [System.Threading.Tasks.Task]::CompletedTask + } + $result = $application.AcquireTokenWithDeviceCode($scope, $deviceCodeCallback).ExecuteAsync().GetAwaiter().GetResult() + } elseif (-not $result) { + $interactiveRequest = $application.AcquireTokenInteractive($scope).WithUseEmbeddedWebView($false) + if ($Context.Account) { + $interactiveRequest = $interactiveRequest.WithLoginHint([string]$Context.Account) + } + $result = $interactiveRequest.ExecuteAsync().GetAwaiter().GetResult() + } + + if (-not $result -or [string]::IsNullOrWhiteSpace($result.AccessToken)) { + throw 'Microsoft identity platform returned no access token for Teams unified app settings.' + } + + return $result.AccessToken +} diff --git a/powershell/internal/session/Clear-ModuleVariable.ps1 b/powershell/internal/session/Clear-ModuleVariable.ps1 index 2817a52795..9b220a878a 100644 --- a/powershell/internal/session/Clear-ModuleVariable.ps1 +++ b/powershell/internal/session/Clear-ModuleVariable.ps1 @@ -33,6 +33,7 @@ $__MtSession.GitHubCache = @{} $__MtSession.SpoCache = @{} $__MtSession.FabricCache = @{} + $__MtSession.TeamsCache = @{} $__MtSession.Console = $null $__MtSession.ReportAttachments = $null # $__MtSession.Connections = @() # Do not clear connections as they are used to track the connection state. This module variable should only be set by Connect-Maester and Disconnect-Maester. diff --git a/powershell/public/connect/Connect-Maester.ps1 b/powershell/public/connect/Connect-Maester.ps1 index e09e2e5b5d..28a97cde16 100644 --- a/powershell/public/connect/Connect-Maester.ps1 +++ b/powershell/public/connect/Connect-Maester.ps1 @@ -554,6 +554,8 @@ 'MicrosoftTeams' { if ($Service -contains 'Teams' -or $Service -contains 'All') { + $__MtSession.TeamsUseDeviceCode = $UseDeviceCode.IsPresent + $__MtSession.TeamsEnvironmentName = $TeamsEnvironmentName Write-Verbose 'Connecting to Microsoft Teams' try { $teamsParams = @{ ErrorAction = 'Stop' } diff --git a/powershell/public/connect/Disconnect-Maester.ps1 b/powershell/public/connect/Disconnect-Maester.ps1 index 3e183aa42c..e8d5405b66 100644 --- a/powershell/public/connect/Disconnect-Maester.ps1 +++ b/powershell/public/connect/Disconnect-Maester.ps1 @@ -71,6 +71,11 @@ } if ($MyInvocation.InvocationName -notmatch '(^|\\)Disconnect-MtGraph$') { + $__MtSession.TeamsCache = @{} + $__MtSession.TeamsAuthCache = @{} + $__MtSession.TeamsUseDeviceCode = $false + $__MtSession.TeamsEnvironmentName = $null + # The service list can be replaced by another Connect-Maester call. Clean up a loaded # ADOPS session too, without importing an optional module just to disconnect it. try { diff --git a/powershell/tests/functions/CisaTeamsChecks.Tests.ps1 b/powershell/tests/functions/CisaTeamsChecks.Tests.ps1 new file mode 100644 index 0000000000..287eb3becf --- /dev/null +++ b/powershell/tests/functions/CisaTeamsChecks.Tests.ps1 @@ -0,0 +1,164 @@ +BeforeAll { + Import-Module "$PSScriptRoot/../../Maester.psd1" -Force + + $script:createdTeamsStubs = @() + $teamsStubs = @{ + 'Get-CsTeamsMeetingPolicy' = { + [CmdletBinding()] + param([string]$Identity) + $null = $PSBoundParameters + } + 'Get-CsTeamsAppPermissionPolicy' = { + [CmdletBinding()] + param([string]$Identity) + $null = $PSBoundParameters + } + 'Get-CsTenantFederationConfiguration' = { + [CmdletBinding()] + param() + } + 'Get-CsTeamsMeetingBroadcastPolicy' = { + [CmdletBinding()] + param([string]$Identity) + $null = $PSBoundParameters + } + 'Get-CsTeamsClientConfiguration' = { + [CmdletBinding()] + param([string]$Identity) + $null = $PSBoundParameters + } + } + foreach ($name in $teamsStubs.Keys) { + if (-not (Get-Command $name -ErrorAction SilentlyContinue)) { + Set-Item -Path "Function:global:$name" -Value $teamsStubs[$name] + $script:createdTeamsStubs += $name + } + } +} + +AfterAll { + foreach ($name in $script:createdTeamsStubs) { + Remove-Item -Path "Function:global:$name" -ErrorAction SilentlyContinue + } +} + +Describe 'CISA Teams checks' { + BeforeEach { + InModuleScope Maester { + $__MtSession.TeamsCache = @{} + } + Mock Add-MtTestResultDetail -ModuleName Maester {} + } + + It 'Checks every meeting policy for a Boolean setting' { + Mock Get-CsTeamsMeetingPolicy -ModuleName Maester { + @( + [pscustomobject]@{ Identity = 'Global'; AllowAnonymousUsersToStartMeeting = $false } + [pscustomobject]@{ Identity = 'Custom'; AllowAnonymousUsersToStartMeeting = $false } + ) + } + + InModuleScope Maester { + Test-MtCisaTeamsMeetingPolicyBoolean -SettingName AllowAnonymousUsersToStartMeeting -ExpectedValue $false -PolicyDescription 'Anonymous meeting start' | Should -BeTrue + } + Should -Invoke Get-CsTeamsMeetingPolicy -ModuleName Maester -Times 1 -Exactly + } + + It 'Fails a meeting policy when the setting differs from the baseline' { + Mock Get-CsTeamsMeetingPolicy -ModuleName Maester { + @( + [pscustomobject]@{ Identity = 'Global'; AllowAnonymousUsersToStartMeeting = $false } + [pscustomobject]@{ Identity = 'Custom'; AllowAnonymousUsersToStartMeeting = $true } + ) + } + + InModuleScope Maester { + Test-MtCisaTeamsMeetingPolicyBoolean -SettingName AllowAnonymousUsersToStartMeeting -ExpectedValue $false -PolicyDescription 'Anonymous meeting start' | Should -BeFalse + } + } + + It 'Errors when a meeting policy does not return the checked setting' { + Mock Get-CsTeamsMeetingPolicy -ModuleName Maester { + [pscustomobject]@{ Identity = 'Global' } + } + + { InModuleScope Maester { Test-MtCisaTeamsMeetingPolicyBoolean -SettingName AllowAnonymousUsersToStartMeeting -ExpectedValue $false -PolicyDescription 'Anonymous meeting start' } } | Should -Throw '*did not return a Boolean value*' + } + + It 'Passes external federation when disabled' { + Mock Get-MtCisaTeamsFederationConfiguration -ModuleName Maester { + [pscustomobject]@{ AllowFederatedUsers = $false } + } + + InModuleScope Maester { + Test-MtCisaTeamsExternalAccessScope | Should -BeTrue + } + } + + It 'Fails external federation when all known domains are allowed' { + Mock Get-MtCisaTeamsFederationConfiguration -ModuleName Maester { + $domains = [pscustomobject]@{} + $domains.PSObject.TypeNames.Insert(0, 'AllowAllKnownDomains') + [pscustomobject]@{ + AllowFederatedUsers = $true + AllowedDomains = $domains + } + } + + InModuleScope Maester { + Test-MtCisaTeamsExternalAccessScope | Should -BeFalse + } + } + + It 'Passes external federation when only configured domains are allowed' { + Mock Get-MtCisaTeamsFederationConfiguration -ModuleName Maester { + [pscustomobject]@{ + AllowFederatedUsers = $true + AllowedDomains = [pscustomobject]@{ AllowedDomain = @('contoso.com') } + } + } + + InModuleScope Maester { + Test-MtCisaTeamsExternalAccessScope | Should -BeTrue + } + } + + It 'Uses an available unified org-wide app setting instead of legacy policies' { + Mock Get-MtTeamsUnifiedTenantSettings -ModuleName Maester { + @([pscustomobject]@{ SettingName = 'GlobalApp'; SettingValue = 'None' }) + } + Mock Get-CsTeamsAppPermissionPolicy -ModuleName Maester { throw 'Legacy policies should not be queried.' } + + InModuleScope Maester { + Test-MtCisaTeamsAppControl -SettingName GlobalApp -AppType Third-party | Should -BeTrue + } + Should -Invoke Get-CsTeamsAppPermissionPolicy -ModuleName Maester -Times 0 -Exactly + } + + It 'Fails an available unified org-wide app setting that is not None' { + Mock Get-MtTeamsUnifiedTenantSettings -ModuleName Maester { + @([pscustomobject]@{ SettingName = 'PrivateApp'; SettingValue = 'All' }) + } + Mock Get-CsTeamsAppPermissionPolicy -ModuleName Maester { throw 'Legacy policies should not be queried.' } + + InModuleScope Maester { + Test-MtCisaTeamsAppControl -SettingName PrivateApp -AppType Custom | Should -BeFalse + } + Should -Invoke Get-CsTeamsAppPermissionPolicy -ModuleName Maester -Times 0 -Exactly + } + + It 'Falls back to all legacy app permission policies when the unified setting is unavailable' { + Mock Get-MtTeamsUnifiedTenantSettings -ModuleName Maester { $null } + Mock Get-CsTeamsAppPermissionPolicy -ModuleName Maester { + @( + [pscustomobject]@{ Identity = 'Global'; DefaultCatalogAppsType = 'AllowedAppList' } + [pscustomobject]@{ Identity = 'Custom'; DefaultCatalogAppsType = 'BlockedAppList' } + ) + } + + InModuleScope Maester { + Test-MtCisaTeamsAppControl -SettingName DefaultApp -AppType Microsoft | Should -BeFalse + } + Should -Invoke Get-CsTeamsAppPermissionPolicy -ModuleName Maester -Times 1 -Exactly + } +} diff --git a/powershell/tests/functions/Clear-ModuleVariable.Tests.ps1 b/powershell/tests/functions/Clear-ModuleVariable.Tests.ps1 index 23afa4bb0b..cd0e63a8b0 100644 --- a/powershell/tests/functions/Clear-ModuleVariable.Tests.ps1 +++ b/powershell/tests/functions/Clear-ModuleVariable.Tests.ps1 @@ -1,4 +1,4 @@ -BeforeAll { +BeforeAll { Import-Module "$PSScriptRoot/../../Maester.psd1" -Force } @@ -9,6 +9,7 @@ Describe 'Clear-ModuleVariable — GitHub session lifecycle' { $__MtSession.GitHubAuthHeader = @{ Authorization = 'Bearer testtoken' } $__MtSession.GitHubCache = @{ somekey = 'cached-value' } } + } AfterEach { @@ -49,3 +50,17 @@ Describe 'Clear-ModuleVariable — GitHub session lifecycle' { } } } + +Describe 'Clear-ModuleVariable - Teams API cache lifecycle' { + It 'Clears per-run Teams settings but preserves the connected MSAL client' { + InModuleScope Maester { + $client = [pscustomobject]@{ Cached = $true } + $__MtSession.TeamsCache = @{ 'UnifiedAppSettings:tenant' = @('setting') } + $__MtSession.TeamsAuthCache = @{ Clients = @{ tenant = $client } } + Clear-ModuleVariable + + $__MtSession.TeamsCache.Count | Should -Be 0 + $__MtSession.TeamsAuthCache.Clients.tenant.Cached | Should -BeTrue + } + } +} diff --git a/powershell/tests/functions/Connect-Maester.Tests.ps1 b/powershell/tests/functions/Connect-Maester.Tests.ps1 index 950f319473..1691ff22c6 100644 --- a/powershell/tests/functions/Connect-Maester.Tests.ps1 +++ b/powershell/tests/functions/Connect-Maester.Tests.ps1 @@ -1,4 +1,4 @@ -BeforeAll { +BeforeAll { Import-Module "$PSScriptRoot/../../Maester.psd1" -Force # Keep -Service All tests isolated from optional service modules that may not be installed. @@ -303,6 +303,10 @@ Describe 'Connect-Maester connection summary' { Should -Invoke Connect-MicrosoftTeams -ModuleName Maester -Times 1 -Exactly -ParameterFilter { $UseDeviceAuthentication -and $TeamsEnvironmentName -eq 'TeamsGCCH' } + InModuleScope Maester { + $__MtSession.TeamsUseDeviceCode | Should -BeTrue + $__MtSession.TeamsEnvironmentName | Should -Be 'TeamsGCCH' + } } Context 'When an Az context from another tenant already exists' { diff --git a/powershell/tests/functions/Disconnect-Maester.Tests.ps1 b/powershell/tests/functions/Disconnect-Maester.Tests.ps1 index b3eeca9561..635725a78b 100644 --- a/powershell/tests/functions/Disconnect-Maester.Tests.ps1 +++ b/powershell/tests/functions/Disconnect-Maester.Tests.ps1 @@ -1,4 +1,4 @@ -BeforeAll { +BeforeAll { Import-Module "$PSScriptRoot/../../Maester.psd1" -Force } @@ -177,6 +177,7 @@ Describe 'Disconnect-Maester - Active Directory session lifecycle' { Connected = $true DomainController = 'dc01.contoso.com' } + $__MtSession.ADCredential = [PSCredential]::new('CONTOSO\Maester', ([System.Security.SecureString]::new())) $__MtSession.ADCache = @{ DomainState = [PSCustomObject]@{ Domain = 'contoso.com' } } } @@ -216,6 +217,29 @@ Describe 'Disconnect-Maester - Active Directory session lifecycle' { } } +Describe 'Disconnect-Maester - Teams unified settings authentication lifecycle' { + BeforeEach { + Mock Get-Module -ModuleName Maester { $null } -ParameterFilter { $Name -eq 'ADOPS' } + InModuleScope Maester { + $__MtSession.Connections = @() + $__MtSession.TeamsCache = @{ 'UnifiedAppSettings:tenant' = @('stale') } + $__MtSession.TeamsAuthCache = @{ Clients = @{ tenant = 'cached-client' } } + $__MtSession.TeamsUseDeviceCode = $true + $__MtSession.TeamsEnvironmentName = 'TeamsGCCH' + } + } + + It 'Clears the Teams authentication client and device-code preference' { + Disconnect-Maester + InModuleScope Maester { + $__MtSession.TeamsCache.Count | Should -Be 0 + $__MtSession.TeamsAuthCache.Count | Should -Be 0 + $__MtSession.TeamsUseDeviceCode | Should -BeFalse + $__MtSession.TeamsEnvironmentName | Should -BeNullOrEmpty + } + } +} + Describe 'Disconnect-Maester - service disconnects' { BeforeAll { # The Exchange Online and Teams modules are optional; stand in for their commands when they are not installed. diff --git a/powershell/tests/functions/TeamsUnifiedSettings.Tests.ps1 b/powershell/tests/functions/TeamsUnifiedSettings.Tests.ps1 new file mode 100644 index 0000000000..3574152230 --- /dev/null +++ b/powershell/tests/functions/TeamsUnifiedSettings.Tests.ps1 @@ -0,0 +1,110 @@ +BeforeAll { + Import-Module "$PSScriptRoot/../../Maester.psd1" -Force +} + +Describe 'Get-MtTeamsUnifiedTenantSettings' { + BeforeEach { + InModuleScope Maester { + $__MtSession.TeamsCache = @{} + $__MtSession.TeamsAuthCache = @{} + $__MtSession.TeamsEnvironmentName = $null + } + Mock Get-MgContext -ModuleName Maester { + [pscustomobject]@{ + TenantId = '11111111-1111-1111-1111-111111111111' + Environment = 'Global' + AuthType = 'Delegated' + Account = 'admin@contoso.com' + } + } + Mock Get-MtTeamsUnifiedSettingsAccessToken -ModuleName Maester { + 'test-token' + } + Mock Invoke-RestMethod -ModuleName Maester { + @([pscustomobject]@{ SettingName = 'DefaultApp'; SettingValue = 'None' }) + } + } + + It 'Retrieves settings with a delegated token and caches them for the current run' { + $first = InModuleScope Maester { @(Get-MtTeamsUnifiedTenantSettings) } + $second = InModuleScope Maester { @(Get-MtTeamsUnifiedTenantSettings) } + + $first | Should -HaveCount 1 + $first[0].SettingName | Should -Be 'DefaultApp' + $second[0].SettingValue | Should -Be 'None' + Should -Invoke Get-MtTeamsUnifiedSettingsAccessToken -ModuleName Maester -Times 1 -Exactly + Should -Invoke Invoke-RestMethod -ModuleName Maester -Times 1 -Exactly -ParameterFilter { + $Uri -eq 'https://substrate.office.com/AdminAppCatalog/ps/v2/admin/unifiedApp/settings' -and + $Method -eq 'Get' -and + $Headers.Authorization -eq 'Bearer test-token' + } + } + + It 'Uses legacy policies in US Government environments' { + Mock Get-MgContext -ModuleName Maester { + [pscustomobject]@{ + TenantId = '11111111-1111-1111-1111-111111111111' + Environment = 'USGov' + AuthType = 'Delegated' + } + } + + InModuleScope Maester { Get-MtTeamsUnifiedTenantSettings } | Should -BeNullOrEmpty + Should -Invoke Invoke-RestMethod -ModuleName Maester -Times 0 -Exactly + } + + It 'Uses legacy policies in US Government DoD environments' { + Mock Get-MgContext -ModuleName Maester { + [pscustomobject]@{ + TenantId = '11111111-1111-1111-1111-111111111111' + Environment = 'USGovDoD' + AuthType = 'Delegated' + } + } + + InModuleScope Maester { Get-MtTeamsUnifiedTenantSettings } | Should -BeNullOrEmpty + Should -Invoke Invoke-RestMethod -ModuleName Maester -Times 0 -Exactly + } + + It 'Uses legacy policies when Teams is connected to GCC High' { + InModuleScope Maester { + $__MtSession.TeamsEnvironmentName = 'TeamsGCCH' + } + + InModuleScope Maester { Get-MtTeamsUnifiedTenantSettings } | Should -BeNullOrEmpty + Should -Invoke Invoke-RestMethod -ModuleName Maester -Times 0 -Exactly + } + + It 'Uses legacy policies for app-only authentication' { + Mock Get-MgContext -ModuleName Maester { + [pscustomobject]@{ + TenantId = '11111111-1111-1111-1111-111111111111' + Environment = 'Global' + AuthType = 'AppOnly' + } + } + + InModuleScope Maester { Get-MtTeamsUnifiedTenantSettings } | Should -BeNullOrEmpty + Should -Invoke Invoke-RestMethod -ModuleName Maester -Times 0 -Exactly + } + + It 'Throws when the unified settings API returns a malformed setting' { + Mock Invoke-RestMethod -ModuleName Maester { + @([pscustomobject]@{ SettingName = 'DefaultApp' }) + } + + { InModuleScope Maester { Get-MtTeamsUnifiedTenantSettings } } | Should -Throw '*without SettingName or SettingValue*' + } + + It 'Surfaces API request failures instead of silently falling back' { + Mock Invoke-RestMethod -ModuleName Maester { throw 'request failed' } + + { InModuleScope Maester { Get-MtTeamsUnifiedTenantSettings } } | Should -Throw '*request failed*' + } + + It 'Throws when no Graph context is available' { + Mock Get-MgContext -ModuleName Maester { $null } + + { InModuleScope Maester { Get-MtTeamsUnifiedTenantSettings } } | Should -Throw '*Graph context is required*' + } +} diff --git a/powershell/tests/functions/engine/Frameworks.Tests.ps1 b/powershell/tests/functions/engine/Frameworks.Tests.ps1 index 06a6372fd8..75eb9ee1af 100644 --- a/powershell/tests/functions/engine/Frameworks.Tests.ps1 +++ b/powershell/tests/functions/engine/Frameworks.Tests.ps1 @@ -1,4 +1,4 @@ -# Benchmarks and framework files (docs/proposals/maester-3.0-test-attributes.md, section 6): the Frameworks a +# Benchmarks and framework files (docs/proposals/maester-3.0-test-attributes.md, section 6): the Frameworks a # suite.json declares, the framework files beside it, the Control that maps a test to a requirement, Benchmark and # Control on a test, on its rows and in the catalog, what the catalog build rejects, and what a run does with a # custom suite whose framework file is wrong. @@ -336,7 +336,7 @@ Describe 'Benchmark of the tests shipped with Maester' { It 'Gives every test a Benchmark of