Merge pull request #550 from Icinga:fix/negative_thresholds_interpreted_as_argument

Fix: Negative thresholds interpreted as argument

Fixes negative thresholds being interpreted wrongly as argument instead of an value for an argument
This commit is contained in:
Lord Hepipud 2022-08-24 16:50:28 +02:00 committed by GitHub
commit 4c5eeaf50b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 35 additions and 17 deletions

View file

@ -22,6 +22,7 @@ Released closed milestones can be found on [GitHub](https://github.com/Icinga/ic
* [#529](https://github.com/Icinga/icinga-powershell-framework/pull/529) Fixes package manifest reader for Icinga for Windows components on Windows 2012 R2 and older * [#529](https://github.com/Icinga/icinga-powershell-framework/pull/529) Fixes package manifest reader for Icinga for Windows components on Windows 2012 R2 and older
* [#523](https://github.com/Icinga/icinga-powershell-framework/pull/523) Fixes errors on encapsulated PowerShell calls for missing Cmdlets `Write-IcingaConsoleError` and `Optimize-IcingaForWindowsMemory` * [#523](https://github.com/Icinga/icinga-powershell-framework/pull/523) Fixes errors on encapsulated PowerShell calls for missing Cmdlets `Write-IcingaConsoleError` and `Optimize-IcingaForWindowsMemory`
* [#524](https://github.com/Icinga/icinga-powershell-framework/issues/524) Fixes uninstallation process by improving the location handling of PowerShell instances with Icinga IMC or Shell * [#524](https://github.com/Icinga/icinga-powershell-framework/issues/524) Fixes uninstallation process by improving the location handling of PowerShell instances with Icinga IMC or Shell
* [#528](https://github.com/Icinga/icinga-powershell-framework/issues/528) Fixes negative thresholds being interpreted wrongly as argument instead of an value for an argument
* [#545](https://github.com/Icinga/icinga-powershell-framework/issues/545) Fixes `RemoteSource` being cleared within repository `.json` files during `Update-IcingaRepository` tasks * [#545](https://github.com/Icinga/icinga-powershell-framework/issues/545) Fixes `RemoteSource` being cleared within repository `.json` files during `Update-IcingaRepository` tasks
* [#552](https://github.com/Icinga/icinga-powershell-framework/pull/552) Fixes file encoding for Icinga for Windows v1.10.0 to ensure the cache is always properly created with the correct encoding * [#552](https://github.com/Icinga/icinga-powershell-framework/pull/552) Fixes file encoding for Icinga for Windows v1.10.0 to ensure the cache is always properly created with the correct encoding
* [#553](https://github.com/Icinga/icinga-powershell-framework/pull/553) Fixes an exception caused by service recovery setting, if the required service was not installed before * [#553](https://github.com/Icinga/icinga-powershell-framework/pull/553) Fixes an exception caused by service recovery setting, if the required service was not installed before

View file

@ -11,15 +11,22 @@
The array of arguments for re-encoding. By default, this could be $args The array of arguments for re-encoding. By default, this could be $args
for calls from Exit-IcingaExecutePlugin for calls from Exit-IcingaExecutePlugin
.EXAMPLE .EXAMPLE
PS> [hashtable]$ConvertedArgs = ConvertTo-IcingaPowerShellArguments -Arguments $args; PS> [hashtable]$ConvertedArgs = ConvertTo-IcingaPowerShellArguments -Command $CheckCommand -Arguments $args;
#> #>
function ConvertTo-IcingaPowerShellArguments() function ConvertTo-IcingaPowerShellArguments()
{ {
param ( param (
[string]$Command = '',
[array]$Arguments = @() [array]$Arguments = @()
); );
if ([string]::IsNullOrEmpty($Command)) {
return @{ };
}
$CommandHelp = Get-Help -Name $Command -Full;
[hashtable]$IcingaArguments = @{ }; [hashtable]$IcingaArguments = @{ };
[int]$ArgumentIndex = 0; [int]$ArgumentIndex = 0;
@ -37,8 +44,8 @@ function ConvertTo-IcingaPowerShellArguments()
continue; continue;
} }
# Check if it starts with '-', which should indicate it being an argument # Check if our string value is a argument contained inside the command being executed
if ($Arguments[$ArgumentIndex][0] -ne '-') { if ($CommandHelp.parameters.parameter.name -Contains ($Arguments[$ArgumentIndex].SubString(1, $Arguments[$ArgumentIndex].Length - 1)) -eq $FALSE) {
# Continue if we are not an argument # Continue if we are not an argument
$ArgumentIndex += 1; $ArgumentIndex += 1;
continue; continue;
@ -52,7 +59,9 @@ function ConvertTo-IcingaPowerShellArguments()
# Check if there is anything beyond this argument, if not # Check if there is anything beyond this argument, if not
# -> We are a switch argument, adding TRUE; # -> We are a switch argument, adding TRUE;
if (($ArgumentIndex + 1) -ge $Arguments.Count) { if (($ArgumentIndex + 1) -ge $Arguments.Count) {
$IcingaArguments.Add($Argument, $TRUE); if ($IcingaArguments.ContainsKey($Argument) -eq $FALSE) {
$IcingaArguments.Add($Argument, $TRUE);
}
$ArgumentIndex += 1; $ArgumentIndex += 1;
continue; continue;
} }
@ -61,10 +70,12 @@ function ConvertTo-IcingaPowerShellArguments()
if ($Arguments[$ArgumentIndex + 1] -Is [string]) { if ($Arguments[$ArgumentIndex + 1] -Is [string]) {
[string]$NextValue = $Arguments[$ArgumentIndex + 1]; [string]$NextValue = $Arguments[$ArgumentIndex + 1];
# If our next value on the index starts with '-', we found another argument # If our next value on the index is an argument in our command
# -> The current argument seems to be a switch argument # -> The current argument seems to be a switch argument
if ($NextValue[0] -eq '-') { if ($CommandHelp.parameters.parameter.name -Contains ($NextValue.SubString(1, $NextValue.Length - 1))) {
$IcingaArguments.Add($Argument, $TRUE); if ($IcingaArguments.ContainsKey($Argument) -eq $FALSE) {
$IcingaArguments.Add($Argument, $TRUE);
}
$ArgumentIndex += 1; $ArgumentIndex += 1;
continue; continue;
} }
@ -86,8 +97,8 @@ function ConvertTo-IcingaPowerShellArguments()
[string]$NextValue = $Arguments[$ReadStringIndex + 1]; [string]$NextValue = $Arguments[$ReadStringIndex + 1];
# In case the next string element starts with '-', this could be an argument # Check the next string element and evaluate if it is an argument for our command
if ($NextValue[0] -eq '-') { if ($CommandHelp.parameters.parameter.name -Contains ($NextValue.SubString(1, $NextValue.Length - 1))) {
break; break;
} }
@ -103,7 +114,9 @@ function ConvertTo-IcingaPowerShellArguments()
# Add our argument with the string builder value, in case we had something to add there # Add our argument with the string builder value, in case we had something to add there
if ($StringValue.Length -ne 0) { if ($StringValue.Length -ne 0) {
$IcingaArguments.Add($Argument, (ConvertTo-IcingaUTF8Value -InputObject $StringValue.ToString())); if ($IcingaArguments.ContainsKey($Argument) -eq $FALSE) {
$IcingaArguments.Add($Argument, (ConvertTo-IcingaUTF8Value -InputObject $StringValue.ToString()));
}
$ArgumentIndex += 1; $ArgumentIndex += 1;
continue; continue;
} }
@ -114,17 +127,21 @@ function ConvertTo-IcingaPowerShellArguments()
# If we are an array object, handle empty arrays # If we are an array object, handle empty arrays
if ($Arguments[$ArgumentIndex + 1] -Is [array]) { if ($Arguments[$ArgumentIndex + 1] -Is [array]) {
if ($null -eq $Arguments[$ArgumentIndex + 1] -Or ($Arguments[$ArgumentIndex + 1]).Count -eq 0) { if ($null -eq $Arguments[$ArgumentIndex + 1] -Or ($Arguments[$ArgumentIndex + 1]).Count -eq 0) {
$IcingaArguments.Add($Argument, @()); if ($IcingaArguments.ContainsKey($Argument) -eq $FALSE) {
$IcingaArguments.Add($Argument, @());
}
$ArgumentIndex += 1; $ArgumentIndex += 1;
continue; continue;
} }
} }
# Add everything else if ($IcingaArguments.ContainsKey($Argument) -eq $FALSE) {
$IcingaArguments.Add( # Add everything else
$Argument, $IcingaArguments.Add(
(ConvertTo-IcingaUTF8Value -InputObject $Arguments[$ArgumentIndex + 1]) $Argument,
); (ConvertTo-IcingaUTF8Value -InputObject $Arguments[$ArgumentIndex + 1])
);
}
$ArgumentIndex += 1; $ArgumentIndex += 1;
} }

View file

@ -5,7 +5,7 @@ function Exit-IcingaExecutePlugin()
); );
# We need to fix the argument encoding hell # We need to fix the argument encoding hell
[hashtable]$ConvertedArgs = ConvertTo-IcingaPowerShellArguments -Arguments $args; [hashtable]$ConvertedArgs = ConvertTo-IcingaPowerShellArguments -Command $Command -Arguments $args;
[string]$JEAProfile = Get-IcingaJEAContext; [string]$JEAProfile = Get-IcingaJEAContext;
[bool]$CheckByIcingaForWindows = $FALSE; [bool]$CheckByIcingaForWindows = $FALSE;
[bool]$CheckByJEAShell = $FALSE; [bool]$CheckByJEAShell = $FALSE;