From 55a0ce4987c460c92d14b5687802c2aedef38c5a Mon Sep 17 00:00:00 2001 From: Lord Hepipud Date: Tue, 4 Jan 2022 20:50:38 +0100 Subject: [PATCH] Fixes repo sync by properly checking result --- doc/100-General/10-Changelog.md | 1 + .../getters/Get-IcingaAgentMSIPackage.psm1 | 2 +- .../repository/Sync-IcingaRepository.psm1 | 16 +++++++------ lib/core/repository/Test-IcingaValidJSON.psm1 | 23 +++++++++++++++++++ 4 files changed, 34 insertions(+), 8 deletions(-) create mode 100644 lib/core/repository/Test-IcingaValidJSON.psm1 diff --git a/doc/100-General/10-Changelog.md b/doc/100-General/10-Changelog.md index 7f59557..eaf66f1 100644 --- a/doc/100-General/10-Changelog.md +++ b/doc/100-General/10-Changelog.md @@ -18,6 +18,7 @@ Released closed milestones can be found on [GitHub](https://github.com/Icinga/ic * [#402](https://github.com/Icinga/icinga-powershell-framework/pull/402) Fixes missing address attribute for REST-Api daemon, making it unable to change the listening address * [#403](https://github.com/Icinga/icinga-powershell-framework/pull/403) Fixes memory leak on newly EventLog reader for CLI event stream * [#407](https://github.com/Icinga/icinga-powershell-framework/pull/407) Removes unnecessary module import inside `Invoke-IcingaNamespaceCmdlets` +* [#409](https://github.com/Icinga/icinga-powershell-framework/issues/409) Fixes URL builder for `Sync-IcingaRepository` which will now properly test the JSON file and try a secondary fallback by pointing to the `ifw.repo.json` in case a URL is returning the directory listing instead * [#411](https://github.com/Icinga/icinga-powershell-framework/pull/411) Fixes Icinga Director error message output because of missing `[string]::Format()` * [#412](https://github.com/Icinga/icinga-powershell-framework/issues/412) Fixes possible defective state of the Icinga Agent by using a custom service user for JEA profiles which is larger than 20 digits * [#418](https://github.com/Icinga/icinga-powershell-framework/pull/418) Fixes crash on wrong variable usage introduced by [#411](https://github.com/Icinga/icinga-powershell-framework/pull/411) diff --git a/lib/core/icingaagent/getters/Get-IcingaAgentMSIPackage.psm1 b/lib/core/icingaagent/getters/Get-IcingaAgentMSIPackage.psm1 index 3dde0a2..321c3b6 100644 --- a/lib/core/icingaagent/getters/Get-IcingaAgentMSIPackage.psm1 +++ b/lib/core/icingaagent/getters/Get-IcingaAgentMSIPackage.psm1 @@ -110,7 +110,7 @@ function Get-IcingaAgentMSIPackage() if ($SkipDownload -eq $FALSE) { $DownloadPath = Join-Path $Env:TEMP -ChildPath $UsePackage; Write-IcingaConsoleNotice ([string]::Format('Downloading Icinga 2 Agent installer "{0}" into temp directory "{1}"', $UsePackage, $DownloadPath)); - Invoke-IcingaWebRequest -Uri (Join-WebPath -Path $Source -ChildPath $UsePackage) -OutFile $DownloadPath; + Invoke-IcingaWebRequest -Uri (Join-WebPath -Path $Source -ChildPath $UsePackage) -OutFile $DownloadPath | Out-Null; } return @{ diff --git a/lib/core/repository/Sync-IcingaRepository.psm1 b/lib/core/repository/Sync-IcingaRepository.psm1 index bed338f..e12497f 100644 --- a/lib/core/repository/Sync-IcingaRepository.psm1 +++ b/lib/core/repository/Sync-IcingaRepository.psm1 @@ -96,12 +96,14 @@ function Sync-IcingaRepository() $Success = Copy-ItemSecure -Path $CopySource -Destination $TmpDir -Recurse -Force; } else { # Sync Source is web path $ProgressPreference = "SilentlyContinue"; - try { - Invoke-IcingaWebRequest -UseBasicParsing -Uri $Source -OutFile $RepoFile; - } catch { - try { - Invoke-IcingaWebRequest -UseBasicParsing -Uri (Join-WebPath -Path $Source -ChildPath 'ifw.repo.json') -OutFile $RepoFile; - } catch { + + $Result = Invoke-IcingaWebRequest -UseBasicParsing -Uri $Source -OutFile $RepoFile; + + if ($Result.HasError -Or (Test-IcingaValidJSON -File $RepoFile) -eq $FALSE) { + + $Result = Invoke-IcingaWebRequest -UseBasicParsing -Uri (Join-WebPath -Path $Source -ChildPath 'ifw.repo.json') -OutFile $RepoFile; + + if ($Result.HasError -Or (Test-IcingaValidJSON -File $RepoFile) -eq $FALSE) { Write-IcingaConsoleError 'Unable to download repository file from "{0}". Exception: "{1}"' -Objects $Source, $_.Exception.Message; $Success = Remove-Item -Path $TmpDir -Recurse -Force; return; @@ -145,7 +147,7 @@ function Sync-IcingaRepository() try { Write-IcingaConsoleNotice 'Syncing repository component "{0}" as file "{1}" into temp directory' -Objects $component, $package.Location; - Invoke-IcingaWebRequest -USeBasicParsing -Uri $DownloadLink -OutFile $TargetLocation; + Invoke-IcingaWebRequest -USeBasicParsing -Uri $DownloadLink -OutFile $TargetLocation | Out-Null; } catch { Write-IcingaConsoleError 'Failed to download repository component "{0}". Exception: "{1}"' -Objects $DownloadLink, $_.Exception.Message; continue; diff --git a/lib/core/repository/Test-IcingaValidJSON.psm1 b/lib/core/repository/Test-IcingaValidJSON.psm1 new file mode 100644 index 0000000..0c9e856 --- /dev/null +++ b/lib/core/repository/Test-IcingaValidJSON.psm1 @@ -0,0 +1,23 @@ +function Test-IcingaValidJSON() +{ + param ( + [string]$String = '', + [string]$File = '' + ); + + if ([string]::IsNullOrEmpty($File) -eq $FALSE) { + if ((Test-Path $File) -eq $FALSE) { + return $FALSE; + } + + $String = Get-Content -Path $File -Raw; + } + try { + # Test the conversion to JSON and return false on failure and true on success + ConvertFrom-Json -InputObject $String -ErrorAction Stop | Out-Null; + } catch { + return $FALSE; + } + + return $TRUE; +}