From 0b6e2235edd9e05b445b25fbe644a61f29742713 Mon Sep 17 00:00:00 2001 From: Lord Hepipud Date: Mon, 3 May 2021 19:10:36 +0200 Subject: [PATCH] Exit-IcingaExecutePlugin throws exception for localized PerfCounters Fixes #232 --- doc/31-Changelog.md | 1 + icinga-powershell-framework.psd1 | 8 ++++++-- .../Invoke-IcingaInternalServiceCall.psm1 | 4 ++-- lib/core/tools/ConvertFrom-JsonUTF8.psm1 | 15 +++++++++++++++ lib/core/tools/ConvertTo-JsonUTF8Bytes.psm1 | 17 +++++++++++++++++ 5 files changed, 41 insertions(+), 4 deletions(-) create mode 100644 lib/core/tools/ConvertFrom-JsonUTF8.psm1 create mode 100644 lib/core/tools/ConvertTo-JsonUTF8Bytes.psm1 diff --git a/doc/31-Changelog.md b/doc/31-Changelog.md index 0c53205..ea6ed19 100644 --- a/doc/31-Changelog.md +++ b/doc/31-Changelog.md @@ -21,6 +21,7 @@ Released closed milestones can be found on [GitHub](https://github.com/Icinga/ic ### Bugfixes +* [#232](https://github.com/Icinga/icinga-powershell-framework/pull/232) Fixes wrong encoding while using REST-Api checks experimental feature, and now forces UTF8 * [#240](https://github.com/Icinga/icinga-powershell-framework/pull/240) While filtering for certain services with `Get-IcingaServices`, there were some attributes missing from the collection. These are now added resulting in always correct output data. ## 1.4.1 (2021-03-10) diff --git a/icinga-powershell-framework.psd1 b/icinga-powershell-framework.psd1 index daf17db..cc530d1 100644 --- a/icinga-powershell-framework.psd1 +++ b/icinga-powershell-framework.psd1 @@ -31,7 +31,9 @@ '.\lib\core\tools\Test-IcingaFunction.psm1', '.\lib\core\tools\Write-IcingaConsoleHeader.psm1', '.\lib\core\framework\Test-IcingaFrameworkConsoleOutput.psm1', - '.\lib\core\tools\ConvertTo-IcingaSecureString.psm1' + '.\lib\core\tools\ConvertTo-IcingaSecureString.psm1', + '.\lib\core\tools\ConvertTo-JsonUTF8Bytes.psm1', + '.\lib\core\tools\ConvertFrom-JsonUTF8.psm1' ) FunctionsToExport = @( 'Use-Icinga', @@ -71,7 +73,9 @@ 'Test-IcingaFunction', 'Write-IcingaConsoleHeader', 'Test-IcingaFrameworkConsoleOutput', - 'ConvertTo-IcingaSecureString' + 'ConvertTo-IcingaSecureString', + 'ConvertTo-JsonUTF8Bytes', + 'ConvertFrom-JsonUTF8' ) CmdletsToExport = @('*') VariablesToExport = '*' diff --git a/lib/core/framework/Invoke-IcingaInternalServiceCall.psm1 b/lib/core/framework/Invoke-IcingaInternalServiceCall.psm1 index aedf88d..3b8fe99 100644 --- a/lib/core/framework/Invoke-IcingaInternalServiceCall.psm1 +++ b/lib/core/framework/Invoke-IcingaInternalServiceCall.psm1 @@ -86,7 +86,7 @@ function Invoke-IcingaInternalServiceCall() # Now queue the check inside our REST-Api try { - $ApiResult = Invoke-WebRequest -Method POST -UseBasicParsing -Uri ([string]::Format('https://localhost:{0}/v1/checker?command={1}', $RestApiPort, $Command)) -Body ($CommandArguments | ConvertTo-Json -Depth 100) -ContentType 'application/json' -TimeoutSec $Timeout; + $ApiResult = Invoke-WebRequest -Method POST -UseBasicParsing -Uri ([string]::Format('https://localhost:{0}/v1/checker?command={1}', $RestApiPort, $Command)) -Body (ConvertTo-JsonUTF8Bytes -InputObject $CommandArguments -Depth 100 -Compress) -ContentType 'application/json' -TimeoutSec $Timeout; } catch { # Something went wrong -> fallback to local execution $ExMsg = $_.Exception.message; @@ -96,7 +96,7 @@ function Invoke-IcingaInternalServiceCall() } # Resolve our result from the API - $IcingaResult = ConvertFrom-Json -InputObject $ApiResult; + $IcingaResult = ConvertFrom-JsonUTF8 -InputObject $ApiResult.Content; $IcingaCR = ''; # In case we didn't receive a check result, fallback to local execution diff --git a/lib/core/tools/ConvertFrom-JsonUTF8.psm1 b/lib/core/tools/ConvertFrom-JsonUTF8.psm1 new file mode 100644 index 0000000..ff7fa3e --- /dev/null +++ b/lib/core/tools/ConvertFrom-JsonUTF8.psm1 @@ -0,0 +1,15 @@ +function ConvertFrom-JsonUTF8() +{ + [CmdletBinding()] + param ( + [parameter(Mandatory = $TRUE, ValueFromPipeline = $TRUE)] + $InputObject = $null + ); + + # We need to properly encode our String to UTF8 + $ContentBytes = [System.Text.Encoding]::Default.GetBytes($InputObject); + $UTF8String = [System.Text.Encoding]::UTF8.GetString($ContentBytes); + + # Return the correct encoded JSON + return (ConvertFrom-Json -InputObject $UTF8String); +} diff --git a/lib/core/tools/ConvertTo-JsonUTF8Bytes.psm1 b/lib/core/tools/ConvertTo-JsonUTF8Bytes.psm1 new file mode 100644 index 0000000..6434dd2 --- /dev/null +++ b/lib/core/tools/ConvertTo-JsonUTF8Bytes.psm1 @@ -0,0 +1,17 @@ +function ConvertTo-JsonUTF8Bytes() +{ + [CmdletBinding()] + param ( + [parameter(Mandatory = $TRUE, ValueFromPipeline = $TRUE)] + $InputObject = $null, + [int]$Depth = 10, + [switch]$Compress = $FALSE + ); + + $JsonBody = ConvertTo-Json -InputObject $InputObject -Depth 100 -Compress; + $UTF8Bytes = ([System.Text.Encoding]::UTF8.GetBytes($JsonBody)); + + # Do not remove the "," as we require to force our PowerShell to handle our return value + # as proper collection + return , $UTF8Bytes; +}