Skip to content

Commit

Permalink
SSC backend: remove redundant fields from class
Browse files Browse the repository at this point in the history
Change-Id: I307dbb3e9ae9108608e91aa9165362bffa298020
  • Loading branch information
mo-ki committed Sep 17, 2024
1 parent c5ac24f commit 88141a9
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 37 deletions.
2 changes: 1 addition & 1 deletion cmk/base/automations/check_mk.py
Original file line number Diff line number Diff line change
Expand Up @@ -2625,7 +2625,7 @@ def execute(self, args: list[str]) -> ActiveCheckResult:
command_line = self._replace_service_macros(
host_name,
service_data.description,
service_data.command,
" ".join(service_data.command),
)
return ActiveCheckResult(*self._execute_check_plugin(command_line))

Expand Down
4 changes: 2 additions & 2 deletions cmk/base/core_nagios/_create_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ def transform_active_service_command(
cfg.custom_commands_to_define.add("check-mk-custom")
return f"{service_data.command_name}!{service_data.command}"

escaped_args = service_data.args.replace("\\", "\\\\").replace("!", "\\!")
escaped_args = " ".join(service_data.command[1:]).replace("\\", "\\\\").replace("!", "\\!")
return f"{service_data.command_name}!{escaped_args}"


Expand Down Expand Up @@ -556,7 +556,7 @@ def get_dependencies(hostname: HostName, servicedesc: ServiceName) -> str:
_extra_service_conf_of(cfg, config_cache, hostname, service_data.description)
)

cfg.active_checks_to_define[service_data.plugin_name] = service_data.detected_executable
cfg.active_checks_to_define[service_data.plugin_name] = service_data.command[0]
active_services.append(service_spec)

if actchecks:
Expand Down
18 changes: 5 additions & 13 deletions cmk/base/server_side_calls/_active_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,13 @@
from ._commons import ConfigSet, replace_passwords, SSCRules


# This class can probably be consolidated to have fewer fields.
# But it's close to the release and I don't dare to touch it.
@dataclass(frozen=True)
class ActiveServiceData:
plugin_name: str
configuration: Mapping[str, object]
description: ServiceName
command_name: str
params: Mapping[str, object]
detected_executable: str
args: str

@property
def command(self) -> str:
return f"{self.detected_executable} {self.args}".rstrip()
command: tuple[str, ...]


@dataclass(frozen=True)
Expand Down Expand Up @@ -121,11 +114,10 @@ def _iterate_services(

yield ActiveServiceData(
plugin_name=plugin_name,
configuration=conf_dict,
description=service.service_description,
command_name=f"check_mk_active-{active_check.name}",
params=conf_dict,
detected_executable=detected_executable,
args=" ".join(arguments),
command=(detected_executable, *arguments),
)

def _sanitize_service_descriptions(
Expand Down Expand Up @@ -161,7 +153,7 @@ def get_active_service_descriptions(
yield ActiveServiceDescription(
plugin_name=raw_service.plugin_name,
description=raw_service.description,
params=raw_service.params,
params=raw_service.configuration,
)
except Exception as e:
if cmk.ccc.debug.enabled():
Expand Down
57 changes: 36 additions & 21 deletions tests/unit/cmk/base/server_side_calls/test_active_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,8 @@ def argument_function_with_exception(*args, **kwargs):
plugin_name="http",
description="HTTP myHTTPName on my_host_alias",
command_name="check_mk_active-http",
params={"name": "myHTTPName on my_host_alias"},
detected_executable="check_http",
args="--arg1 argument1 --arg2 argument2",
configuration={"name": "myHTTPName on my_host_alias"},
command=("check_http", "--arg1", "argument1", "--arg2", "argument2"),
),
],
id="http_active_service_plugin",
Expand Down Expand Up @@ -153,17 +152,15 @@ def argument_function_with_exception(*args, **kwargs):
plugin_name="my_active_check",
description="First service",
command_name="check_mk_active-my_active_check",
params={"description": "My active check", "param1": "param1"},
detected_executable="check_my_active_check",
args="--arg1 argument1",
configuration={"description": "My active check", "param1": "param1"},
command=("check_my_active_check", "--arg1", "argument1"),
),
ActiveServiceData(
plugin_name="my_active_check",
description="Second service",
command_name="check_mk_active-my_active_check",
params={"description": "My active check", "param1": "param1"},
detected_executable="check_my_active_check",
args="--arg2 argument2",
configuration={"description": "My active check", "param1": "param1"},
command=("check_my_active_check", "--arg2", "argument2"),
),
],
id="multiple_services",
Expand Down Expand Up @@ -225,16 +222,19 @@ def argument_function_with_exception(*args, **kwargs):
plugin_name="my_active_check",
description="My service",
command_name="check_mk_active-my_active_check",
params={
configuration={
"description": "My active check",
"password": (
"cmk_postprocessed",
"stored_password",
("stored_password", ""),
),
},
detected_executable="check_my_active_check",
args="--pwstore=1@9@/pw/store@stored_password '--secret=**********'",
command=(
"check_my_active_check",
"--pwstore=1@9@/pw/store@stored_password",
"'--secret=**********'",
),
),
],
id="one_service_password_store",
Expand Down Expand Up @@ -334,12 +334,18 @@ def test_get_active_service_data_password_with_hack(
plugin_name="test_check",
description="My service",
command_name="check_mk_active-test_check",
params={
configuration={
"description": "My active check",
"password": ("cmk_postprocessed", "explicit_password", ("uuid1234", "p4ssw0rd!")),
},
detected_executable="/path/to/check_test_check",
args="--pwstore=4@1@/pw/store@uuid1234 --password-id uuid1234:/pw/store --password-plain-in-curly '{*********}'",
command=(
"/path/to/check_test_check",
"--pwstore=4@1@/pw/store@uuid1234",
"--password-id",
"uuid1234:/pw/store",
"--password-plain-in-curly",
"'{*********}'",
),
),
]

Expand Down Expand Up @@ -386,12 +392,17 @@ def test_get_active_service_data_password_without_hack(
plugin_name="test_check",
description="My service",
command_name="check_mk_active-test_check",
params={
configuration={
"description": "My active check",
"password": ("cmk_postprocessed", "explicit_password", ("uuid1234", "p4ssw0rd!")),
},
detected_executable="/path/to/check_test_check",
args="--password-id uuid1234:/pw/store --password-plain-in-curly '{p4ssw0rd!}'",
command=(
"/path/to/check_test_check",
"--password-id",
"uuid1234:/pw/store",
"--password-plain-in-curly",
"'{p4ssw0rd!}'",
),
),
]

Expand Down Expand Up @@ -572,16 +583,20 @@ def test_test_get_active_service_data_crash_with_debug(
plugin_name="my_active_check",
description="My service",
command_name="check_mk_active-my_active_check",
params={
configuration={
"description": "My active check",
"password": (
"cmk_postprocessed",
"stored_password",
("stored_password", ""),
),
},
detected_executable="check_my_active_check",
args="--pwstore=2@0@/pw/store@stored_password --password '***'",
command=(
"check_my_active_check",
"--pwstore=2@0@/pw/store@stored_password",
"--password",
"'***'",
),
),
],
'\nWARNING: The stored password "stored_password" used by host "myhost" does not exist (anymore).\n',
Expand Down

0 comments on commit 88141a9

Please sign in to comment.