]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
mgr/mgr_module.py: CLICommand: Fix parsing of kwargs arguments 36877/head
authorSebastian Wagner <sebastian.wagner@suse.com>
Fri, 28 Aug 2020 10:56:49 +0000 (12:56 +0200)
committerSebastian Wagner <sebastian.wagner@suse.com>
Fri, 28 Aug 2020 10:56:49 +0000 (12:56 +0200)
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 <sebastian.wagner@suse.com>
src/pybind/mgr/mgr_module.py
src/pybind/mgr/orchestrator/tests/test_orchestrator.py

index 6cdff082301ab74b5c3450cb7d52ea3606a60eeb..9e8c3ba4ca6fb6e8145e05a3140cc4cd52b75612 100644 (file)
@@ -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'
index fff2e245ce1076baada54fc15e53b7164e6e7aea..a28b1cc251c03cae49144b107b590c5e9eb73e34 100644 (file)
@@ -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`)')