Module: check_mk
Branch: master
Commit: 4b558182d2c0d2cf077fb039cddf78243030b7c2
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=4b558182d2c0d2…
Author: Lars Michelsen <lm(a)mathias-kettner.de>
Date: Wed May 9 13:41:03 2018 +0200
6063 FIX Special agents: Remove unreplaced macros from special agent command lines
In previous versions the command lines of special agent calls like e.g. custom
data source programs could contain unreplaced macros in the format
<tt>$_HOST_MACRO$</tt>
which were later inpterpreted as shell variables. In case there was no such variable in
the shell environment, the trailing <tt>$</tt> was left on the command line as
single
character.
This could lead to some broken command line calls. We now do the replacement as usual.
After replacing all known macros, the command line is searched for left over $...$ macros
and remove the whole macro from the command line. This should make optional macro
handling
a lot easier.
Change-Id: I83d4152af691ea7a764c9fd7f6c210f0c078a64e
---
.werks/6063 | 20 ++++++++++++++++++++
cmk_base/data_sources/programs.py | 8 ++++++++
tests/unit/cmk_base/test_data_sources_unit.py | 20 ++++++++++++++++++++
3 files changed, 48 insertions(+)
diff --git a/.werks/6063 b/.werks/6063
new file mode 100644
index 0000000..8187cb5
--- /dev/null
+++ b/.werks/6063
@@ -0,0 +1,20 @@
+Title: Special agents: Remove unreplaced macros from special agent command lines
+Level: 1
+Component: core
+Class: fix
+Compatible: compat
+Edition: cre
+State: unknown
+Version: 1.6.0i1
+Date: 1525865298
+
+In previous versions the command lines of special agent calls like e.g. custom
+data source programs could contain unreplaced macros in the format
<tt>$_HOST_MACRO$</tt>
+which were later inpterpreted as shell variables. In case there was no such variable in
+the shell environment, the trailing <tt>$</tt> was left on the command line
as single
+character.
+
+This could lead to some broken command line calls. We now do the replacement as usual.
+After replacing all known macros, the command line is searched for left over $...$
macros
+and remove the whole macro from the command line. This should make optional macro
handling
+a lot easier.
diff --git a/cmk_base/data_sources/programs.py b/cmk_base/data_sources/programs.py
index e3f49e4..8dd4f1f 100644
--- a/cmk_base/data_sources/programs.py
+++ b/cmk_base/data_sources/programs.py
@@ -29,6 +29,7 @@ import signal
import subprocess
import cmk.paths
+import cmk.regex
import cmk_base.console as console
import cmk_base.config as config
@@ -136,6 +137,7 @@ class DSProgramDataSource(ProgramDataSource):
cmd = self._translate_legacy_macros(cmd)
cmd = self._translate_host_macros(cmd)
+ cmd = self._cleanup_left_over_macros(cmd)
return cmd
@@ -157,6 +159,12 @@ class DSProgramDataSource(ProgramDataSource):
return core_config.replace_macros(cmd, macros)
+ def _cleanup_left_over_macros(self, cmd):
+ """Now after we have replaced all known macros remove all
remaining macros in
+ the format $...$ to prevent shell expansion of them"""
+ return cmk.regex.regex(r"\$[a-zA-Z0-9_-]+\$").sub("", cmd)
+
+
class SpecialAgentDataSource(ProgramDataSource):
def __init__(self, hostname, ipaddress, special_agent_id, params):
diff --git a/tests/unit/cmk_base/test_data_sources_unit.py
b/tests/unit/cmk_base/test_data_sources_unit.py
index 65503ef..10de607 100644
--- a/tests/unit/cmk_base/test_data_sources_unit.py
+++ b/tests/unit/cmk_base/test_data_sources_unit.py
@@ -34,3 +34,23 @@ def test_disable_data_source_cache_no_write(mocker):
disabled_checker = mocker.patch.object(source, "is_agent_cache_disabled")
assert source._write_cache_file("X") is None
disabled_checker.assert_called_once()
+
+
+def test_ds_command_line_expansion_legacy_macros():
+ source = cmk_base.data_sources.programs.DSProgramDataSource("hostname",
"ipaddress", "echo '<IP> <HOST>'")
+ assert source._get_command_line() == "echo 'ipaddress hostname'"
+
+
+def test_ds_command_line_expansion_host_macros():
+ source = cmk_base.data_sources.programs.DSProgramDataSource("hostname",
"ipaddress", "echo '$HOSTNAME$'")
+ assert source._get_command_line() == "echo 'hostname'"
+
+
+def test_ds_command_line_expansion_left_over_macros():
+ source = cmk_base.data_sources.programs.DSProgramDataSource("hostname",
"ipaddress", "echo '$BLABLUB$'")
+ assert source._get_command_line() == "echo ''"
+
+
+def test_ds_command_line_expansion_skip_shell_variables():
+ source = cmk_base.data_sources.programs.DSProgramDataSource("hostname",
"ipaddress", "echo '$BLABLUB$ $XXX'")
+ assert source._get_command_line() == "echo ' $XXX'"