Example #1
0
 def test_generate_password(self):
     password = utils.generate_password()
     self.assertTrue([c for c in password if c in '0123456789'])
     self.assertTrue([c for c in password
                      if c in 'abcdefghijklmnopqrstuvwxyz'])
     self.assertTrue([c for c in password
                      if c in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'])
Example #2
0
 def add_console(self,
                 context,
                 instance_id,
                 password=None,
                 port=None,
                 **kwargs):
     instance = self.db.instance_get(context, instance_id)
     host = instance['host']
     name = instance['name']
     pool = self.get_pool_for_instance_host(context, host)
     try:
         console = self.db.console_get_by_pool_instance(
             context, pool['id'], instance_id)
     except exception.NotFound:
         logging.debug(_('Adding console'))
         if not password:
             password = utils.generate_password(8)
         if not port:
             port = self.driver.get_port(context)
         console_data = {
             'instance_name': name,
             'instance_id': instance_id,
             'password': password,
             'pool_id': pool['id']
         }
         if port:
             console_data['port'] = port
         console = self.db.console_create(context, console_data)
         self.driver.setup_console(context, console)
     return console['id']
Example #3
0
 def add_console(self, context, instance_id, password=None,
                 port=None, **kwargs):
     instance = self.db.instance_get(context, instance_id)
     host = instance['host']
     name = instance['name']
     pool = self.get_pool_for_instance_host(context, host)
     try:
         console = self.db.console_get_by_pool_instance(context,
                                                   pool['id'],
                                                   instance_id)
     except exception.NotFound:
         logging.debug(_('Adding console'))
         if not password:
             password = utils.generate_password(8)
         if not port:
             port = self.driver.get_port(context)
         console_data = {'instance_name': name,
                         'instance_id': instance_id,
                         'password': password,
                         'pool_id': pool['id']}
         if port:
             console_data['port'] = port
         console = self.db.console_create(context, console_data)
         self.driver.setup_console(context, console)
     return console['id']
Example #4
0
 def test_generate_password(self):
     password = utils.generate_password()
     self.assertTrue([c for c in password if c in '0123456789'])
     self.assertTrue(
         [c for c in password if c in 'abcdefghijklmnopqrstuvwxyz'])
     self.assertTrue(
         [c for c in password if c in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'])
Example #5
0
    def _get_server_admin_password(self, server):
        """ Determine the admin password for a server on creation """
        password = server.get('adminPass')

        if password is None:
            return utils.generate_password(FLAGS.password_length)
        if not isinstance(password, basestring) or password == '':
            msg = _("Invalid adminPass")
            raise exc.HTTPBadRequest(explanation=msg)
        return password
Example #6
0
    def _get_server_admin_password(self, server):
        """ Determine the admin password for a server on creation """
        password = server.get('adminPass')

        if password is None:
            return utils.generate_password(FLAGS.password_length)
        if not isinstance(password, basestring) or password == '':
            msg = _("Invalid adminPass")
            raise exc.HTTPBadRequest(explanation=msg)
        return password
Example #7
0
    def _rescue(self, input_dict, req, instance_id):
        """Rescue an instance."""
        context = req.environ["engine.context"]

        if input_dict['rescue'] and 'adminPass' in input_dict['rescue']:
            password = input_dict['rescue']['adminPass']
        else:
            password = utils.generate_password(FLAGS.password_length)

        instance = self._get_instance(context, instance_id)
        self.compute_api.rescue(context, instance, rescue_password=password)
        return {'adminPass': password}
Example #8
0
    def test_user_update(self):
        new_secret = utils.generate_password()
        body = dict(user=dict(name='guy2', access='acc2', secret=new_secret))

        req = fakes.HTTPRequest.blank('/v2/fake/users/id2')
        res_dict = self.controller.update(req, 'id2', body)

        self.assertEqual(res_dict['user']['id'], 'id2')
        self.assertEqual(res_dict['user']['name'], 'guy2')
        self.assertEqual(res_dict['user']['access'], 'acc2')
        self.assertEqual(res_dict['user']['secret'], new_secret)
        self.assertEqual(res_dict['user']['admin'], True)
Example #9
0
    def _rescue(self, input_dict, req, instance_id):
        """Rescue an instance."""
        context = req.environ["engine.context"]

        if input_dict['rescue'] and 'adminPass' in input_dict['rescue']:
            password = input_dict['rescue']['adminPass']
        else:
            password = utils.generate_password(FLAGS.password_length)

        instance = self._get_instance(context, instance_id)
        self.compute_api.rescue(context, instance, rescue_password=password)
        return {'adminPass': password}
Example #10
0
    def test_user_update(self):
        new_secret = utils.generate_password()
        body = dict(user=dict(name='guy2',
                              access='acc2',
                              secret=new_secret))

        req = fakes.HTTPRequest.blank('/v2/fake/users/id2')
        res_dict = self.controller.update(req, 'id2', body)

        self.assertEqual(res_dict['user']['id'], 'id2')
        self.assertEqual(res_dict['user']['name'], 'guy2')
        self.assertEqual(res_dict['user']['access'], 'acc2')
        self.assertEqual(res_dict['user']['secret'], new_secret)
        self.assertEqual(res_dict['user']['admin'], True)
Example #11
0
    def test_user_create(self):
        secret = utils.generate_password()
        body = dict(user=dict(
            name='test_guy', access='acc3', secret=secret, admin=True))
        req = fakes.HTTPRequest.blank('/v2/fake/users')
        res_dict = self.controller.create(req, body)

        # NOTE(justinsb): This is a questionable assertion in general
        # fake sets id=name, but others might not...
        self.assertEqual(res_dict['user']['id'], 'test_guy')

        self.assertEqual(res_dict['user']['name'], 'test_guy')
        self.assertEqual(res_dict['user']['access'], 'acc3')
        self.assertEqual(res_dict['user']['secret'], secret)
        self.assertEqual(res_dict['user']['admin'], True)
        self.assertTrue(
            'test_guy' in [u.id for u in fakes.FakeAuthManager.auth_data])
        self.assertEqual(len(fakes.FakeAuthManager.auth_data), 3)
Example #12
0
    def test_user_create(self):
        secret = utils.generate_password()
        body = dict(user=dict(name='test_guy',
                              access='acc3',
                              secret=secret,
                              admin=True))
        req = fakes.HTTPRequest.blank('/v2/fake/users')
        res_dict = self.controller.create(req, body)

        # NOTE(justinsb): This is a questionable assertion in general
        # fake sets id=name, but others might not...
        self.assertEqual(res_dict['user']['id'], 'test_guy')

        self.assertEqual(res_dict['user']['name'], 'test_guy')
        self.assertEqual(res_dict['user']['access'], 'acc3')
        self.assertEqual(res_dict['user']['secret'], secret)
        self.assertEqual(res_dict['user']['admin'], True)
        self.assertTrue('test_guy' in [u.id for u in
                        fakes.FakeAuthManager.auth_data])
        self.assertEqual(len(fakes.FakeAuthManager.auth_data), 3)
Example #13
0
    def _action_rebuild(self, info, request, instance_id):
        """Rebuild an instance with the given attributes"""
        try:
            body = info['rebuild']
        except (KeyError, TypeError):
            raise exc.HTTPBadRequest(_("Invalid request body"))

        try:
            image_href = body["imageRef"]
        except (KeyError, TypeError):
            msg = _("Could not parse imageRef from request.")
            raise exc.HTTPBadRequest(explanation=msg)

        try:
            password = body['adminPass']
        except (KeyError, TypeError):
            password = utils.generate_password(FLAGS.password_length)

        context = request.environ['engine.context']
        instance = self._get_server(context, instance_id)

        attr_map = {
            'personality': 'files_to_inject',
            'name': 'display_name',
            'accessIPv4': 'access_ip_v4',
            'accessIPv6': 'access_ip_v6',
            'metadata': 'metadata',
        }

        kwargs = {}

        for request_attribute, instance_attribute in attr_map.items():
            try:
                kwargs[instance_attribute] = body[request_attribute]
            except (KeyError, TypeError):
                pass

        self._validate_metadata(kwargs.get('metadata', {}))

        if 'files_to_inject' in kwargs:
            personality = kwargs['files_to_inject']
            kwargs['files_to_inject'] = self._get_injected_files(personality)

        try:
            self.compute_api.rebuild(context, instance, image_href, password,
                                     **kwargs)

        except exception.RebuildRequiresActiveInstance:
            msg = _("Instance must be active to rebuild.")
            raise exc.HTTPConflict(explanation=msg)
        except exception.InstanceNotFound:
            msg = _("Instance could not be found")
            raise exc.HTTPNotFound(explanation=msg)

        instance = self._get_server(context, instance_id)

        self._add_instance_faults(context, [instance])
        view = self._view_builder.show(request, instance)

        # Add on the adminPass attribute since the view doesn't do it
        view['server']['adminPass'] = password

        robj = wsgi.ResponseObject(view)
        return self._add_location(robj)
Example #14
0
    def _action_rebuild(self, info, request, instance_id):
        """Rebuild an instance with the given attributes"""
        try:
            body = info['rebuild']
        except (KeyError, TypeError):
            raise exc.HTTPBadRequest(_("Invalid request body"))

        try:
            image_href = body["imageRef"]
        except (KeyError, TypeError):
            msg = _("Could not parse imageRef from request.")
            raise exc.HTTPBadRequest(explanation=msg)

        try:
            password = body['adminPass']
        except (KeyError, TypeError):
            password = utils.generate_password(FLAGS.password_length)

        context = request.environ['engine.context']
        instance = self._get_server(context, instance_id)

        attr_map = {
            'personality': 'files_to_inject',
            'name': 'display_name',
            'accessIPv4': 'access_ip_v4',
            'accessIPv6': 'access_ip_v6',
            'metadata': 'metadata',
        }

        kwargs = {}

        for request_attribute, instance_attribute in attr_map.items():
            try:
                kwargs[instance_attribute] = body[request_attribute]
            except (KeyError, TypeError):
                pass

        self._validate_metadata(kwargs.get('metadata', {}))

        if 'files_to_inject' in kwargs:
            personality = kwargs['files_to_inject']
            kwargs['files_to_inject'] = self._get_injected_files(personality)

        try:
            self.compute_api.rebuild(context,
                                     instance,
                                     image_href,
                                     password,
                                     **kwargs)

        except exception.RebuildRequiresActiveInstance:
            msg = _("Instance must be active to rebuild.")
            raise exc.HTTPConflict(explanation=msg)
        except exception.InstanceNotFound:
            msg = _("Instance could not be found")
            raise exc.HTTPNotFound(explanation=msg)

        instance = self._get_server(context, instance_id)

        self._add_instance_faults(context, [instance])
        view = self._view_builder.show(request, instance)

        # Add on the adminPass attribute since the view doesn't do it
        view['server']['adminPass'] = password

        robj = wsgi.ResponseObject(view)
        return self._add_location(robj)