From: John Mulligan Date: Mon, 10 Apr 2023 18:21:13 +0000 (-0400) Subject: pybind/mgr: improve error when inbuf is given to commands that don't use it X-Git-Tag: v18.1.0~46^2 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=e15d9fcac5829cc57bfaf85e0f5e663de623c23f;p=ceph.git pybind/mgr: improve error when inbuf is given to commands that don't use it When the inbuf (-i) argument is passed to the cli and then onto a mgr module, improve the error handling for CLICommand based calls and return an error that can make sense to users rather than an obscure trace back. Fixes: https://tracker.ceph.com/issues/59270 Signed-off-by: John Mulligan (cherry picked from commit cd2495e2279bc7e4ba9b369cd91a4a6af1196bad) --- diff --git a/src/pybind/mgr/mgr_module.py b/src/pybind/mgr/mgr_module.py index 2bbdb4bdd5c..1b2338871ae 100644 --- a/src/pybind/mgr/mgr_module.py +++ b/src/pybind/mgr/mgr_module.py @@ -1,7 +1,7 @@ import ceph_module # noqa from typing import cast, Tuple, Any, Dict, Generic, Optional, Callable, List, \ - Mapping, NamedTuple, Sequence, Union, TYPE_CHECKING + Mapping, NamedTuple, Sequence, Union, Set, TYPE_CHECKING if TYPE_CHECKING: import sys if sys.version_info >= (3, 8): @@ -379,6 +379,10 @@ class CLICommand(object): positional = True for index, arg in enumerate(full_argspec.args): if arg in cls.KNOWN_ARGS: + # record that this function takes an inbuf if it is present + # in the full_argspec and not already in the arg_spec + if arg == 'inbuf' and 'inbuf' not in arg_spec: + arg_spec['inbuf'] = 'str' continue if arg == '_end_positional_': positional = False @@ -435,11 +439,13 @@ class CLICommand(object): k, v = key, val return kwargs_switch, k.replace('-', '_'), v - def _collect_args_by_argspec(self, cmd_dict: Dict[str, Any]) -> Dict[str, Any]: + def _collect_args_by_argspec(self, cmd_dict: Dict[str, Any]) -> Tuple[Dict[str, Any], Set[str]]: kwargs = {} + special_args = set() kwargs_switch = False for index, (name, tp) in enumerate(self.arg_spec.items()): if name in CLICommand.KNOWN_ARGS: + special_args.add(name) continue assert self.first_default >= 0 raw_v = cmd_dict.get(name) @@ -449,14 +455,20 @@ class CLICommand(object): kwargs_switch, k, v = self._get_arg_value(kwargs_switch, name, raw_v) kwargs[k] = CephArgtype.cast_to(tp, v) - return kwargs + return kwargs, special_args def call(self, mgr: Any, cmd_dict: Dict[str, Any], inbuf: Optional[str] = None) -> HandleCommandResult: - kwargs = self._collect_args_by_argspec(cmd_dict) + kwargs, specials = self._collect_args_by_argspec(cmd_dict) if inbuf: + if 'inbuf' not in specials: + return HandleCommandResult( + -errno.EINVAL, + '', + 'Invalid command: Input file data (-i) not supported', + ) kwargs['inbuf'] = inbuf assert self.func return self.func(mgr, **kwargs)