Example #1
0
    def invoke(self, context):
        try:
            # Raise NotImplemented exception for Admin specific request if
            # admin flag is set to false in nova.conf
            if (isinstance(self.controller, AdminController)
                    and (not FLAGS.allow_ec2_admin_api)):
                ## Raise InvalidRequest exception for EC2 Admin interface ##
                LOG.exception("Unsupported API request")
                raise exception.InvalidRequest()

            method = getattr(self.controller,
                             ec2utils.camelcase_to_underscore(self.action))
        except AttributeError:
            controller = self.controller
            action = self.action
            _error = _('Unsupported API request: controller = %(controller)s,'
                       ' action = %(action)s') % locals()
            LOG.exception(_error)
            # TODO: Raise custom exception, trap in apiserver,
            #       and reraise as 400 error.
            raise exception.InvalidRequest()

        args = ec2utils.dict_from_dotted_str(self.args.items())

        for key in args.keys():
            # NOTE(vish): Turn numeric dict keys into lists
            if isinstance(args[key], dict):
                if args[key] != {} and args[key].keys()[0].isdigit():
                    s = args[key].items()
                    s.sort()
                    args[key] = [v for k, v in s]

        result = method(context, **args)
        return self._render_response(result, context.request_id)
    def invoke(self, context):
        try:
            method = getattr(self.controller,
                             ec2utils.camelcase_to_underscore(self.action))
        except AttributeError:
            LOG.debug('Unsupported API request: controller = '
                        '%(controller)s, action = %(action)s',
                        {'controller': self.controller,
                         'action': self.action})
            # TODO(gundlach): Raise custom exception, trap in apiserver,
            #       and reraise as 400 error.
            raise exception.InvalidRequest()

        args = ec2utils.dict_from_dotted_str(self.args.items())

        for key in args.keys():
            # NOTE(vish): Turn numeric dict keys into lists
            if isinstance(args[key], dict):
                if args[key] != {} and args[key].keys()[0].isdigit():
                    s = args[key].items()
                    s.sort()
                    args[key] = [v for k, v in s]

        result = method(context, **args)
        return self._render_response(result, context.request_id)
Example #3
0
    def test_backup_volume_backed_instance(self, mock_backup,
                                           mock_check_image):
        body = {
            'createBackup': {
                'name': 'BackupMe',
                'backup_type': 'daily',
                'rotation': 3
            },
        }

        instance = fake_instance.fake_instance_obj(self.context)
        instance.image_ref = None
        self.mock_get.return_value = instance
        mock_backup.side_effect = exception.InvalidRequest()

        self.assertRaises(webob.exc.HTTPBadRequest,
                          self.controller._create_backup,
                          self.req,
                          instance['uuid'],
                          body=body)
        mock_check_image.assert_called_once_with(self.context, {})
        mock_backup.assert_called_once_with(self.context,
                                            instance,
                                            'BackupMe',
                                            'daily',
                                            3,
                                            extra_properties={})
Example #4
0
    def test_backup_volume_backed_instance(self):
        body = {
            'createBackup': {
                'name': 'BackupMe',
                'backup_type': 'daily',
                'rotation': 3
            },
        }

        common.check_img_metadata_properties_quota(self.context, {})
        instance = self._stub_instance_get()
        instance.image_ref = None

        self.compute_api.backup(self.context,
                                instance,
                                'BackupMe',
                                'daily',
                                3,
                                extra_properties={}).AndRaise(
                                    exception.InvalidRequest())

        self.mox.ReplayAll()

        res = self._make_request(self._make_url(instance['uuid']), body)
        self.assertEqual(400, res.status_int)
Example #5
0
def is_ec2_timestamp_expired(request, expires=None):
    """Checks the timestamp or expiry time included in an EC2 request
    and returns true if the request is expired
    """
    query_time = None
    timestamp = request.get('Timestamp')
    expiry_time = request.get('Expires')
    try:
        if timestamp and expiry_time:
            msg = _("Request must include either Timestamp or Expires,"
                    " but cannot contain both")
            LOG.error(msg)
            raise exception.InvalidRequest(msg)
        elif expiry_time:
            query_time = timeutils.parse_strtime(expiry_time,
                                                 "%Y-%m-%dT%H:%M:%SZ")
            return timeutils.is_older_than(query_time, -1)
        elif timestamp:
            query_time = timeutils.parse_strtime(timestamp,
                                                 "%Y-%m-%dT%H:%M:%SZ")

            # Check if the difference between the timestamp in the request
            # and the time on our servers is larger than 5 minutes, the
            # request is too old (or too new).
            if query_time and expires:
                return timeutils.is_older_than(query_time, expires) or \
                       timeutils.is_newer_than(query_time, expires)
        return False
    except ValueError:
        LOG.audit(_("Timestamp is invalid."))
        return True
Example #6
0
    def test_backup_volume_backed_instance(self):
        body = {
            'createBackup': {
                'name': 'BackupMe',
                'backup_type': 'daily',
                'rotation': 3
            },
        }

        common.check_img_metadata_properties_quota(self.context, {})
        instance = self._stub_instance_get()
        instance.image_ref = None

        self.compute_api.backup(self.context,
                                instance,
                                'BackupMe',
                                'daily',
                                3,
                                extra_properties={}).AndRaise(
                                    exception.InvalidRequest())

        self.mox.ReplayAll()

        self.assertRaises(webob.exc.HTTPBadRequest,
                          self.controller._create_backup,
                          self.req,
                          instance['uuid'],
                          body=body)
Example #7
0
def is_ec2_timestamp_expired(request, expires=None):
    """Checks the timestamp or expiry time included in an EC2 request
    and returns true if the request is expired
    """
    query_time = None
    timestamp = request.get('Timestamp')
    expiry_time = request.get('Expires')

    def parse_strtime(strtime):
        if _ms_time_regex.match(strtime):
            # NOTE(MotoKen): time format for aws-sdk-java contains millisecond
            time_format = "%Y-%m-%dT%H:%M:%S.%fZ"
        else:
            time_format = "%Y-%m-%dT%H:%M:%SZ"
        return timeutils.parse_strtime(strtime, time_format)

    try:
        if timestamp and expiry_time:
            msg = _("Request must include either Timestamp or Expires,"
                    " but cannot contain both")
            LOG.error(msg)
            raise exception.InvalidRequest(msg)
        elif expiry_time:
            query_time = parse_strtime(expiry_time)
            return timeutils.is_older_than(query_time, -1)
        elif timestamp:
            query_time = parse_strtime(timestamp)

            # Check if the difference between the timestamp in the request
            # and the time on our servers is larger than 5 minutes, the
            # request is too old (or too new).
            if query_time and expires:
                return timeutils.is_older_than(query_time, expires) or \
                       timeutils.is_newer_than(query_time, expires)
        return False
    except ValueError:
        LOG.info(_LI("Timestamp is invalid."))
        return True