From 922f962ae123b94ee7f92722190d19280f4ef6e3 Mon Sep 17 00:00:00 2001 From: Sebastian Wagner Date: Fri, 28 Aug 2020 12:56:49 +0200 Subject: [PATCH] mgr/mgr_module.py: CLICommand: Fix parsing of kwargs arguments Make parsing of keyword arguments a bit safer by makeing sure the key is actually a valid parameter name Fixes: https://tracker.ceph.com/issues/47185 Signed-off-by: Sebastian Wagner --- src/pybind/mgr/mgr_module.py | 12 ++++++++---- .../mgr/orchestrator/tests/test_orchestrator.py | 15 ++++++++++++++- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/src/pybind/mgr/mgr_module.py b/src/pybind/mgr/mgr_module.py index 6cdff082301ab..9e8c3ba4ca6fb 100644 --- a/src/pybind/mgr/mgr_module.py +++ b/src/pybind/mgr/mgr_module.py @@ -311,11 +311,15 @@ class CLICommand(object): for a, d in self.args_dict.items(): if 'req' in d and d['req'] == "false" and a not in cmd_dict: continue - if kwargs_switch or (isinstance(cmd_dict[a], str) and '=' in cmd_dict[a]): - mgr.log.debug('found kwarg, assuming all following args are kw style') - kwargs_switch = True + + if isinstance(cmd_dict[a], str) and '=' in cmd_dict[a]: + k, arg = cmd_dict[a].split('=', 1) + if k in self.args_dict: + kwargs_switch = True + + if kwargs_switch: try: - k, arg = cmd_dict[a].split('=') + k, arg = cmd_dict[a].split('=', 1) except ValueError as e: mgr.log.error('found positional arg after switching to kwarg parsing') return -errno.EINVAL, '', 'Error EINVAL: postitional arg not allowed after kwarg' diff --git a/src/pybind/mgr/orchestrator/tests/test_orchestrator.py b/src/pybind/mgr/orchestrator/tests/test_orchestrator.py index fff2e245ce107..a28b1cc251c03 100644 --- a/src/pybind/mgr/orchestrator/tests/test_orchestrator.py +++ b/src/pybind/mgr/orchestrator/tests/test_orchestrator.py @@ -8,6 +8,7 @@ import yaml from ceph.deployment.service_spec import ServiceSpec from ceph.deployment import inventory +from mgr_module import HandleCommandResult from test_orchestrator import TestOrchestrator as _TestOrchestrator from tests import mock @@ -15,7 +16,7 @@ from tests import mock from orchestrator import raise_if_exception, Completion, ProgressReference from orchestrator import InventoryHost, DaemonDescription, ServiceDescription from orchestrator import OrchestratorValidationError -from orchestrator.module import to_format +from orchestrator.module import to_format, OrchestratorCli def _test_resource(data, resource_class, extra=None): @@ -295,3 +296,15 @@ def test_event_multiline(): e = OrchestratorEvent(datetime.datetime.utcnow(), 'service', 'subject', 'ERROR', 'multiline\nmessage') assert OrchestratorEvent.from_json(e.to_json()) == e + + +def test_handle_command(): + cmd = { + 'prefix': 'orch daemon add', + 'daemon_type': 'mon', + 'placement': 'smithi044:[v2:172.21.15.44:3301,v1:172.21.15.44:6790]=c', + } + m = OrchestratorCli('orchestrator', 0, 0) + r = m._handle_command(None, cmd) + assert r == HandleCommandResult( + retval=-2, stdout='', stderr='No orchestrator configured (try `ceph orch set backend`)') -- 2.39.5