]> 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>
Tue, 29 Jun 2021 12:43:09 +0000 (18:13 +0530)
Signed-off-by: Varsha Rao <varao@redhat.com>
(cherry picked from commit 2dfd00995aca5fc8c8796a220c8f2c491aea5605)

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 dd32712494fc527664cdb58b3eb4446bc741e2c2..a6d72ec854a92995b40d420be0a78c302038569b 100644 (file)
@@ -25,7 +25,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):
@@ -550,7 +550,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>
@@ -689,7 +689,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,
@@ -843,7 +843,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):
     '''
@@ -865,7 +865,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 93abbcd190a329b84498a7ec1b90d0d81cd51207..71e6c9f3d9a2f33a0ae287054ed3aae96fabd65b 100644 (file)
@@ -23,7 +23,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 6c1b90374fb72660ce5ee69160ca1616b52b8e74..d76710a98e6e2cffabdcf01e0a5fb237c387b33e 100644 (file)
@@ -254,6 +254,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 9415d7e57064f7a738ec15d7069b689dd9dd5b16..80b423e51958692e6a33b4b67f3fb4dca0c81a64 100644 (file)
@@ -610,7 +610,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 994a0b91a43662fa1daa1ac36750d9dd7579a32f..4060c6c4a044cc4c752e7fa60e417b1e47445c02 100644 (file)
@@ -48,7 +48,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 e92b580e3a6277d206ca72c349fb8d0dd3c2b058..6e382b9d40f7d1ccd0c9297fa793da43b84eff4c 100644 (file)
@@ -69,7 +69,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 7d9d355539919d58400dd1e0108ea3cb2acc4592..44ee8a1e9a1ceeb55fdc64f5c396a9aaa96277d4 100644 (file)
@@ -38,8 +38,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",
@@ -413,19 +413,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: