Skip to content

Commit

Permalink
added a try catch for error handling
Browse files Browse the repository at this point in the history
Added try catch for config error handling as well as a start of a unit test
  • Loading branch information
amart241 committed Jan 30, 2025
1 parent 7693a7e commit cb43a0e
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 3 deletions.
8 changes: 7 additions & 1 deletion PowerShell/ScubaGear/Modules/ScubaConfig/ScubaConfig.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,13 @@ class ScubaConfig {
}
[ScubaConfig]::ResetInstance()
$Content = Get-Content -Raw -Path $Path
$this.Configuration = $Content | ConvertFrom-Yaml
try {
$this.Configuration = $Content | ConvertFrom-Yaml
}
catch {
Write-Warning "Stacktrace: $($_.ScriptStackTrace)"
throw $_
}

$this.SetParameterDefaults()
[ScubaConfig]::_IsLoaded = $true
Expand Down
4 changes: 2 additions & 2 deletions PowerShell/ScubaGear/Sample-Config-Files/omit_policies.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ OutRegoFileName: TestResults
OutReportName: BaselineReports
OmitPolicy:
MS.EXO.2.2v1:
Rationale: "Known false positive; our SPF policy currently cannot to be retrieved via ScubaGear due to a split
horizon setup but is available publicly."
Rationale: "Known false positive; our SPF policy currently cannot to be retrieved via ScubaGear due to a split
horizon setup but is available publicly."
Expiration: "2023-12-31"
MS.TEAMS.6.1v1:
Rationale: &DLPRationale "The DLP capability required for Teams is implemented by third party product, [x],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
OmitPolicy:
MS.EXO.2.2v1:
Rationale: "Known false positive; our SPF policy currently cannot to be retrieved via ScubaGear due to a split
horizon setup but is available publicly."
Rationale: "Known false positive; our SPF policy currently cannot to be retrieved via ScubaGear due to a split
horizon setup but is available publicly."
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@ InModuleScope ScubaConfig {
function Get-ScubaDefault {throw 'this will be mocked'}
Mock -ModuleName ScubaConfig Get-ScubaDefault {"."}
}
context 'Handling repeated keys in YAML file' {
It 'Load config with dupliacte keys'{
$cfg = [ScubaConfig]::GetInstance()
# Load the first file and check the ProductNames value.

{[ScubaConfig]::GetInstance().LoadConfig((Join-Path -Path $PSScriptRoot -ChildPath "./MockLoadConfig.yaml"))} | Should -Throw
}
AfterAll {
[ScubaConfig]::ResetInstance()
}
}
context 'Handling repeated LoadConfig invocations' {
It 'Load valid config file followed by another'{
$cfg = [ScubaConfig]::GetInstance()
Expand Down

0 comments on commit cb43a0e

Please sign in to comment.