-
Notifications
You must be signed in to change notification settings - Fork 21
/
container.py
439 lines (400 loc) · 17.8 KB
/
container.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
# Copyright (c) 2010-2012 OpenStack Foundation
# Copyright (c) 2016-2020 OpenIO SAS
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import json
from xml.etree.cElementTree import Element, SubElement, tostring
from swift.common.utils import public, Timestamp, \
override_bytes_from_content_type
from swift.common.constraints import check_metadata
from swift.common import constraints
try:
from swift.common.middleware.listing_formats import \
get_listing_content_type
except ImportError:
# Before Queens
from swift.common.request_helpers import get_listing_content_type
from oioswift.common.middleware.versioned_writes import \
DELETE_MARKER_CONTENT_TYPE
from swift.common.swob import Response, HTTPBadRequest, HTTPNotFound, \
HTTPNoContent, HTTPConflict, HTTPPreconditionFailed, HTTPForbidden, \
HTTPCreated
from swift.common.http import is_success, HTTP_ACCEPTED
from swift.common.request_helpers import is_sys_or_user_meta, get_param
from swift.proxy.controllers.container import ContainerController \
as SwiftContainerController
from swift.proxy.controllers.base import clear_info_cache, \
delay_denial, cors_validation, set_info_cache
from oio.common import exceptions
from oioswift.utils import \
handle_oio_no_such_container, handle_oio_timeout, \
handle_service_busy, REQID_HEADER, BUCKET_NAME_PROP, MULTIUPLOAD_SUFFIX
class ContainerController(SwiftContainerController):
pass_through_headers = ['x-container-read', 'x-container-write',
'x-container-sync-key', 'x-container-sync-to',
'x-versions-location']
@handle_oio_no_such_container
@handle_oio_timeout
@handle_service_busy
def GETorHEAD(self, req):
"""Handler for HTTP GET/HEAD requests."""
if self.account_info(self.account_name, req) is None:
if 'swift.authorize' in req.environ:
aresp = req.environ['swift.authorize'](req)
if aresp:
return aresp
return HTTPNotFound(request=req)
if req.method == 'GET':
resp = self.get_container_list_resp(req)
else:
resp = self.get_container_head_resp(req)
set_info_cache(self.app, req.environ, self.account_name,
self.container_name, resp)
resp = self.convert_policy(resp)
if 'swift.authorize' in req.environ:
req.acl = resp.headers.get('x-container-read')
aresp = req.environ['swift.authorize'](req)
if aresp:
return aresp
if not req.environ.get('swift_owner', False):
for key in self.app.swift_owner_headers:
if key in resp.headers:
del resp.headers[key]
return resp
def convert_policy(self, resp):
if 'X-Backend-Storage-Policy-Index' in resp.headers and \
is_success(resp.status_int):
policy = self.app.POLICIES.get_by_index(
resp.headers['X-Backend-Storage-Policy-Index'])
if policy:
resp.headers['X-Storage-Policy'] = policy.name
else:
self.app.logger.error(
'Could not translate %s (%r) from %r to policy',
'X-Backend-Storage-Policy-Index',
resp.headers['X-Backend-Storage-Policy-Index'])
return resp
def get_metadata_resp_headers(self, meta):
headers = {}
system = meta.get('system') or {}
# sys.m2.ctime is microseconds
ctime = float(system.get('sys.m2.ctime', 0)) / 1000000.0
headers.update({
'X-Container-Object-Count': system.get('sys.m2.objects', 0),
'X-Container-Bytes-Used': system.get('sys.m2.usage', 0),
'X-Timestamp': Timestamp(ctime).normal,
# FIXME: save modification time somewhere
'X-PUT-Timestamp': Timestamp(ctime).normal,
})
for (k, v) in meta['properties'].iteritems():
if v and (k.lower() in self.pass_through_headers or
is_sys_or_user_meta('container', k)):
headers[k] = v
return headers
def get_container_list_resp(self, req):
path = get_param(req, 'path')
prefix = get_param(req, 'prefix')
delimiter = get_param(req, 'delimiter')
if delimiter and (len(delimiter) > 1 or ord(delimiter) > 254):
# delimiters can be made more flexible later
return HTTPPreconditionFailed(body='Bad delimiter')
marker = get_param(req, 'marker', '')
end_marker = get_param(req, 'end_marker')
limit = constraints.CONTAINER_LISTING_LIMIT
given_limit = get_param(req, 'limit')
if given_limit and given_limit.isdigit():
limit = int(given_limit)
if limit > constraints.CONTAINER_LISTING_LIMIT:
return HTTPPreconditionFailed(
request=req,
body='Maximum limit is %d'
% constraints.CONTAINER_LISTING_LIMIT)
out_content_type = get_listing_content_type(req)
if path is not None:
prefix = path
if path:
prefix = path.rstrip('/') + '/'
delimiter = '/'
opts = req.environ.get('oio.query', {})
oio_headers = {REQID_HEADER: self.trans_id}
oio_cache = req.environ.get('oio.cache')
perfdata = req.environ.get('oio.perfdata')
result = self.app.storage.object_list(
self.account_name, self.container_name, prefix=prefix,
limit=limit, delimiter=delimiter, marker=marker,
end_marker=end_marker, properties=True,
versions=opts.get('versions', False),
deleted=opts.get('deleted', False),
force_master=opts.get('force_master', False),
headers=oio_headers, cache=oio_cache, perfdata=perfdata)
resp_headers = self.get_metadata_resp_headers(result)
resp = self.create_listing(
req, out_content_type, resp_headers, result,
self.container_name, **opts)
return resp
def create_listing(self, req, out_content_type, resp_headers,
result, container, **kwargs):
container_list = result['objects']
for p in result.get('prefixes', []):
record = {'name': p,
'subdir': True}
container_list.append(record)
container_list.sort(key=lambda x: x['name'])
ret = Response(request=req, headers=resp_headers,
content_type=out_content_type, charset='utf-8')
versions = kwargs.get('versions', False)
slo = kwargs.get('slo', False)
if out_content_type == 'application/json':
ret.body = json.dumps(
[self.update_data_record(r, versions, slo)
for r in container_list])
elif out_content_type.endswith('/xml'):
doc = Element('container', name=container.decode('utf-8'))
for obj in container_list:
record = self.update_data_record(obj, versions, slo)
if 'subdir' in record:
name = record['subdir'].decode('utf-8')
sub = SubElement(doc, 'subdir', name=name)
SubElement(sub, 'name').text = name
else:
obj_element = SubElement(doc, 'object')
for field in ["name", "hash", "bytes", "content_type",
"last_modified"]:
SubElement(obj_element, field).text = str(
record.pop(field)).decode('utf-8')
for field in sorted(record):
SubElement(obj_element, field).text = str(
record[field]).decode('utf-8')
ret.body = tostring(doc, encoding='UTF-8').replace(
"<?xml version='1.0' encoding='UTF-8'?>",
'<?xml version="1.0" encoding="UTF-8"?>', 1)
else:
if not container_list:
return HTTPNoContent(request=req, headers=resp_headers)
ret.body = '\n'.join(rec['name'] for rec in container_list) + '\n'
return ret
def update_data_record(self, record, versions=False, slo=False):
if 'subdir' in record:
return {'subdir': record['name']}
props = record.get('properties', {})
# This metadata is added by encryption middleware.
if 'x-object-sysmeta-container-update-override-etag' in props:
hash_ = props['x-object-sysmeta-container-update-override-etag']
else:
hash_ = record.get('hash')
if hash_ is not None:
hash_ = hash_.lower()
response = {'name': record['name'],
'bytes': record['size'],
'last_modified': Timestamp(record['mtime']).isoformat,
'content_type': record.get(
'mime_type', 'application/octet-stream')}
if hash_:
response['hash'] = hash_
else:
response['hash'] = ''
if record.get('deleted', False):
response['content_type'] = DELETE_MARKER_CONTENT_TYPE
if versions:
response['version'] = record.get('version', 'null')
if slo:
response['slo'] = props.get("x-static-large-object")
override_bytes_from_content_type(response)
return response
@public
@delay_denial
@cors_validation
def GET(self, req):
"""Handler for HTTP GET requests."""
return self.GETorHEAD(req)
@public
@delay_denial
@cors_validation
def HEAD(self, req):
"""Handler for HTTP HEAD requests."""
return self.GETorHEAD(req)
def get_container_head_resp(self, req):
headers = dict()
out_content_type = get_listing_content_type(req)
headers['Content-Type'] = out_content_type
oio_headers = {REQID_HEADER: self.trans_id}
oio_cache = req.environ.get('oio.cache')
perfdata = req.environ.get('oio.perfdata')
meta = self.app.storage.container_get_properties(
self.account_name, self.container_name, headers=oio_headers,
cache=oio_cache, perfdata=perfdata)
headers.update(self.get_metadata_resp_headers(meta))
return HTTPNoContent(request=req, headers=headers, charset='utf-8')
def properties_from_headers(self, headers):
metadata = {
k: v
for k, v in headers.items()
if k.lower() in self.pass_through_headers or
is_sys_or_user_meta('container', k)
}
system = dict()
# This header enables versioning
ver_loc = headers.get('X-Container-Sysmeta-Versions-Location')
if ver_loc is not None:
# When suspending versioning, header has empty string value
ver_val = "-1" if ver_loc else "1"
system['sys.m2.policy.version'] = ver_val
return metadata, system
def _convert_policy(self, req):
policy_name = req.headers.get('X-Storage-Policy')
if not policy_name:
return
policy = self.app.POLICIES.get_by_name(policy_name)
if not policy:
msg = "Invalid X-Storage-Policy '%s'" % policy_name
raise HTTPBadRequest(
request=req, content_type='text/plain', body=msg)
return policy
def get_container_create_resp(self, req, headers):
properties, system = self.properties_from_headers(headers)
# Save the name of the S3 bucket in a container property.
# This will be used when aggregating container statistics
# to make bucket statistics.
bname = self.container_name
if bname.endswith(MULTIUPLOAD_SUFFIX):
bname = bname[:-len(MULTIUPLOAD_SUFFIX)]
system[BUCKET_NAME_PROP] = bname
# TODO container update metadata
oio_headers = {REQID_HEADER: self.trans_id}
oio_cache = req.environ.get('oio.cache')
perfdata = req.environ.get('oio.perfdata')
created = self.app.storage.container_create(
self.account_name, self.container_name,
properties=properties, system=system,
headers=oio_headers, cache=oio_cache, perfdata=perfdata)
if created:
return HTTPCreated(request=req)
else:
return HTTPNoContent(request=req)
@public
@cors_validation
@handle_oio_timeout
@handle_service_busy
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
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
headers = self.generate_request_headers(req, transfer=True)
clear_info_cache(self.app, req.environ, self.account_name,
self.container_name)
resp = self.get_container_create_resp(req, headers)
return resp
@public
@cors_validation
@handle_oio_timeout
@handle_service_busy
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
def get_container_post_resp(self, req, headers):
properties, system = self.properties_from_headers(headers)
if not properties:
return self.PUT(req)
oio_headers = {REQID_HEADER: self.trans_id}
oio_cache = req.environ.get('oio.cache')
perfdata = req.environ.get('oio.perfdata')
try:
self.app.storage.container_set_properties(
self.account_name, self.container_name,
properties=properties, system=system,
headers=oio_headers, cache=oio_cache, perfdata=perfdata)
resp = HTTPNoContent(request=req)
except exceptions.NoSuchContainer:
resp = self.PUT(req)
return resp
def get_container_delete_resp(self, req):
oio_headers = {REQID_HEADER: self.trans_id}
oio_cache = req.environ.get('oio.cache')
perfdata = req.environ.get('oio.perfdata')
try:
self.app.storage.container_delete(
self.account_name, self.container_name, headers=oio_headers,
cache=oio_cache, perfdata=perfdata)
except exceptions.ContainerNotEmpty:
return HTTPConflict(request=req)
resp = HTTPNoContent(request=req)
return resp
@public
@cors_validation
@handle_oio_no_such_container
@handle_oio_timeout
@handle_service_busy
def DELETE(self, req):
"""HTTP DELETE request handler."""
account_partition, accounts, container_count = \
self.account_info(self.account_name, req)
if not accounts:
return HTTPNotFound(request=req)
clear_info_cache(self.app, req.environ,
self.account_name, self.container_name)
resp = self.get_container_delete_resp(req)
if resp.status_int == HTTP_ACCEPTED:
return HTTPNotFound(request=req)
return resp