Skip to content

Commit

Permalink
Merge pull request #332 from fflaten/eidsca-internal
Browse files Browse the repository at this point in the history
  • Loading branch information
f-bader authored Jul 14, 2024
2 parents 085b426 + f5ff3f6 commit bc788af
Show file tree
Hide file tree
Showing 96 changed files with 195 additions and 140 deletions.
169 changes: 93 additions & 76 deletions build/eidsca/Update-EidscaTests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,16 @@

param (
# Folder where generated test file should be written to.
[string] $TestFilePath = "./tests/EIDSCA/Test-EIDSCA.Generated.Tests.ps1",
[string] $TestFilePath = "$PSScriptRoot/../../tests/EIDSCA/Test-EIDSCA.Generated.Tests.ps1",

# Folder where docs should be generated
[string] $DocsPath = "./website/docs/tests/eidsca",
[string] $DocsPath = "$PSScriptRoot/../../website/docs/tests/eidsca",

[string] $PowerShellFunctionsPath = "./powershell/public/eidsca",
# Folder where control functions should be generated
[string] $PowerShellFunctionsPath = "$PSScriptRoot/../../powershell/internal/eidsca",

# Foldere where the public function should be generated
[string] $PublicFunctionPath = "$PSScriptRoot/../../powershell/public/eidsca",

# Control name to filter on
[string] $ControlName = "*",
Expand Down Expand Up @@ -284,58 +288,59 @@ Function UpdateTemplate($template, $control, $controlItem, $docName, $isDoc) {
$compareOperator = GetCompareOperator($controlItem.RecommendedValue)
$currentValue = $controlItem.CurrentValue

$psFunctionName = GetEidscaPsFunctionName -controlItem $controlItem
$psFunctionName = GetEidscaPsFunctionName -checkId $controlItem.CheckId
$portalDeepLinkMarkdown = GetPortalDeepLinkMarkdown -portalDeepLink $controlItem.PortalDeepLink
$graphDocsUrlMarkdown = GetMarkdownLink -uri $control.GraphDocsUrl -title "Graph Docs" -lookupTitle

$output = ''
if ($currentValue -eq '' -or $control.ControlName -eq '') {
Write-Warning 'Skipping'
} else {
$graphExplorerUrl = GetGraphExplorerMarkDownLink -relativeUri $relativeUri -apiVersion $apiVersion

if ($isDoc) {
# Only do this for docs
$graphDocsUrl = GetPageMarkdownLink($control.GraphDocsUrl)
$recommendation = GetPageMarkdownLink($controlItem.Recommendation)
$mitreDiagram = GetMitreDiagram -controlItem $controlItem
}
$output = ''
if ($currentValue -eq '' -or $control.ControlName -eq '') {
Write-Warning 'Skipping'
} else {
$graphExplorerUrl = GetGraphExplorerMarkDownLink -relativeUri $relativeUri -apiVersion $apiVersion

$output = $template
$output = $output -replace '%DocName%', $docName
$output = $output -replace '%ControlName%', $control.ControlName
$output = $output -replace '%Description%', $control.Description
$output = $output -replace '%ControlItemDescription%', $controlItem.Description
$output = $output -replace '%Severity%', $controlItem.Severity
$output = $output -replace '%DisplayName%', $controlItem.DisplayName
$output = $output -replace '%Name%', $controlItem.Name
$output = $output -replace '%CheckId%', $controlItem.CheckId
$output = $output -replace '%Recommendation%', $recommendation
$output = $output -replace '%MitreTactic%', $controlItem.MitreTactic
$output = $output -replace '%MitreTechnique%', $controlItem.MitreTechnique
$output = $output -replace '%MitreMitigation%', $controlItem.MitreMitigation
$output = $output -replace '%PortalDeepLink%', $portalDeepLink
$output = $output -replace '%DefaultValue%', $controlItem.DefaultValue
$output = $output -replace '%RelativeUri%', $relativeUri
$output = $output -replace '%ApiVersion%', $apiVersion
$output = $output -replace '%ShouldOperator%', $compareOperator.pester.Replace("'", "")
$output = $output -replace '%CompareOperatorText%', $compareOperator.Text
$output = $output -replace '%CompareOperator%', $compareOperator.Name
$output = $output -replace '%PwshCompareOperator%', $compareOperator.powershell.Replace("'", "")
$output = $output -replace '%RecommendedValue%', $recommendedValue
$output = $output -replace '%RecommendedValueMarkdown%', $recommendedValueMarkdown
$output = $output -replace '%CurrentValue%', $CurrentValue
$output = $output -replace '%GraphEndPoint%', $control.GraphEndpoint
$output = $output -replace '%GraphDocsUrl%', $graphDocsUrl
$output = $output -replace '%HowToFix%', $controlItem.howToFix
$output = $output -replace '%GraphExplorerUrl%', $graphExplorerUrl
$output = $output -replace '%MitreDiagram%', $mitreDiagram
$output = $output -replace '%PSFunctionName%', $psFunctionName
$output = $output -replace '%PortalDeepLinkMarkdown%', $portalDeepLinkMarkdown
$output = $output -replace '%GraphDocsUrlMarkdown%', $graphDocsUrlMarkdown
}
if ($isDoc) {
# Only do this for docs
$graphDocsUrl = GetPageMarkdownLink($control.GraphDocsUrl)
$recommendation = GetPageMarkdownLink($controlItem.Recommendation)
$mitreDiagram = GetMitreDiagram -controlItem $controlItem
}

return $output
$output = $template
$output = $output -replace '%DocName%', $docName
$output = $output -replace '%ControlName%', $control.ControlName
$output = $output -replace '%Description%', $control.Description
$output = $output -replace '%ControlItemDescription%', $controlItem.Description
$output = $output -replace '%Severity%', $controlItem.Severity
$output = $output -replace '%DisplayName%', $controlItem.DisplayName
$output = $output -replace '%Name%', $controlItem.Name
$output = $output -replace '%CheckId%', $controlItem.CheckId
$output = $output -replace '%CheckShortId%', ($controlItem.CheckId -replace '^EIDSCA\.')
$output = $output -replace '%Recommendation%', $recommendation
$output = $output -replace '%MitreTactic%', $controlItem.MitreTactic
$output = $output -replace '%MitreTechnique%', $controlItem.MitreTechnique
$output = $output -replace '%MitreMitigation%', $controlItem.MitreMitigation
$output = $output -replace '%PortalDeepLink%', $portalDeepLink
$output = $output -replace '%DefaultValue%', $controlItem.DefaultValue
$output = $output -replace '%RelativeUri%', $relativeUri
$output = $output -replace '%ApiVersion%', $apiVersion
$output = $output -replace '%ShouldOperator%', $compareOperator.pester.Replace("'", "")
$output = $output -replace '%CompareOperatorText%', $compareOperator.Text
$output = $output -replace '%CompareOperator%', $compareOperator.Name
$output = $output -replace '%PwshCompareOperator%', $compareOperator.powershell.Replace("'", "")
$output = $output -replace '%RecommendedValue%', $recommendedValue
$output = $output -replace '%RecommendedValueMarkdown%', $recommendedValueMarkdown
$output = $output -replace '%CurrentValue%', $CurrentValue
$output = $output -replace '%GraphEndPoint%', $control.GraphEndpoint
$output = $output -replace '%GraphDocsUrl%', $graphDocsUrl
$output = $output -replace '%HowToFix%', $controlItem.howToFix
$output = $output -replace '%GraphExplorerUrl%', $graphExplorerUrl
$output = $output -replace '%MitreDiagram%', $mitreDiagram
$output = $output -replace '%PSFunctionName%', $psFunctionName
$output = $output -replace '%PortalDeepLinkMarkdown%', $portalDeepLinkMarkdown
$output = $output -replace '%GraphDocsUrlMarkdown%', $graphDocsUrlMarkdown
}

return $output
}

# Returns the contents of a file named @template.txt at the given folder path
Expand All @@ -349,16 +354,23 @@ Function CreateFile($folderPath, $fileName, $content) {
$content | Out-File $filePath -Encoding utf8
}

Function GetEidscaPsFunctionName($controlItem) {
$powerShellFunctionName = "Test-Mt$($controlItem.CheckId)"
Function GetEidscaPsFunctionName($checkId) {
$powerShellFunctionName = "Test-Mt$($checkId)"
$powerShellFunctionName = $powerShellFunctionName.Replace("EIDSCA.", "Eidsca")
return $powerShellFunctionName
}

Function GeneratePublicFunction($folderPath, $controlIds) {
$output = GetTemplate -folderPath $folderPath -templateFileName '@Test-MtEidscaControl.txt'
$output = $output -replace '%ArrayOfControlIds%', "'$($controlIds -replace '^.*\.' -join "','")'"
$output = $output -replace '%InternalFunctionNameTemplate%', (GetEidscaPsFunctionName -checkId 'EIDSCA.$CheckId')
CreateFile -folderPath $folderPath -fileName 'Test-MtEidscaControl.ps1' -content $output
}

# Start by getting the latest EIDSCA config
$aadsc = Invoke-WebRequest -Uri $AadSecConfigUrl | ConvertFrom-Json
$aadsc = ($aadsc | Where-Object {$_.CollectedBy -eq "Maester"}).ControlArea
$Discovery = ($aadsc | where-Object {$_.discovery -ne ""}).Discovery
$Discovery = ($aadsc | Where-Object {$_.discovery -ne ""}).Discovery

# Remove previously generated files
Get-ChildItem -Path $DocsPath -Filter "*.md" -Exclude "readme.md" | Remove-Item -Force
Expand All @@ -374,15 +386,21 @@ if ($null -ne $ControlName) {
$aadsc = $aadsc | Where-Object { $_.ControlName -like $ControlName }
}

$exportedControls = [System.Collections.Generic.List[string]]::new()
foreach ($control in $aadsc) {
Write-Verbose "Generating test for $($control.ControlName)"

$testOutputList = [System.Text.StringBuilder]::new()

foreach ($controlItem in $control.Controls) {
# Export check only if RecommendedValue is set
if (($null -ne $controlItem.RecommendedValue -and $controlItem.RecommendedValue -ne "")) {
$docName = $controlItem.CheckId
if ($null -eq $controlItem.RecommendedValue -or $controlItem.RecommendedValue -eq '') {
Write-Warning "$($controlItem.CheckId) - $($controlItem.DisplayName) has no recommended value!"
continue
}

$exportedControls.Add($controlItem.CheckId)
$docName = $controlItem.CheckId

$testTemplate = @'
Describe "%ControlName%" -Tag "EIDSCA", "Security", "All", "%CheckId%" {
Expand All @@ -391,39 +409,38 @@ Describe "%ControlName%" -Tag "EIDSCA", "Security", "All", "%CheckId%" {
Check if "https://graph.microsoft.com/%ApiVersion%/%RelativeUri%"
.%CurrentValue% %CompareOperator% %RecommendedValue%
#>
%PSFunctionName% | Should -%ShouldOperator% %RecommendedValue%
Test-MtEidscaControl -CheckId %CheckShortId% | Should -%ShouldOperator% %RecommendedValue%
}
}
'@

# Add condition to test template if defined in EidscaTest
if ($controlItem.SkipCondition -ne "") {

$testTemplate = $testTemplate.Replace( '"%CheckId%"', '"%CheckId%" -Skip:( ' + $controlItem.SkipCondition + ' )')
}
$testOutput = UpdateTemplate -template $testTemplate -control $control -controlItem $controlItem -docName $docName
$docsOutput = UpdateTemplate -template $docsTemplate -control $control -controlItem $controlItem -docName $docName -isDoc $true
$psOutput = UpdateTemplate -template $psTemplate -control $control -controlItem $controlItem -docName $docName
$psMarkdownOutput = UpdateTemplate -template $psMarkdownTemplate -control $control -controlItem $controlItem -docName $docName -isDoc $true


if ($testOutput -ne '') {
[void]$testOutputList.AppendLine($testOutput)
# Add condition to test template if defined in EidscaTest
if ($controlItem.SkipCondition -ne "") {

CreateFile $DocsPath "$docName.md" $docsOutput
$psFunctionName = GetEidscaPsFunctionName -controlItem $controlItem
CreateFile $PowerShellFunctionsPath "$psFunctionName.ps1" $psOutput
CreateFile $PowerShellFunctionsPath "$psFunctionName.md" $psMarkdownOutput
}
} else {
Write-Warning "$($controlItem.CheckId) - $($controlItem.DisplayName) has no recommended value!"
$testTemplate = $testTemplate.Replace( '"%CheckId%"', '"%CheckId%" -Skip:( ' + $controlItem.SkipCondition + ' )')
}
$testOutput = UpdateTemplate -template $testTemplate -control $control -controlItem $controlItem -docName $docName
$docsOutput = UpdateTemplate -template $docsTemplate -control $control -controlItem $controlItem -docName $docName -isDoc $true
$psOutput = UpdateTemplate -template $psTemplate -control $control -controlItem $controlItem -docName $docName
$psMarkdownOutput = UpdateTemplate -template $psMarkdownTemplate -control $control -controlItem $controlItem -docName $docName -isDoc $true

if ($testOutput -ne '') {
[void]$testOutputList.AppendLine($testOutput)

CreateFile $DocsPath "$docName.md" $docsOutput
$psFunctionName = GetEidscaPsFunctionName -checkId $controlItem.CheckId
CreateFile $PowerShellFunctionsPath "$psFunctionName.ps1" $psOutput
CreateFile $PowerShellFunctionsPath "$psFunctionName.md" $psMarkdownOutput
}
}
if ($testOutputList.Length -ne 0) {
[void]$sb.AppendLine($testOutputList)
}
}

# Generate Test-MtEidscaControl
GeneratePublicFunction -folderPath $PublicFunctionPath -controlIds $exportedControls

$output = @'
BeforeDiscovery {
<DiscoveryFromJson>}
Expand Down
16 changes: 1 addition & 15 deletions powershell/Maester.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -122,21 +122,7 @@ FunctionsToExport = 'Add-MtTestResultDetail', 'Clear-MtGraphCache', 'Connect-Mae
'Test-MtCisaDlp',
'Test-MtConditionalAccessWhatIf',
'Test-MtConnection',
'Test-MtEidscaAF01',
'Test-MtEidscaAF02', 'Test-MtEidscaAF03', 'Test-MtEidscaAF04',
'Test-MtEidscaAF05', 'Test-MtEidscaAF06', 'Test-MtEidscaAG01',
'Test-MtEidscaAG02', 'Test-MtEidscaAG03', 'Test-MtEidscaAM01',
'Test-MtEidscaAM02', 'Test-MtEidscaAM03', 'Test-MtEidscaAM04',
'Test-MtEidscaAM06', 'Test-MtEidscaAM07', 'Test-MtEidscaAM09',
'Test-MtEidscaAM10', 'Test-MtEidscaAP01', 'Test-MtEidscaAP04',
'Test-MtEidscaAP05', 'Test-MtEidscaAP06', 'Test-MtEidscaAP07',
'Test-MtEidscaAP08', 'Test-MtEidscaAP09', 'Test-MtEidscaAP10',
'Test-MtEidscaAP14', 'Test-MtEidscaAT01', 'Test-MtEidscaAT02',
'Test-MtEidscaAV01', 'Test-MtEidscaCP01', 'Test-MtEidscaCP03',
'Test-MtEidscaCP04', 'Test-MtEidscaCR01', 'Test-MtEidscaCR02',
'Test-MtEidscaCR03', 'Test-MtEidscaCR04', 'Test-MtEidscaPR01',
'Test-MtEidscaPR02', 'Test-MtEidscaPR03', 'Test-MtEidscaPR05',
'Test-MtEidscaPR06', 'Test-MtEidscaST08', 'Test-MtEidscaST09',
'Test-MtEidscaControl',
'Test-MtPimAlertsExists', 'Test-MtPrivPermanentDirectoryRole',
'Update-MaesterTests', 'Compare-MtTestResult', 'Get-MailAuthenticationRecord',
'ConvertFrom-MailAuthenticationRecordSpf', 'ConvertFrom-MailAuthenticationRecordMx',
Expand Down
File renamed without changes.
File renamed without changes.
26 changes: 26 additions & 0 deletions powershell/public/eidsca/@Test-MtEidscaControl.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<#
.SYNOPSIS
Tests your environment for compliance with the specified EIDSCA control

.DESCRIPTION
Validates your environment against the specified EIDSCA control by comparing MS Graph result with the recommended value.

.EXAMPLE
Test-MtEidscaControl -CheckId AP01

Returns the result of the EIDSCA AP01 control check
#>

Function Test-MtEidscaControl {
[CmdletBinding()]
[OutputType([bool])]
param(
# Id for the EIDSCA control check to test
[Parameter(Mandatory)]
[ValidateSet(%ArrayOfControlIds%)]
[string]
$CheckId
)

& "%InternalFunctionNameTemplate%"
}
26 changes: 26 additions & 0 deletions powershell/public/eidsca/Test-MtEidscaControl.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<#
.SYNOPSIS
Tests your environment for compliance with the specified EIDSCA control
.DESCRIPTION
Validates your environment against the specified EIDSCA control by comparing MS Graph result with the recommended value.
.EXAMPLE
Test-MtEidscaControl -CheckId AP01
Returns the result of the EIDSCA AP01 control check
#>

Function Test-MtEidscaControl {
[CmdletBinding()]
[OutputType([bool])]
param(
# Id for the EIDSCA control check to test
[Parameter(Mandatory)]
[ValidateSet('AP01','AP04','AP05','AP06','AP07','AP08','AP09','AP10','AP14','CP01','CP03','CP04','PR01','PR02','PR03','PR05','PR06','ST08','ST09','AG01','AG02','AG03','AM01','AM02','AM03','AM04','AM06','AM07','AM09','AM10','AF01','AF02','AF03','AF04','AF05','AF06','AT01','AT02','AV01','CR01','CR02','CR03','CR04')]
[string]
$CheckId
)

& "Test-MtEidsca$CheckId"
}
Loading

0 comments on commit bc788af

Please sign in to comment.