Merge pull request #473 from Icinga:fix/icinga_config_string_dsl

Fix: Adds DSL parser to string arguments to fix whitespace and $ escaping

Fixes an issue with current implementation, as string values containing whitespaces or `$` are rendered wrong by default, if not set in single quotes `''`

Example:

Using `My Service`

will output

`My`
`Service`

This issue is already fixed for arrays, but plain string values on arguments require this workaround.

As Icinga 2 will always set arguments if a DSL is specified, regardless if a value is present or not, we have to use a `set_if` expression for this, but can't as the Icinga Director baskets currently ignore `set_if_format` which requires to be an expression instead of a string.
This commit is contained in:
Lord Hepipud 2022-08-17 17:02:16 +02:00 committed by GitHub
commit fd3ddd1538
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 2 deletions

View file

@ -13,6 +13,7 @@ Released closed milestones can be found on [GitHub](https://github.com/Icinga/ic
### Bugfixes
* [#473](https://github.com/Icinga/icinga-powershell-framework/pull/473) Fixes an issue with current string rendering config implementation, as string values containing whitespaces or `$` are rendered wrong by default, if not set in single quotes `''`
* [#476](https://github.com/Icinga/icinga-powershell-framework/pull/476) Fixes exception `You cannot call a method on va null-valued expression` during installation in case no background daemon is configured
* [#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`

View file

@ -268,6 +268,26 @@ function Get-IcingaCheckCommandConfig()
'order' = $Order;
}
);
} elseif ($parameter.type.name -eq 'String') {
# Conditional whether type of parameter is String
$Basket.Command[$check].arguments.Add(
[string]::Format('-{0}', $parameter.Name), @{
'value' = @{
'type' = 'Function';
'body' = [string]::Format(
'var str = macro("{0}");{1}var argLen = len(str);{1}{1}if (argLen == 0) {2}{1} return;{1}{3}{1}{1}if (argLen != 0 && str.substr(0,1) == "{4}" && str.substr(argLen - 1, argLen) == "{4}") {2}{1} return str;{1}{3}{1}{1}return ("{4}" + str + "{4}");',
$IcingaCustomVariable,
"`r`n",
'{',
'}',
"'"
);
}
'set_if' = [string]::Format('var str = macro("{0}"); if (len(str) == 0) {{ return false; }}; return true;', $IcingaCustomVariable);
'set_if_format' = 'expression';
'order' = $Order;
}
);
} elseif ($parameter.type.name -eq 'SecureString') {
# Convert out input string as SecureString
$Basket.Command[$check].arguments.Add(
@ -528,9 +548,11 @@ function Write-IcingaPlainConfigurationFiles()
# Order is numeric -> no "" required
if ($argconfig -eq 'order') {
$StringFormater = ' {0} = {1}{2}';
} elseif ($argconfig -eq 'set_if' -And $CheckArgument[$argconfig] -Like '*var str = macro*') {
$StringFormater = ' {0} = {{{{{2} {1}{2} }}}}{2}';
} else {
# All other entries should be handled as strings and contain ""
$StringFormater =' {0} = "{1}"{2}'
$StringFormater = ' {0} = "{1}"{2}'
}
# In case it is a hashtable, this is most likely a DSL function