From ec5062602736ffdf161c26893f728064b272c315 Mon Sep 17 00:00:00 2001 From: Jeremy Finney Date: Thu, 12 Nov 2026 13:14:04 +1300 Subject: [PATCH 1/8] feat: add CISA SCuBA Power Platform baseline checks Adds MS.POWERPLATFORM.1.1-6.1 as native checks with a Power Platform admin API service layer, the power-platform product taxonomy and framework mappings. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- AGENTS.md | 2 +- powershell/Maester.psm1 | 1 + powershell/assets/MaesterTaxonomy.psd1 | 12 ++ ...Test-MtCisaPowerPlatformDlpPolicyScope.ps1 | 97 +++++++++++++ ...est-MtCisaPowerPlatformTenantIsolation.ps1 | 46 +++++++ .../Test-MtCisaPowerPlatformTenantSetting.ps1 | 56 ++++++++ .../Get-MtPowerPlatformDlpPolicy.ps1 | 45 +++++++ .../Get-MtPowerPlatformEnvironment.ps1 | 30 +++++ .../Get-MtPowerPlatformTenantIsolation.ps1 | 27 ++++ .../Get-MtPowerPlatformTenantSetting.ps1 | 24 ++++ .../Invoke-MtPowerPlatformRequest.ps1 | 67 +++++++++ .../internal/session/Clear-ModuleVariable.ps1 | 1 + .../tests/fixtures/golden/selection.json | 77 ++++++++++- ...Test-MtCisaPowerPlatformBaseline.Tests.ps1 | 127 ++++++++++++++++++ .../functions/engine/Frameworks.Tests.ps1 | 8 +- .../tests/general/NativeTestLint.Tests.ps1 | 1 + .../tests/general/RepositoryLayout.Tests.ps1 | 2 +- tests/cisa/frameworks/cisa-scuba.json | 11 +- .../Test.CISA.MS.POWERPLATFORM.1.1.md | 18 +++ .../Test.CISA.MS.POWERPLATFORM.1.1.ps1 | 37 +++++ .../Test.CISA.MS.POWERPLATFORM.1.2.md | 18 +++ .../Test.CISA.MS.POWERPLATFORM.1.2.ps1 | 37 +++++ .../Test.CISA.MS.POWERPLATFORM.2.1.md | 19 +++ .../Test.CISA.MS.POWERPLATFORM.2.1.ps1 | 37 +++++ .../Test.CISA.MS.POWERPLATFORM.2.2.md | 18 +++ .../Test.CISA.MS.POWERPLATFORM.2.2.ps1 | 37 +++++ .../Test.CISA.MS.POWERPLATFORM.3.1.md | 18 +++ .../Test.CISA.MS.POWERPLATFORM.3.1.ps1 | 37 +++++ .../Test.CISA.MS.POWERPLATFORM.3.2.md | 18 +++ .../Test.CISA.MS.POWERPLATFORM.3.2.ps1 | 62 +++++++++ .../Test.CISA.MS.POWERPLATFORM.4.1.md | 20 +++ .../Test.CISA.MS.POWERPLATFORM.4.1.ps1 | 47 +++++++ .../Test.CISA.MS.POWERPLATFORM.5.1.md | 20 +++ .../Test.CISA.MS.POWERPLATFORM.5.1.ps1 | 37 +++++ .../Test.CISA.MS.POWERPLATFORM.6.1.md | 22 +++ .../Test.CISA.MS.POWERPLATFORM.6.1.ps1 | 37 +++++ website/docs/contributing.md | 2 +- 37 files changed, 1160 insertions(+), 15 deletions(-) create mode 100644 powershell/internal/checks/cisa/power-platform/Test-MtCisaPowerPlatformDlpPolicyScope.ps1 create mode 100644 powershell/internal/checks/cisa/power-platform/Test-MtCisaPowerPlatformTenantIsolation.ps1 create mode 100644 powershell/internal/checks/cisa/power-platform/Test-MtCisaPowerPlatformTenantSetting.ps1 create mode 100644 powershell/internal/services/power-platform/Get-MtPowerPlatformDlpPolicy.ps1 create mode 100644 powershell/internal/services/power-platform/Get-MtPowerPlatformEnvironment.ps1 create mode 100644 powershell/internal/services/power-platform/Get-MtPowerPlatformTenantIsolation.ps1 create mode 100644 powershell/internal/services/power-platform/Get-MtPowerPlatformTenantSetting.ps1 create mode 100644 powershell/internal/services/power-platform/Invoke-MtPowerPlatformRequest.ps1 create mode 100644 powershell/tests/functions/Test-MtCisaPowerPlatformBaseline.Tests.ps1 create mode 100644 tests/cisa/power-platform/Test.CISA.MS.POWERPLATFORM.1.1.md create mode 100644 tests/cisa/power-platform/Test.CISA.MS.POWERPLATFORM.1.1.ps1 create mode 100644 tests/cisa/power-platform/Test.CISA.MS.POWERPLATFORM.1.2.md create mode 100644 tests/cisa/power-platform/Test.CISA.MS.POWERPLATFORM.1.2.ps1 create mode 100644 tests/cisa/power-platform/Test.CISA.MS.POWERPLATFORM.2.1.md create mode 100644 tests/cisa/power-platform/Test.CISA.MS.POWERPLATFORM.2.1.ps1 create mode 100644 tests/cisa/power-platform/Test.CISA.MS.POWERPLATFORM.2.2.md create mode 100644 tests/cisa/power-platform/Test.CISA.MS.POWERPLATFORM.2.2.ps1 create mode 100644 tests/cisa/power-platform/Test.CISA.MS.POWERPLATFORM.3.1.md create mode 100644 tests/cisa/power-platform/Test.CISA.MS.POWERPLATFORM.3.1.ps1 create mode 100644 tests/cisa/power-platform/Test.CISA.MS.POWERPLATFORM.3.2.md create mode 100644 tests/cisa/power-platform/Test.CISA.MS.POWERPLATFORM.3.2.ps1 create mode 100644 tests/cisa/power-platform/Test.CISA.MS.POWERPLATFORM.4.1.md create mode 100644 tests/cisa/power-platform/Test.CISA.MS.POWERPLATFORM.4.1.ps1 create mode 100644 tests/cisa/power-platform/Test.CISA.MS.POWERPLATFORM.5.1.md create mode 100644 tests/cisa/power-platform/Test.CISA.MS.POWERPLATFORM.5.1.ps1 create mode 100644 tests/cisa/power-platform/Test.CISA.MS.POWERPLATFORM.6.1.md create mode 100644 tests/cisa/power-platform/Test.CISA.MS.POWERPLATFORM.6.1.ps1 diff --git a/AGENTS.md b/AGENTS.md index 6db6151908..286bc3daa1 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -85,7 +85,7 @@ enforces them. When writing or reviewing a change, check every added, moved or r (`global-secure-access`, not `GlobalSecureAccess`). No two paths may differ only in case. - One name per service, the same everywhere: `ad`, `azure`, `azure-devops`, `copilot`, `copilot-studio`, `defender`, `entra`, `exchange`, `fabric`, `foundry`, `github`, - `global-secure-access`, `graph`, `intune`, `purview`, `sharepoint`, `teams`, `xspm`. + `global-secure-access`, `graph`, `intune`, `power-platform`, `purview`, `sharepoint`, `teams`, `xspm`. Folders name the product whose settings a check reads, never a theme: an AI check goes in the folder of its product (an Entra agent identity check in `entra/`) and declares `Area = 'ai'`. A new service must be added to the guide and the test. diff --git a/powershell/Maester.psm1 b/powershell/Maester.psm1 index ff65045908..07fc715b44 100644 --- a/powershell/Maester.psm1 +++ b/powershell/Maester.psm1 @@ -32,6 +32,7 @@ $__MtSession = @{ DataverseEnvironmentId = $null # Environment identifier for display (e.g. org123.crm.dynamics.com) SpoCache = @{} # Cache for SharePoint Online tenant settings retrieved via PnP FabricCache = @{} # Fabric admin API data cached for the current test run + PowerPlatformCache = @{} # Power Platform admin API data cached for the current test run GitHubCache = @{} # Per-session REST response cache; cleared each Invoke-Maester run ADCache = @{} # Active Directory data cache ADConnection = $null # Active Directory connection state diff --git a/powershell/assets/MaesterTaxonomy.psd1 b/powershell/assets/MaesterTaxonomy.psd1 index 1c0373c93f..d6cfb1980f 100644 --- a/powershell/assets/MaesterTaxonomy.psd1 +++ b/powershell/assets/MaesterTaxonomy.psd1 @@ -144,6 +144,18 @@ @{ Key = 'tenant-settings'; Name = 'Tenant settings' } ) } + @{ + Key = 'power-platform' + Name = 'Power Platform' + Aliases = @('Power Platform', 'Power Apps', 'Power Automate', 'Power Pages') + Features = @( + @{ Key = 'environments'; Name = 'Environments' } + @{ Key = 'data-policies'; Name = 'Data policies' } + @{ Key = 'tenant-isolation'; Name = 'Tenant isolation' } + @{ Key = 'power-apps'; Name = 'Power Apps' } + @{ Key = 'power-pages'; Name = 'Power Pages' } + ) + } @{ Key = 'microsoft-365' Name = 'Microsoft 365' diff --git a/powershell/internal/checks/cisa/power-platform/Test-MtCisaPowerPlatformDlpPolicyScope.ps1 b/powershell/internal/checks/cisa/power-platform/Test-MtCisaPowerPlatformDlpPolicyScope.ps1 new file mode 100644 index 0000000000..b2c6eac759 --- /dev/null +++ b/powershell/internal/checks/cisa/power-platform/Test-MtCisaPowerPlatformDlpPolicyScope.ps1 @@ -0,0 +1,97 @@ +function Test-MtCisaPowerPlatformDlpPolicyScope { + <# + .SYNOPSIS + Evaluates whether Power Platform data loss prevention (DLP) policies cover the environments a CISA SCuBA check names. + + .DESCRIPTION + Follows the ScubaGear audit for MS.POWERPLATFORM.2.1 and MS.POWERPLATFORM.2.2. + A policy covers an environment when its scope is all environments, when it names the environment in an + OnlyEnvironments scope, or when it excludes other environments but not this one. + DefaultEnvironment passes when at least one policy covers the default environment. + NonDefaultEnvironment passes when a policy applies to all environments, or every non-default environment is covered. + + .EXAMPLE + Test-MtCisaPowerPlatformDlpPolicyScope -Scope DefaultEnvironment + + Returns true when a DLP policy applies to the default environment. + #> + [CmdletBinding()] + [OutputType([bool])] + param( + # DefaultEnvironment checks the default environment; NonDefaultEnvironment checks every other environment. + [Parameter(Mandatory)] + [ValidateSet('DefaultEnvironment', 'NonDefaultEnvironment')] + [string] $Scope + ) + + $environments = @(Get-MtPowerPlatformEnvironment) + $policies = @(Get-MtPowerPlatformDlpPolicy) + $defaultEnvironments = @($environments | Where-Object { $_.IsDefault }) + if ($defaultEnvironments.Count -ne 1) { + throw "Expected exactly one default Power Platform environment; received $($defaultEnvironments.Count)." + } + + $covers = { + param($Policy, $EnvironmentName) + switch ($Policy.Scope) { + 'AllEnvironments' { $true } + 'OnlyEnvironments' { $Policy.Environments -contains $EnvironmentName } + default { $Policy.Environments -notcontains $EnvironmentName } + } + } + + $defaultName = $defaultEnvironments[0].Name + if ($Scope -eq 'DefaultEnvironment') { + $coveringPolicies = @($policies | Where-Object { & $covers $_ $defaultName }) + $testResult = $coveringPolicies.Count -gt 0 + + if ($testResult) { + $message = 'Well done. At least one DLP policy applies to the default Power Platform environment.' + } + elseif ($policies.Count -eq 0) { + $message = 'No Power Platform DLP policy exists. Create a policy that restricts connectors in the default environment.' + } + else { + $message = 'No Power Platform DLP policy applies to the default environment. Add the default environment to a DLP policy scope.' + } + + $resultMd = "| Policy | Scope | Covers default environment |`n" + $resultMd += "| --- | --- | --- |`n" + foreach ($policy in $policies) { + $covered = if (& $covers $policy $defaultName) { 'Yes' } else { 'No' } + $resultMd += "| $($policy.DisplayName) | $($policy.Scope) | $covered |`n" + } + } + else { + $nonDefaultEnvironments = @($environments | Where-Object { -not $_.IsDefault }) + $allEnvironmentPolicies = @($policies | Where-Object { $_.Scope -eq 'AllEnvironments' }) + $uncovered = @($nonDefaultEnvironments | Where-Object { + $environment = $_ + -not ($policies | Where-Object { & $covers $_ $environment.Name }) + }) + $testResult = $allEnvironmentPolicies.Count -gt 0 -or $uncovered.Count -eq 0 + + if ($allEnvironmentPolicies.Count -gt 0) { + $message = 'Well done. At least one DLP policy applies to all Power Platform environments.' + } + elseif ($nonDefaultEnvironments.Count -eq 0) { + $message = 'Well done. The tenant has no non-default Power Platform environments that need a DLP policy.' + } + elseif ($testResult) { + $message = 'Well done. Every non-default Power Platform environment is covered by a DLP policy.' + } + else { + $message = 'Some non-default Power Platform environments are not covered by any DLP policy. Add them to a DLP policy scope.' + } + + $resultMd = "| Environment | Type | Covered by a DLP policy |`n" + $resultMd += "| --- | --- | --- |`n" + foreach ($environment in $nonDefaultEnvironments) { + $covered = if ($environment -in $uncovered) { 'No' } else { 'Yes' } + $resultMd += "| $($environment.DisplayName) | $($environment.EnvironmentType) | $covered |`n" + } + } + + Add-MtTestResultDetail -Result "$message`n`n$resultMd" + return $testResult +} diff --git a/powershell/internal/checks/cisa/power-platform/Test-MtCisaPowerPlatformTenantIsolation.ps1 b/powershell/internal/checks/cisa/power-platform/Test-MtCisaPowerPlatformTenantIsolation.ps1 new file mode 100644 index 0000000000..114c0a7fda --- /dev/null +++ b/powershell/internal/checks/cisa/power-platform/Test-MtCisaPowerPlatformTenantIsolation.ps1 @@ -0,0 +1,46 @@ +function Test-MtCisaPowerPlatformTenantIsolation { + <# + .SYNOPSIS + Evaluates whether Power Platform tenant isolation is enabled. + + .DESCRIPTION + Follows the ScubaGear audit for MS.POWERPLATFORM.3.1: the tenant isolation policy must exist and its isDisabled + property must be false. A tenant that has never configured the policy fails. + + .EXAMPLE + Test-MtCisaPowerPlatformTenantIsolation + + Returns true when Power Platform tenant isolation is enabled. + #> + [CmdletBinding()] + [OutputType([bool])] + param() + + $policy = Get-MtPowerPlatformTenantIsolation + if ($null -eq $policy) { + Add-MtTestResultDetail -Result 'The Power Platform tenant isolation policy has never been configured in this tenant, so cross-tenant connections are not restricted.' + return $false + } + + $isDisabledProperty = if ($null -eq $policy.properties) { $null } else { $policy.properties.PSObject.Properties['isDisabled'] } + if ($null -eq $isDisabledProperty -or $isDisabledProperty.Value -isnot [bool]) { + throw 'The Power Platform tenant isolation policy did not include a Boolean isDisabled value.' + } + + $testResult = -not $isDisabledProperty.Value + $enabledText = if ($testResult) { 'On' } else { 'Off' } + $resultText = if ($testResult) { 'Pass' } else { 'Fail' } + $resultMd = "| Setting | Value | Result |`n" + $resultMd += "| --- | --- | --- |`n" + $resultMd += "| Restrict cross-tenant connections | $enabledText | $resultText |`n" + + if ($testResult) { + $message = 'Well done. Power Platform tenant isolation is enabled.' + } + else { + $message = 'Power Platform tenant isolation is disabled, so cross-tenant connections are not restricted.' + } + + Add-MtTestResultDetail -Result "$message`n`n$resultMd" + return $testResult +} diff --git a/powershell/internal/checks/cisa/power-platform/Test-MtCisaPowerPlatformTenantSetting.ps1 b/powershell/internal/checks/cisa/power-platform/Test-MtCisaPowerPlatformTenantSetting.ps1 new file mode 100644 index 0000000000..0ce9f03b8f --- /dev/null +++ b/powershell/internal/checks/cisa/power-platform/Test-MtCisaPowerPlatformTenantSetting.ps1 @@ -0,0 +1,56 @@ +function Test-MtCisaPowerPlatformTenantSetting { + <# + .SYNOPSIS + Evaluates a Power Platform tenant setting that must be true for a CISA SCuBA check. + + .DESCRIPTION + Reads one Boolean tenant setting from the Power Platform admin API, records the observed value, and returns true + when it is true. A missing or non-Boolean value is an error rather than a failed audit, because the baseline cannot + be assessed without it. + + .EXAMPLE + Test-MtCisaPowerPlatformTenantSetting -SettingName 'disableEnvironmentCreationByNonAdminUsers' -SettingTitle 'Production and sandbox environment creation is restricted to administrators' + + Returns true when only administrators can create production and sandbox environments. + #> + [CmdletBinding()] + [OutputType([bool])] + param( + # Dot-separated path of the Boolean tenant setting, for example powerPlatform.powerApps.disableShareWithEveryone. + [Parameter(Mandatory)] + [string] $SettingName, + + # Sentence-style description of the restriction the setting enforces, without a trailing period. + [Parameter(Mandatory)] + [string] $SettingTitle + ) + + $value = Get-MtPowerPlatformTenantSetting + foreach ($segment in $SettingName.Split('.')) { + $property = if ($null -eq $value) { $null } else { $value.PSObject.Properties[$segment] } + if ($null -eq $property) { + throw "Power Platform tenant setting '$SettingName' was not returned by the tenant settings API." + } + + $value = $property.Value + } + + if ($value -isnot [bool]) { + throw "Power Platform tenant setting '$SettingName' did not have a Boolean value." + } + + $resultText = if ($value) { 'Pass' } else { 'Fail' } + $resultMd = "| Setting | Value | Result |`n" + $resultMd += "| --- | --- | --- |`n" + $resultMd += "| ``$SettingName`` | $value | $resultText |`n" + + if ($value) { + $message = "Well done. $SettingTitle." + } + else { + $message = "$SettingTitle is not enforced. Set ``$SettingName`` to ``true``." + } + + Add-MtTestResultDetail -Result "$message`n`n$resultMd" + return $value +} diff --git a/powershell/internal/services/power-platform/Get-MtPowerPlatformDlpPolicy.ps1 b/powershell/internal/services/power-platform/Get-MtPowerPlatformDlpPolicy.ps1 new file mode 100644 index 0000000000..375726cdb4 --- /dev/null +++ b/powershell/internal/services/power-platform/Get-MtPowerPlatformDlpPolicy.ps1 @@ -0,0 +1,45 @@ +function Get-MtPowerPlatformDlpPolicy { + <# + .SYNOPSIS + Retrieves the tenant-level Power Platform data loss prevention (DLP) policies. + + .DESCRIPTION + Returns one object per policy with its scope: AllEnvironments, OnlyEnvironments or ExceptEnvironments, and the + names of the environments the scope lists, cached for the current test run. A policy without an environment filter + applies to every environment. + #> + [CmdletBinding()] + [OutputType([object[]])] + param() + + if ($__MtSession.PowerPlatformCache.ContainsKey('DlpPolicies')) { + return $__MtSession.PowerPlatformCache['DlpPolicies'] + } + + $response = Invoke-MtPowerPlatformRequest -RelativeUri '/providers/Microsoft.BusinessAppPlatform/scopes/admin/apiPolicies?api-version=2016-11-01' + $policies = @(foreach ($policy in @($response.value)) { + $filter = $policy.properties.definition.constraints.environmentFilter1 + $scope = 'AllEnvironments' + $environmentNames = @() + if ($null -ne $filter) { + if ($filter.parameters.filterType -eq 'include') { + $scope = 'OnlyEnvironments' + } + else { + $scope = 'ExceptEnvironments' + } + + $environmentNames = @($filter.parameters.environments | Where-Object { $_ } | ForEach-Object { $_.name }) + } + + [pscustomobject]@{ + Name = $policy.name + DisplayName = $policy.properties.displayName + Scope = $scope + Environments = $environmentNames + } + }) + + $__MtSession.PowerPlatformCache['DlpPolicies'] = $policies + return $policies +} diff --git a/powershell/internal/services/power-platform/Get-MtPowerPlatformEnvironment.ps1 b/powershell/internal/services/power-platform/Get-MtPowerPlatformEnvironment.ps1 new file mode 100644 index 0000000000..192a0d347b --- /dev/null +++ b/powershell/internal/services/power-platform/Get-MtPowerPlatformEnvironment.ps1 @@ -0,0 +1,30 @@ +function Get-MtPowerPlatformEnvironment { + <# + .SYNOPSIS + Retrieves the Power Platform environments of the tenant. + + .DESCRIPTION + Returns one object per environment with its name, display name, type and whether it is the default environment, + cached for the current test run. + #> + [CmdletBinding()] + [OutputType([object[]])] + param() + + if ($__MtSession.PowerPlatformCache.ContainsKey('Environments')) { + return $__MtSession.PowerPlatformCache['Environments'] + } + + $response = Invoke-MtPowerPlatformRequest -RelativeUri '/providers/Microsoft.BusinessAppPlatform/scopes/admin/environments?api-version=2023-06-01' + $environments = @(foreach ($environment in @($response.value)) { + [pscustomobject]@{ + Name = $environment.name + DisplayName = $environment.properties.displayName + IsDefault = $environment.properties.isDefault -eq $true + EnvironmentType = $environment.properties.environmentSku + } + }) + + $__MtSession.PowerPlatformCache['Environments'] = $environments + return $environments +} diff --git a/powershell/internal/services/power-platform/Get-MtPowerPlatformTenantIsolation.ps1 b/powershell/internal/services/power-platform/Get-MtPowerPlatformTenantIsolation.ps1 new file mode 100644 index 0000000000..2349ca6fcc --- /dev/null +++ b/powershell/internal/services/power-platform/Get-MtPowerPlatformTenantIsolation.ps1 @@ -0,0 +1,27 @@ +function Get-MtPowerPlatformTenantIsolation { + <# + .SYNOPSIS + Retrieves the Power Platform tenant isolation policy. + + .DESCRIPTION + Returns the tenant isolation policy of the signed-in tenant, cached for the current test run. Returns $null when the + policy has never been configured. + #> + [CmdletBinding()] + [OutputType([object])] + param() + + if ($__MtSession.PowerPlatformCache.ContainsKey('TenantIsolation')) { + return $__MtSession.PowerPlatformCache['TenantIsolation'] + } + + $tenantId = (Get-AzContext -ErrorAction Stop).Tenant.Id + $policy = Invoke-MtPowerPlatformRequest -RelativeUri "/providers/PowerPlatform.Governance/v1/tenants/$tenantId/tenantIsolationPolicy?api-version=2020-06-01" + # Windows PowerShell keeps a null response body as the text "null". + if ($policy -is [string]) { + $policy = $null + } + + $__MtSession.PowerPlatformCache['TenantIsolation'] = $policy + return $policy +} diff --git a/powershell/internal/services/power-platform/Get-MtPowerPlatformTenantSetting.ps1 b/powershell/internal/services/power-platform/Get-MtPowerPlatformTenantSetting.ps1 new file mode 100644 index 0000000000..84b802ddb0 --- /dev/null +++ b/powershell/internal/services/power-platform/Get-MtPowerPlatformTenantSetting.ps1 @@ -0,0 +1,24 @@ +function Get-MtPowerPlatformTenantSetting { + <# + .SYNOPSIS + Retrieves the Power Platform tenant settings. + + .DESCRIPTION + Returns the tenant settings object of the Power Platform admin API, cached for the current test run. + #> + [CmdletBinding()] + [OutputType([object])] + param() + + if ($__MtSession.PowerPlatformCache.ContainsKey('TenantSettings')) { + return $__MtSession.PowerPlatformCache['TenantSettings'] + } + + $settings = Invoke-MtPowerPlatformRequest -Method Post -RelativeUri '/providers/Microsoft.BusinessAppPlatform/listTenantSettings?api-version=2023-06-01' + if ($null -eq $settings) { + throw 'The Power Platform admin API returned no tenant settings.' + } + + $__MtSession.PowerPlatformCache['TenantSettings'] = $settings + return $settings +} diff --git a/powershell/internal/services/power-platform/Invoke-MtPowerPlatformRequest.ps1 b/powershell/internal/services/power-platform/Invoke-MtPowerPlatformRequest.ps1 new file mode 100644 index 0000000000..a1efd0c3ef --- /dev/null +++ b/powershell/internal/services/power-platform/Invoke-MtPowerPlatformRequest.ps1 @@ -0,0 +1,67 @@ +function Invoke-MtPowerPlatformRequest { + <# + .SYNOPSIS + Calls the Power Platform admin (Business Application Platform) API. + + .DESCRIPTION + Uses the active Azure context to acquire a Power Platform access token and calls the endpoint for the signed-in + cloud. Collection responses that carry a nextLink are followed and combined into one value array. + #> + [CmdletBinding()] + [OutputType([object])] + param( + # Path and query of the request, relative to the Power Platform admin API host. + [Parameter(Mandatory)] + [string] $RelativeUri, + + # HTTP method of the request. + [ValidateSet('Get', 'Post')] + [string] $Method = 'Get' + ) + + $azureEnvironment = (Get-AzContext -ErrorAction Stop).Environment.Name + $endpoints = @{ + AzureCloud = @{ BaseUri = 'https://api.bap.microsoft.com'; ResourceUrl = 'https://service.powerapps.com/' } + AzureUSGovernment = @{ BaseUri = 'https://high.api.bap.microsoft.us'; ResourceUrl = 'https://high.service.powerapps.us/' } + } + $endpoint = $endpoints[$azureEnvironment] + if (-not $endpoint) { + throw "The Power Platform admin API is not supported for the '$azureEnvironment' Azure environment." + } + + $accessToken = Get-AzAccessToken -ResourceUrl $endpoint.ResourceUrl -AsSecureString -ErrorAction Stop + if (-not $accessToken.Token) { + throw 'The connected Azure context did not return a Power Platform access token.' + } + + $requestUri = "$($endpoint.BaseUri)$RelativeUri" + $values = [System.Collections.Generic.List[object]]::new() + while (-not [string]::IsNullOrEmpty($requestUri)) { + $response = Invoke-RestMethod -Method $Method ` + -Uri $requestUri ` + -Authentication Bearer ` + -Token $accessToken.Token ` + -ContentType 'application/json' ` + -ErrorAction Stop + + if ($null -eq $response -or $response -is [string] -or $null -eq $response.PSObject.Properties['value']) { + return $response + } + + foreach ($value in @($response.value)) { + if ($null -ne $value) { + $values.Add($value) + } + } + + $nextLink = $response.PSObject.Properties['nextLink'] + if ($null -eq $nextLink) { + $requestUri = $null + } + else { + $requestUri = $nextLink.Value + } + } + + return [pscustomobject]@{ value = $values.ToArray() } +} diff --git a/powershell/internal/session/Clear-ModuleVariable.ps1 b/powershell/internal/session/Clear-ModuleVariable.ps1 index 2c4eb15b76..5742dac1da 100644 --- a/powershell/internal/session/Clear-ModuleVariable.ps1 +++ b/powershell/internal/session/Clear-ModuleVariable.ps1 @@ -34,6 +34,7 @@ $__MtSession.GitHubCache = @{} $__MtSession.SpoCache = @{} $__MtSession.FabricCache = @{} + $__MtSession.PowerPlatformCache = @{} $__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/tests/fixtures/golden/selection.json b/powershell/tests/fixtures/golden/selection.json index ae970e7cfd..e85e30162d 100644 --- a/powershell/tests/fixtures/golden/selection.json +++ b/powershell/tests/fixtures/golden/selection.json @@ -21,7 +21,7 @@ "AD" ] }, - "SelectedCount": 582, + "SelectedCount": 591, "SelectedFamilies": [ "MT.1024", "MT.1059", @@ -306,6 +306,15 @@ "CISA.MS.EXO.9.3", "CISA.MS.EXO.9.4", "CISA.MS.EXO.9.5", + "CISA.MS.POWERPLATFORM.1.1", + "CISA.MS.POWERPLATFORM.1.2", + "CISA.MS.POWERPLATFORM.2.1", + "CISA.MS.POWERPLATFORM.2.2", + "CISA.MS.POWERPLATFORM.3.1", + "CISA.MS.POWERPLATFORM.3.2", + "CISA.MS.POWERPLATFORM.4.1", + "CISA.MS.POWERPLATFORM.5.1", + "CISA.MS.POWERPLATFORM.6.1", "CISA.MS.SHAREPOINT.1.1", "CISA.MS.SHAREPOINT.1.2", "CISA.MS.SHAREPOINT.1.3", @@ -771,7 +780,7 @@ "AD" ] }, - "SelectedCount": 538, + "SelectedCount": 547, "SelectedFamilies": [ "MT.1024", "MT.1059", @@ -1056,6 +1065,15 @@ "CISA.MS.EXO.9.3", "CISA.MS.EXO.9.4", "CISA.MS.EXO.9.5", + "CISA.MS.POWERPLATFORM.1.1", + "CISA.MS.POWERPLATFORM.1.2", + "CISA.MS.POWERPLATFORM.2.1", + "CISA.MS.POWERPLATFORM.2.2", + "CISA.MS.POWERPLATFORM.3.1", + "CISA.MS.POWERPLATFORM.3.2", + "CISA.MS.POWERPLATFORM.4.1", + "CISA.MS.POWERPLATFORM.5.1", + "CISA.MS.POWERPLATFORM.6.1", "CISA.MS.SHAREPOINT.1.1", "CISA.MS.SHAREPOINT.1.2", "CISA.MS.SHAREPOINT.1.3", @@ -1330,7 +1348,7 @@ "AD" ] }, - "SelectedCount": 598, + "SelectedCount": 607, "SelectedFamilies": [ "MT.1024", "MT.1033", @@ -1618,6 +1636,15 @@ "CISA.MS.EXO.9.3", "CISA.MS.EXO.9.4", "CISA.MS.EXO.9.5", + "CISA.MS.POWERPLATFORM.1.1", + "CISA.MS.POWERPLATFORM.1.2", + "CISA.MS.POWERPLATFORM.2.1", + "CISA.MS.POWERPLATFORM.2.2", + "CISA.MS.POWERPLATFORM.3.1", + "CISA.MS.POWERPLATFORM.3.2", + "CISA.MS.POWERPLATFORM.4.1", + "CISA.MS.POWERPLATFORM.5.1", + "CISA.MS.POWERPLATFORM.6.1", "CISA.MS.SHAREPOINT.1.1", "CISA.MS.SHAREPOINT.1.2", "CISA.MS.SHAREPOINT.1.3", @@ -1951,7 +1978,7 @@ "AD" ] }, - "SelectedCount": 609, + "SelectedCount": 618, "SelectedFamilies": [ "MT.1024", "MT.1059", @@ -2237,6 +2264,15 @@ "CISA.MS.EXO.9.3", "CISA.MS.EXO.9.4", "CISA.MS.EXO.9.5", + "CISA.MS.POWERPLATFORM.1.1", + "CISA.MS.POWERPLATFORM.1.2", + "CISA.MS.POWERPLATFORM.2.1", + "CISA.MS.POWERPLATFORM.2.2", + "CISA.MS.POWERPLATFORM.3.1", + "CISA.MS.POWERPLATFORM.3.2", + "CISA.MS.POWERPLATFORM.4.1", + "CISA.MS.POWERPLATFORM.5.1", + "CISA.MS.POWERPLATFORM.6.1", "CISA.MS.SHAREPOINT.1.1", "CISA.MS.SHAREPOINT.1.2", "CISA.MS.SHAREPOINT.1.3", @@ -2581,7 +2617,7 @@ "AD" ] }, - "SelectedCount": 639, + "SelectedCount": 648, "SelectedFamilies": [ "MT.1024", "MT.1033", @@ -2870,6 +2906,15 @@ "CISA.MS.EXO.9.3", "CISA.MS.EXO.9.4", "CISA.MS.EXO.9.5", + "CISA.MS.POWERPLATFORM.1.1", + "CISA.MS.POWERPLATFORM.1.2", + "CISA.MS.POWERPLATFORM.2.1", + "CISA.MS.POWERPLATFORM.2.2", + "CISA.MS.POWERPLATFORM.3.1", + "CISA.MS.POWERPLATFORM.3.2", + "CISA.MS.POWERPLATFORM.4.1", + "CISA.MS.POWERPLATFORM.5.1", + "CISA.MS.POWERPLATFORM.6.1", "CISA.MS.SHAREPOINT.1.1", "CISA.MS.SHAREPOINT.1.2", "CISA.MS.SHAREPOINT.1.3", @@ -3441,7 +3486,7 @@ "AD" ] }, - "SelectedCount": 538, + "SelectedCount": 547, "SelectedFamilies": [ "MT.1024", "MT.1059", @@ -3726,6 +3771,15 @@ "CISA.MS.EXO.9.3", "CISA.MS.EXO.9.4", "CISA.MS.EXO.9.5", + "CISA.MS.POWERPLATFORM.1.1", + "CISA.MS.POWERPLATFORM.1.2", + "CISA.MS.POWERPLATFORM.2.1", + "CISA.MS.POWERPLATFORM.2.2", + "CISA.MS.POWERPLATFORM.3.1", + "CISA.MS.POWERPLATFORM.3.2", + "CISA.MS.POWERPLATFORM.4.1", + "CISA.MS.POWERPLATFORM.5.1", + "CISA.MS.POWERPLATFORM.6.1", "CISA.MS.SHAREPOINT.1.1", "CISA.MS.SHAREPOINT.1.2", "CISA.MS.SHAREPOINT.1.3", @@ -4006,7 +4060,7 @@ "AD" ] }, - "SelectedCount": 595, + "SelectedCount": 604, "SelectedFamilies": [ "MT.1024", "MT.1033", @@ -4295,6 +4349,15 @@ "CISA.MS.EXO.9.3", "CISA.MS.EXO.9.4", "CISA.MS.EXO.9.5", + "CISA.MS.POWERPLATFORM.1.1", + "CISA.MS.POWERPLATFORM.1.2", + "CISA.MS.POWERPLATFORM.2.1", + "CISA.MS.POWERPLATFORM.2.2", + "CISA.MS.POWERPLATFORM.3.1", + "CISA.MS.POWERPLATFORM.3.2", + "CISA.MS.POWERPLATFORM.4.1", + "CISA.MS.POWERPLATFORM.5.1", + "CISA.MS.POWERPLATFORM.6.1", "CISA.MS.SHAREPOINT.1.1", "CISA.MS.SHAREPOINT.1.2", "CISA.MS.SHAREPOINT.1.3", diff --git a/powershell/tests/functions/Test-MtCisaPowerPlatformBaseline.Tests.ps1 b/powershell/tests/functions/Test-MtCisaPowerPlatformBaseline.Tests.ps1 new file mode 100644 index 0000000000..1487a23134 --- /dev/null +++ b/powershell/tests/functions/Test-MtCisaPowerPlatformBaseline.Tests.ps1 @@ -0,0 +1,127 @@ +BeforeDiscovery { + $settingChecks = @( + @{ Check = 'Test-MtCisaPowerPlatformEnvironmentCreation'; Setting = 'disableEnvironmentCreationByNonAdminUsers'; Nested = $false } + @{ Check = 'Test-MtCisaPowerPlatformTrialEnvironmentCreation'; Setting = 'disableTrialEnvironmentCreationByNonAdminUsers'; Nested = $false } + @{ Check = 'Test-MtCisaPowerPlatformPowerPagesCreation'; Setting = 'disablePortalsCreationByNonAdminUsers'; Nested = $false } + @{ Check = 'Test-MtCisaPowerPlatformShareWithEveryoneDisabled'; Setting = 'disableShareWithEveryone'; Nested = $true } + ) +} + +BeforeAll { + Import-Module "$PSScriptRoot\..\..\Maester.psd1" -Force +} + +Describe 'CISA Power Platform baseline: tenant setting ' -ForEach $settingChecks { + BeforeEach { + Mock -ModuleName Maester Add-MtTestResultDetail {} + } + + It 'passes only when the audited setting is true' { + foreach ($value in $true, $false) { + Mock -ModuleName Maester Get-MtPowerPlatformTenantSetting { + if ($Nested) { + [pscustomobject]@{ powerPlatform = [pscustomobject]@{ powerApps = [pscustomobject]@{ $Setting = $value } } } + } + else { + [pscustomobject]@{ $Setting = $value } + } + } + + $result = @(InModuleScope Maester -Parameters @{ Check = $Check } { + param($Check) + & $Check + }) + $result | Should -HaveCount 1 + $result[0] | Should -Be $value + } + } + + It 'throws when the setting is missing' { + Mock -ModuleName Maester Get-MtPowerPlatformTenantSetting { [pscustomobject]@{ unrelated = $true } } + + InModuleScope Maester -Parameters @{ Check = $Check } { + param($Check) + { & $Check } | Should -Throw '*was not returned*' + } + } +} + +Describe 'CISA Power Platform baseline: DLP policy scope' { + BeforeEach { + Mock -ModuleName Maester Add-MtTestResultDetail {} + Mock -ModuleName Maester Get-MtPowerPlatformEnvironment { + [pscustomobject]@{ Name = 'Default-1'; DisplayName = 'Default'; IsDefault = $true; EnvironmentType = 'Default' } + [pscustomobject]@{ Name = 'Dev'; DisplayName = 'Dev'; IsDefault = $false; EnvironmentType = 'Sandbox' } + } + } + + It 'fails the default environment check when no policy exists' { + Mock -ModuleName Maester Get-MtPowerPlatformDlpPolicy { } + InModuleScope Maester { + Test-MtCisaPowerPlatformDefaultEnvironmentDlp | Should -BeFalse + } + } + + It 'passes the default environment check when an except policy does not list it' { + Mock -ModuleName Maester Get-MtPowerPlatformDlpPolicy { + [pscustomobject]@{ DisplayName = 'P'; Scope = 'ExceptEnvironments'; Environments = @('Dev') } + } + InModuleScope Maester { + Test-MtCisaPowerPlatformDefaultEnvironmentDlp | Should -BeTrue + } + } + + It 'fails the non-default check when an environment is only excluded' { + Mock -ModuleName Maester Get-MtPowerPlatformDlpPolicy { + [pscustomobject]@{ DisplayName = 'P'; Scope = 'ExceptEnvironments'; Environments = @('Dev') } + } + InModuleScope Maester { + Test-MtCisaPowerPlatformNonDefaultEnvironmentDlp | Should -BeFalse + } + } + + It 'passes the non-default check for an all-environments policy' { + Mock -ModuleName Maester Get-MtPowerPlatformDlpPolicy { + [pscustomobject]@{ DisplayName = 'P'; Scope = 'AllEnvironments'; Environments = @() } + } + InModuleScope Maester { + Test-MtCisaPowerPlatformNonDefaultEnvironmentDlp | Should -BeTrue + } + } +} + +Describe 'CISA Power Platform baseline: tenant isolation' { + BeforeEach { + Mock -ModuleName Maester Add-MtTestResultDetail {} + } + + It 'fails when the policy was never configured' { + Mock -ModuleName Maester Get-MtPowerPlatformTenantIsolation { $null } + InModuleScope Maester { + Test-MtCisaPowerPlatformTenantIsolationEnabled | Should -BeFalse + } + } + + It 'passes only when isDisabled is false' { + Mock -ModuleName Maester Get-MtPowerPlatformTenantIsolation { + [pscustomobject]@{ properties = [pscustomobject]@{ isDisabled = $false; allowedTenants = @() } } + } + InModuleScope Maester { + Test-MtCisaPowerPlatformTenantIsolationEnabled | Should -BeTrue + } + Mock -ModuleName Maester Get-MtPowerPlatformTenantIsolation { + [pscustomobject]@{ properties = [pscustomobject]@{ isDisabled = $true } } + } + InModuleScope Maester { + Test-MtCisaPowerPlatformTenantIsolationEnabled | Should -BeFalse + } + } + + It 'requires investigation for the allowlist review' { + Mock -ModuleName Maester Get-MtPowerPlatformTenantIsolation { $null } + InModuleScope Maester { + Test-MtCisaPowerPlatformTenantIsolationAllowlist | Should -BeFalse + } + Should -Invoke Add-MtTestResultDetail -ModuleName Maester -Exactly -Times 1 -ParameterFilter { $Investigate } + } +} diff --git a/powershell/tests/functions/engine/Frameworks.Tests.ps1 b/powershell/tests/functions/engine/Frameworks.Tests.ps1 index 06a6372fd8..f7d9eecb00 100644 --- a/powershell/tests/functions/engine/Frameworks.Tests.ps1 +++ b/powershell/tests/functions/engine/Frameworks.Tests.ps1 @@ -336,7 +336,7 @@ Describe 'Benchmark of the tests shipped with Maester' { It 'Gives every test a Benchmark of