Browse Source

[2641] Remove the /users-exist cmdctl API call, and client code from bindctl

Mukund Sivaraman 12 years ago
parent
commit
f75fcf0b19

+ 0 - 34
src/bin/bindctl/bindcmd.py

@@ -248,33 +248,6 @@ WARNING: Python readline module isn't available, so the command line editor
             pass
             pass
         raise FailToLogin()
         raise FailToLogin()
 
 
-    def _have_users(self):
-        '''
-        Checks if cmdctl knows of any users by making a POST to it.  On
-        success of the POST, it returns True if cmdctl has users
-        configured, and False otherwise. On failure, a FailToLogin
-        exception is raised, and some information on the failure is
-        printed.
-        '''
-        try:
-            response = self.send_POST('/users-exist')
-            if response.status == http.client.OK:
-                return json.loads(response.read().decode())
-            # if not OK, fall through to raise error
-            self._print("Failure in cmdctl when checking if users already exist")
-        except ssl.SSLError as err:
-            self._print("SSL error checking if users exist: ", err)
-            if err.errno == ssl.SSL_ERROR_EOF:
-                self.__print_check_ssl_msg()
-        except socket.error as err:
-            self._print("Socket error checking if users exist: ", err)
-            # An SSL setup error can also bubble up as a plain CONNRESET...
-            # (on some systems it usually does)
-            if err.errno == errno.ECONNRESET:
-                self.__print_check_ssl_msg()
-            pass
-        raise FailToLogin()
-
     def login_to_cmdctl(self):
     def login_to_cmdctl(self):
         '''Login to cmdctl with the username and password given by
         '''Login to cmdctl with the username and password given by
         the user. After the login is sucessful, the username and
         the user. After the login is sucessful, the username and
@@ -282,13 +255,6 @@ WARNING: Python readline module isn't available, so the command line editor
         time, username and password saved in 'default_user.csv' will be
         time, username and password saved in 'default_user.csv' will be
         used first.
         used first.
         '''
         '''
-        # First, check that valid users exist. If not, ask the person at
-        # the tty to configure one using b10-cmdctl-usermgr.
-        if not self._have_users():
-            self._print('There are no existing users. Please configure '
-                        'a user account using b10-cmdctl-usermgr.')
-            return False
-
         # Look at existing username/password combinations and try to log in
         # Look at existing username/password combinations and try to log in
         users = self._get_saved_user_info(self.csv_file_dir, CSV_FILE_NAME)
         users = self._get_saved_user_info(self.csv_file_dir, CSV_FILE_NAME)
         for row in users:
         for row in users:

+ 0 - 186
src/bin/bindctl/tests/bindctl_test.py

@@ -496,192 +496,6 @@ class TestConfigCommands(unittest.TestCase):
         finally:
         finally:
             self.tool.conn = orig_conn
             self.tool.conn = orig_conn
 
 
-    def test_have_users(self):
-        # Make sure _have_users raises the correct exception
-        # upon failure of either send_POST or the read() on the
-        # response
-
-        orig_send_POST = self.tool.send_POST
-        expected_printed_messages = []
-        try:
-            # Check what happens when cmdctl returns a HTTP server failure
-            def send_POST_HTTPFailure(self, params=None):
-                '''Replacement send_POST() method that returns a
-                HTTP failure code.'''
-                class MyResponse:
-                    def __init__(self):
-                        self.status = http.client.SERVICE_UNAVAILABLE
-                    def read(self):
-                        return ''
-                return MyResponse()
-
-            self.tool.send_POST = send_POST_HTTPFailure
-            self.assertRaises(FailToLogin, self.tool._have_users)
-            expected_printed_messages.append(
-                'Failure in cmdctl when checking if users already exist')
-            self.__check_printed_messages(expected_printed_messages)
-
-            # Check what happens when cmdctl returns a result successfully
-            def create_send_POST_success(status):
-                '''Create a replacement send_POST() method that
-                successfully returns status.'''
-                def send_POST_success(self, params=None):
-                    class MyResponse:
-                        def __init__(self):
-                            self.status = http.client.OK
-                        def read(self):
-                            class MyData:
-                                def decode(self):
-                                    return json.dumps(status)
-                            return MyData()
-                    return MyResponse()
-                return send_POST_success
-
-            # Users exist
-            self.tool.send_POST = create_send_POST_success(True)
-            self.assertTrue(self.tool._have_users())
-
-            # Users don't exist
-            self.tool.send_POST = create_send_POST_success(False)
-            self.assertFalse(self.tool._have_users())
-
-            # Check what happens when send_POST() raises an exception
-            def send_POST_raiseImmediately(self, params=None):
-                raise socket.error("test error")
-
-            self.tool.send_POST = send_POST_raiseImmediately
-            self.assertRaises(FailToLogin, self.tool._have_users)
-            expected_printed_messages.append(
-                'Socket error checking if users exist:  test error')
-            self.__check_printed_messages(expected_printed_messages)
-
-            # Check what happens when reading a HTTP response raises an
-            # exception
-            def create_send_POST_raiseOnRead(exception):
-                '''Create a replacement send_POST() method that raises
-                   the given exception when read() is called on the value
-                   returned from send_POST()'''
-                def send_POST_raiseOnRead(self, params=None):
-                    class MyResponse:
-                        def __init__(self):
-                            self.status = http.client.OK
-                        def read(self):
-                            raise exception
-                    return MyResponse()
-                return send_POST_raiseOnRead
-
-            # basic socket error
-            self.tool.send_POST =\
-                create_send_POST_raiseOnRead(socket.error("read error"))
-            self.assertRaises(FailToLogin, self.tool._have_users)
-            expected_printed_messages.append(
-                'Socket error checking if users exist:  read error')
-            self.__check_printed_messages(expected_printed_messages)
-
-            # connection reset
-            exc = socket.error("connection reset")
-            exc.errno = errno.ECONNRESET
-            self.tool.send_POST =\
-                create_send_POST_raiseOnRead(exc)
-            self.assertRaises(FailToLogin, self.tool._have_users)
-            expected_printed_messages.append(
-                'Socket error checking if users exist:  '
-                'connection reset')
-            expected_printed_messages.append(
-                'Please check the logs of b10-cmdctl, there may be a '
-                'problem accepting SSL connections, such as a permission '
-                'problem on the server certificate file.'
-            )
-            self.__check_printed_messages(expected_printed_messages)
-
-            # 'normal' SSL error
-            exc = ssl.SSLError()
-            self.tool.send_POST =\
-                create_send_POST_raiseOnRead(exc)
-            self.assertRaises(FailToLogin, self.tool._have_users)
-            expected_printed_messages.append(
-                'SSL error checking if users exist:  .*')
-            self.__check_printed_messages(expected_printed_messages)
-
-            # 'EOF' SSL error
-            exc = ssl.SSLError()
-            exc.errno = ssl.SSL_ERROR_EOF
-            self.tool.send_POST =\
-                create_send_POST_raiseOnRead(exc)
-            self.assertRaises(FailToLogin, self.tool._have_users)
-            expected_printed_messages.append(
-                'SSL error checking if users exist: .*')
-            expected_printed_messages.append(
-                'Please check the logs of b10-cmdctl, there may be a '
-                'problem accepting SSL connections, such as a permission '
-                'problem on the server certificate file.'
-            )
-            self.__check_printed_messages(expected_printed_messages)
-
-            # any other exception should be passed through
-            self.tool.send_POST =\
-                create_send_POST_raiseOnRead(ImportError())
-            self.assertRaises(ImportError, self.tool._have_users)
-            self.__check_printed_messages(expected_printed_messages)
-
-        finally:
-            self.tool.send_POST = orig_send_POST
-
-    def test_have_users_calls_cmdctl(self):
-        # Make sure _have_users() makes the right API call to
-        # cmdctl. This also checks that send_POST() can handle an empty
-        # post_param.
-        orig_conn = self.tool.conn
-        try:
-            class MyConn:
-                def __init__(self):
-                    self.method = None
-                    self.url = None
-                    self.param = None
-                    self.headers = None
-
-                def request(self, method, url, param, headers):
-                    self.method = method
-                    self.url = url
-                    self.param = param
-                    self.headers = headers
-
-                def getresponse(self):
-                    class MyResponse:
-                        def __init__(self):
-                            self.status = http.client.OK
-                        def read(self):
-                            class MyData:
-                                def decode(self):
-                                    return json.dumps(True)
-                            return MyData()
-                    return MyResponse()
-
-            self.tool.conn = MyConn()
-            self.assertTrue(self.tool._have_users())
-            self.assertEqual(self.tool.conn.method, 'POST')
-            self.assertEqual(self.tool.conn.url, '/users-exist')
-            self.assertIsNone(self.tool.conn.param)
-            self.assertIn('cookie', self.tool.conn.headers)
-        finally:
-            self.tool.conn = orig_conn
-
-    def test_login_to_cmdctl_calls_have_users(self):
-        # Test that login_to_cmdctl() calls _have_users() and fails if
-        # _have_users() returns False.
-        orig_have_users = self.tool._have_users
-        try:
-            def my_have_users():
-                return False
-
-            self.tool._have_users = my_have_users
-            self.assertFalse(self.tool.login_to_cmdctl())
-            self.__check_printed_messages(
-                ['There are no existing users. Please configure '
-                 'a user account using b10-cmdctl-usermgr.'])
-        finally:
-            self.tool._have_users = orig_have_users
-
     def test_run(self):
     def test_run(self):
         def login_to_cmdctl():
         def login_to_cmdctl():
             return True
             return True

+ 1 - 15
src/bin/cmdctl/cmdctl.py.in

@@ -157,9 +157,7 @@ class SecureHTTPRequestHandler(http.server.BaseHTTPRequestHandler):
         self.session_id = self.headers.get('cookie')
         self.session_id = self.headers.get('cookie')
         rcode, reply = http.client.OK, []
         rcode, reply = http.client.OK, []
         if self._is_session_valid():
         if self._is_session_valid():
-            if self.path == '/users-exist':
-                rcode, reply = self._handle_users_exist()
-            elif self.path == '/login':
+            if self.path == '/login':
                 rcode, reply = self._handle_login()
                 rcode, reply = self._handle_login()
             elif self._is_user_logged_in():
             elif self._is_user_logged_in():
                 rcode, reply = self._handle_post_request()
                 rcode, reply = self._handle_post_request()
@@ -182,12 +180,6 @@ class SecureHTTPRequestHandler(http.server.BaseHTTPRequestHandler):
         else:
         else:
             return http.client.UNAUTHORIZED, error_info
             return http.client.UNAUTHORIZED, error_info
 
 
-    def _handle_users_exist(self):
-        if self.server.get_num_users() > 0:
-            return http.client.OK, True
-        else:
-            return http.client.OK, False
-
     def _check_user_name_and_pwd(self):
     def _check_user_name_and_pwd(self):
         '''Check user name and its password '''
         '''Check user name and its password '''
         length = self.headers.get('Content-Length')
         length = self.headers.get('Content-Length')
@@ -545,12 +537,6 @@ class SecureHTTPServer(socketserver_mixin.NoPollMixIn,
             info = self._user_infos.get(username)
             info = self._user_infos.get(username)
         return info
         return info
 
 
-    def get_num_users(self):
-        ''' Returns the number of users known to the server. '''
-        with self._lock:
-            num = len(self._user_infos)
-        return num
-
     def save_user_session_id(self, session_id):
     def save_user_session_id(self, session_id):
         ''' Record user's id and login time. '''
         ''' Record user's id and login time. '''
         self.user_sessions[session_id] = time.time()
         self.user_sessions[session_id] = time.time()

+ 0 - 40
src/bin/cmdctl/tests/cmdctl_test.py

@@ -290,39 +290,6 @@ class TestSecureHTTPRequestHandler(unittest.TestCase):
         rcode, reply = self.handler._handle_post_request()
         rcode, reply = self.handler._handle_post_request()
         self.assertEqual(http.client.BAD_REQUEST, rcode)
         self.assertEqual(http.client.BAD_REQUEST, rcode)
 
 
-    def test_handle_users_exist(self):
-        orig_get_num_users = self.handler.server.get_num_users
-        try:
-            def create_get_num_users(n):
-                '''Create a replacement get_num_users() method.'''
-                def my_get_num_users():
-                    return n
-                return my_get_num_users
-
-            # Check case where get_num_users() returns 0
-            self.handler.server.get_num_users = create_get_num_users(0)
-            self.handler.headers['cookie'] = 12345
-            self.handler.path = '/users-exist'
-            self.handler.do_POST()
-            self.assertEqual(self.handler.rcode, http.client.OK)
-            self.handler.wfile.seek(0, 0)
-            d = self.handler.wfile.read()
-            self.assertFalse(json.loads(d.decode()))
-
-            # Clear the output
-            self.handler.wfile.seek(0, 0)
-            self.handler.wfile.truncate()
-
-            # Check case where get_num_users() returns > 0
-            self.handler.server.get_num_users = create_get_num_users(4)
-            self.handler.do_POST()
-            self.assertEqual(self.handler.rcode, http.client.OK)
-            self.handler.wfile.seek(0, 0)
-            d = self.handler.wfile.read()
-            self.assertTrue(json.loads(d.decode()))
-        finally:
-            self.handler.server.get_num_users = orig_get_num_users
-
     def test_handle_login(self):
     def test_handle_login(self):
         orig_is_user_logged_in = self.handler._is_user_logged_in
         orig_is_user_logged_in = self.handler._is_user_logged_in
         orig_check_user_name_and_pwd = self.handler._check_user_name_and_pwd
         orig_check_user_name_and_pwd = self.handler._check_user_name_and_pwd
@@ -566,13 +533,6 @@ class TestSecureHTTPServer(unittest.TestCase):
         self.assertIn('6f0c73bd33101a5ec0294b3ca39fec90ef4717fe',
         self.assertIn('6f0c73bd33101a5ec0294b3ca39fec90ef4717fe',
                       self.server.get_user_info('root'))
                       self.server.get_user_info('root'))
 
 
-    def test_get_num_users(self):
-        self.server._create_user_info('/local/not-exist')
-        self.assertEqual(0, self.server.get_num_users())
-
-        self.server._create_user_info(SRC_FILE_PATH + 'cmdctl-accounts.csv')
-        self.assertEqual(1, self.server.get_num_users())
-
     def test_check_file(self):
     def test_check_file(self):
         # Just some file that we know exists
         # Just some file that we know exists
         file_name = BUILD_FILE_PATH + 'cmdctl-keyfile.pem'
         file_name = BUILD_FILE_PATH + 'cmdctl-keyfile.pem'