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 a UTC timestamp to the Provider JSON #1009

Merged
merged 6 commits into from
Mar 21, 2024
Merged
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
21 changes: 13 additions & 8 deletions PowerShell/ScubaGear/Modules/Orchestrator.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,7 @@ function Invoke-ProviderList {
$ProviderJSON = $ProviderJSON.TrimEnd(",")
$TimeZone = ""
$CurrentDate = Get-Date -ErrorAction 'Stop'
$TimestampZulu = $CurrentDate.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss.fffZ")
$GetTimeZone = Get-TimeZone -ErrorAction 'Stop'
if (($CurrentDate).IsDaylightSavingTime()) {
$TimeZone = ($GetTimeZone).DaylightName
Expand All @@ -601,6 +602,7 @@ function Invoke-ProviderList {
"baseline_version": "1",
"module_version": "$ModuleVersion",
"date": "$($CurrentDate) $($TimeZone)",
"timestamp_zulu": "$($TimestampZulu)",
"tenant_details": $($TenantDetails),
"scuba_config": $($ConfigDetails),

Expand Down Expand Up @@ -812,6 +814,15 @@ function Merge-JsonOutput {
)
process {
try {
# Files to delete at the end if no errors are encountered
$DeletionList = @()

# Load the raw provider output
$SettingsExportPath = Join-Path $OutFolderPath -ChildPath "$($OutProviderFileName).json"
$DeletionList += $SettingsExportPath
$SettingsExport = Get-Content $SettingsExportPath -Raw
$TimestampZulu = $(ConvertFrom-Json $SettingsExport).timestamp_zulu

# Extract the metadata
$Results = [pscustomobject]@{}
$Summary = [pscustomobject]@{}
Expand All @@ -822,10 +833,9 @@ function Merge-JsonOutput {
"Product" = "M365";
"Tool" = "ScubaGear";
"ToolVersion" = $ModuleVersion;
"TimestampZulu" = $TimestampZulu;
}

# Files to delete at the end if no errors are encountered
$DeletionList = @()

# Aggregate the report results and summaries
$IndividualReportPath = Join-Path -Path $OutFolderPath $IndividualReportFolderName -ErrorAction 'Stop'
Expand All @@ -844,11 +854,6 @@ function Merge-JsonOutput {
-NotePropertyValue $IndividualResults.ReportSummary
}

# Load the raw provider output
$SettingsFileName = Join-Path -Path $OutFolderPath -ChildPath "$($OutProviderFileName).json"
$DeletionList += $SettingsFileName
$SettingsExport = Get-Content $SettingsFileName -Raw

# Convert the output a json string
$MetaData = ConvertTo-Json $MetaData
$Results = ConvertTo-Json $Results -Depth 3
Expand All @@ -870,7 +875,7 @@ function Merge-JsonOutput {

# Save the file
$JsonFileName = Join-Path -Path $OutFolderPath "$($OutJsonFileName).json" -ErrorAction 'Stop'
$ReportJson | Out-File $JsonFileName
$ReportJson | Set-Content -Path $JsonFileName -Encoding $(Get-FileEncoding) -ErrorAction 'Stop'

# Delete the now redundant files
foreach ($File in $DeletionList) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@ InModuleScope Orchestrator {
BeforeAll {
Mock -CommandName Join-Path { "." }
Mock -CommandName Out-File {}
Mock -CommandName Set-Content {}
Mock -CommandName Remove-Item {}
Mock -CommandName Get-Content { "" }
Mock -CommandName ConvertFrom-Json { @{"ReportSummary"=@{"Date"=""}; "Results"=@();} }
Mock -CommandName ConvertFrom-Json { @{
"ReportSummary"=@{"Date"=""}
"Results"=@();
"timestamp_zulu"="";
}
}
Mock -CommandName Add-Member {}
Mock -CommandName ConvertTo-Json { "" }
}
Expand All @@ -28,15 +34,15 @@ InModuleScope Orchestrator {
ProductNames = @("aad")
}
{ Merge-JsonOutput @JsonParameters} | Should -Not -Throw
Should -Invoke -CommandName ConvertFrom-Json -Exactly -Times 1
Should -Invoke -CommandName ConvertFrom-Json -Exactly -Times 2
$JsonParameters.ProductNames = @()
}
It 'Merge multiple results' {
$JsonParameters += @{
ProductNames = @("aad", "teams")
}
{ Merge-JsonOutput @JsonParameters} | Should -Not -Throw
Should -Invoke -CommandName ConvertFrom-Json -Exactly -Times 2
Should -Invoke -CommandName ConvertFrom-Json -Exactly -Times 3
$JsonParameters.ProductNames = @()
}
It 'Delete redundant files' {
Expand Down
Loading