Skip to content
Open
79 changes: 69 additions & 10 deletions build/golden/Export-MtLegacyIdTable.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@
function - the single check function called in the It body is called by exactly one current test
retired - nothing matched

The output is deterministic: GeneratedFrom is the last commit that touched the scanned test files,
and current IDs are read from that commit's tree, so rerunning on a later commit that does not touch
tests/ produces identical output.
Existing mappings are retained when their target still exists, so moving a check from a Pester wrapper
to a native test does not discard its legacy IDs.

The output is deterministic for the current tests tree and checked-in table: GeneratedFrom is the last
commit that touched the scanned legacy test files, and current IDs include native Test.<ID>.ps1 files.

.EXAMPLE
./build/golden/Export-MtLegacyIdTable.ps1
Expand Down Expand Up @@ -55,6 +57,11 @@ $ManualOverrides = [ordered]@{
# Pre-colon names of the split checks; the current titles say 'Global Administrator' and both call one function.
'MT.1050 Apps with high-risk permissions having a direct path to Global Admin' = 'MT.1050'
'MT.1051 Apps with high-risk permissions having an indirect path to Global Admin' = 'MT.1051'
'MS.EXO.12.1' = 'CISA.MS.SECURITYSUITE.8.1'
'MS.EXO.12.2' = 'CISA.MS.SECURITYSUITE.8.2'
'MS.EXO.15.1' = 'CISA.MS.SECURITYSUITE.7.1'
'MS.EXO.15.2' = 'CISA.MS.SECURITYSUITE.7.2'
'MS.EXO.15.3' = 'CISA.MS.SECURITYSUITE.7.3'
# Mock-based unit tests that briefly lived under tests/; they were never security checks.
'MFA for All users should pass even if not targeting guests' = 'retired'
'MFA for All users that excludes any guest type should fail' = 'retired'
Expand Down Expand Up @@ -244,15 +251,14 @@ foreach ($commit in $commits) {
}
#endregion Scan history

#region Current IDs (from the tree of $generatedFrom)
#region Current IDs
$currentIds = [System.Collections.Generic.Dictionary[string, object]]::new([System.StringComparer]::OrdinalIgnoreCase)
$familyPrefixes = [System.Collections.Generic.SortedSet[string]]::new([System.StringComparer]::Ordinal)
$treeLines = Invoke-Git ls-tree -r $generatedFrom -- tests
foreach ($line in $treeLines) {
if ($line -notmatch '^\d+ blob ([0-9a-f]{40})\t(tests/.+\.tests\.ps1)$') { continue }
if ($Matches[2] -like 'tests/Custom/*') { continue }
$path = $Matches[2]
foreach ($it in (Get-BlobItInfo -Blob $Matches[1])) {
$pesterFiles = Get-ChildItem -Path (Join-Path $RepoRoot 'tests') -Recurse -File -Filter '*.Tests.ps1'
foreach ($pesterFile in $pesterFiles) {
$path = [System.IO.Path]::GetRelativePath($RepoRoot, $pesterFile.FullName).Replace('\', '/')
if ($path -like 'tests/Custom/*') { continue }
foreach ($it in (Get-ItInfo -Content (Get-Content -LiteralPath $pesterFile.FullName -Raw))) {
$split = Get-StaticSplit -Name $it.Name
if (-not $split) {
if ($it.Name -match '^([A-Za-z]+\.?\d+)\.(<|\$\()') { $null = $familyPrefixes.Add("$($Matches[1]).") }
Expand All @@ -261,6 +267,42 @@ foreach ($line in $treeLines) {
$currentIds[$split.Id] = [pscustomobject]@{ Id = $split.Id; Title = (Get-NormalizedTitle $split.Title); Functions = $it.Functions; Path = $path }
}
}
# Native test IDs and their metadata are not present in the legacy Pester wrappers scanned above.
foreach ($nativeFile in (Get-ChildItem -Path (Join-Path $RepoRoot 'tests') -Recurse -File -Filter 'Test.*.ps1' |
Where-Object { $_.FullName -notmatch '[\\/]Custom[\\/]' })) {
$id = $nativeFile.Name -replace '^Test\.', '' -replace '\.ps1$', ''
$tokens = $null
$parseErrors = $null
$nativeAst = [System.Management.Automation.Language.Parser]::ParseFile($nativeFile.FullName, [ref] $tokens, [ref] $parseErrors)
if ($parseErrors.Count -gt 0) {
throw "Could not parse native test '$($nativeFile.FullName)': $($parseErrors[0].Message)"
}
$testFunction = $nativeAst.Find({
param($node)
$node -is [System.Management.Automation.Language.FunctionDefinitionAst] -and
@($node.Body.ParamBlock.Attributes | Where-Object { $_.TypeName.FullName -eq 'MaesterTest' }).Count -gt 0
}, $true) | Select-Object -First 1
$title = ''
$functions = @()
if ($testFunction) {
$testAttribute = $testFunction.Body.ParamBlock.Attributes |
Where-Object { $_.TypeName.FullName -eq 'MaesterTest' } |
Select-Object -First 1
$titleArgument = $testAttribute.NamedArguments | Where-Object { $_.ArgumentName -eq 'Title' } | Select-Object -First 1
if (-not $titleArgument -or $titleArgument.Argument -isnot [System.Management.Automation.Language.StringConstantExpressionAst]) {
throw "Native test '$($nativeFile.FullName)' has no constant Title in its MaesterTest attribute."
}
$title = Get-NormalizedTitle $titleArgument.Argument.Value
$functions = @($testFunction.Name)
}
$relativePath = [System.IO.Path]::GetRelativePath($RepoRoot, $nativeFile.FullName).Replace('\', '/')
$currentIds[$id] = [pscustomobject]@{
Id = $id
Title = $title
Functions = $functions
Path = $relativePath
}
}
Write-Verbose "Current static IDs: $($currentIds.Count); family prefixes: $($familyPrefixes -join ', ')"

$titleIndex = @{}
Expand All @@ -276,6 +318,18 @@ foreach ($c in $currentIds.Values) {
}
#endregion Current IDs

#region Existing mappings
$priorMappings = @{}
if (Test-Path -LiteralPath $OutputPath) {
$priorTable = Get-Content -LiteralPath $OutputPath -Raw | ConvertFrom-Json
foreach ($entry in $priorTable.Entries) {
if ($entry.CurrentId -and $entry.CurrentId -ne 'retired') {
$priorMappings[$entry.LegacyId] = $entry
}
}
}
#endregion Existing mappings

#region Map
$entries = [System.Collections.Generic.List[object]]::new()
$review = [System.Collections.Generic.List[string]]::new()
Expand Down Expand Up @@ -303,6 +357,8 @@ foreach ($record in $history.Values) {
$currentId = $ManualOverrides[$record.Id]; $rule = 'manual'
} elseif ($prefixTarget) {
$currentId = $prefixTarget; $rule = 'prefix'
} elseif ($priorMappings.ContainsKey($record.Id) -and $currentIds.ContainsKey($priorMappings[$record.Id].CurrentId)) {
$currentId = $priorMappings[$record.Id].CurrentId; $rule = $priorMappings[$record.Id].Rule
} elseif ($titleCandidates.Count -eq 1) {
$currentId = @($titleCandidates)[0]; $rule = 'title'
if ($functionCandidates.Count -gt 0 -and -not $functionCandidates.Contains($currentId)) {
Expand Down Expand Up @@ -351,6 +407,9 @@ if ($retiredFamilies) { Write-Warning "Historical family prefixes no longer at H
foreach ($line in ($review | Sort-Object)) { Write-Verbose "REVIEW $line" }
#endregion Report

# Stale family wrappers must still be suppressed after their Pester sources are removed.
$familyPrefixes.UnionWith($historicalFamilies)

$document = [ordered]@{
SchemaVersion = '1.0'
GeneratedFrom = $generatedFrom
Expand Down
5 changes: 5 additions & 0 deletions build/golden/Export-MtLegacyTagTable.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ $fixtures = Join-Path $RepoRoot 'powershell/tests/fixtures/golden'
# 2.x IDs that have another ID in 3.0.
$RenamedIds = @{
'MT1060' = 'MT.1060' # The drift family: four Its per folder in 2.x, one native family in 3.0.
'CISA.MS.EXO.12.1' = 'CISA.MS.SECURITYSUITE.8.1'
'CISA.MS.EXO.12.2' = 'CISA.MS.SECURITYSUITE.8.2'
'CISA.MS.EXO.15.1' = 'CISA.MS.SECURITYSUITE.7.1'
'CISA.MS.EXO.15.2' = 'CISA.MS.SECURITYSUITE.7.2'
'CISA.MS.EXO.15.3' = 'CISA.MS.SECURITYSUITE.7.3'
}

# The typed selector that takes the place of a tag. Only tags with an obvious one are listed.
Expand Down
11 changes: 8 additions & 3 deletions build/golden/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@ Regenerate it after a change to `tests/` that renames or removes a check:
otherwise `retired`.
- `-Verbose` prints counts by rule and a `REVIEW` line for each ambiguous, conflicting
or title-only match. Resolve ambiguous ones by adding them to `$ManualOverrides`.
- `GeneratedFrom` is the last commit that touched the scanned test files, so the
output only changes when `tests/` changes.
- `GeneratedFrom` is the last commit that touched the scanned legacy test files.
Current IDs come from the working tree's Pester and native tests, so removed
wrappers cannot keep retired tests marked as current.
- Historical family prefixes are retained after their Pester wrappers are removed,
so stale copies of those wrappers remain suppressed.
- `powershell/tests/general/LegacyIds.Tests.ps1` checks that no legacy ID is a current ID
and that every target ID still exists. If it fails after you rename or remove a
check, regenerate the table.
Expand All @@ -35,7 +38,7 @@ uses this table to keep `-Tag CA`, `-ExcludeTag MS.AAD.3.5` and every other 2.x
selecting the tests it selected. The tags are used for selection only and are never
written to a result row.

The table is derived from two frozen files, so it does not change when `tests/` changes:
The table is derived from two frozen files and the generator's explicit ID renames:

- `powershell/tests/fixtures/golden/tags-and-blocks.json`, the 2.x snapshot;
- `powershell/tests/fixtures/golden/tags-3.0-preview.json`, the tags the 3.0 previews added.
Expand All @@ -47,6 +50,8 @@ The table is derived from two frozen files, so it does not change when `tests/`
- A tag that is the ID of the one test that carried it is left out: the engine always
treats a built-in test's own ID as a selection tag.
- A 2.x ID that 3.0 renamed is written as its 3.0 ID (`$RenamedIds` in the script).
Update those renames and the corresponding golden-test expectations when a
migrated check changes ID, then regenerate this table and `selection.json`.
- `$Replacements` in the script lists the typed selector for the suite, product and
topic tags that have one; `Severity:<value>` tags get `-Severity <value>`.
- Tests added after 3.0 are not in the table. Do not add them, and do not edit the file
Expand Down
62 changes: 51 additions & 11 deletions powershell/assets/Maester.LegacyIds.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"SchemaVersion": "1.0",
"GeneratedFrom": "fc6a9078bcdd59b5b2f788682fdcd2be27e15b9c",
"GeneratedFrom": "eb13e7a116e4063bec204e901e4847ae67c1f6f6",
"Entries": [
{
"LegacyId": "1.1.1 (L1) Ensure Administrative accounts are cloud-only",
Expand Down Expand Up @@ -1130,6 +1130,46 @@
"LastSeenCommit": "5810a0dc37538fe0c4884f55938f23f5c7d1d6cc",
"LastSeenFile": "tests/cisa/exchange/Test-MtCisaBlockExecutable.Tests.ps1"
},
{
"LegacyId": "CISA.MS.EXO.12.1",
"CurrentId": "CISA.MS.SECURITYSUITE.8.1",
"Rule": "title",
"FirstSeenCommit": "d176a815b8cfef203cfe0672af5bc092385f05b9",
"LastSeenCommit": "eb38f4357cdf3576600aeca7d8da3138f77a37ca",
"LastSeenFile": "tests/cisa/exchange/Test-MtCisaAntiSpamAllowList.Tests.ps1"
},
{
"LegacyId": "CISA.MS.EXO.12.2",
"CurrentId": "CISA.MS.SECURITYSUITE.8.2",
"Rule": "title",
"FirstSeenCommit": "d176a815b8cfef203cfe0672af5bc092385f05b9",
"LastSeenCommit": "eb38f4357cdf3576600aeca7d8da3138f77a37ca",
"LastSeenFile": "tests/cisa/exchange/Test-MtCisaAntiSpamSafeList.Tests.ps1"
},
{
"LegacyId": "CISA.MS.EXO.15.1",
"CurrentId": "CISA.MS.SECURITYSUITE.7.1",
"Rule": "function",
"FirstSeenCommit": "d176a815b8cfef203cfe0672af5bc092385f05b9",
"LastSeenCommit": "eb38f4357cdf3576600aeca7d8da3138f77a37ca",
"LastSeenFile": "tests/cisa/exchange/Test-MtCisaSafeLink.Tests.ps1"
},
{
"LegacyId": "CISA.MS.EXO.15.2",
"CurrentId": "CISA.MS.SECURITYSUITE.7.2",
"Rule": "title",
"FirstSeenCommit": "d176a815b8cfef203cfe0672af5bc092385f05b9",
"LastSeenCommit": "eb38f4357cdf3576600aeca7d8da3138f77a37ca",
"LastSeenFile": "tests/cisa/exchange/Test-MtCisaSafeLinkDownloadScan.Tests.ps1"
},
{
"LegacyId": "CISA.MS.EXO.15.3",
"CurrentId": "CISA.MS.SECURITYSUITE.7.3",
"Rule": "title",
"FirstSeenCommit": "d176a815b8cfef203cfe0672af5bc092385f05b9",
"LastSeenCommit": "eb38f4357cdf3576600aeca7d8da3138f77a37ca",
"LastSeenFile": "tests/cisa/exchange/Test-MtCisaSafeLinkClickTracking.Tests.ps1"
},
{
"LegacyId": "Configure which users are allowed to present in Teams meetings",
"CurrentId": "retired",
Expand Down Expand Up @@ -1892,16 +1932,16 @@
},
{
"LegacyId": "MS.EXO.12.1",
"CurrentId": "CISA.MS.EXO.12.1",
"Rule": "prefix",
"CurrentId": "CISA.MS.SECURITYSUITE.8.1",
"Rule": "manual",
"FirstSeenCommit": "d119939a0fca63d684941e7194531ec6e6e0a455",
"LastSeenCommit": "726b9fd3bc414db3c41db8c6ec9a825c0713b34e",
"LastSeenFile": "tests/cisa/exchange/Test-MtCisaAntiSpamAllowList.Tests.ps1"
},
{
"LegacyId": "MS.EXO.12.2",
"CurrentId": "CISA.MS.EXO.12.2",
"Rule": "prefix",
"CurrentId": "CISA.MS.SECURITYSUITE.8.2",
"Rule": "manual",
"FirstSeenCommit": "4b0afaad10741d4fff485316df5a6b843d3afcfa",
"LastSeenCommit": "6d7e5099d9d830c07cab2a660a471f29f74d18f3",
"LastSeenFile": "tests/cisa/exchange/Test-MtCisaAntiSpamSafeList.Tests.ps1"
Expand Down Expand Up @@ -1948,24 +1988,24 @@
},
{
"LegacyId": "MS.EXO.15.1",
"CurrentId": "CISA.MS.EXO.15.1",
"Rule": "prefix",
"CurrentId": "CISA.MS.SECURITYSUITE.7.1",
"Rule": "manual",
"FirstSeenCommit": "e7c8045968db237b765bb43687d055ed9ad4ec6e",
"LastSeenCommit": "e7c8045968db237b765bb43687d055ed9ad4ec6e",
"LastSeenFile": "tests/cisa/exchange/Test-MtCisaSafeLink.Tests.ps1"
},
{
"LegacyId": "MS.EXO.15.2",
"CurrentId": "CISA.MS.EXO.15.2",
"Rule": "prefix",
"CurrentId": "CISA.MS.SECURITYSUITE.7.2",
"Rule": "manual",
"FirstSeenCommit": "e7c8045968db237b765bb43687d055ed9ad4ec6e",
"LastSeenCommit": "e7c8045968db237b765bb43687d055ed9ad4ec6e",
"LastSeenFile": "tests/cisa/exchange/Test-MtCisaSafeLinkDownloadScan.Tests.ps1"
},
{
"LegacyId": "MS.EXO.15.3",
"CurrentId": "CISA.MS.EXO.15.3",
"Rule": "prefix",
"CurrentId": "CISA.MS.SECURITYSUITE.7.3",
"Rule": "manual",
"FirstSeenCommit": "e7c8045968db237b765bb43687d055ed9ad4ec6e",
"LastSeenCommit": "e7c8045968db237b765bb43687d055ed9ad4ec6e",
"LastSeenFile": "tests/cisa/exchange/Test-MtCisaSafeLinkClickTracking.Tests.ps1"
Expand Down
Loading
Loading