]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
pybind/mgr: generalize CLICheckNonemptyFileInput() error msg
authorVarsha Rao <varao@redhat.com>
Tue, 18 May 2021 09:12:29 +0000 (14:42 +0530)
committerVarsha Rao <varao@redhat.com>
Wed, 26 May 2021 06:08:14 +0000 (11:38 +0530)
Signed-off-by: Varsha Rao <varao@redhat.com>
src/pybind/mgr/dashboard/services/access_control.py
src/pybind/mgr/dashboard/services/iscsi_cli.py
src/pybind/mgr/dashboard/settings.py
src/pybind/mgr/dashboard/tests/test_access_control.py
src/pybind/mgr/dashboard/tests/test_iscsi.py
src/pybind/mgr/dashboard/tests/test_settings.py
src/pybind/mgr/mgr_module.py

index 08bee8ec42917b2dfadd22c3ba819869b8272b0e..6176b70f280875fb0f61bdf17fa38b572887d38d 100644 (file)
@@ -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 <file>
@@ -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 <file>
index e46924d8c3534f9acf6878886a0ee79e27db5c1c..0e2e0b215ff3ac642009ff8c575da215d7d8720a 100644 (file)
@@ -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 <file>
index 26b8d638a765d9734415f00ddf24e098cc7e94e4..141555b4707fabe3d3b87410d1d4b9dc5ddcb064 100644 (file)
@@ -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
index f05490de52f99467e8b486f5f1d02f8de4d61260..a4a64174cee19ae82555eaf7f433236856f72f2a 100644 (file)
@@ -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()
index 04c163c7dccb9b5d27d9db0165de3fd94b4ed006..addcc09a1c485ba30895724db225c1215c16f73b 100644 (file)
@@ -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',
index 0644ecb8833e46c5b19fc9de8d80105453399c28..049475bbf48bc3f72845079f0037a4c80d266037 100644 (file)
@@ -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(
index 8ca80b6794df28c5f664a010a16acd82404018d3..fa6b2dd2689a5c34a71e69deb403b5727161159f 100644 (file)
@@ -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: