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
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
function Test-MtCisFabricTenantSetting {
<#
.SYNOPSIS
Evaluates a Microsoft Fabric tenant setting for a CIS check.
Preserves the CIS entry point for the shared Fabric tenant-setting evaluator.

.DESCRIPTION
Reads one validated tenant setting, applies its audit rule, records the observed state, and returns a Boolean result.

.EXAMPLE
Test-MtCisFabricTenantSetting -SettingName 'ElevatedGuestsTenant' -SettingTitle 'Guest users can browse Fabric content' -Rule DisabledOrGroups

Returns true when guest browsing is disabled or limited to one or more security groups.
Delegates to the service-neutral evaluator without changing existing CIS audit rules or results.
#>
[CmdletBinding()]
[OutputType([bool])]
Expand All @@ -27,98 +22,5 @@
[string] $Rule
)

$settings = @(Get-MtFabricTenantSettings | Where-Object { $_.settingName -eq $SettingName })
if ($settings.Count -ne 1) {
throw "Expected exactly one Fabric tenant setting named '$SettingName'; received $($settings.Count)."
}

$setting = $settings[0]
if ($setting.enabled -isnot [bool]) {
throw "Fabric tenant setting '$SettingName' did not include a Boolean enabled value."
}

$isEnabled = $setting.enabled -eq $true
$isDisabled = -not $isEnabled
$groupProperty = $setting.PSObject.Properties['enabledSecurityGroups']
$enabledGroups = if ($null -eq $groupProperty) { @() } else { @($groupProperty.Value | Where-Object { $_ }) }
$hasGroups = $enabledGroups.Count -gt 0
$scope = if ($isDisabled) { 'Not applicable' } elseif ($hasGroups) { "$($enabledGroups.Count) security group(s)" } else { 'Entire organization' }
$additionalCondition = 'Not applicable'
$createP2wIsBoolean = $false
$createP2wIsDisabled = $false

if ($Rule -eq 'PublishToWeb' -and $isEnabled) {
$propertiesProperty = $setting.PSObject.Properties['properties']
$createP2wProperties = if ($null -eq $propertiesProperty -or $null -eq $propertiesProperty.Value) {
@()
}
else {
@($propertiesProperty.Value | Where-Object { $_.name -eq 'CreateP2w' })
}

$createP2wValue = if ($createP2wProperties.Count -eq 1) { $createP2wProperties[0].value } else { $null }
$parsedCreateP2wValue = $false
$createP2wIsBoolean = $createP2wValue -is [string] -and [bool]::TryParse($createP2wValue, [ref] $parsedCreateP2wValue)
$createP2wIsDisabled = $createP2wIsBoolean -and -not $parsedCreateP2wValue
$additionalCondition = if (-not $createP2wIsBoolean) {
'Not found or invalid'
}
elseif ($createP2wIsDisabled) {
'Existing codes only'
}
else {
'New codes allowed'
}
}

switch ($Rule) {
'DisabledOrGroups' {
$testResult = $isDisabled -or ($isEnabled -and $hasGroups)
}
'PublishToWeb' {
$testResult = $isDisabled -or ($isEnabled -and $hasGroups -and $createP2wIsDisabled)
}
'Disabled' {
$testResult = $isDisabled
}
'Enabled' {
$testResult = $isEnabled
}
}

$stateText = if ($isDisabled) { 'disabled' } elseif ($hasGroups) { "enabled for $($enabledGroups.Count) security group(s)" } else { 'enabled for the entire organization' }
if ($Rule -eq 'PublishToWeb' -and $testResult -and $isEnabled) {
$stateText += ' with only existing embed codes allowed'
}

$resultText = if ($testResult) { 'Pass' } else { 'Fail' }
$enabledText = if ($isEnabled) { 'True' } else { 'False' }
$resultMd = "| Setting | Enabled | Scope | Additional condition | Result |`n"
$resultMd += "| --- | --- | --- | --- | --- |`n"
$resultMd += "| $SettingTitle | $enabledText | $scope | $additionalCondition | $resultText |`n"

if ($testResult) {
$resultMessage = "Well done. $SettingTitle is $stateText."
}
else {
$resultMessage = switch ($Rule) {
'DisabledOrGroups' { "$SettingTitle is enabled for the entire organization. Disable it or restrict it to one or more security groups." }
'PublishToWeb' {
$restrictionsToFix = @()
if (-not $hasGroups) { $restrictionsToFix += 'restrict it to one or more security groups' }
if (-not $createP2wIsBoolean) {
$restrictionsToFix += 'confirm that new embed codes are disabled'
}
elseif (-not $createP2wIsDisabled) {
$restrictionsToFix += 'allow only existing embed codes'
}
"$SettingTitle does not meet the required restrictions. Set it to " + ($restrictionsToFix -join ' and ') + '.'
}
'Disabled' { "$SettingTitle is enabled. Disable it in Fabric tenant settings." }
'Enabled' { "$SettingTitle is disabled. Enable it in Fabric tenant settings." }
}
}

Add-MtTestResultDetail -Result "$resultMessage`n`n$resultMd"
return [bool] $testResult
return Test-MtFabricTenantSetting -SettingName $SettingName -SettingTitle $SettingTitle -Rule $Rule
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,24 @@
return $__MtSession.FabricCache['TenantSettings']
}

$accessToken = Get-AzAccessToken -ResourceUrl 'https://api.fabric.microsoft.com' -AsSecureString -ErrorAction Stop
$context = Get-AzContext
if ($null -eq $context -or $null -eq $context.Environment -or [string]::IsNullOrWhiteSpace($context.Environment.Name)) {
throw 'The connected Azure context did not identify an Azure environment.'
}

$environmentName = $context.Environment.Name
$apiEndpoint = switch ($environmentName) {
'AzureCloud' { 'https://api.fabric.microsoft.com' }
'AzureUSGovernment' { 'https://highapi.fabric.microsoft.us' }
default { throw "Microsoft Fabric tenant settings are not supported in Azure environment '$environmentName'." }
}

$accessToken = Get-AzAccessToken -ResourceUrl $apiEndpoint -AsSecureString -ErrorAction Stop
if (-not $accessToken.Token) {
throw 'The connected Azure context did not return a Microsoft Fabric access token.'
}

$requestUri = 'https://api.fabric.microsoft.com/v1/admin/tenantsettings'
$requestUri = "$apiEndpoint/v1/admin/tenantsettings"
$settings = [System.Collections.Generic.List[object]]::new()
while (-not [string]::IsNullOrEmpty($requestUri)) {
$response = Invoke-RestMethod -Method Get `
Expand Down
124 changes: 124 additions & 0 deletions powershell/internal/services/fabric/Test-MtFabricTenantSetting.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
function Test-MtFabricTenantSetting {
<#
.SYNOPSIS
Evaluates a Microsoft Fabric tenant setting using a benchmark audit rule.

.DESCRIPTION
Reads one validated tenant setting, applies its audit rule, records the observed state, and returns a Boolean result.

.EXAMPLE
Test-MtFabricTenantSetting -SettingName 'ElevatedGuestsTenant' -SettingTitle 'Guest users can browse Fabric content' -Rule DisabledOrGroups

Returns true when guest browsing is disabled or limited to one or more security groups.
#>
[CmdletBinding()]
[OutputType([bool])]
param (
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string] $SettingName,

[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string] $SettingTitle,

[Parameter(Mandatory)]
[ValidateSet('DisabledOrGroups', 'PublishToWeb', 'Disabled', 'Enabled')]
[string] $Rule
)

$settings = @(Get-MtFabricTenantSettings | Where-Object { $_.settingName -eq $SettingName })
if ($settings.Count -ne 1) {
throw "Expected exactly one Fabric tenant setting named '$SettingName'; received $($settings.Count)."
}

$setting = $settings[0]
if ($setting.enabled -isnot [bool]) {
throw "Fabric tenant setting '$SettingName' did not include a Boolean enabled value."
}

$isEnabled = $setting.enabled -eq $true
$isDisabled = -not $isEnabled
$groupProperty = $setting.PSObject.Properties['enabledSecurityGroups']
$enabledGroups = if ($null -eq $groupProperty) { @() } else { @($groupProperty.Value | Where-Object { $_ }) }
$hasGroups = $enabledGroups.Count -gt 0
$scope = if ($isDisabled) { 'Not applicable' } elseif ($hasGroups) { "$($enabledGroups.Count) security group(s)" } else { 'Entire organization' }
$additionalCondition = 'Not applicable'
$createP2wIsBoolean = $false
$createP2wIsDisabled = $false

if ($Rule -eq 'PublishToWeb' -and $isEnabled) {
$propertiesProperty = $setting.PSObject.Properties['properties']
$createP2wProperties = if ($null -eq $propertiesProperty -or $null -eq $propertiesProperty.Value) {
@()
}
else {
@($propertiesProperty.Value | Where-Object { $_.name -eq 'CreateP2w' })
}

$createP2wValue = if ($createP2wProperties.Count -eq 1) { $createP2wProperties[0].value } else { $null }
$parsedCreateP2wValue = $false
$createP2wIsBoolean = $createP2wValue -is [string] -and [bool]::TryParse($createP2wValue, [ref] $parsedCreateP2wValue)
$createP2wIsDisabled = $createP2wIsBoolean -and -not $parsedCreateP2wValue
$additionalCondition = if (-not $createP2wIsBoolean) {
'Not found or invalid'
}
elseif ($createP2wIsDisabled) {
'Existing codes only'
}
else {
'New codes allowed'
}
}

switch ($Rule) {
'DisabledOrGroups' {
$testResult = $isDisabled -or ($isEnabled -and $hasGroups)
}
'PublishToWeb' {
$testResult = $isDisabled -or ($isEnabled -and $hasGroups -and $createP2wIsDisabled)
}
'Disabled' {
$testResult = $isDisabled
}
'Enabled' {
$testResult = $isEnabled
}
}

$stateText = if ($isDisabled) { 'disabled' } elseif ($hasGroups) { "enabled for $($enabledGroups.Count) security group(s)" } else { 'enabled for the entire organization' }
if ($Rule -eq 'PublishToWeb' -and $testResult -and $isEnabled) {
$stateText += ' with only existing embed codes allowed'
}

$resultText = if ($testResult) { 'Pass' } else { 'Fail' }
$enabledText = if ($isEnabled) { 'True' } else { 'False' }
$resultMd = "| Setting | Enabled | Scope | Additional condition | Result |`n"
$resultMd += "| --- | --- | --- | --- | --- |`n"
$resultMd += "| $SettingTitle | $enabledText | $scope | $additionalCondition | $resultText |`n"

if ($testResult) {
$resultMessage = "Well done. $SettingTitle is $stateText."
}
else {
$resultMessage = switch ($Rule) {
'DisabledOrGroups' { "$SettingTitle is enabled for the entire organization. Disable it or restrict it to one or more security groups." }
'PublishToWeb' {
$restrictionsToFix = @()
if (-not $hasGroups) { $restrictionsToFix += 'restrict it to one or more security groups' }
if (-not $createP2wIsBoolean) {
$restrictionsToFix += 'confirm that new embed codes are disabled'
}
elseif (-not $createP2wIsDisabled) {
$restrictionsToFix += 'allow only existing embed codes'
}
"$SettingTitle does not meet the required restrictions. Set it to " + ($restrictionsToFix -join ' and ') + '.'
}
'Disabled' { "$SettingTitle is enabled. Disable it in Fabric tenant settings." }
'Enabled' { "$SettingTitle is disabled. Enable it in Fabric tenant settings." }
}
}

Add-MtTestResultDetail -Result "$resultMessage`n`n$resultMd"
return [bool] $testResult
}
Loading
Loading