From: Varsha Rao Date: Tue, 18 May 2021 09:12:29 +0000 (+0530) Subject: pybind/mgr: generalize CLICheckNonemptyFileInput() error msg X-Git-Tag: v17.1.0~1822^2~2 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=2dfd00995aca5fc8c8796a220c8f2c491aea5605;p=ceph.git pybind/mgr: generalize CLICheckNonemptyFileInput() error msg Signed-off-by: Varsha Rao --- diff --git a/src/pybind/mgr/dashboard/services/access_control.py b/src/pybind/mgr/dashboard/services/access_control.py index 08bee8ec4291..6176b70f2808 100644 --- a/src/pybind/mgr/dashboard/services/access_control.py +++ b/src/pybind/mgr/dashboard/services/access_control.py @@ -24,7 +24,7 @@ from ..security import Permission, Scope from ..settings import Settings logger = logging.getLogger('access_control') - +DEFAULT_FILE_DESC = 'password/secret' # password hashing algorithm def password_hash(password, salt_password=None): @@ -549,7 +549,7 @@ def load_access_control_db(): # CLI dashboard access control scope commands @CLIWriteCommand('dashboard set-login-credentials') -@CLICheckNonemptyFileInput +@CLICheckNonemptyFileInput(desc=DEFAULT_FILE_DESC) def set_login_credentials_cmd(_, username: str, inbuf: str): ''' Set the login credentials. Password read from -i @@ -688,7 +688,7 @@ def ac_user_show_cmd(_, username: Optional[str] = None): @CLIWriteCommand('dashboard ac-user-create') -@CLICheckNonemptyFileInput +@CLICheckNonemptyFileInput(desc=DEFAULT_FILE_DESC) def ac_user_create_cmd(_, username: str, inbuf: str, rolename: Optional[str] = None, name: Optional[str] = None, @@ -842,7 +842,7 @@ def ac_user_del_roles_cmd(_, username: str, roles: Sequence[str]): @CLIWriteCommand('dashboard ac-user-set-password') -@CLICheckNonemptyFileInput +@CLICheckNonemptyFileInput(desc=DEFAULT_FILE_DESC) def ac_user_set_password(_, username: str, inbuf: str, force_password: bool = False): ''' @@ -864,7 +864,7 @@ def ac_user_set_password(_, username: str, inbuf: str, @CLIWriteCommand('dashboard ac-user-set-password-hash') -@CLICheckNonemptyFileInput +@CLICheckNonemptyFileInput(desc=DEFAULT_FILE_DESC) def ac_user_set_password_hash(_, username: str, inbuf: str): ''' Set user password bcrypt hash from -i diff --git a/src/pybind/mgr/dashboard/services/iscsi_cli.py b/src/pybind/mgr/dashboard/services/iscsi_cli.py index e46924d8c353..0e2e0b215ff3 100644 --- a/src/pybind/mgr/dashboard/services/iscsi_cli.py +++ b/src/pybind/mgr/dashboard/services/iscsi_cli.py @@ -22,7 +22,7 @@ def list_iscsi_gateways(_): @CLIWriteCommand('dashboard iscsi-gateway-add') -@CLICheckNonemptyFileInput +@CLICheckNonemptyFileInput(desc='iSCSI gateway configuration') def add_iscsi_gateway(_, inbuf, name: Optional[str] = None): ''' Add iSCSI gateway configuration. Gateway URL read from -i diff --git a/src/pybind/mgr/dashboard/settings.py b/src/pybind/mgr/dashboard/settings.py index 26b8d638a765..141555b4707f 100644 --- a/src/pybind/mgr/dashboard/settings.py +++ b/src/pybind/mgr/dashboard/settings.py @@ -252,6 +252,6 @@ def handles_secret(cmd: str) -> bool: return bool([cmd for secret_word in ['password', 'key'] if (secret_word in cmd)]) -@CLICheckNonemptyFileInput +@CLICheckNonemptyFileInput(desc='password/secret') def get_secret(inbuf=None): return inbuf, None, None diff --git a/src/pybind/mgr/dashboard/tests/test_access_control.py b/src/pybind/mgr/dashboard/tests/test_access_control.py index f05490de52f9..a4a64174cee1 100644 --- a/src/pybind/mgr/dashboard/tests/test_access_control.py +++ b/src/pybind/mgr/dashboard/tests/test_access_control.py @@ -609,7 +609,7 @@ class AccessControlTest(unittest.TestCase, CLICommandTestMixin): force_password=True) self.assertEqual(ctx.exception.retcode, -errno.EINVAL) - self.assertEqual(str(ctx.exception), ERROR_MSG_EMPTY_INPUT_FILE) + self.assertIn(ERROR_MSG_EMPTY_INPUT_FILE, str(ctx.exception)) def test_set_user_password_hash(self): user_orig = self.test_create_user() diff --git a/src/pybind/mgr/dashboard/tests/test_iscsi.py b/src/pybind/mgr/dashboard/tests/test_iscsi.py index 04c163c7dccb..addcc09a1c48 100644 --- a/src/pybind/mgr/dashboard/tests/test_iscsi.py +++ b/src/pybind/mgr/dashboard/tests/test_iscsi.py @@ -50,7 +50,7 @@ class IscsiTestCli(unittest.TestCase, CLICommandTestMixin): inbuf='') self.assertEqual(ctx.exception.retcode, -errno.EINVAL) - self.assertEqual(str(ctx.exception), ERROR_MSG_NO_INPUT_FILE) + self.assertIn(ERROR_MSG_NO_INPUT_FILE, str(ctx.exception)) def test_cli_add_gateway(self): self.exec_cmd('iscsi-gateway-add', name='node1', diff --git a/src/pybind/mgr/dashboard/tests/test_settings.py b/src/pybind/mgr/dashboard/tests/test_settings.py index 0644ecb8833e..049475bbf48b 100644 --- a/src/pybind/mgr/dashboard/tests/test_settings.py +++ b/src/pybind/mgr/dashboard/tests/test_settings.py @@ -68,7 +68,7 @@ class SettingsTest(unittest.TestCase, KVStoreMockMixin): ) self.assertEqual(r, -errno.EINVAL) self.assertEqual(out, '') - self.assertEqual(err, ERROR_MSG_EMPTY_INPUT_FILE) + self.assertIn(ERROR_MSG_EMPTY_INPUT_FILE, err) def test_set_secret(self): r, out, err = handle_option_command( diff --git a/src/pybind/mgr/mgr_module.py b/src/pybind/mgr/mgr_module.py index 8ca80b6794df..fa6b2dd2689a 100644 --- a/src/pybind/mgr/mgr_module.py +++ b/src/pybind/mgr/mgr_module.py @@ -37,8 +37,8 @@ else: return getattr(tp, '__origin__', None) -ERROR_MSG_EMPTY_INPUT_FILE = 'Empty content: please add a password/secret to the file.' -ERROR_MSG_NO_INPUT_FILE = 'Please specify the file containing the password/secret with "-i" option.' +ERROR_MSG_EMPTY_INPUT_FILE = 'Empty input file' +ERROR_MSG_NO_INPUT_FILE = 'Input file not specified' # Full list of strings in "osd_types.cc:pg_state_string()" PG_STATES = [ "active", @@ -409,19 +409,23 @@ def CLIWriteCommand(prefix: str, poll: bool = False) -> CLICommand: return CLICommand(prefix, "w", poll) -def CLICheckNonemptyFileInput(func: HandlerFuncType) -> HandlerFuncType: - @functools.wraps(func) - def check(*args: Any, **kwargs: Any) -> Tuple[int, str, str]: - if 'inbuf' not in kwargs: - return -errno.EINVAL, '', ERROR_MSG_NO_INPUT_FILE - if isinstance(kwargs['inbuf'], str): - # Delete new line separator at EOF (it may have been added by a text editor). - kwargs['inbuf'] = kwargs['inbuf'].rstrip('\r\n').rstrip('\n') - if not kwargs['inbuf'] or not kwargs['inbuf'].strip(): - return -errno.EINVAL, '', ERROR_MSG_EMPTY_INPUT_FILE - return func(*args, **kwargs) - check.__signature__ = inspect.signature(func) # type: ignore[attr-defined] - return check +def CLICheckNonemptyFileInput(desc: str) -> Callable[[HandlerFuncType], HandlerFuncType]: + def CheckFileInput(func: HandlerFuncType) -> HandlerFuncType: + @functools.wraps(func) + def check(*args: Any, **kwargs: Any) -> Tuple[int, str, str]: + if 'inbuf' not in kwargs: + return -errno.EINVAL, '', f'{ERROR_MSG_NO_INPUT_FILE}: Please specify the file '\ + f'containing {desc} with "-i" option' + if isinstance(kwargs['inbuf'], str): + # Delete new line separator at EOF (it may have been added by a text editor). + kwargs['inbuf'] = kwargs['inbuf'].rstrip('\r\n').rstrip('\n') + if not kwargs['inbuf'] or not kwargs['inbuf'].strip(): + return -errno.EINVAL, '', f'{ERROR_MSG_EMPTY_INPUT_FILE}: Please add {desc} to '\ + 'the file' + return func(*args, **kwargs) + check.__signature__ = inspect.signature(func) # type: ignore[attr-defined] + return check + return CheckFileInput def _get_localized_key(prefix: str, key: str) -> str: