Example #1
0
 def test_check_metadata_size(self):
     headers = {}
     size = 0
     chunk = constraints.MAX_META_NAME_LENGTH + \
         constraints.MAX_META_VALUE_LENGTH
     x = 0
     while size + chunk < constraints.MAX_META_OVERALL_SIZE:
         headers['X-Object-Meta-%04d%s' %
                 (x, 'a' * (constraints.MAX_META_NAME_LENGTH - 4))] = \
             'v' * constraints.MAX_META_VALUE_LENGTH
         size += chunk
         x += 1
     self.assertIsNone(constraints.check_metadata(Request.blank(
         '/', headers=headers), 'object'))
     # add two more headers in case adding just one falls exactly on the
     # limit (eg one header adds 1024 and the limit is 2048)
     headers['X-Object-Meta-%04d%s' %
             (x, 'a' * (constraints.MAX_META_NAME_LENGTH - 4))] = \
         'v' * constraints.MAX_META_VALUE_LENGTH
     headers['X-Object-Meta-%04d%s' %
             (x + 1, 'a' * (constraints.MAX_META_NAME_LENGTH - 4))] = \
         'v' * constraints.MAX_META_VALUE_LENGTH
     resp = constraints.check_metadata(Request.blank(
         '/', headers=headers), 'object')
     self.assertEqual(resp.status_int, HTTP_BAD_REQUEST)
     self.assertIn(b'Total metadata too large', resp.body)
Example #2
0
 def test_check_metadata_size(self):
     headers = {}
     size = 0
     chunk = constraints.MAX_META_NAME_LENGTH + \
         constraints.MAX_META_VALUE_LENGTH
     x = 0
     while size + chunk < constraints.MAX_META_OVERALL_SIZE:
         headers['X-Object-Meta-%04d%s' %
                 (x, 'a' * (constraints.MAX_META_NAME_LENGTH - 4))] = \
             'v' * constraints.MAX_META_VALUE_LENGTH
         size += chunk
         x += 1
     self.assertIsNone(
         constraints.check_metadata(Request.blank('/', headers=headers),
                                    'object'))
     # add two more headers in case adding just one falls exactly on the
     # limit (eg one header adds 1024 and the limit is 2048)
     headers['X-Object-Meta-%04d%s' %
             (x, 'a' * (constraints.MAX_META_NAME_LENGTH - 4))] = \
         'v' * constraints.MAX_META_VALUE_LENGTH
     headers['X-Object-Meta-%04d%s' %
             (x + 1, 'a' * (constraints.MAX_META_NAME_LENGTH - 4))] = \
         'v' * constraints.MAX_META_VALUE_LENGTH
     resp = constraints.check_metadata(Request.blank('/', headers=headers),
                                       'object')
     self.assertEqual(resp.status_int, HTTP_BAD_REQUEST)
     self.assertIn(b'Total metadata too large', resp.body)
 def test_check_metadata_value_length(self):
     value = 'a' * constraints.MAX_META_VALUE_LENGTH
     headers = {'X-Object-Meta-Name': value}
     self.assertEquals(constraints.check_metadata(Request.blank('/',
         headers=headers), 'object'), None)
     value = 'a' * (constraints.MAX_META_VALUE_LENGTH + 1)
     headers = {'X-Object-Meta-Name': value}
     self.assertEquals(constraints.check_metadata(Request.blank('/',
         headers=headers), 'object').status_int, HTTP_BAD_REQUEST)
Example #4
0
 def test_check_metadata_count(self):
     headers = {}
     for x in range(constraints.MAX_META_COUNT):
         headers['X-Object-Meta-%d' % x] = 'v'
     self.assertEqual(constraints.check_metadata(Request.blank(
         '/', headers=headers), 'object'), None)
     headers['X-Object-Meta-Too-Many'] = 'v'
     self.assertEqual(constraints.check_metadata(Request.blank(
         '/', headers=headers), 'object').status_int, HTTP_BAD_REQUEST)
Example #5
0
 def test_validate_bad_meta(self):
     req = Request.blank(
         '/v/a/c/o',
         headers={'x-object-meta-hello':
                  'ab' * constraints.MAX_HEADER_SIZE})
     self.assertEquals(constraints.check_metadata(req, 'object').status_int,
                       HTTP_BAD_REQUEST)
     self.assertIn('x-object-meta-hello', constraints.check_metadata(req,
                   'object').body.lower())
Example #6
0
 def test_check_metadata_value_length(self):
     value = 'a' * constraints.MAX_META_VALUE_LENGTH
     headers = {'X-Object-Meta-Name': value}
     self.assertEquals(constraints.check_metadata(Request.blank('/',
         headers=headers), 'object'), None)
     value = 'a' * (constraints.MAX_META_VALUE_LENGTH + 1)
     headers = {'X-Object-Meta-Name': value}
     self.assertEquals(constraints.check_metadata(Request.blank('/',
         headers=headers), 'object').status_int, HTTP_BAD_REQUEST)
 def test_check_metadata_count(self):
     headers = {}
     for x in xrange(constraints.MAX_META_COUNT):
         headers['X-Object-Meta-%d' % x] = 'v'
     self.assertEquals(constraints.check_metadata(Request.blank('/',
         headers=headers), 'object'), None)
     headers['X-Object-Meta-Too-Many'] = 'v'
     self.assert_(isinstance(constraints.check_metadata(Request.blank('/',
         headers=headers), 'object'), HTTPBadRequest))
Example #8
0
 def test_validate_bad_meta(self):
     req = Request.blank(
         '/v/a/c/o',
         headers={'x-object-meta-hello':
                  'ab' * constraints.MAX_HEADER_SIZE})
     self.assertEqual(constraints.check_metadata(req, 'object').status_int,
                      HTTP_BAD_REQUEST)
     self.assertIn('x-object-meta-hello', constraints.check_metadata(req,
                   'object').body.lower())
Example #9
0
 def test_check_metadata_count(self):
     headers = {}
     for x in xrange(constraints.MAX_META_COUNT):
         headers['X-Object-Meta-%d' % x] = 'v'
     self.assertEquals(constraints.check_metadata(Request.blank(
         '/', headers=headers), 'object'), None)
     headers['X-Object-Meta-Too-Many'] = 'v'
     self.assertEquals(constraints.check_metadata(Request.blank(
         '/', headers=headers), 'object').status_int, HTTP_BAD_REQUEST)
 def test_check_metadata_name_length(self):
     name = 'a' * constraints.MAX_META_NAME_LENGTH
     headers = {'X-Object-Meta-%s' % name: 'v'}
     self.assertEquals(constraints.check_metadata(Request.blank('/',
         headers=headers), 'object'), None)
     name = 'a' * (constraints.MAX_META_NAME_LENGTH + 1)
     headers = {'X-Object-Meta-%s' % name: 'v'}
     self.assertEquals(constraints.check_metadata(Request.blank('/',
         headers=headers), 'object').status_int, HTTP_BAD_REQUEST)
 def test_check_metadata_count(self):
     headers = {}
     for x in range(constraints.MAX_META_COUNT):
         headers["X-Object-Meta-%d" % x] = "v"
     self.assertEquals(constraints.check_metadata(Request.blank("/", headers=headers), "object"), None)
     headers["X-Object-Meta-Too-Many"] = "v"
     self.assertEquals(
         constraints.check_metadata(Request.blank("/", headers=headers), "object").status_int, HTTP_BAD_REQUEST
     )
Example #12
0
 def test_check_metadata_name_length(self):
     name = 'a' * constraints.MAX_META_NAME_LENGTH
     headers = {'X-Object-Meta-%s' % name: 'v'}
     self.assertEquals(constraints.check_metadata(Request.blank('/',
         headers=headers), 'object'), None)
     name = 'a' * (constraints.MAX_META_NAME_LENGTH + 1)
     headers = {'X-Object-Meta-%s' % name: 'v'}
     self.assertEquals(constraints.check_metadata(Request.blank('/',
         headers=headers), 'object').status_int, HTTP_BAD_REQUEST)
Example #13
0
 def test_check_metadata_non_utf8(self):
     headers = {'X-Account-Meta-Foo': b'\xff'}
     self.assertEqual(constraints.check_metadata(Request.blank(
         '/', headers=headers), 'account').status_int, HTTP_BAD_REQUEST)
     headers = {b'X-Container-Meta-\xff': 'foo'}
     self.assertEqual(constraints.check_metadata(Request.blank(
         '/', headers=headers), 'container').status_int, HTTP_BAD_REQUEST)
     # Object's OK; its metadata isn't serialized as JSON
     headers = {'X-Object-Meta-Foo': b'\xff'}
     self.assertIsNone(constraints.check_metadata(Request.blank(
         '/', headers=headers), 'object'))
Example #14
0
    def test_check_metadata_count(self):
        headers = {}
        for x in range(constraints.MAX_META_COUNT):
            headers['X-Object-Meta-%d' % x] = 'v'
        self.assertIsNone(constraints.check_metadata(Request.blank(
            '/', headers=headers), 'object'))

        headers['X-Object-Meta-Too-Many'] = 'v'
        resp = constraints.check_metadata(Request.blank(
            '/', headers=headers), 'object')
        self.assertEqual(resp.status_int, HTTP_BAD_REQUEST)
        self.assertIn(b'Too many metadata items', resp.body)
Example #15
0
    def test_check_metadata_count(self):
        headers = {}
        for x in range(constraints.MAX_META_COUNT):
            headers['X-Object-Meta-%d' % x] = 'v'
        self.assertIsNone(constraints.check_metadata(Request.blank(
            '/', headers=headers), 'object'))

        headers['X-Object-Meta-Too-Many'] = 'v'
        resp = constraints.check_metadata(Request.blank(
            '/', headers=headers), 'object')
        self.assertEqual(resp.status_int, HTTP_BAD_REQUEST)
        self.assertIn(b'Too many metadata items', resp.body)
 def test_check_metadata_name_length(self):
     name = "a" * constraints.MAX_META_NAME_LENGTH
     headers = {"X-Object-Meta-%s" % name: "v"}
     self.assertEquals(constraints.check_metadata(Request.blank("/", headers=headers), "object"), None)
     name = "a" * (constraints.MAX_META_NAME_LENGTH + 1)
     headers = {"X-Object-Meta-%s" % name: "v"}
     self.assertEquals(
         constraints.check_metadata(Request.blank("/", headers=headers), "object").status_int, HTTP_BAD_REQUEST
     )
     self.assertIn(
         ("X-Object-Meta-%s" % name).lower(),
         constraints.check_metadata(Request.blank("/", headers=headers), "object").body.lower(),
     )
Example #17
0
    def test_check_metadata_name_length(self):
        name = 'a' * constraints.MAX_META_NAME_LENGTH
        headers = {'X-Object-Meta-%s' % name: 'v'}
        self.assertIsNone(constraints.check_metadata(Request.blank(
            '/', headers=headers), 'object'))

        name = 'a' * (constraints.MAX_META_NAME_LENGTH + 1)
        headers = {'X-Object-Meta-%s' % name: 'v'}
        resp = constraints.check_metadata(Request.blank(
            '/', headers=headers), 'object')
        self.assertEqual(resp.status_int, HTTP_BAD_REQUEST)
        self.assertIn(
            b'x-object-meta-%s' % name.encode('ascii'), resp.body.lower())
        self.assertIn(b'Metadata name too long', resp.body)
Example #18
0
    def test_check_metadata_name_length(self):
        name = 'a' * constraints.MAX_META_NAME_LENGTH
        headers = {'X-Object-Meta-%s' % name: 'v'}
        self.assertIsNone(constraints.check_metadata(Request.blank(
            '/', headers=headers), 'object'))

        name = 'a' * (constraints.MAX_META_NAME_LENGTH + 1)
        headers = {'X-Object-Meta-%s' % name: 'v'}
        resp = constraints.check_metadata(Request.blank(
            '/', headers=headers), 'object')
        self.assertEqual(resp.status_int, HTTP_BAD_REQUEST)
        self.assertIn(
            b'x-object-meta-%s' % name.encode('ascii'), resp.body.lower())
        self.assertIn(b'Metadata name too long', resp.body)
 def test_check_metadata_non_utf8(self):
     headers = {'X-Account-Meta-Foo': b'\xff'}
     self.assertEqual(
         constraints.check_metadata(Request.blank('/', headers=headers),
                                    'account').status_int, HTTP_BAD_REQUEST)
     headers = {b'X-Container-Meta-\xff': 'foo'}
     self.assertEqual(
         constraints.check_metadata(Request.blank('/', headers=headers),
                                    'container').status_int,
         HTTP_BAD_REQUEST)
     # Object's OK; its metadata isn't serialized as JSON
     headers = {'X-Object-Meta-Foo': b'\xff'}
     self.assertIsNone(
         constraints.check_metadata(Request.blank('/', headers=headers),
                                    'object'))
Example #20
0
    def test_check_metadata_value_length(self):
        value = 'a' * constraints.MAX_META_VALUE_LENGTH
        headers = {'X-Object-Meta-Name': value}
        self.assertIsNone(constraints.check_metadata(Request.blank(
            '/', headers=headers), 'object'))

        value = 'a' * (constraints.MAX_META_VALUE_LENGTH + 1)
        headers = {'X-Object-Meta-Name': value}
        resp = constraints.check_metadata(Request.blank(
            '/', headers=headers), 'object')
        self.assertEqual(resp.status_int, HTTP_BAD_REQUEST)
        self.assertIn(b'x-object-meta-name', resp.body.lower())
        self.assertIn(
            str(constraints.MAX_META_VALUE_LENGTH).encode('ascii'), resp.body)
        self.assertIn(b'Metadata value longer than 256', resp.body)
Example #21
0
    def test_check_metadata_value_length(self):
        value = 'a' * constraints.MAX_META_VALUE_LENGTH
        headers = {'X-Object-Meta-Name': value}
        self.assertIsNone(constraints.check_metadata(Request.blank(
            '/', headers=headers), 'object'))

        value = 'a' * (constraints.MAX_META_VALUE_LENGTH + 1)
        headers = {'X-Object-Meta-Name': value}
        resp = constraints.check_metadata(Request.blank(
            '/', headers=headers), 'object')
        self.assertEqual(resp.status_int, HTTP_BAD_REQUEST)
        self.assertIn(b'x-object-meta-name', resp.body.lower())
        self.assertIn(
            str(constraints.MAX_META_VALUE_LENGTH).encode('ascii'), resp.body)
        self.assertIn(b'Metadata value longer than 256', resp.body)
 def POST(self, req):
     """HTTP POST request handler."""
     error_response = check_metadata(req, "account")
     if error_response:
         return error_response
     account_partition, accounts = self.app.account_ring.get_nodes(self.account_name)
     headers = {"X-Timestamp": normalize_timestamp(time.time()), "X-Trans-Id": self.trans_id, "Connection": "close"}
     self.transfer_headers(req.headers, headers)
     if self.app.memcache:
         self.app.memcache.delete("account%s" % req.path_info.rstrip("/"))
     resp = self.make_requests(
         req, self.app.account_ring, account_partition, "POST", req.path_info, [headers] * len(accounts)
     )
     if resp.status_int == HTTP_NOT_FOUND and self.app.account_autocreate:
         if len(self.account_name) > MAX_ACCOUNT_NAME_LENGTH:
             resp = HTTPBadRequest(request=req)
             resp.body = "Account name length of %d longer than %d" % (
                 len(self.account_name),
                 MAX_ACCOUNT_NAME_LENGTH,
             )
             return resp
         resp = self.make_requests(
             Request.blank("/v1/" + self.account_name),
             self.app.account_ring,
             account_partition,
             "PUT",
             "/" + self.account_name,
             [headers] * len(accounts),
         )
         if not is_success(resp.status_int):
             self.app.logger.warning("Could not autocreate account %r" % self.account_name)
             return resp
     return resp
Example #23
0
    def POST(self, req):
        """HTTP POST request handler."""
        if self.app.object_post_as_copy:
            req.method = 'PUT'
            req.path_info = '/v1/%s/%s/%s' % (
                self.account_name, self.container_name, self.object_name)
            req.headers['Content-Length'] = 0
            req.headers['X-Copy-From'] = quote(
                '/%s/%s' % (self.container_name, self.object_name))
            req.headers['X-Fresh-Metadata'] = 'true'
            req.environ['swift_versioned_copy'] = True
            if req.environ.get('QUERY_STRING'):
                req.environ['QUERY_STRING'] += '&multipart-manifest=get'
            else:
                req.environ['QUERY_STRING'] = 'multipart-manifest=get'
            resp = self.PUT(req)
            # Older editions returned 202 Accepted on object POSTs, so we'll
            # convert any 201 Created responses to that for compatibility with
            # picky clients.
            if resp.status_int != HTTP_CREATED:
                return resp
            return HTTPAccepted(request=req)
        else:
            error_response = check_metadata(req, 'object')
            if error_response:
                return error_response
            container_info = self.container_info(self.account_name,
                                                 self.container_name, req)
            container_partition = container_info['partition']
            containers = container_info['nodes']
            req.acl = container_info['write_acl']
            if 'swift.authorize' in req.environ:
                aresp = req.environ['swift.authorize'](req)
                if aresp:
                    return aresp
            if not containers:
                return HTTPNotFound(request=req)

            req, delete_at_container, delete_at_part, \
                delete_at_nodes = self._config_obj_expiration(req)

            # pass the policy index to storage nodes via req header
            policy_index = req.headers.get('X-Backend-Storage-Policy-Index',
                                           container_info['storage_policy'])
            obj_ring = self.app.get_object_ring(policy_index)
            req.headers['X-Backend-Storage-Policy-Index'] = policy_index
            partition, nodes = obj_ring.get_nodes(self.account_name,
                                                  self.container_name,
                                                  self.object_name)

            req.headers['X-Timestamp'] = Timestamp(time.time()).internal

            headers = self._backend_requests(req, len(nodes),
                                             container_partition, containers,
                                             delete_at_container,
                                             delete_at_part, delete_at_nodes)

            resp = self.make_requests(req, obj_ring, partition, 'POST',
                                      req.swift_entity_path, headers)
            return resp
Example #24
0
 def POST(self, req):
     """HTTP POST request handler."""
     error_response = \
         self.clean_acls(req) or check_metadata(req, 'container')
     if error_response:
         return error_response
     if not req.environ.get('swift_owner'):
         for key in self.app.swift_owner_headers:
             req.headers.pop(key, None)
     if req.environ.get('reseller_request', False) and \
             'X-Container-Sharding' in req.headers:
         req.headers[get_sys_meta_prefix('container') + 'Sharding'] = \
             str(config_true_value(req.headers['X-Container-Sharding']))
     account_partition, accounts, container_count = \
         self.account_info(self.account_name, req)
     if not accounts:
         return HTTPNotFound(request=req)
     container_partition, containers = self.app.container_ring.get_nodes(
         self.account_name, self.container_name)
     headers = self.generate_request_headers(req, transfer=True)
     clear_info_cache(self.app, req.environ,
                      self.account_name, self.container_name)
     resp = self.make_requests(
         req, self.app.container_ring, container_partition, 'POST',
         req.swift_entity_path, [headers] * len(containers))
     return resp
 def POST(self, req):
     """HTTP POST request handler."""
     error_response = \
         self.clean_acls(req) or check_metadata(req, 'container')
     if error_response:
         return error_response
     account_partition, accounts, container_count = \
         self.account_info(self.account_name,
                           autocreate=self.app.account_autocreate)
     if not accounts:
         return HTTPNotFound(request=req)
     container_partition, containers = self.app.container_ring.get_nodes(
         self.account_name, self.container_name)
     headers = {'X-Timestamp': normalize_timestamp(time.time()),
                'x-trans-id': self.trans_id,
                'Connection': 'close'}
     self.transfer_headers(req.headers, headers)
     if self.app.memcache:
         cache_key = get_container_memcache_key(self.account_name,
                                                self.container_name)
         self.app.memcache.delete(cache_key)
     resp = self.make_requests(req, self.app.container_ring,
             container_partition, 'POST', req.path_info,
             [headers] * len(containers))
     return resp
Example #26
0
    def POST(self, req):
        """HTTP POST request handler."""
        error_response = \
            self.clean_acls(req) or check_metadata(req, 'container')
        if error_response:
            return error_response
        if not req.environ.get('swift_owner'):
            for key in self.app.swift_owner_headers:
                req.headers.pop(key, None)

        headers = self.generate_request_headers(req, transfer=True)
        clear_info_cache(self.app, req.environ,
                         self.account_name, self.container_name)

        storage = self.app.storage
        metadata = {}
        metadata.update(("user.%s" % k, v) for k, v in req.headers.iteritems()
                        if k.lower() in self.pass_through_headers or
                        is_sys_or_user_meta('container', k))

        try:
            storage.container_update(self.account_name, self.container_name,
                                     metadata, headers=headers)
            resp = HTTPNoContent(request=req)
        except exceptions.NoSuchContainer:
            resp = self.PUT(req)
        return resp
Example #27
0
 def POST(self, req):
     """HTTP POST request handler."""
     if len(self.account_name) > MAX_ACCOUNT_NAME_LENGTH:
         resp = HTTPBadRequest(request=req)
         resp.body = 'Account name length of %d longer than %d' % \
                     (len(self.account_name), MAX_ACCOUNT_NAME_LENGTH)
         return resp
     error_response = check_metadata(req, 'account')
     if error_response:
         return error_response
     account_partition, accounts = \
         self.app.account_ring.get_nodes(self.account_name)
     headers = self.generate_request_headers(req, transfer=True)
     if self.app.memcache:
         self.app.memcache.delete(
             get_account_memcache_key(self.account_name))
     resp = self.make_requests(req, self.app.account_ring,
                               account_partition, 'POST', req.path_info,
                               [headers] * len(accounts))
     if resp.status_int == HTTP_NOT_FOUND and self.app.account_autocreate:
         self.autocreate_account(self.account_name)
         resp = self.make_requests(req, self.app.account_ring,
                                   account_partition, 'POST', req.path_info,
                                   [headers] * len(accounts))
     return resp
Example #28
0
    def POST(self, req):
        """HTTP POST request handler."""
        error_response = \
            self.clean_acls(req) or check_metadata(req, 'container')
        if error_response:
            return error_response
        if not req.environ.get('swift_owner'):
            for key in self.app.swift_owner_headers:
                req.headers.pop(key, None)
        account_partition, accounts, container_count = \
            self.account_info(self.account_name, req)
        if not accounts:
            return HTTPNotFound(request=req)

        headers = self.generate_request_headers(req, transfer=True)
        clear_info_cache(self.app, req.environ, self.account_name,
                         self.container_name)

        memcache = getattr(self.app, 'memcache', None) or \
            req.environ.get('swift.cache')
        if memcache is not None:
            key = "/".join(
                ("versioning", self.account_name, self.container_name))
            memcache.delete(key)

        resp = self.get_container_post_resp(req, headers)
        return resp
Example #29
0
 def PUT(self, req):
     """HTTP PUT request handler."""
     if not self.app.allow_account_management:
         return HTTPMethodNotAllowed(
             request=req,
             headers={'Allow': ', '.join(self.allowed_methods)})
     error_response = check_metadata(req, 'account')
     if error_response:
         return error_response
     if len(self.account_name) > MAX_ACCOUNT_NAME_LENGTH:
         resp = HTTPBadRequest(request=req)
         resp.body = 'Account name length of %d longer than %d' % \
                     (len(self.account_name), MAX_ACCOUNT_NAME_LENGTH)
         return resp
     account_partition, accounts = \
         self.app.account_ring.get_nodes(self.account_name)
     headers = {
         'X-Timestamp': normalize_timestamp(time.time()),
         'x-trans-id': self.trans_id,
         'Connection': 'close'
     }
     self.transfer_headers(req.headers, headers)
     if self.app.memcache:
         self.app.memcache.delete(
             get_account_memcache_key(self.account_name))
     resp = self.make_requests(req, self.app.account_ring,
                               account_partition, 'PUT', req.path_info,
                               [headers] * len(accounts))
     return resp
Example #30
0
    def PUT(self, req):
        """HTTP PUT request handler."""
	print 'in PUT function of accountcontroller class'
        if not self.app.allow_account_management:
            return HTTPMethodNotAllowed(
                request=req,
                headers={'Allow': ', '.join(self.allowed_methods)})
        error_response = check_metadata(req, 'account')
	print 'error_response'
        if error_response:
            return error_response
        if len(self.account_name) > constraints.MAX_ACCOUNT_NAME_LENGTH:
            resp = HTTPBadRequest(request=req)
            resp.body = 'Account name length of %d longer than %d' % \
                        (len(self.account_name),
                         constraints.MAX_ACCOUNT_NAME_LENGTH)
            return resp
        account_partition, accounts = \
            self.app.account_ring.get_nodes(self.account_name)
	print ' account_partition, accounts',account_partion,accounts
        headers = self.generate_request_headers(req, transfer=True)
	print 'headers',headers
        clear_info_cache(self.app, req.environ, self.account_name)
        resp = self.make_requests(
            req, self.app.account_ring, account_partition, 'PUT',
            req.swift_entity_path, [headers] * len(accounts))
	print 'resp',resp
        self.add_acls_from_sys_metadata(resp)
	print 'in PUT function of accountcontroller class END'
        return resp
Example #31
0
 def POST(self, req):
     """HTTP POST request handler."""
     error_response = \
         self.clean_acls(req) or check_metadata(req, 'container')
     if error_response:
         return error_response
     account_partition, accounts, container_count = \
         self.account_info(self.account_name,
                           autocreate=self.app.account_autocreate)
     if not accounts:
         return HTTPNotFound(request=req)
     container_partition, containers = self.app.container_ring.get_nodes(
         self.account_name, self.container_name)
     headers = {
         'X-Timestamp': normalize_timestamp(time.time()),
         'x-trans-id': self.trans_id,
         'Connection': 'close'
     }
     self.transfer_headers(req.headers, headers)
     if self.app.memcache:
         self.app.memcache.delete(
             get_container_memcache_key(self.account_name,
                                        self.container_name))
     resp = self.make_requests(req, self.app.container_ring,
                               container_partition, 'POST', req.path_info,
                               [headers] * len(containers))
     return resp
 def test_check_metadata_value_length(self):
     value = "a" * constraints.MAX_META_VALUE_LENGTH
     headers = {"X-Object-Meta-Name": value}
     self.assertEquals(constraints.check_metadata(Request.blank("/", headers=headers), "object"), None)
     value = "a" * (constraints.MAX_META_VALUE_LENGTH + 1)
     headers = {"X-Object-Meta-Name": value}
     self.assertEquals(
         constraints.check_metadata(Request.blank("/", headers=headers), "object").status_int, HTTP_BAD_REQUEST
     )
     self.assertIn(
         "x-object-meta-name", constraints.check_metadata(Request.blank("/", headers=headers), "object").body.lower()
     )
     self.assertIn(
         str(constraints.MAX_META_VALUE_LENGTH),
         constraints.check_metadata(Request.blank("/", headers=headers), "object").body,
     )
Example #33
0
 def PUT(self, req):
     """HTTP PUT request handler."""
     error_response = \
         self.clean_acls(req) or check_metadata(req, 'container')
     if error_response:
         return error_response
     if len(self.container_name) > MAX_CONTAINER_NAME_LENGTH:
         resp = HTTPBadRequest(request=req)
         resp.body = 'Container name length of %d longer than %d' % \
                     (len(self.container_name), MAX_CONTAINER_NAME_LENGTH)
         return resp
     account_partition, accounts, container_count = \
         self.account_info(self.account_name,
                           autocreate=self.app.account_autocreate)
     if self.app.max_containers_per_account > 0 and \
             container_count >= self.app.max_containers_per_account and \
             self.account_name not in self.app.max_containers_whitelist:
         resp = HTTPForbidden(request=req)
         resp.body = 'Reached container limit of %s' % \
                     self.app.max_containers_per_account
         return resp
     if not accounts:
         return HTTPNotFound(request=req)
     container_partition, containers = self.app.container_ring.get_nodes(
         self.account_name, self.container_name)
     headers = self._backend_requests(req, len(containers),
                                      account_partition, accounts)
     if self.app.memcache:
         cache_key = get_container_memcache_key(self.account_name,
                                                self.container_name)
         self.app.memcache.delete(cache_key)
     resp = self.make_requests(req, self.app.container_ring,
                               container_partition, 'PUT', req.path_info,
                               headers)
     return resp
Example #34
0
    def PUT(self, req):
        """HTTP PUT request handler."""
        error_response = \
            self.clean_acls(req) or check_metadata(req, 'container')
        if error_response:
            return error_response
        if not req.environ.get('swift_owner'):
            for key in self.app.swift_owner_headers:
                req.headers.pop(key, None)
        if len(self.container_name) > constraints.MAX_CONTAINER_NAME_LENGTH:
            resp = HTTPBadRequest(request=req)
            resp.body = 'Container name length of %d longer than %d' % \
                        (len(self.container_name),
                         constraints.MAX_CONTAINER_NAME_LENGTH)
            return resp
        container_count = self.account_info(self.account_name, req)

        clear_info_cache(self.app, req.environ,
                         self.account_name, self.container_name)

        storage = self.app.storage
        try:
            storage.container_create(self.account_name, self.container_name)
        except exceptions.OioException:
            return HTTPServerError(request=req)
        resp = HTTPCreated(request=req)
        return resp
Example #35
0
 def POST(self, req):
     """HTTP POST request handler."""
     if len(self.account_name) > constraints.MAX_ACCOUNT_NAME_LENGTH:
         resp = HTTPBadRequest(request=req)
         resp.body = 'Account name length of %d longer than %d' % \
                     (len(self.account_name),
                      constraints.MAX_ACCOUNT_NAME_LENGTH)
         return resp
     error_response = check_metadata(req, 'account')
     if error_response:
         return error_response
     account_partition, accounts = \
         self.app.account_ring.get_nodes(self.account_name)
     headers = self.generate_request_headers(req, transfer=True)
     clear_info_cache(self.app, req.environ, self.account_name)
     resp = self.make_requests(req, self.app.account_ring,
                               account_partition, 'POST',
                               req.swift_entity_path,
                               [headers] * len(accounts))
     if resp.status_int == HTTP_NOT_FOUND and self.app.account_autocreate:
         self.autocreate_account(req, self.account_name)
         resp = self.make_requests(req, self.app.account_ring,
                                   account_partition, 'POST',
                                   req.swift_entity_path,
                                   [headers] * len(accounts))
     self.add_acls_from_sys_metadata(resp)
     return resp
Example #36
0
 def POST(self, req):
     """HTTP POST request handler."""
     error_response = check_metadata(req, 'account')
     if error_response:
         return error_response
     account_partition, accounts = \
         self.app.account_ring.get_nodes(self.account_name)
     headers = self.generate_request_headers(req, transfer=True)
     if self.app.memcache:
         self.app.memcache.delete(
             get_account_memcache_key(self.account_name))
     resp = self.make_requests(
         req, self.app.account_ring, account_partition, 'POST',
         req.path_info, [headers] * len(accounts))
     if resp.status_int == HTTP_NOT_FOUND and self.app.account_autocreate:
         if len(self.account_name) > MAX_ACCOUNT_NAME_LENGTH:
             resp = HTTPBadRequest(request=req)
             resp.body = 'Account name length of %d longer than %d' % \
                         (len(self.account_name), MAX_ACCOUNT_NAME_LENGTH)
             return resp
         resp = self.make_requests(
             Request.blank('/v1/' + self.account_name),
             self.app.account_ring, account_partition, 'PUT',
             '/' + self.account_name, [headers] * len(accounts))
         if not is_success(resp.status_int):
             self.app.logger.warning('Could not autocreate account %r' %
                                     self.account_name)
             return resp
     return resp
Example #37
0
 def POST(self, req):
     """HTTP POST request handler."""
     error_response = check_metadata(req, 'account')
     if error_response:
         return error_response
     account_partition, accounts = \
         self.app.account_ring.get_nodes(self.account_name)
     headers = {
         'X-Timestamp': normalize_timestamp(time.time()),
         'X-Trans-Id': self.trans_id,
         'Connection': 'close'
     }
     self.transfer_headers(req.headers, headers)
     if self.app.memcache:
         self.app.memcache.delete(
             get_account_memcache_key(self.account_name))
     resp = self.make_requests(req, self.app.account_ring,
                               account_partition, 'POST', req.path_info,
                               [headers] * len(accounts))
     if resp.status_int == HTTP_NOT_FOUND and self.app.account_autocreate:
         if len(self.account_name) > MAX_ACCOUNT_NAME_LENGTH:
             resp = HTTPBadRequest(request=req)
             resp.body = 'Account name length of %d longer than %d' % \
                         (len(self.account_name), MAX_ACCOUNT_NAME_LENGTH)
             return resp
         resp = self.make_requests(
             Request.blank('/v1/' + self.account_name),
             self.app.account_ring, account_partition, 'PUT',
             '/' + self.account_name, [headers] * len(accounts))
         if not is_success(resp.status_int):
             self.app.logger.warning('Could not autocreate account %r' %
                                     self.account_name)
             return resp
     return resp
Example #38
0
 def PUT(self, req):
     """HTTP PUT request handler."""
     if not self.app.allow_account_management:
         return HTTPMethodNotAllowed(
             request=req,
             headers={'Allow': ', '.join(self.allowed_methods)})
     error_response = check_metadata(req, 'account')
     if error_response:
         return error_response
     if len(self.account_name) > constraints.MAX_ACCOUNT_NAME_LENGTH:
         resp = HTTPBadRequest(request=req)
         resp.body = 'Account name length of %d longer than %d' % \
                     (len(self.account_name),
                      constraints.MAX_ACCOUNT_NAME_LENGTH)
         return resp
     account_partition, accounts = \
         self.app.account_ring.get_nodes(self.account_name)
     headers = self.generate_request_headers(req, transfer=True)
     clear_info_cache(self.app, req.environ, self.account_name)
     resp = self.make_requests(req, self.app.account_ring,
                               account_partition, 'PUT',
                               req.swift_entity_path,
                               [headers] * len(accounts))
     self.add_acls_from_sys_metadata(resp)
     return resp
Example #39
0
 def POST(self, req):
     
     error_response = check_metadata(req, 'object')
     if error_response:
         return error_response
     
     container_partition, containers,_ = self.container_info(self.account_name, self.container_name,
             account_autocreate=self.app.account_autocreate)
         
     if not containers:
         return jresponse('-1', 'not found', req,404)
     
     partition, nodes = self.app.object_ring.get_nodes(self.account_name, self.container_name, self.object_name)
     
     req.headers['X-Timestamp'] = normalize_timestamp(time.time())
     
     headers = []
     for container in containers:
         nheaders = dict(req.headers.iteritems())
         nheaders['Connection'] = 'close'
         nheaders['X-Container-Host'] = '%(ip)s:%(port)s' % container
         nheaders['X-Container-Partition'] = container_partition
         nheaders['X-Container-Device'] = container['device']
         
         headers.append(nheaders)
     resp = self.make_requests(self.account_name,req, self.app.object_ring, partition,
                               'POST', req.path_info, headers)
     return resp
Example #40
0
 def PUT(self, req):
     """HTTP PUT request handler."""
     error_response = self.clean_acls(req) or check_metadata(req, "container")
     if error_response:
         return error_response
     if len(self.container_name) > MAX_CONTAINER_NAME_LENGTH:
         resp = HTTPBadRequest(request=req)
         resp.body = "Container name length of %d longer than %d" % (
             len(self.container_name),
             MAX_CONTAINER_NAME_LENGTH,
         )
         return resp
     account_partition, accounts, container_count = self.account_info(self.account_name, req)
     if not accounts and self.app.account_autocreate:
         self.autocreate_account(req.environ, self.account_name)
         account_partition, accounts, container_count = self.account_info(self.account_name, req)
     if not accounts:
         return HTTPNotFound(request=req)
     if (
         self.app.max_containers_per_account > 0
         and container_count >= self.app.max_containers_per_account
         and self.account_name not in self.app.max_containers_whitelist
     ):
         resp = HTTPForbidden(request=req)
         resp.body = "Reached container limit of %s" % self.app.max_containers_per_account
         return resp
     container_partition, containers = self.app.container_ring.get_nodes(self.account_name, self.container_name)
     headers = self._backend_requests(req, len(containers), account_partition, accounts)
     clear_info_cache(self.app, req.environ, self.account_name, self.container_name)
     resp = self.make_requests(req, self.app.container_ring, container_partition, "PUT", req.path_info, headers)
     return resp
Example #41
0
 def POST(self, req):
     """HTTP POST request handler."""
     if len(self.account_name) > constraints.MAX_ACCOUNT_NAME_LENGTH:
         resp = HTTPBadRequest(request=req)
         resp.body = 'Account name length of %d longer than %d' % \
                     (len(self.account_name),
                      constraints.MAX_ACCOUNT_NAME_LENGTH)
         return resp
     error_response = check_metadata(req, 'account')
     if error_response:
         return error_response
     account_partition, accounts = \
         self.app.account_ring.get_nodes(self.account_name)
     headers = self.generate_request_headers(req, transfer=True)
     clear_info_cache(self.app, req.environ, self.account_name)
     resp = self.make_requests(
         req, self.app.account_ring, account_partition, 'POST',
         req.swift_entity_path, [headers] * len(accounts))
     if resp.status_int == HTTP_NOT_FOUND and self.app.account_autocreate:
         self.autocreate_account(req, self.account_name)
         resp = self.make_requests(
             req, self.app.account_ring, account_partition, 'POST',
             req.swift_entity_path, [headers] * len(accounts))
     self.add_acls_from_sys_metadata(resp)
     return resp
    def POST(self, req):
        """HTTP POST request handler."""
        # containerå…ƒę•°ę®å‚ę•°ę£€ęŸ„
        error_response = \
            self.clean_acls(req) or check_metadata(req, 'container')
        if error_response:
            return error_response
        if not req.environ.get('swift_owner'):
            for key in self.app.swift_owner_headers:
                req.headers.pop(key, None)

        # čŽ·å–accountēš„å…ƒę•°ę®äæ”ęÆļ¼Œåˆ†åŒŗ态节ē‚¹ć€ä»„及containerꕰ量
        account_partition, accounts, container_count = \
            self.account_info(self.account_name, req)
        # å¦‚ęžœaccountäøå­˜åœØļ¼Œåˆ™ęŠ„错
        if not accounts:
            return HTTPNotFound(request=req)

        # 通čæ‡ringēŽÆč®”ē®—分åŒŗ态containerꉀåœØ节ē‚¹
        container_partition, containers = self.app.container_ring.get_nodes(
            self.account_name, self.container_name)
        # ē”Ÿęˆåˆ›å»ŗcontainerčÆ·ę±‚ēš„header
        headers = self.generate_request_headers(req, transfer=True)
        # ęø…é™¤ęœ¬åœ°ē¼“å­˜account和containerēš„å…ƒę•°ę®äæ”ęÆ
        clear_info_cache(self.app, req.environ,
                         self.account_name, self.container_name)
        # 发送čÆ·ę±‚åˆ°ę‰€ęœ‰container节ē‚¹ļ¼Œę›“ꖰcontainerå…ƒę•°ę®čÆ·ę±‚
        resp = self.make_requests(
            req, self.app.container_ring, container_partition, 'POST',
            req.swift_entity_path, [headers] * len(containers))
        return resp
Example #43
0
 def PUT(self, req):
     """HTTP PUT request handler."""
     error_response = \
         self.clean_acls(req) or check_metadata(req, 'container')
     if error_response:
         return error_response
     if len(self.container_name) > MAX_CONTAINER_NAME_LENGTH:
         resp = HTTPBadRequest(request=req)
         resp.body = 'Container name length of %d longer than %d' % \
                     (len(self.container_name), MAX_CONTAINER_NAME_LENGTH)
         return resp
     account_partition, accounts, container_count = \
         self.account_info(self.account_name,
                           autocreate=self.app.account_autocreate)
     if self.app.max_containers_per_account > 0 and \
             container_count >= self.app.max_containers_per_account and \
             self.account_name not in self.app.max_containers_whitelist:
         resp = HTTPForbidden(request=req)
         resp.body = 'Reached container limit of %s' % \
                     self.app.max_containers_per_account
         return resp
     if not accounts:
         return HTTPNotFound(request=req)
     container_partition, containers = self.app.container_ring.get_nodes(
         self.account_name, self.container_name)
     headers = self._backend_requests(req, len(containers),
                                      account_partition, accounts)
     if self.app.memcache:
         cache_key = get_container_memcache_key(self.account_name,
                                                self.container_name)
         self.app.memcache.delete(cache_key)
     resp = self.make_requests(
         req, self.app.container_ring,
         container_partition, 'PUT', req.path_info, headers)
     return resp
Example #44
0
 def PUT(self, req):
     """HTTP PUT request handler."""
     if not self.app.allow_account_management:
         return HTTPMethodNotAllowed(
             request=req,
             headers={'Allow': ', '.join(self.allowed_methods)})
     error_response = check_metadata(req, 'account')
     if error_response:
         return error_response
     if len(self.account_name) > MAX_ACCOUNT_NAME_LENGTH:
         resp = HTTPBadRequest(request=req)
         resp.body = 'Account name length of %d longer than %d' % \
                     (len(self.account_name), MAX_ACCOUNT_NAME_LENGTH)
         return resp
     account_partition, accounts = \
         self.app.account_ring.get_nodes(self.account_name)
     headers = {'X-Timestamp': normalize_timestamp(time.time()),
                'x-trans-id': self.trans_id,
                'Connection': 'close'}
     self.transfer_headers(req.headers, headers)
     if self.app.memcache:
         self.app.memcache.delete('account%s' % req.path_info.rstrip('/'))
     resp = self.make_requests(
         req, self.app.account_ring, account_partition, 'PUT',
         req.path_info, [headers] * len(accounts))
     return resp
Example #45
0
 def POST(self, req):
     """HTTP POST request handler."""
     error_response = check_metadata(req, 'account')
     if error_response:
         return error_response
     account_partition, accounts = \
         self.app.account_ring.get_nodes(self.account_name)
     headers = {'X-Timestamp': normalize_timestamp(time.time()),
                'X-Trans-Id': self.trans_id,
                'Connection': 'close'}
     self.transfer_headers(req.headers, headers)
     if self.app.memcache:
         self.app.memcache.delete('account%s' % req.path_info.rstrip('/'))
     resp = self.make_requests(
         req, self.app.account_ring, account_partition, 'POST',
         req.path_info, [headers] * len(accounts))
     if resp.status_int == HTTP_NOT_FOUND and self.app.account_autocreate:
         if len(self.account_name) > MAX_ACCOUNT_NAME_LENGTH:
             resp = HTTPBadRequest(request=req)
             resp.body = 'Account name length of %d longer than %d' % \
                         (len(self.account_name), MAX_ACCOUNT_NAME_LENGTH)
             return resp
         resp = self.make_requests(
             Request.blank('/v1/' + self.account_name),
             self.app.account_ring, account_partition, 'PUT',
             '/' + self.account_name, [headers] * len(accounts))
         if not is_success(resp.status_int):
             self.app.logger.warning('Could not autocreate account %r' %
                                     self.account_name)
             return resp
     return resp
Example #46
0
 def POST(self, req):
     """HTTP POST request handler."""
     error_response = \
         self.clean_acls(req) or check_metadata(req, 'container')
     if error_response:
         return error_response
     if not req.environ.get('swift_owner'):
         for key in self.app.swift_owner_headers:
             req.headers.pop(key, None)
     if req.environ.get('reseller_request', False) and \
             'X-Container-Sharding' in req.headers:
         req.headers[get_sys_meta_prefix('container') + 'Sharding'] = \
             str(config_true_value(req.headers['X-Container-Sharding']))
     account_partition, accounts, container_count = \
         self.account_info(self.account_name, req)
     if not accounts:
         return HTTPNotFound(request=req)
     container_partition, containers = self.app.container_ring.get_nodes(
         self.account_name, self.container_name)
     headers = self.generate_request_headers(req, transfer=True)
     clear_info_cache(self.app, req.environ, self.account_name,
                      self.container_name)
     resp = self.make_requests(req, self.app.container_ring,
                               container_partition, 'POST',
                               req.swift_entity_path,
                               [headers] * len(containers))
     return resp
Example #47
0
    def test_check_metadata_non_utf8(self):
        # Consciously using native "WSGI strings" in headers
        headers = {'X-Account-Meta-Foo': '\xff'}
        resp = constraints.check_metadata(Request.blank(
            '/', headers=headers), 'account')
        self.assertEqual(resp.status_int, HTTP_BAD_REQUEST)
        self.assertIn(b'Metadata must be valid UTF-8', resp.body)

        headers = {'X-Container-Meta-\xff': 'foo'}
        resp = constraints.check_metadata(Request.blank(
            '/', headers=headers), 'container')
        self.assertEqual(resp.status_int, HTTP_BAD_REQUEST)
        self.assertIn(b'Metadata must be valid UTF-8', resp.body)
        # Object's OK; its metadata isn't serialized as JSON
        headers = {'X-Object-Meta-Foo': '\xff'}
        self.assertIsNone(constraints.check_metadata(Request.blank(
            '/', headers=headers), 'object'))
Example #48
0
    def test_check_metadata_non_utf8(self):
        # Consciously using native "WSGI strings" in headers
        headers = {'X-Account-Meta-Foo': '\xff'}
        resp = constraints.check_metadata(Request.blank(
            '/', headers=headers), 'account')
        self.assertEqual(resp.status_int, HTTP_BAD_REQUEST)
        self.assertIn(b'Metadata must be valid UTF-8', resp.body)

        headers = {'X-Container-Meta-\xff': 'foo'}
        resp = constraints.check_metadata(Request.blank(
            '/', headers=headers), 'container')
        self.assertEqual(resp.status_int, HTTP_BAD_REQUEST)
        self.assertIn(b'Metadata must be valid UTF-8', resp.body)
        # Object's OK; its metadata isn't serialized as JSON
        headers = {'X-Object-Meta-Foo': '\xff'}
        self.assertIsNone(constraints.check_metadata(Request.blank(
            '/', headers=headers), 'object'))
Example #49
0
    def POST(self, req):
        """HTTP POST request handler."""
        if self.app.object_post_as_copy:
            req.method = 'PUT'
            req.path_info = '/v1/%s/%s/%s' % (
                self.account_name, self.container_name, self.object_name)
            req.headers['Content-Length'] = 0
            req.headers['X-Copy-From'] = quote('/%s/%s' % (self.container_name,
                                               self.object_name))
            req.headers['X-Fresh-Metadata'] = 'true'
            req.environ['swift_versioned_copy'] = True
            if req.environ.get('QUERY_STRING'):
                req.environ['QUERY_STRING'] += '&multipart-manifest=get'
            else:
                req.environ['QUERY_STRING'] = 'multipart-manifest=get'
            resp = self.PUT(req)
            # Older editions returned 202 Accepted on object POSTs, so we'll
            # convert any 201 Created responses to that for compatibility with
            # picky clients.
            if resp.status_int != HTTP_CREATED:
                return resp
            return HTTPAccepted(request=req)
        else:
            error_response = check_metadata(req, 'object')
            if error_response:
                return error_response
            container_info = self.container_info(
                self.account_name, self.container_name, req)
            container_partition = container_info['partition']
            containers = container_info['nodes']
            req.acl = container_info['write_acl']
            if 'swift.authorize' in req.environ:
                aresp = req.environ['swift.authorize'](req)
                if aresp:
                    return aresp
            if not containers:
                return HTTPNotFound(request=req)

            req, delete_at_container, delete_at_part, \
                delete_at_nodes = self._config_obj_expiration(req)

            # pass the policy index to storage nodes via req header
            policy_index = req.headers.get('X-Backend-Storage-Policy-Index',
                                           container_info['storage_policy'])
            obj_ring = self.app.get_object_ring(policy_index)
            req.headers['X-Backend-Storage-Policy-Index'] = policy_index
            partition, nodes = obj_ring.get_nodes(
                self.account_name, self.container_name, self.object_name)

            req.headers['X-Timestamp'] = Timestamp(time.time()).internal

            headers = self._backend_requests(
                req, len(nodes), container_partition, containers,
                delete_at_container, delete_at_part, delete_at_nodes)

            resp = self.make_requests(req, obj_ring, partition,
                                      'POST', req.swift_entity_path, headers)
            return resp
 def test_check_metadata_size(self):
     headers = {}
     size = 0
     chunk = constraints.MAX_META_NAME_LENGTH + \
             constraints.MAX_META_VALUE_LENGTH
     x = 0
     while size + chunk < constraints.MAX_META_OVERALL_SIZE:
         headers['X-Object-Meta-%04d%s' %
                 (x, 'a' * (constraints.MAX_META_NAME_LENGTH - 4))] = \
                     'v' * constraints.MAX_META_VALUE_LENGTH
         size += chunk
         x += 1
     self.assertEquals(constraints.check_metadata(Request.blank('/',
         headers=headers), 'object'), None)
     headers['X-Object-Meta-9999%s' %
             ('a' * (constraints.MAX_META_NAME_LENGTH - 4))] = \
                 'v' * constraints.MAX_META_VALUE_LENGTH
     self.assert_(isinstance(constraints.check_metadata(Request.blank('/',
         headers=headers), 'object'), HTTPBadRequest))
Example #51
0
 def test_check_metadata_size(self):
     headers = {}
     size = 0
     chunk = constraints.MAX_META_NAME_LENGTH + \
             constraints.MAX_META_VALUE_LENGTH
     x = 0
     while size + chunk < constraints.MAX_META_OVERALL_SIZE:
         headers['X-Object-Meta-%04d%s' %
                 (x, 'a' * (constraints.MAX_META_NAME_LENGTH - 4))] = \
                     'v' * constraints.MAX_META_VALUE_LENGTH
         size += chunk
         x += 1
     self.assertEquals(constraints.check_metadata(Request.blank('/',
         headers=headers), 'object'), None)
     headers['X-Object-Meta-9999%s' %
             ('a' * (constraints.MAX_META_NAME_LENGTH - 4))] = \
                 'v' * constraints.MAX_META_VALUE_LENGTH
     self.assert_(isinstance(constraints.check_metadata(Request.blank('/',
         headers=headers), 'object'), HTTPBadRequest))
Example #52
0
    def PUT(self, req):
        """HTTP PUT request handler."""
        error_response = \
            self.clean_acls(req) or check_metadata(req, 'container')
        if error_response:
            return error_response
        policy_index = self._convert_policy_to_index(req)
        if policy_index is None:
            policy_index = int(POLICIES.default)
        if not req.environ.get('swift_owner'):
            for key in self.app.swift_owner_headers:
                req.headers.pop(key, None)
        if len(self.container_name) > constraints.MAX_CONTAINER_NAME_LENGTH:
            resp = HTTPBadRequest(request=req)
            resp.body = 'Container name length of %d longer than %d' % \
                        (len(self.container_name),
                         constraints.MAX_CONTAINER_NAME_LENGTH)
            return resp
        account_partition, accounts, container_count = \
            self.account_info(self.account_name, req)
        if not accounts and self.app.account_autocreate:
            self.autocreate_account(req, self.account_name)
            account_partition, accounts, container_count = \
                self.account_info(self.account_name, req)
        if not accounts:
            return HTTPNotFound(request=req)
        if self.app.max_containers_per_account > 0 and \
                container_count >= self.app.max_containers_per_account and \
                self.account_name not in self.app.max_containers_whitelist:
            container_info = \
                self.container_info(self.account_name, self.container_name,
                                    req)
            if not is_success(container_info.get('status')):
                resp = HTTPForbidden(request=req)
                resp.body = 'Reached container limit of %s' % \
                    self.app.max_containers_per_account
                return resp
        container_partition, containers = self.app.container_ring.get_nodes(
            self.account_name, self.container_name)
        headers = self._backend_requests(req, len(containers),
                                         account_partition, accounts,
                                         policy_index)
        clear_info_cache(self.app, req.environ,
                         self.account_name, self.container_name)
        resp = self.make_requests(
            req, self.app.container_ring,
            container_partition, 'PUT', req.swift_entity_path, headers)

        cloud_ring = CloudRing(self.container_name, POLICIES.get_by_index(policy_index))
        return_flag, _info = cloud_ring.create_containers()
        if not return_flag:
            msg = 'Failed:' + str(_info)
            raise PUTCloudContainerException(msg)

        return resp
Example #53
0
 def PUT(self, req):
     """HTTP PUT request handler."""
     error_response = \
         self.clean_acls(req) or check_metadata(req, 'container')
     if error_response:
         return error_response
     policy_index = self._convert_policy_to_index(req)
     if policy_index is None:
         # make sure all backend servers get the same default policy
         policy_index = POLICIES.default.idx
     policy = POLICIES[policy_index]
     if policy.is_deprecated:
         resp = HTTPBadRequest(request=req)
         resp.body = 'Storage Policy %r is deprecated' % \
                     (policy.name)
         return resp
     if not req.environ.get('swift_owner'):
         for key in self.app.swift_owner_headers:
             req.headers.pop(key, None)
     if len(self.container_name) > constraints.MAX_CONTAINER_NAME_LENGTH:
         resp = HTTPBadRequest(request=req)
         resp.body = 'Container name length of %d longer than %d' % \
                     (len(self.container_name),
                      constraints.MAX_CONTAINER_NAME_LENGTH)
         return resp
     account_partition, accounts, container_count = \
         self.account_info(self.account_name, req)
     if not accounts and self.app.account_autocreate:
         self.autocreate_account(req.environ, self.account_name)
         account_partition, accounts, container_count = \
             self.account_info(self.account_name, req)
     if not accounts:
         return HTTPNotFound(request=req)
     if self.app.max_containers_per_account > 0 and \
             container_count >= self.app.max_containers_per_account and \
             self.account_name not in self.app.max_containers_whitelist:
         resp = HTTPForbidden(request=req)
         resp.body = 'Reached container limit of %s' % \
                     self.app.max_containers_per_account
         return resp
     container_partition, containers = self.app.container_ring.get_nodes(
         self.account_name, self.container_name)
     headers = self._backend_requests(req, len(containers),
                                      account_partition, accounts,
                                      policy_index)
     clear_info_cache(self.app, req.environ,
                      self.account_name, self.container_name)
     resp = self.make_requests(
         req, self.app.container_ring,
         container_partition, 'PUT', req.swift_entity_path, headers)
     return resp
Example #54
0
 def PUT(self, req):
     """HTTP PUT request handler."""
     error_response = \
         self.clean_acls(req) or check_metadata(req, 'container')
     if error_response:
         return error_response
     policy_index = self._convert_policy_to_index(req)
     if not req.environ.get('swift_owner'):
         for key in self.app.swift_owner_headers:
             req.headers.pop(key, None)
     if req.environ.get('reseller_request', False) and \
             'X-Container-Sharding' in req.headers:
         req.headers[get_sys_meta_prefix('container') + 'Sharding'] = \
             str(config_true_value(req.headers['X-Container-Sharding']))
     length_limit = self.get_name_length_limit()
     if len(self.container_name) > length_limit:
         body = 'Container name length of %d longer than %d' % (len(
             self.container_name), length_limit)
         resp = HTTPBadRequest(request=req, body=body)
         return resp
     account_partition, accounts, container_count = \
         self.account_info(self.account_name, req)
     if not accounts and self.app.account_autocreate:
         if not self.autocreate_account(req, self.account_name):
             return HTTPServiceUnavailable(request=req)
         account_partition, accounts, container_count = \
             self.account_info(self.account_name, req)
     if not accounts:
         return HTTPNotFound(request=req)
     if 0 < self.app.max_containers_per_account <= container_count and \
             self.account_name not in self.app.max_containers_whitelist:
         container_info = \
             self.container_info(self.account_name, self.container_name,
                                 req)
         if not is_success(container_info.get('status')):
             body = 'Reached container limit of %s' % (
                 self.app.max_containers_per_account, )
             resp = HTTPForbidden(request=req, body=body)
             return resp
     container_partition, containers = self.app.container_ring.get_nodes(
         self.account_name, self.container_name)
     headers = self._backend_requests(req, len(containers),
                                      account_partition, accounts,
                                      policy_index)
     resp = self.make_requests(req, self.app.container_ring,
                               container_partition, 'PUT',
                               req.swift_entity_path, headers)
     clear_info_cache(self.app, req.environ, self.account_name,
                      self.container_name)
     return resp
Example #55
0
def check_object_creation(req, object_name):
    """
    Check to ensure that everything is alright about an object to be created.

    :param req: HTTP request object
    :param object_name: name of object to be created
    :raises HTTPRequestEntityTooLarge: the object is too large
    :raises HTTPLengthRequered: missing content-length header and not
                                a chunked request
    :raises HTTPBadRequest: missing or bad content-type header, or
                            bad metadata
    """
    if req.content_length and req.content_length > MAX_FILE_SIZE:
        return HTTPRequestEntityTooLarge(body='Your request is too large.',
                                         request=req,
                                         content_type='text/plain')
    if req.content_length is None and \
            req.headers.get('transfer-encoding') != 'chunked':
        return HTTPLengthRequired(request=req)
    if 'X-Copy-From' in req.headers and req.content_length:
        return HTTPBadRequest(body='Copy requests require a zero byte body',
                              request=req,
                              content_type='text/plain')
    for obj in object_name.split('/'):
        if not validate_obj_name(obj):
            return HTTPBadRequest(body='Invalid object name %s' % (obj),
                                  request=req,
                                  content_type='text/plain')
    if 'Content-Type' not in req.headers:
        return HTTPBadRequest(request=req,
                              content_type='text/plain',
                              body='No content type')
    if not check_utf8(req.headers['Content-Type']):
        return HTTPBadRequest(request=req,
                              body='Invalid Content-Type',
                              content_type='text/plain')
    if 'x-object-manifest' in req.headers:
        value = req.headers['x-object-manifest']
        container = prefix = None
        try:
            container, prefix = value.split('/', 1)
        except ValueError:
            pass
        if not container or not prefix or '?' in value or '&' in value or \
                prefix[0] == '/':
            return HTTPBadRequest(
                request=req,
                body='X-Object-Manifest must in the format container/prefix')
    return check_metadata(req, 'object')
Example #56
0
 def test_check_metadata_size(self):
     headers = {}
     size = 0
     chunk = constraints.MAX_META_NAME_LENGTH + \
             constraints.MAX_META_VALUE_LENGTH
     x = 0
     while size + chunk < constraints.MAX_META_OVERALL_SIZE:
         headers['X-Object-Meta-%04d%s' %
                 (x, 'a' * (constraints.MAX_META_NAME_LENGTH - 4))] = \
                     'v' * constraints.MAX_META_VALUE_LENGTH
         size += chunk
         x += 1
     self.assertEquals(constraints.check_metadata(Request.blank('/',
         headers=headers), 'object'), None)
     # add two more headers in case adding just one falls exactly on the
     # limit (eg one header adds 1024 and the limit is 2048)
     headers['X-Object-Meta-%04d%s' %
             (x, 'a' * (constraints.MAX_META_NAME_LENGTH - 4))] = \
                 'v' * constraints.MAX_META_VALUE_LENGTH
     headers['X-Object-Meta-%04d%s' %
             (x + 1, 'a' * (constraints.MAX_META_NAME_LENGTH - 4))] = \
                 'v' * constraints.MAX_META_VALUE_LENGTH
     self.assert_(isinstance(constraints.check_metadata(Request.blank('/',
         headers=headers), 'object'), HTTPBadRequest))
Example #57
0
    def POST(self, req):
        """HTTP POST request handler."""
        if len(self.account_name) > constraints.MAX_ACCOUNT_NAME_LENGTH:
            resp = HTTPBadRequest(request=req)
            resp.body = 'Account name length of %d longer than %d' % \
                        (len(self.account_name),
                         constraints.MAX_ACCOUNT_NAME_LENGTH)
            return resp
        error_response = check_metadata(req, 'account')
        if error_response:
            return error_response

        headers = self.generate_request_headers(req, transfer=True)
        clear_info_cache(self.app, req.environ, self.account_name)
        resp = self.get_account_post_resp(req, headers)
        self.add_acls_from_sys_metadata(resp)
        return resp
Example #58
0
 def PUT(self, req):
     """HTTP PUT request handler."""
     error_response = \
         self.clean_acls(req) or check_metadata(req, 'container')
     if error_response:
         return error_response
     policy_index = self._convert_policy_to_index(req)
     if not req.environ.get('swift_owner'):
         for key in self.app.swift_owner_headers:
             req.headers.pop(key, None)
     if len(self.container_name) > constraints.MAX_CONTAINER_NAME_LENGTH:
         resp = HTTPBadRequest(request=req)
         resp.body = 'Container name length of %d longer than %d' % \
                     (len(self.container_name),
                      constraints.MAX_CONTAINER_NAME_LENGTH)
         return resp
     account_partition, accounts, container_count = \
         self.account_info(self.account_name, req)
     if not accounts and self.app.account_autocreate:
         if not self.autocreate_account(req, self.account_name):
             return HTTPServerError(request=req)
         account_partition, accounts, container_count = \
             self.account_info(self.account_name, req)
     if not accounts:
         return HTTPNotFound(request=req)
     if 0 < self.app.max_containers_per_account <= container_count and \
             self.account_name not in self.app.max_containers_whitelist:
         container_info = \
             self.container_info(self.account_name, self.container_name,
                                 req)
         if not is_success(container_info.get('status')):
             resp = HTTPForbidden(request=req)
             resp.body = 'Reached container limit of %s' % \
                 self.app.max_containers_per_account
             return resp
     container_partition, containers = self.app.container_ring.get_nodes(
         self.account_name, self.container_name)
     headers = self._backend_requests(req, len(containers),
                                      account_partition, accounts,
                                      policy_index)
     resp = self.make_requests(req, self.app.container_ring,
                               container_partition, 'PUT',
                               req.swift_entity_path, headers)
     clear_info_cache(self.app, req.environ, self.account_name,
                      self.container_name)
     return resp
Example #59
0
 def PUT(self, req):
     """HTTP PUT request handler."""
     error_response = \
         self.clean_acls(req) or check_metadata(req, 'container')
     if error_response:
         return error_response
     if len(self.container_name) > MAX_CONTAINER_NAME_LENGTH:
         resp = HTTPBadRequest(request=req)
         resp.body = 'Container name length of %d longer than %d' % \
                     (len(self.container_name), MAX_CONTAINER_NAME_LENGTH)
         return resp
     account_partition, accounts, container_count = \
         self.account_info(self.account_name,
                           autocreate=self.app.account_autocreate)
     if self.app.max_containers_per_account > 0 and \
             container_count >= self.app.max_containers_per_account and \
             self.account_name not in self.app.max_containers_whitelist:
         resp = HTTPForbidden(request=req)
         resp.body = 'Reached container limit of %s' % \
                     self.app.max_containers_per_account
         return resp
     if not accounts:
         return HTTPNotFound(request=req)
     container_partition, containers = self.app.container_ring.get_nodes(
         self.account_name, self.container_name)
     headers = []
     for account in accounts:
         nheaders = {
             'X-Timestamp': normalize_timestamp(time.time()),
             'x-trans-id': self.trans_id,
             'X-Account-Host': '%(ip)s:%(port)s' % account,
             'X-Account-Partition': account_partition,
             'X-Account-Device': account['device'],
             'Connection': 'close'
         }
         self.transfer_headers(req.headers, nheaders)
         headers.append(nheaders)
     if self.app.memcache:
         cache_key = get_container_memcache_key(self.account_name,
                                                self.container_name)
         self.app.memcache.delete(cache_key)
     resp = self.make_requests(req, self.app.container_ring,
                               container_partition, 'PUT', req.path_info,
                               headers)
     return resp
Example #60
0
    def POST(self, req):
        """HTTP POST request handler."""
        container_info = self.container_info(self.account_name,
                                             self.container_name, req)
        req.acl = container_info['write_acl']
        if 'swift.authorize' in req.environ:
            aresp = req.environ['swift.authorize'](req)
            if aresp:
                return aresp
        error_response = check_metadata(req, 'object')
        if error_response:
            return error_response

        policy_index = req.headers.get('X-Backend-Storage-Policy-Index',
                                       container_info['storage_policy'])
        stgpol = self._stgpol_from_policy_index(policy_index)
        headers = self._prepare_headers(req)
        return self._post_object(req, headers, stgpol)