Repository navigation
feat(azure): add MT.1266 - Azure secure score per subscription #2712
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
earbona23
wants to merge
11
commits into
maester365:main
Choose a base branch
from
earbona23:feat/2515-azure-secure-score
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 all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
ed17a9d
feat(azure): add MT.1266 - Azure secure score per subscription
earbona23 0481ebc
fix(MT.1266): ResourceType is AzureResource, not Tenant
earbona23 dd2f189
fix(MT.1266): ask Azure for the default initiative only
earbona23 7df8b71
chore: save the new PowerShell files as UTF-8 with BOM
earbona23 e50565c
fix(azure): MT.1266 compares the unrounded secure score with the target
merill 69d2ab6
Merge remote-tracking branch 'origin/main' into resolver/2712
earbona23 1a244f7
style: canonical attribute property order
earbona23 d26e32c
Merge main and regenerate the golden selection fixture
earbona23 267a33e
chore(website): regenerate the test builder data for the new parameter
earbona23 ad47f55
Merge branch 'main' into feat/2515-azure-secure-score
earbona23 c92055a
fix(azure): bind the secure score list before enumerating it (MT.1266)
earbona23 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
137 changes: 137 additions & 0 deletions
137
powershell/internal/services/azure/Get-MtAzureSecureScore.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,137 @@ | ||
| function Get-MtAzureSecureScore { | ||
| <# | ||
| .SYNOPSIS | ||
| Returns one row per Azure subscription with the Microsoft Defender for Cloud secure score and the | ||
| state of the regulatory compliance standards assigned to it. | ||
|
|
||
| .DESCRIPTION | ||
| Runs three Azure Resource Graph queries over the subscriptions the signed-in account can read: the | ||
| secure score per subscription, the regulatory compliance standards per subscription, and the | ||
| subscription names from ResourceContainers so the result names a subscription instead of only its | ||
| GUID. | ||
|
|
||
| The percentage is computed from the current and maximum score rather than read from | ||
| properties.score.percentage, whose unit the Azure samples present two different ways: the secure | ||
| score page divides current by max itself while the Resource Graph sample projects the property | ||
| directly. Dividing here removes the ambiguity. | ||
|
|
||
| A subscription with a maximum score of zero is still returned. Defender for Cloud produced a score | ||
| row for it but assessed nothing, and the caller must say so rather than drop the subscription | ||
| silently or read it as a pass. | ||
|
|
||
| An empty secure score result means Defender for Cloud produced no score for these subscriptions. | ||
| The caller must skip in that case. | ||
|
|
||
| Errors are deliberately not caught here, so a failed query surfaces instead of being read as an | ||
| estate with no score. | ||
|
|
||
| .EXAMPLE | ||
| Get-MtAzureSecureScore | ||
|
|
||
| Returns the secure score and compliance standards of each readable subscription. | ||
| #> | ||
| [CmdletBinding()] | ||
| [OutputType([System.Collections.Generic.List[pscustomobject]])] | ||
| param() | ||
|
|
||
| # The name filter is not cosmetic. Score resources are per initiative, and the instance ID for | ||
| # this family is the subscription ID, so a second initiative in one subscription would produce a | ||
| # repeated instance ID. Invoke-MtNativePlan returns a single InvalidInstanceId Error row for the | ||
| # whole family in that case and evaluates nothing, so one extra initiative anywhere in the estate | ||
| # would take MT.1266 out everywhere. 'ascScore' is the ASC Default initiative, per the Azure API. | ||
| # properties.score.max is cast because the score arrives as a dynamic value. | ||
| $scoreQuery = @' | ||
| securityresources | ||
| | where type == "microsoft.security/securescores" | ||
| | where name == "ascScore" | ||
| | extend currentScore = todouble(properties.score.current), | ||
| maxScore = todouble(properties.score.max), | ||
| initiative = tostring(properties.displayName) | ||
| | project subscriptionId, currentScore, maxScore, initiative | ||
| | sort by subscriptionId asc | ||
| '@ | ||
|
|
||
| # The standards level already carries the control counts this needs, so there is no reason to read | ||
| # the control level and aggregate it back up. | ||
| $standardQuery = @' | ||
| securityresources | ||
| | where type == "microsoft.security/regulatorycompliancestandards" | ||
| | extend standard = name, | ||
| state = tostring(properties.state), | ||
| passedControls = toint(properties.passedControls), | ||
| failedControls = toint(properties.failedControls), | ||
| skippedControls = toint(properties.skippedControls), | ||
| unsupportedControls = toint(properties.unsupportedControls) | ||
| | project subscriptionId, standard, state, passedControls, failedControls, skippedControls, unsupportedControls | ||
| | sort by subscriptionId asc, standard asc | ||
| '@ | ||
|
|
||
| $nameQuery = @' | ||
| resourcecontainers | ||
| | where type == "microsoft.resources/subscriptions" | ||
| | project subscriptionId, subscriptionName = name | ||
| '@ | ||
|
|
||
| Write-Verbose 'Querying the Defender for Cloud secure score through Azure Resource Graph...' | ||
| $scoreRows = @(Invoke-MtAzureResourceGraphRequest -Query $scoreQuery) | ||
|
|
||
| $result = [System.Collections.Generic.List[pscustomobject]]::new() | ||
| if ($scoreRows.Count -eq 0) { | ||
| # -NoEnumerate keeps an empty list from collapsing to $null. | ||
| Write-Output $result -NoEnumerate | ||
| return | ||
| } | ||
|
|
||
| Write-Verbose 'Querying the regulatory compliance standards...' | ||
| $standardRows = @(Invoke-MtAzureResourceGraphRequest -Query $standardQuery) | ||
| Write-Verbose 'Querying the subscription names...' | ||
| $nameRows = @(Invoke-MtAzureResourceGraphRequest -Query $nameQuery) | ||
|
|
||
| $nameLookup = @{} | ||
| foreach ($row in $nameRows) { | ||
| if (-not $row -or [string]::IsNullOrWhiteSpace([string]$row.subscriptionId)) { continue } | ||
| $nameLookup[[string]$row.subscriptionId] = [string]$row.subscriptionName | ||
| } | ||
|
|
||
| $standardLookup = @{} | ||
| foreach ($row in $standardRows) { | ||
| if (-not $row -or [string]::IsNullOrWhiteSpace([string]$row.subscriptionId)) { continue } | ||
| $id = [string]$row.subscriptionId | ||
| if (-not $standardLookup.ContainsKey($id)) { | ||
| $standardLookup[$id] = [System.Collections.Generic.List[pscustomobject]]::new() | ||
| } | ||
| $standardLookup[$id].Add([pscustomobject]@{ | ||
| Standard = [string]$row.standard | ||
| State = [string]$row.state | ||
| PassedControls = [int]($row.passedControls) | ||
| FailedControls = [int]($row.failedControls) | ||
| SkippedControls = [int]($row.skippedControls) | ||
| UnsupportedControls = [int]($row.unsupportedControls) | ||
| }) | ||
| } | ||
|
|
||
| foreach ($row in $scoreRows) { | ||
| if (-not $row -or [string]::IsNullOrWhiteSpace([string]$row.subscriptionId)) { continue } | ||
| $id = [string]$row.subscriptionId | ||
| $current = [double]($row.currentScore) | ||
| $max = [double]($row.maxScore) | ||
| # A maximum of zero means nothing was assessed. Reporting 0% there would read as a bad score | ||
| # rather than as no score, so the percentage stays null and the caller skips the subscription. | ||
| # Not rounded: the caller compares it with the target, and 79.96 rounded to 80.0 would pass a target | ||
| # of 80. Round only for display. Decimal, not double: in double 57 of 100 is 56.99999999999999 and would | ||
| # miss a target of 57. | ||
| $percentage = if ($max -gt 0) { [double]([decimal]$current * 100 / [decimal]$max) } else { $null } | ||
|
|
||
| $result.Add([pscustomobject]@{ | ||
| SubscriptionId = $id | ||
| SubscriptionName = if ($nameLookup.ContainsKey($id)) { $nameLookup[$id] } else { $id } | ||
| Initiative = [string]$row.initiative | ||
| CurrentScore = $current | ||
| MaxScore = $max | ||
| ScorePercentage = $percentage | ||
| StandardList = if ($standardLookup.ContainsKey($id)) { @($standardLookup[$id]) } else { @() } | ||
| }) | ||
| } | ||
|
|
||
| Write-Output $result -NoEnumerate | ||
| } | ||
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.