Skip to content
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

Add UUID to JSON output #1342

Merged
merged 11 commits into from
Oct 2, 2024
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ <h1>SCuBA M365 Security Baseline Conformance Reports</h1>
{TABLES}
<footer>
Report generated with <a class="individual_reports" href="https://github.com/cisagov/ScubaGear">CISA's ScubaGear</a> tool {MODULE_VERSION}
<br></br>
<span class="uuid">Report UUID: {REPORT_UUID}</span>
</footer>
</main>
</body>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ footer {
width: 100%;
}

.uuid {
font-size: 0.7em;
float: right;
padding-right: 0.313em;
color: var(--uuid-color);
}

.summary {
display: inline-block;
padding: 0.313em;
Expand Down
2 changes: 2 additions & 0 deletions PowerShell/ScubaGear/Modules/CreateReport/styles/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
--link-color: #85B065;
--unvisited-link-color: #0092CC;
--text-color: black;
--uuid-color: #424242;
--border-color: black;
--toggle-height: 1.563em;
--toggle-width: 2.875em;
Expand All @@ -39,6 +40,7 @@ html[data-theme='dark'] {
--link-color: #85B065;
--unvisited-link-color: #0092CC;
--text-color: #bdbdbd;
--uuid-color: #999999;
--border-color: #7b7b7b;
}

Expand Down
38 changes: 36 additions & 2 deletions PowerShell/ScubaGear/Modules/Orchestrator.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -615,15 +615,24 @@ function Invoke-ProviderList {
$ConfigDetails = "{}"
}

try {
$Guid = New-Guid -ErrorAction 'Stop'
}
catch {
$Guid = "00000000-0000-0000-0000-000000000000"
$Warning = "Error generating new UUID. See the exception message for more details: $($_)"
Write-Warning $Warning
}

$BaselineSettingsExport = @"
{
"baseline_version": "1",
"module_version": "$ModuleVersion",
"date": "$($CurrentDate) $($TimeZone)",
"timestamp_zulu": "$($TimestampZulu)",
"report_uuid": "$($Guid)",
"tenant_details": $($TenantDetails),
"scuba_config": $($ConfigDetails),

$ProviderJSON
}
"@
Expand Down Expand Up @@ -961,7 +970,9 @@ function Merge-JsonOutput {
$SettingsExportPath = Join-Path $OutFolderPath -ChildPath "$($OutProviderFileName).json"
$DeletionList += $SettingsExportPath
$SettingsExport = Get-Content $SettingsExportPath -Raw
$TimestampZulu = $(ConvertFrom-Json $SettingsExport).timestamp_zulu
$SettingsExportObject = $(ConvertFrom-Json $SettingsExport)
$TimestampZulu = $SettingsExportObject.timestamp_zulu
$ReportUuid = $SettingsExportObject.report_uuid

# Get a list and abbreviation mapping of the products assessed
$FullNames = @()
Expand All @@ -985,6 +996,7 @@ function Merge-JsonOutput {
"Tool" = "ScubaGear";
"ToolVersion" = $ModuleVersion;
"TimestampZulu" = $TimestampZulu;
"ReportUUID" = $ReportUuid;
}


Expand Down Expand Up @@ -1197,10 +1209,14 @@ function Invoke-ReportCreation {
$TenantMetaData = $TenantMetaData -replace '^(.*?)<table>','<table class ="tenantdata" style = "text-align:center;">'
$Fragment = $Fragment | ConvertTo-Html -Fragment -ErrorAction 'Stop'

$ProviderJSONFilePath = Join-Path -Path $OutFolderPath -ChildPath "$($OutProviderFileName).json" -Resolve
$ReportUuid = $(Get-Utf8NoBom -FilePath $ProviderJSONFilePath | ConvertFrom-Json).report_uuid

$ReportHtmlPath = Join-Path -Path $ReporterPath -ChildPath "ParentReport" -ErrorAction 'Stop'
$ReportHTML = (Get-Content $(Join-Path -Path $ReportHtmlPath -ChildPath "ParentReport.html") -ErrorAction 'Stop') -Join "`n"
$ReportHTML = $ReportHTML.Replace("{TENANT_DETAILS}", $TenantMetaData)
$ReportHTML = $ReportHTML.Replace("{TABLES}", $Fragment)
$ReportHTML = $ReportHTML.Replace("{REPORT_UUID}", $ReportUuid)
$ReportHTML = $ReportHTML.Replace("{MODULE_VERSION}", "v$ModuleVersion")
$ReportHTML = $ReportHTML.Replace("{BASELINE_URL}", $BaselineURL)

Expand Down Expand Up @@ -1749,6 +1765,24 @@ function Invoke-SCuBACached {
Write-Debug $ActualSavedLocation
}
$SettingsExport = Get-Content $ProviderJSONFilePath | ConvertFrom-Json

# Generate a new UUID if the original data doesn't have one
if (-not (Get-Member -InputObject $SettingsExport -Name "report_uuid" -MemberType Properties)) {
try {
$Guid = New-Guid -ErrorAction 'Stop'
}
catch {
$Guid = "00000000-0000-0000-0000-000000000000"
$Warning = "Error generating new UUID. See the exception message for more details: $($_)"
Write-Warning $Warning
}
$SettingsExport | Add-Member -Name 'report_uuid' -Value $Guid -Type NoteProperty
$ProviderContent = $SettingsExport | ConvertTo-Json -Depth 20
$ActualSavedLocation = Set-Utf8NoBom -Content $ProviderContent `
-Location $OutPath -FileName "$OutProviderFileName.json"
Write-Debug $ActualSavedLocation
}

$TenantDetails = $SettingsExport.tenant_details
$RegoParams = @{
'ProductNames' = $ProductNames;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ InModuleScope Orchestrator {
Mock -CommandName Get-Content {}
Mock -CommandName Add-Type {}
Mock -CommandName Invoke-Item {}

function Get-Utf8NoBom {throw 'this will be mocked'}
Mock -CommandName Get-Utf8NoBom {}
Mock -CommandName ConvertFrom-Json { @{ "report_uuid"="" } }
}
Context 'When creating the reports from Provider and OPA results JSON' {
BeforeAll {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ InModuleScope Orchestrator {
Mock -CommandName Write-Debug {}
Mock -CommandName New-Item {}
Mock -CommandName Get-Content {}
Mock -CommandName Get-Member { $true }
Mock -CommandName New-Guid { "00000000-0000-0000-0000-000000000000" }
}
Context 'When checking the conformance of commercial tenants' {
BeforeAll {
Expand Down Expand Up @@ -116,6 +118,19 @@ InModuleScope Orchestrator {
}
{Invoke-SCuBACached @SplatParams} | Should -Not -Throw
}
It 'Given an existing UUID should not generate a new one' {
# Get-Member was mocked above to return True so as far as the
# provider can tell, the existing output already has a UUID
{Invoke-SCuBACached @SplatParams} | Should -Not -Throw
Should -Invoke -CommandName New-Guid -Exactly -Times 0
}
It 'Given output without a UUID should generate a new one' {
Mock -CommandName Get-Member { $false }
# Now Get-Member will return False so as far as the provider
# can tell, the existing output does not have a UUID
{Invoke-SCuBACached @SplatParams} | Should -Not -Throw
Should -Invoke -CommandName New-Guid -Exactly -Times 1
}
}
Context 'When checking module version' {
It 'Given -Version should not throw' {
Expand Down
Loading