Repository navigation
Add CISA Teams baseline checks #2789
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
JeremyFinney-lgtm
wants to merge
11
commits into
maester365:main
Choose a base branch
from
JeremyFinney-lgtm:feat/cisa-teams-baseline-alignment-followup
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
20d7683
feat(cisa): align Teams checks with pinned baseline
JeremyFinney-lgtm 0d21700
fix(cisa): evaluate Teams federation configurations
JeremyFinney-lgtm fee6b31
Address CISA Teams review feedback
JeremyFinney-lgtm 47e5c8d
Merge upstream main into CISA Teams baseline checks
JeremyFinney-lgtm 35f3b9a
fix(cisa-teams): address review feedback
JeremyFinney-lgtm 534c1b6
fix(teams): pass typed scopes to unified settings authentication
JeremyFinney-lgtm 7a9dc27
fix(cisa-teams): credit auth fix reporter
JeremyFinney-lgtm 8badfac
fix: show device codes and reload built-in tests
JeremyFinney-lgtm 62ae1f1
fix: isolate built-in test reloads
JeremyFinney-lgtm 31183b4
fix: preserve built-in test reload isolation
JeremyFinney-lgtm 35d4d59
fix: reject cross-file test ID collisions on reload
JeremyFinney-lgtm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,154 @@ | ||
| 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-MtCisaTeamsFederationConfigurations { | ||
| [CmdletBinding()] | ||
| [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseSingularNouns', '', Justification = 'Teams can return multiple federation configurations.')] | ||
| param() | ||
|
|
||
| $configurations = @(Get-CsTenantFederationConfiguration -ErrorAction Stop) | ||
| if ($configurations.Count -eq 0) { | ||
| throw 'Teams returned no tenant federation configurations.' | ||
| } | ||
| return $configurations | ||
|
github-advanced-security[bot] marked this conversation as resolved.
Fixed
|
||
| } | ||
|
|
||
| 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 | ||
| } | ||
133 changes: 133 additions & 0 deletions
133
powershell/internal/services/teams/Get-MtTeamsUnifiedTenantSettings.ps1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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() | ||
|
JeremyFinney-lgtm marked this conversation as resolved.
Resolved
|
||
| } 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 | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.