Module: check_mk
Branch: master
Commit: cc231449745a00582a3e4b7d15c016b0c4a4af52
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=cc231449745a00…
Author: Lars Michelsen <lm(a)mathias-kettner.de>
Date: Wed Apr 24 07:39:59 2019 +0200
Move config.agent_config.onlyfrom processing to HostConfig
CMK-2030
Change-Id: I711c6e368db817cba3de666a204769834423d6a8
---
cmk_base/config.py | 14 ++++++++++++++
cmk_base/data_sources/tcp.py | 8 +-------
tests/unit/cmk_base/test_config.py | 21 +++++++++++++++++++++
3 files changed, 36 insertions(+), 7 deletions(-)
diff --git a/cmk_base/config.py b/cmk_base/config.py
index 7d0feb0..41b6e28 100644
--- a/cmk_base/config.py
+++ b/cmk_base/config.py
@@ -2539,6 +2539,20 @@ class HostConfig(object):
return matched
@property
+ def only_from(self):
+ # type: () -> Optional[Union[List[str], str]]
+ """The agent of a host may be configured to be accessible only
from specific IPs"""
+ ruleset = agent_config.get("only_from", [])
+ if not ruleset:
+ return None
+
+ entries = self._config_cache.host_extra_conf(self.hostname, ruleset)
+ if not entries:
+ return None
+
+ return entries[0]
+
+ @property
def hostgroups(self):
# type: () -> List[str]
"""Returns the list of hostgroups of this host
diff --git a/cmk_base/data_sources/tcp.py b/cmk_base/data_sources/tcp.py
index 58a80a7..93ccc15 100644
--- a/cmk_base/data_sources/tcp.py
+++ b/cmk_base/data_sources/tcp.py
@@ -208,13 +208,7 @@ class TCPDataSource(CheckMKAgentDataSource):
def _sub_result_only_from(self, agent_info):
agent_only_from = agent_info.get("onlyfrom")
-
- ruleset = config.agent_config.get("only_from")
- if not ruleset:
- return 0, ''
-
- entries = config.get_config_cache().host_extra_conf(self._hostname, ruleset)
- config_only_from = entries[0] if entries else None
+ config_only_from = self._host_config.only_from
if None in (agent_only_from, config_only_from):
return 0, ''
diff --git a/tests/unit/cmk_base/test_config.py b/tests/unit/cmk_base/test_config.py
index c1f2b0e..5e7a48b 100644
--- a/tests/unit/cmk_base/test_config.py
+++ b/tests/unit/cmk_base/test_config.py
@@ -382,6 +382,27 @@ def test_host_config_special_agents(monkeypatch, hostname, result):
@pytest.mark.parametrize("hostname,result", [
+ ("testhost1", None),
+ ("testhost2", ["127.0.0.1"]),
+])
+def test_host_config_only_from(monkeypatch, hostname, result):
+ ts = Scenario().add_host(hostname)
+ ts.set_option(
+ "agent_config", {
+ "only_from": [
+ ([
+ "127.0.0.1",
+ ], [], ["testhost2"], {}),
+ ([
+ "127.0.0.2",
+ ], [], ["testhost2"], {}),
+ ],
+ })
+ config_cache = ts.apply(monkeypatch)
+ assert config_cache.get_host_config(hostname).only_from == result
+
+
+(a)pytest.mark.parametrize("hostname,result"sult", [
("testhost1", ["check_mk"]),
("testhost2", ["dingdong"]),
])