From d88e61d33dcdddde81ef3ee484d00be868169d2b Mon Sep 17 00:00:00 2001 From: Lord Hepipud Date: Fri, 24 Sep 2021 10:21:44 +0200 Subject: [PATCH] Fixes error handling on IMC for invalid JSON --- doc/100-General/10-Changelog.md | 1 + lib/core/installer/Install-Icinga.psm1 | 24 +++++++++++++++++----- lib/core/installer/tools/CustomConfig.psm1 | 12 +++++++++-- 3 files changed, 30 insertions(+), 7 deletions(-) diff --git a/doc/100-General/10-Changelog.md b/doc/100-General/10-Changelog.md index 89aae0d..cf3ed71 100644 --- a/doc/100-General/10-Changelog.md +++ b/doc/100-General/10-Changelog.md @@ -14,6 +14,7 @@ Released closed milestones can be found on [GitHub](https://github.com/Icinga/ic ### Bugfixes * [#375](https://github.com/Icinga/icinga-powershell-framework/pull/375) Fixes exception on last message printed during `Uninstall-IcingaForWindows`, because the prior used function is no longer present at this point +* [#376](https://github.com/Icinga/icinga-powershell-framework/pull/376) Fixes IMC error handling on invalid JSON for installation command/file ## 1.6.1 (2021-09-15) diff --git a/lib/core/installer/Install-Icinga.psm1 b/lib/core/installer/Install-Icinga.psm1 index 672e750..4aaf5d1 100644 --- a/lib/core/installer/Install-Icinga.psm1 +++ b/lib/core/installer/Install-Icinga.psm1 @@ -50,15 +50,25 @@ function Install-Icinga() # Use our install command to configure everything if ([string]::IsNullOrEmpty($InstallCommand) -eq $FALSE) { - Disable-IcingaFrameworkConsoleOutput; + try { + $JsonInstallCmd = ConvertFrom-Json -InputObject $InstallCommand -ErrorAction Stop; + } catch { + Write-IcingaConsoleError 'Failed to deserialize the provided JSON from file or command: {0}' -Objects $_.Exception.Message; + return; + } # Add our "old" swap internally - $OldConfigSwap = Get-IcingaPowerShellConfig -Path 'Framework.Config.Swap'; + $OldConfigSwap = Get-IcingaPowerShellConfig -Path 'Framework.Config.Swap'; + Disable-IcingaFrameworkConsoleOutput; - [hashtable]$IcingaConfiguration = Convert-IcingaForwindowsManagementConsoleJSONConfig -Config (ConvertFrom-Json -InputObject $InstallCommand); + [hashtable]$IcingaConfiguration = Convert-IcingaForwindowsManagementConsoleJSONConfig -Config $JsonInstallCmd; # First run our configuration values - Invoke-IcingaForWindowsManagementConsoleCustomConfig -IcingaConfiguration $IcingaConfiguration; + $Success = Invoke-IcingaForWindowsManagementConsoleCustomConfig -IcingaConfiguration $IcingaConfiguration; + + if ($Success -eq $FALSE) { + return; + } # In case we use the director, we require to first fetch all basic values from the Self-Service API then # require to register the host to fet the remaining content @@ -74,7 +84,11 @@ function Install-Icinga() # Now apply our configuration again to ensure the defaults are overwritten again # Suite a mess, but we can improve this later - Invoke-IcingaForWindowsManagementConsoleCustomConfig -IcingaConfiguration $IcingaConfiguration; + $Success = Invoke-IcingaForWindowsManagementConsoleCustomConfig -IcingaConfiguration $IcingaConfiguration; + + if ($Success -eq $FALSE) { + return; + } Enable-IcingaFrameworkConsoleOutput; diff --git a/lib/core/installer/tools/CustomConfig.psm1 b/lib/core/installer/tools/CustomConfig.psm1 index 68af959..4dc52e2 100644 --- a/lib/core/installer/tools/CustomConfig.psm1 +++ b/lib/core/installer/tools/CustomConfig.psm1 @@ -8,7 +8,7 @@ function Invoke-IcingaForWindowsManagementConsoleCustomConfig() $cmdConfig = $IcingaConfiguration[$cmd]; if ($cmd.Contains(':')) { - continue; # skip for now, as more complicated + continue; } $cmdArguments = @{ @@ -22,6 +22,14 @@ function Invoke-IcingaForWindowsManagementConsoleCustomConfig() $cmdArguments.Add('DefaultInput', $cmdConfig.Selection) } - &$cmd @cmdArguments; + try { + &$cmd @cmdArguments; + } catch { + Enable-IcingaFrameworkConsoleOutput; + Write-IcingaConsoleError 'Failed to apply installation configuration of command "{0}" and argument list{1}because of the following error: "{2}"' -Objects $cmd, ($cmdArguments | Out-String), $_.Exception.Message; + return $FALSE; + } } + + return $TRUE; }