]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- |
2 | // vim: ts=8 sw=2 smarttab | |
3 | ||
4 | #include <errno.h> | |
5 | #include <stdlib.h> | |
31f18b77 | 6 | #include <system_error> |
7c673cae FG |
7 | #include <unistd.h> |
8 | ||
9 | #include <sstream> | |
10 | ||
11 | #include <boost/algorithm/string/predicate.hpp> | |
31f18b77 | 12 | #include <boost/bind.hpp> |
7c673cae | 13 | #include <boost/optional.hpp> |
31f18b77 FG |
14 | #include <boost/utility/in_place_factory.hpp> |
15 | #include <boost/utility/string_view.hpp> | |
7c673cae | 16 | |
11fdf7f2 | 17 | #include "include/scope_guard.h" |
7c673cae FG |
18 | #include "common/Clock.h" |
19 | #include "common/armor.h" | |
31f18b77 | 20 | #include "common/errno.h" |
7c673cae FG |
21 | #include "common/mime.h" |
22 | #include "common/utf8.h" | |
23 | #include "common/ceph_json.h" | |
11fdf7f2 | 24 | #include "common/static_ptr.h" |
7c673cae FG |
25 | |
26 | #include "rgw_rados.h" | |
11fdf7f2 | 27 | #include "rgw_zone.h" |
7c673cae FG |
28 | #include "rgw_op.h" |
29 | #include "rgw_rest.h" | |
30 | #include "rgw_acl.h" | |
31 | #include "rgw_acl_s3.h" | |
32 | #include "rgw_acl_swift.h" | |
11fdf7f2 | 33 | #include "rgw_aio_throttle.h" |
7c673cae FG |
34 | #include "rgw_user.h" |
35 | #include "rgw_bucket.h" | |
36 | #include "rgw_log.h" | |
37 | #include "rgw_multi.h" | |
38 | #include "rgw_multi_del.h" | |
39 | #include "rgw_cors.h" | |
40 | #include "rgw_cors_s3.h" | |
41 | #include "rgw_rest_conn.h" | |
42 | #include "rgw_rest_s3.h" | |
43 | #include "rgw_tar.h" | |
44 | #include "rgw_client_io.h" | |
45 | #include "rgw_compression.h" | |
46 | #include "rgw_role.h" | |
224ce89b | 47 | #include "rgw_tag_s3.h" |
11fdf7f2 TL |
48 | #include "rgw_putobj_processor.h" |
49 | #include "rgw_crypt.h" | |
50 | #include "rgw_perf_counters.h" | |
eafe8130 TL |
51 | #include "rgw_notify.h" |
52 | #include "rgw_notify_event_type.h" | |
11fdf7f2 TL |
53 | |
54 | #include "services/svc_zone.h" | |
55 | #include "services/svc_quota.h" | |
56 | #include "services/svc_sys_obj.h" | |
57 | ||
7c673cae FG |
58 | #include "cls/lock/cls_lock_client.h" |
59 | #include "cls/rgw/cls_rgw_client.h" | |
60 | ||
61 | ||
11fdf7f2 | 62 | #include "include/ceph_assert.h" |
7c673cae FG |
63 | |
64 | #include "compressor/Compressor.h" | |
65 | ||
11fdf7f2 TL |
66 | #ifdef WITH_LTTNG |
67 | #define TRACEPOINT_DEFINE | |
68 | #define TRACEPOINT_PROBE_DYNAMIC_LINKAGE | |
69 | #include "tracing/rgw_op.h" | |
70 | #undef TRACEPOINT_PROBE_DYNAMIC_LINKAGE | |
71 | #undef TRACEPOINT_DEFINE | |
72 | #else | |
73 | #define tracepoint(...) | |
74 | #endif | |
7c673cae FG |
75 | |
76 | #define dout_context g_ceph_context | |
77 | #define dout_subsys ceph_subsys_rgw | |
78 | ||
7c673cae FG |
79 | using namespace librados; |
80 | using ceph::crypto::MD5; | |
31f18b77 FG |
81 | using boost::optional; |
82 | using boost::none; | |
7c673cae | 83 | |
eafe8130 | 84 | using rgw::ARN; |
31f18b77 FG |
85 | using rgw::IAM::Effect; |
86 | using rgw::IAM::Policy; | |
87 | ||
88 | using rgw::IAM::Policy; | |
7c673cae FG |
89 | |
90 | static string mp_ns = RGW_OBJ_NS_MULTIPART; | |
91 | static string shadow_ns = RGW_OBJ_NS_SHADOW; | |
92 | ||
93 | static void forward_req_info(CephContext *cct, req_info& info, const std::string& bucket_name); | |
94 | static int forward_request_to_master(struct req_state *s, obj_version *objv, RGWRados *store, | |
95 | bufferlist& in_data, JSONParser *jp, req_info *forward_info = nullptr); | |
96 | ||
97 | static MultipartMetaFilter mp_filter; | |
98 | ||
11fdf7f2 TL |
99 | // this probably should belong in the rgw_iam_policy_keywords, I'll get it to it |
100 | // at some point | |
101 | static constexpr auto S3_EXISTING_OBJTAG = "s3:ExistingObjectTag"; | |
102 | ||
28e407b8 | 103 | int RGWGetObj::parse_range(void) |
7c673cae FG |
104 | { |
105 | int r = -ERANGE; | |
28e407b8 | 106 | string rs(range_str); |
7c673cae FG |
107 | string ofs_str; |
108 | string end_str; | |
109 | ||
28e407b8 AA |
110 | ignore_invalid_range = s->cct->_conf->rgw_ignore_get_invalid_range; |
111 | partial_content = false; | |
7c673cae | 112 | |
28e407b8 | 113 | size_t pos = rs.find("bytes="); |
7c673cae FG |
114 | if (pos == string::npos) { |
115 | pos = 0; | |
28e407b8 | 116 | while (isspace(rs[pos])) |
7c673cae FG |
117 | pos++; |
118 | int end = pos; | |
28e407b8 | 119 | while (isalpha(rs[end])) |
7c673cae | 120 | end++; |
28e407b8 | 121 | if (strncasecmp(rs.c_str(), "bytes", end - pos) != 0) |
7c673cae | 122 | return 0; |
28e407b8 | 123 | while (isspace(rs[end])) |
7c673cae | 124 | end++; |
28e407b8 | 125 | if (rs[end] != '=') |
7c673cae | 126 | return 0; |
28e407b8 | 127 | rs = rs.substr(end + 1); |
7c673cae | 128 | } else { |
28e407b8 | 129 | rs = rs.substr(pos + 6); /* size of("bytes=") */ |
7c673cae | 130 | } |
28e407b8 | 131 | pos = rs.find('-'); |
7c673cae FG |
132 | if (pos == string::npos) |
133 | goto done; | |
134 | ||
28e407b8 | 135 | partial_content = true; |
7c673cae | 136 | |
28e407b8 AA |
137 | ofs_str = rs.substr(0, pos); |
138 | end_str = rs.substr(pos + 1); | |
7c673cae FG |
139 | if (end_str.length()) { |
140 | end = atoll(end_str.c_str()); | |
141 | if (end < 0) | |
142 | goto done; | |
143 | } | |
144 | ||
145 | if (ofs_str.length()) { | |
146 | ofs = atoll(ofs_str.c_str()); | |
147 | } else { // RFC2616 suffix-byte-range-spec | |
148 | ofs = -end; | |
149 | end = -1; | |
150 | } | |
151 | ||
152 | if (end >= 0 && end < ofs) | |
153 | goto done; | |
154 | ||
28e407b8 AA |
155 | range_parsed = true; |
156 | return 0; | |
157 | ||
7c673cae | 158 | done: |
28e407b8 AA |
159 | if (ignore_invalid_range) { |
160 | partial_content = false; | |
161 | ofs = 0; | |
162 | end = -1; | |
163 | range_parsed = false; // allow retry | |
164 | r = 0; | |
165 | } | |
166 | ||
7c673cae FG |
167 | return r; |
168 | } | |
169 | ||
170 | static int decode_policy(CephContext *cct, | |
171 | bufferlist& bl, | |
172 | RGWAccessControlPolicy *policy) | |
173 | { | |
11fdf7f2 | 174 | auto iter = bl.cbegin(); |
7c673cae FG |
175 | try { |
176 | policy->decode(iter); | |
177 | } catch (buffer::error& err) { | |
178 | ldout(cct, 0) << "ERROR: could not decode policy, caught buffer::error" << dendl; | |
179 | return -EIO; | |
180 | } | |
11fdf7f2 | 181 | if (cct->_conf->subsys.should_gather<ceph_subsys_rgw, 15>()) { |
7c673cae | 182 | ldout(cct, 15) << __func__ << " Read AccessControlPolicy"; |
11fdf7f2 | 183 | RGWAccessControlPolicy_S3 *s3policy = static_cast<RGWAccessControlPolicy_S3 *>(policy); |
7c673cae FG |
184 | s3policy->to_xml(*_dout); |
185 | *_dout << dendl; | |
186 | } | |
187 | return 0; | |
188 | } | |
189 | ||
190 | ||
191 | static int get_user_policy_from_attr(CephContext * const cct, | |
31f18b77 FG |
192 | RGWRados * const store, |
193 | map<string, bufferlist>& attrs, | |
194 | RGWAccessControlPolicy& policy /* out */) | |
7c673cae FG |
195 | { |
196 | auto aiter = attrs.find(RGW_ATTR_ACL); | |
197 | if (aiter != attrs.end()) { | |
198 | int ret = decode_policy(cct, aiter->second, &policy); | |
199 | if (ret < 0) { | |
200 | return ret; | |
201 | } | |
202 | } else { | |
203 | return -ENOENT; | |
204 | } | |
205 | ||
206 | return 0; | |
207 | } | |
208 | ||
209 | static int get_bucket_instance_policy_from_attr(CephContext *cct, | |
31f18b77 FG |
210 | RGWRados *store, |
211 | RGWBucketInfo& bucket_info, | |
212 | map<string, bufferlist>& bucket_attrs, | |
11fdf7f2 | 213 | RGWAccessControlPolicy *policy) |
7c673cae FG |
214 | { |
215 | map<string, bufferlist>::iterator aiter = bucket_attrs.find(RGW_ATTR_ACL); | |
216 | ||
217 | if (aiter != bucket_attrs.end()) { | |
218 | int ret = decode_policy(cct, aiter->second, policy); | |
219 | if (ret < 0) | |
220 | return ret; | |
221 | } else { | |
222 | ldout(cct, 0) << "WARNING: couldn't find acl header for bucket, generating default" << dendl; | |
223 | RGWUserInfo uinfo; | |
224 | /* object exists, but policy is broken */ | |
225 | int r = rgw_get_user_info_by_uid(store, bucket_info.owner, uinfo); | |
226 | if (r < 0) | |
227 | return r; | |
228 | ||
229 | policy->create_default(bucket_info.owner, uinfo.display_name); | |
230 | } | |
231 | return 0; | |
232 | } | |
233 | ||
234 | static int get_obj_policy_from_attr(CephContext *cct, | |
31f18b77 FG |
235 | RGWRados *store, |
236 | RGWObjectCtx& obj_ctx, | |
237 | RGWBucketInfo& bucket_info, | |
238 | map<string, bufferlist>& bucket_attrs, | |
239 | RGWAccessControlPolicy *policy, | |
11fdf7f2 | 240 | string *storage_class, |
31f18b77 | 241 | rgw_obj& obj) |
7c673cae FG |
242 | { |
243 | bufferlist bl; | |
244 | int ret = 0; | |
245 | ||
246 | RGWRados::Object op_target(store, bucket_info, obj_ctx, obj); | |
247 | RGWRados::Object::Read rop(&op_target); | |
248 | ||
249 | ret = rop.get_attr(RGW_ATTR_ACL, bl); | |
250 | if (ret >= 0) { | |
251 | ret = decode_policy(cct, bl, policy); | |
252 | if (ret < 0) | |
253 | return ret; | |
254 | } else if (ret == -ENODATA) { | |
255 | /* object exists, but policy is broken */ | |
256 | ldout(cct, 0) << "WARNING: couldn't find acl header for object, generating default" << dendl; | |
257 | RGWUserInfo uinfo; | |
258 | ret = rgw_get_user_info_by_uid(store, bucket_info.owner, uinfo); | |
259 | if (ret < 0) | |
260 | return ret; | |
261 | ||
262 | policy->create_default(bucket_info.owner, uinfo.display_name); | |
263 | } | |
11fdf7f2 TL |
264 | |
265 | if (storage_class) { | |
266 | bufferlist scbl; | |
267 | int r = rop.get_attr(RGW_ATTR_STORAGE_CLASS, scbl); | |
268 | if (r >= 0) { | |
269 | *storage_class = scbl.to_str(); | |
270 | } else { | |
271 | storage_class->clear(); | |
272 | } | |
273 | } | |
274 | ||
7c673cae FG |
275 | return ret; |
276 | } | |
277 | ||
278 | ||
279 | /** | |
280 | * Get the AccessControlPolicy for an object off of disk. | |
281 | * policy: must point to a valid RGWACL, and will be filled upon return. | |
282 | * bucket: name of the bucket containing the object. | |
283 | * object: name of the object to get the ACL for. | |
284 | * Returns: 0 on success, -ERR# otherwise. | |
285 | */ | |
11fdf7f2 TL |
286 | int rgw_op_get_bucket_policy_from_attr(CephContext *cct, |
287 | RGWRados *store, | |
288 | RGWBucketInfo& bucket_info, | |
289 | map<string, bufferlist>& bucket_attrs, | |
290 | RGWAccessControlPolicy *policy) | |
291 | { | |
292 | return get_bucket_instance_policy_from_attr(cct, store, bucket_info, bucket_attrs, policy); | |
293 | } | |
294 | ||
295 | static boost::optional<Policy> get_iam_policy_from_attr(CephContext* cct, | |
296 | RGWRados* store, | |
297 | map<string, bufferlist>& attrs, | |
298 | const string& tenant) { | |
31f18b77 FG |
299 | auto i = attrs.find(RGW_ATTR_IAM_POLICY); |
300 | if (i != attrs.end()) { | |
301 | return Policy(cct, tenant, i->second); | |
302 | } else { | |
303 | return none; | |
304 | } | |
7c673cae FG |
305 | } |
306 | ||
11fdf7f2 TL |
307 | vector<Policy> get_iam_user_policy_from_attr(CephContext* cct, |
308 | RGWRados* store, | |
309 | map<string, bufferlist>& attrs, | |
310 | const string& tenant) { | |
311 | vector<Policy> policies; | |
312 | if (auto it = attrs.find(RGW_ATTR_USER_POLICY); it != attrs.end()) { | |
313 | bufferlist out_bl = attrs[RGW_ATTR_USER_POLICY]; | |
314 | map<string, string> policy_map; | |
315 | decode(policy_map, out_bl); | |
316 | for (auto& it : policy_map) { | |
317 | bufferlist bl = bufferlist::static_from_string(it.second); | |
318 | Policy p(cct, tenant, bl); | |
319 | policies.push_back(std::move(p)); | |
320 | } | |
321 | } | |
322 | return policies; | |
323 | } | |
324 | ||
eafe8130 | 325 | static int get_obj_attrs(RGWRados *store, struct req_state *s, const rgw_obj& obj, map<string, bufferlist>& attrs, rgw_obj *target_obj = nullptr) |
7c673cae FG |
326 | { |
327 | RGWRados::Object op_target(store, s->bucket_info, *static_cast<RGWObjectCtx *>(s->obj_ctx), obj); | |
328 | RGWRados::Object::Read read_op(&op_target); | |
329 | ||
330 | read_op.params.attrs = &attrs; | |
eafe8130 | 331 | read_op.params.target_obj = target_obj; |
7c673cae FG |
332 | |
333 | return read_op.prepare(); | |
334 | } | |
335 | ||
11fdf7f2 TL |
336 | static int get_obj_head(RGWRados *store, struct req_state *s, |
337 | const rgw_obj& obj, | |
338 | map<string, bufferlist> *attrs, | |
339 | bufferlist *pbl) | |
340 | { | |
341 | store->set_prefetch_data(s->obj_ctx, obj); | |
342 | ||
343 | RGWRados::Object op_target(store, s->bucket_info, *static_cast<RGWObjectCtx *>(s->obj_ctx), obj); | |
344 | RGWRados::Object::Read read_op(&op_target); | |
345 | ||
346 | read_op.params.attrs = attrs; | |
347 | ||
348 | int ret = read_op.prepare(); | |
349 | if (ret < 0) { | |
350 | return ret; | |
351 | } | |
352 | ||
353 | if (!pbl) { | |
354 | return 0; | |
355 | } | |
356 | ||
357 | ret = read_op.read(0, s->cct->_conf->rgw_max_chunk_size, *pbl); | |
358 | ||
359 | return 0; | |
360 | } | |
361 | ||
362 | struct multipart_upload_info | |
363 | { | |
364 | rgw_placement_rule dest_placement; | |
365 | ||
366 | void encode(bufferlist& bl) const { | |
367 | ENCODE_START(1, 1, bl); | |
368 | encode(dest_placement, bl); | |
369 | ENCODE_FINISH(bl); | |
370 | } | |
371 | ||
372 | void decode(bufferlist::const_iterator& bl) { | |
373 | DECODE_START(1, bl); | |
374 | decode(dest_placement, bl); | |
375 | DECODE_FINISH(bl); | |
376 | } | |
377 | }; | |
378 | WRITE_CLASS_ENCODER(multipart_upload_info) | |
379 | ||
380 | static int get_multipart_info(RGWRados *store, struct req_state *s, | |
381 | const rgw_obj& obj, | |
382 | RGWAccessControlPolicy *policy, | |
383 | map<string, bufferlist> *attrs, | |
384 | multipart_upload_info *upload_info) | |
385 | { | |
386 | bufferlist header; | |
387 | ||
388 | bufferlist headbl; | |
389 | bufferlist *pheadbl = (upload_info ? &headbl : nullptr); | |
390 | ||
391 | int op_ret = get_obj_head(store, s, obj, attrs, pheadbl); | |
392 | if (op_ret < 0) { | |
393 | if (op_ret == -ENOENT) { | |
394 | return -ERR_NO_SUCH_UPLOAD; | |
395 | } | |
396 | return op_ret; | |
397 | } | |
398 | ||
399 | if (upload_info && headbl.length() > 0) { | |
400 | auto hiter = headbl.cbegin(); | |
401 | try { | |
402 | decode(*upload_info, hiter); | |
403 | } catch (buffer::error& err) { | |
404 | ldpp_dout(s, 0) << "ERROR: failed to decode multipart upload info" << dendl; | |
405 | return -EIO; | |
406 | } | |
407 | } | |
408 | ||
409 | if (policy && attrs) { | |
410 | for (auto& iter : *attrs) { | |
411 | string name = iter.first; | |
412 | if (name.compare(RGW_ATTR_ACL) == 0) { | |
413 | bufferlist& bl = iter.second; | |
414 | auto bli = bl.cbegin(); | |
415 | try { | |
416 | decode(*policy, bli); | |
417 | } catch (buffer::error& err) { | |
418 | ldpp_dout(s, 0) << "ERROR: could not decode policy" << dendl; | |
419 | return -EIO; | |
420 | } | |
421 | break; | |
422 | } | |
423 | } | |
424 | } | |
425 | ||
426 | return 0; | |
427 | } | |
428 | ||
429 | static int get_multipart_info(RGWRados *store, struct req_state *s, | |
430 | const string& meta_oid, | |
431 | RGWAccessControlPolicy *policy, | |
432 | map<string, bufferlist> *attrs, | |
433 | multipart_upload_info *upload_info) | |
434 | { | |
435 | map<string, bufferlist>::iterator iter; | |
436 | bufferlist header; | |
437 | ||
438 | rgw_obj meta_obj; | |
439 | meta_obj.init_ns(s->bucket, meta_oid, mp_ns); | |
440 | meta_obj.set_in_extra_data(true); | |
441 | ||
442 | return get_multipart_info(store, s, meta_obj, policy, attrs, upload_info); | |
443 | } | |
444 | ||
445 | static int modify_obj_attr(RGWRados *store, struct req_state *s, const rgw_obj& obj, const char* attr_name, bufferlist& attr_val) | |
7c673cae FG |
446 | { |
447 | map<string, bufferlist> attrs; | |
448 | RGWRados::Object op_target(store, s->bucket_info, *static_cast<RGWObjectCtx *>(s->obj_ctx), obj); | |
449 | RGWRados::Object::Read read_op(&op_target); | |
450 | ||
451 | read_op.params.attrs = &attrs; | |
7c673cae FG |
452 | |
453 | int r = read_op.prepare(); | |
454 | if (r < 0) { | |
455 | return r; | |
456 | } | |
457 | store->set_atomic(s->obj_ctx, read_op.state.obj); | |
458 | attrs[attr_name] = attr_val; | |
459 | return store->set_attrs(s->obj_ctx, s->bucket_info, read_op.state.obj, attrs, NULL); | |
460 | } | |
461 | ||
7c673cae FG |
462 | static int read_bucket_policy(RGWRados *store, |
463 | struct req_state *s, | |
464 | RGWBucketInfo& bucket_info, | |
465 | map<string, bufferlist>& bucket_attrs, | |
466 | RGWAccessControlPolicy *policy, | |
467 | rgw_bucket& bucket) | |
468 | { | |
469 | if (!s->system_request && bucket_info.flags & BUCKET_SUSPENDED) { | |
11fdf7f2 TL |
470 | ldpp_dout(s, 0) << "NOTICE: bucket " << bucket_info.bucket.name |
471 | << " is suspended" << dendl; | |
7c673cae FG |
472 | return -ERR_USER_SUSPENDED; |
473 | } | |
474 | ||
475 | if (bucket.name.empty()) { | |
476 | return 0; | |
477 | } | |
478 | ||
11fdf7f2 | 479 | int ret = rgw_op_get_bucket_policy_from_attr(s->cct, store, bucket_info, bucket_attrs, policy); |
7c673cae FG |
480 | if (ret == -ENOENT) { |
481 | ret = -ERR_NO_SUCH_BUCKET; | |
482 | } | |
483 | ||
484 | return ret; | |
485 | } | |
486 | ||
487 | static int read_obj_policy(RGWRados *store, | |
488 | struct req_state *s, | |
489 | RGWBucketInfo& bucket_info, | |
490 | map<string, bufferlist>& bucket_attrs, | |
31f18b77 | 491 | RGWAccessControlPolicy* acl, |
11fdf7f2 TL |
492 | string *storage_class, |
493 | boost::optional<Policy>& policy, | |
7c673cae FG |
494 | rgw_bucket& bucket, |
495 | rgw_obj_key& object) | |
496 | { | |
497 | string upload_id; | |
498 | upload_id = s->info.args.get("uploadId"); | |
499 | rgw_obj obj; | |
500 | ||
501 | if (!s->system_request && bucket_info.flags & BUCKET_SUSPENDED) { | |
11fdf7f2 TL |
502 | ldpp_dout(s, 0) << "NOTICE: bucket " << bucket_info.bucket.name |
503 | << " is suspended" << dendl; | |
7c673cae FG |
504 | return -ERR_USER_SUSPENDED; |
505 | } | |
506 | ||
507 | if (!upload_id.empty()) { | |
508 | /* multipart upload */ | |
509 | RGWMPObj mp(object.name, upload_id); | |
510 | string oid = mp.get_meta(); | |
511 | obj.init_ns(bucket, oid, mp_ns); | |
512 | obj.set_in_extra_data(true); | |
513 | } else { | |
514 | obj = rgw_obj(bucket, object); | |
515 | } | |
31f18b77 FG |
516 | policy = get_iam_policy_from_attr(s->cct, store, bucket_attrs, bucket.tenant); |
517 | ||
7c673cae FG |
518 | RGWObjectCtx *obj_ctx = static_cast<RGWObjectCtx *>(s->obj_ctx); |
519 | int ret = get_obj_policy_from_attr(s->cct, store, *obj_ctx, | |
11fdf7f2 | 520 | bucket_info, bucket_attrs, acl, storage_class, obj); |
7c673cae FG |
521 | if (ret == -ENOENT) { |
522 | /* object does not exist checking the bucket's ACL to make sure | |
523 | that we send a proper error code */ | |
524 | RGWAccessControlPolicy bucket_policy(s->cct); | |
11fdf7f2 | 525 | ret = rgw_op_get_bucket_policy_from_attr(s->cct, store, bucket_info, bucket_attrs, &bucket_policy); |
7c673cae FG |
526 | if (ret < 0) { |
527 | return ret; | |
528 | } | |
7c673cae FG |
529 | const rgw_user& bucket_owner = bucket_policy.get_owner().get_id(); |
530 | if (bucket_owner.compare(s->user->user_id) != 0 && | |
81eedcae TL |
531 | ! s->auth.identity->is_admin_of(bucket_owner)) { |
532 | if (policy) { | |
533 | auto r = policy->eval(s->env, *s->auth.identity, rgw::IAM::s3ListBucket, ARN(bucket)); | |
534 | if (r == Effect::Allow) | |
535 | return -ENOENT; | |
536 | if (r == Effect::Deny) | |
537 | return -EACCES; | |
538 | } | |
539 | if (! bucket_policy.verify_permission(s, *s->auth.identity, s->perm_mask, RGW_PERM_READ)) | |
540 | ret = -EACCES; | |
541 | else | |
542 | ret = -ENOENT; | |
7c673cae FG |
543 | } else { |
544 | ret = -ENOENT; | |
545 | } | |
546 | } | |
547 | ||
548 | return ret; | |
549 | } | |
550 | ||
551 | /** | |
552 | * Get the AccessControlPolicy for an user, bucket or object off of disk. | |
553 | * s: The req_state to draw information from. | |
554 | * only_bucket: If true, reads the user and bucket ACLs rather than the object ACL. | |
555 | * Returns: 0 on success, -ERR# otherwise. | |
556 | */ | |
557 | int rgw_build_bucket_policies(RGWRados* store, struct req_state* s) | |
558 | { | |
559 | int ret = 0; | |
560 | rgw_obj_key obj; | |
561 | RGWUserInfo bucket_owner_info; | |
11fdf7f2 | 562 | auto obj_ctx = store->svc.sysobj->init_obj_ctx(); |
7c673cae FG |
563 | |
564 | string bi = s->info.args.get(RGW_SYS_PARAM_PREFIX "bucket-instance"); | |
565 | if (!bi.empty()) { | |
566 | ret = rgw_bucket_parse_bucket_instance(bi, &s->bucket_instance_id, &s->bucket_instance_shard_id); | |
567 | if (ret < 0) { | |
568 | return ret; | |
569 | } | |
570 | } | |
571 | ||
572 | if(s->dialect.compare("s3") == 0) { | |
11fdf7f2 | 573 | s->bucket_acl = std::make_unique<RGWAccessControlPolicy_S3>(s->cct); |
7c673cae FG |
574 | } else if(s->dialect.compare("swift") == 0) { |
575 | /* We aren't allocating the account policy for those operations using | |
576 | * the Swift's infrastructure that don't really need req_state::user. | |
577 | * Typical example here is the implementation of /info. */ | |
578 | if (!s->user->user_id.empty()) { | |
11fdf7f2 | 579 | s->user_acl = std::make_unique<RGWAccessControlPolicy_SWIFTAcct>(s->cct); |
7c673cae | 580 | } |
11fdf7f2 | 581 | s->bucket_acl = std::make_unique<RGWAccessControlPolicy_SWIFT>(s->cct); |
7c673cae | 582 | } else { |
11fdf7f2 | 583 | s->bucket_acl = std::make_unique<RGWAccessControlPolicy>(s->cct); |
7c673cae FG |
584 | } |
585 | ||
586 | /* check if copy source is within the current domain */ | |
587 | if (!s->src_bucket_name.empty()) { | |
588 | RGWBucketInfo source_info; | |
589 | ||
590 | if (s->bucket_instance_id.empty()) { | |
591 | ret = store->get_bucket_info(obj_ctx, s->src_tenant_name, s->src_bucket_name, source_info, NULL); | |
592 | } else { | |
593 | ret = store->get_bucket_instance_info(obj_ctx, s->bucket_instance_id, source_info, NULL, NULL); | |
594 | } | |
595 | if (ret == 0) { | |
596 | string& zonegroup = source_info.zonegroup; | |
11fdf7f2 | 597 | s->local_source = store->svc.zone->get_zonegroup().equals(zonegroup); |
7c673cae FG |
598 | } |
599 | } | |
600 | ||
601 | struct { | |
602 | rgw_user uid; | |
603 | std::string display_name; | |
604 | } acct_acl_user = { | |
605 | s->user->user_id, | |
606 | s->user->display_name, | |
607 | }; | |
608 | ||
609 | if (!s->bucket_name.empty()) { | |
610 | s->bucket_exists = true; | |
611 | if (s->bucket_instance_id.empty()) { | |
11fdf7f2 TL |
612 | ret = store->get_bucket_info(obj_ctx, s->bucket_tenant, s->bucket_name, |
613 | s->bucket_info, &s->bucket_mtime, | |
614 | &s->bucket_attrs); | |
7c673cae | 615 | } else { |
11fdf7f2 TL |
616 | ret = store->get_bucket_instance_info(obj_ctx, s->bucket_instance_id, |
617 | s->bucket_info, &s->bucket_mtime, | |
618 | &s->bucket_attrs); | |
7c673cae FG |
619 | } |
620 | if (ret < 0) { | |
621 | if (ret != -ENOENT) { | |
622 | string bucket_log; | |
623 | rgw_make_bucket_entry_name(s->bucket_tenant, s->bucket_name, bucket_log); | |
11fdf7f2 TL |
624 | ldpp_dout(s, 0) << "NOTICE: couldn't get bucket from bucket_name (name=" |
625 | << bucket_log << ")" << dendl; | |
7c673cae FG |
626 | return ret; |
627 | } | |
628 | s->bucket_exists = false; | |
629 | } | |
630 | s->bucket = s->bucket_info.bucket; | |
631 | ||
632 | if (s->bucket_exists) { | |
c07f9fc5 FG |
633 | ret = read_bucket_policy(store, s, s->bucket_info, s->bucket_attrs, |
634 | s->bucket_acl.get(), s->bucket); | |
7c673cae FG |
635 | acct_acl_user = { |
636 | s->bucket_info.owner, | |
637 | s->bucket_acl->get_owner().get_display_name(), | |
638 | }; | |
639 | } else { | |
640 | s->bucket_acl->create_default(s->user->user_id, s->user->display_name); | |
641 | ret = -ERR_NO_SUCH_BUCKET; | |
642 | } | |
643 | ||
644 | s->bucket_owner = s->bucket_acl->get_owner(); | |
645 | ||
646 | RGWZoneGroup zonegroup; | |
11fdf7f2 | 647 | int r = store->svc.zone->get_zonegroup(s->bucket_info.zonegroup, zonegroup); |
7c673cae FG |
648 | if (!r) { |
649 | if (!zonegroup.endpoints.empty()) { | |
650 | s->zonegroup_endpoint = zonegroup.endpoints.front(); | |
651 | } else { | |
652 | // use zonegroup's master zone endpoints | |
653 | auto z = zonegroup.zones.find(zonegroup.master_zone); | |
654 | if (z != zonegroup.zones.end() && !z->second.endpoints.empty()) { | |
655 | s->zonegroup_endpoint = z->second.endpoints.front(); | |
656 | } | |
657 | } | |
658 | s->zonegroup_name = zonegroup.get_name(); | |
659 | } | |
660 | if (r < 0 && ret == 0) { | |
661 | ret = r; | |
662 | } | |
663 | ||
11fdf7f2 TL |
664 | if (s->bucket_exists && !store->svc.zone->get_zonegroup().equals(s->bucket_info.zonegroup)) { |
665 | ldpp_dout(s, 0) << "NOTICE: request for data in a different zonegroup (" | |
666 | << s->bucket_info.zonegroup << " != " | |
667 | << store->svc.zone->get_zonegroup().get_id() << ")" << dendl; | |
7c673cae FG |
668 | /* we now need to make sure that the operation actually requires copy source, that is |
669 | * it's a copy operation | |
670 | */ | |
11fdf7f2 | 671 | if (store->svc.zone->get_zonegroup().is_master_zonegroup() && s->system_request) { |
7c673cae | 672 | /*If this is the master, don't redirect*/ |
3efd9988 FG |
673 | } else if (s->op_type == RGW_OP_GET_BUCKET_LOCATION ) { |
674 | /* If op is get bucket location, don't redirect */ | |
7c673cae FG |
675 | } else if (!s->local_source || |
676 | (s->op != OP_PUT && s->op != OP_COPY) || | |
677 | s->object.empty()) { | |
678 | return -ERR_PERMANENT_REDIRECT; | |
679 | } | |
680 | } | |
11fdf7f2 TL |
681 | |
682 | /* init dest placement -- only if bucket exists, otherwise request is either not relevant, or | |
683 | * it's a create_bucket request, in which case the op will deal with the placement later */ | |
684 | if (s->bucket_exists) { | |
685 | s->dest_placement.storage_class = s->info.storage_class; | |
686 | s->dest_placement.inherit_from(s->bucket_info.placement_rule); | |
687 | ||
688 | if (!store->svc.zone->get_zone_params().valid_placement(s->dest_placement)) { | |
689 | ldpp_dout(s, 0) << "NOTICE: invalid dest placement: " << s->dest_placement.to_str() << dendl; | |
690 | return -EINVAL; | |
691 | } | |
692 | } | |
7c673cae FG |
693 | } |
694 | ||
695 | /* handle user ACL only for those APIs which support it */ | |
696 | if (s->user_acl) { | |
697 | map<string, bufferlist> uattrs; | |
7c673cae FG |
698 | ret = rgw_get_user_attrs_by_uid(store, acct_acl_user.uid, uattrs); |
699 | if (!ret) { | |
700 | ret = get_user_policy_from_attr(s->cct, store, uattrs, *s->user_acl); | |
701 | } | |
702 | if (-ENOENT == ret) { | |
703 | /* In already existing clusters users won't have ACL. In such case | |
704 | * assuming that only account owner has the rights seems to be | |
705 | * reasonable. That allows to have only one verification logic. | |
706 | * NOTE: there is small compatibility kludge for global, empty tenant: | |
707 | * 1. if we try to reach an existing bucket, its owner is considered | |
708 | * as account owner. | |
709 | * 2. otherwise account owner is identity stored in s->user->user_id. */ | |
710 | s->user_acl->create_default(acct_acl_user.uid, | |
711 | acct_acl_user.display_name); | |
712 | ret = 0; | |
11fdf7f2 TL |
713 | } else if (ret < 0) { |
714 | ldpp_dout(s, 0) << "NOTICE: couldn't get user attrs for handling ACL " | |
715 | "(user_id=" << s->user->user_id << ", ret=" << ret << ")" << dendl; | |
7c673cae FG |
716 | return ret; |
717 | } | |
718 | } | |
11fdf7f2 TL |
719 | // We don't need user policies in case of STS token returned by AssumeRole, |
720 | // hence the check for user type | |
721 | if (! s->user->user_id.empty() && s->auth.identity->get_identity_type() != TYPE_ROLE) { | |
722 | try { | |
723 | map<string, bufferlist> uattrs; | |
724 | if (ret = rgw_get_user_attrs_by_uid(store, s->user->user_id, uattrs); ! ret) { | |
725 | if (s->iam_user_policies.empty()) { | |
726 | s->iam_user_policies = get_iam_user_policy_from_attr(s->cct, store, uattrs, s->user->user_id.tenant); | |
727 | } else { | |
728 | // This scenario can happen when a STS token has a policy, then we need to append other user policies | |
729 | // to the existing ones. (e.g. token returned by GetSessionToken) | |
730 | auto user_policies = get_iam_user_policy_from_attr(s->cct, store, uattrs, s->user->user_id.tenant); | |
731 | s->iam_user_policies.insert(s->iam_user_policies.end(), user_policies.begin(), user_policies.end()); | |
732 | } | |
733 | } else { | |
734 | if (ret == -ENOENT) | |
735 | ret = 0; | |
736 | else ret = -EACCES; | |
737 | } | |
738 | } catch (const std::exception& e) { | |
739 | lderr(s->cct) << "Error reading IAM User Policy: " << e.what() << dendl; | |
740 | ret = -EACCES; | |
741 | } | |
742 | } | |
7c673cae | 743 | |
31f18b77 FG |
744 | try { |
745 | s->iam_policy = get_iam_policy_from_attr(s->cct, store, s->bucket_attrs, | |
746 | s->bucket_tenant); | |
747 | } catch (const std::exception& e) { | |
748 | // Really this is a can't happen condition. We parse the policy | |
749 | // when it's given to us, so perhaps we should abort or otherwise | |
750 | // raise bloody murder. | |
11fdf7f2 | 751 | ldpp_dout(s, 0) << "Error reading IAM Policy: " << e.what() << dendl; |
31f18b77 FG |
752 | ret = -EACCES; |
753 | } | |
7c673cae | 754 | |
11fdf7f2 TL |
755 | bool success = store->svc.zone->get_redirect_zone_endpoint(&s->redirect_zone_endpoint); |
756 | if (success) { | |
757 | ldpp_dout(s, 20) << "redirect_zone_endpoint=" << s->redirect_zone_endpoint << dendl; | |
758 | } | |
759 | ||
7c673cae FG |
760 | return ret; |
761 | } | |
762 | ||
763 | /** | |
764 | * Get the AccessControlPolicy for a bucket or object off of disk. | |
765 | * s: The req_state to draw information from. | |
766 | * only_bucket: If true, reads the bucket ACL rather than the object ACL. | |
767 | * Returns: 0 on success, -ERR# otherwise. | |
768 | */ | |
769 | int rgw_build_object_policies(RGWRados *store, struct req_state *s, | |
770 | bool prefetch_data) | |
771 | { | |
772 | int ret = 0; | |
773 | ||
774 | if (!s->object.empty()) { | |
775 | if (!s->bucket_exists) { | |
776 | return -ERR_NO_SUCH_BUCKET; | |
777 | } | |
11fdf7f2 | 778 | s->object_acl = std::make_unique<RGWAccessControlPolicy>(s->cct); |
7c673cae FG |
779 | rgw_obj obj(s->bucket, s->object); |
780 | ||
781 | store->set_atomic(s->obj_ctx, obj); | |
782 | if (prefetch_data) { | |
783 | store->set_prefetch_data(s->obj_ctx, obj); | |
784 | } | |
c07f9fc5 | 785 | ret = read_obj_policy(store, s, s->bucket_info, s->bucket_attrs, |
11fdf7f2 | 786 | s->object_acl.get(), nullptr, s->iam_policy, s->bucket, |
c07f9fc5 | 787 | s->object); |
7c673cae FG |
788 | } |
789 | ||
790 | return ret; | |
791 | } | |
792 | ||
11fdf7f2 TL |
793 | void rgw_add_to_iam_environment(rgw::IAM::Environment& e, std::string_view key, std::string_view val){ |
794 | // This variant just adds non empty key pairs to IAM env., values can be empty | |
795 | // in certain cases like tagging | |
796 | if (!key.empty()) | |
797 | e.emplace(key,val); | |
798 | } | |
799 | ||
800 | static int rgw_iam_add_tags_from_bl(struct req_state* s, bufferlist& bl){ | |
801 | RGWObjTags tagset; | |
802 | try { | |
803 | auto bliter = bl.cbegin(); | |
804 | tagset.decode(bliter); | |
805 | } catch (buffer::error& err) { | |
806 | ldpp_dout(s, 0) << "ERROR: caught buffer::error, couldn't decode TagSet" << dendl; | |
807 | return -EIO; | |
808 | } | |
809 | ||
810 | for (const auto& tag: tagset.get_tags()){ | |
811 | rgw_add_to_iam_environment(s->env, "s3:ExistingObjectTag/" + tag.first, tag.second); | |
812 | } | |
813 | return 0; | |
814 | } | |
815 | ||
816 | static int rgw_iam_add_existing_objtags(RGWRados* store, struct req_state* s, rgw_obj& obj, std::uint64_t action){ | |
817 | map <string, bufferlist> attrs; | |
818 | store->set_atomic(s->obj_ctx, obj); | |
819 | int op_ret = get_obj_attrs(store, s, obj, attrs); | |
820 | if (op_ret < 0) | |
821 | return op_ret; | |
822 | auto tags = attrs.find(RGW_ATTR_TAGS); | |
823 | if (tags != attrs.end()){ | |
824 | return rgw_iam_add_tags_from_bl(s, tags->second); | |
825 | } | |
826 | return 0; | |
827 | } | |
828 | ||
829 | static void rgw_add_grant_to_iam_environment(rgw::IAM::Environment& e, struct req_state *s){ | |
830 | ||
831 | using header_pair_t = std::pair <const char*, const char*>; | |
832 | static const std::initializer_list <header_pair_t> acl_header_conditionals { | |
833 | {"HTTP_X_AMZ_GRANT_READ", "s3:x-amz-grant-read"}, | |
834 | {"HTTP_X_AMZ_GRANT_WRITE", "s3:x-amz-grant-write"}, | |
835 | {"HTTP_X_AMZ_GRANT_READ_ACP", "s3:x-amz-grant-read-acp"}, | |
836 | {"HTTP_X_AMZ_GRANT_WRITE_ACP", "s3:x-amz-grant-write-acp"}, | |
837 | {"HTTP_X_AMZ_GRANT_FULL_CONTROL", "s3:x-amz-grant-full-control"} | |
838 | }; | |
839 | ||
840 | if (s->has_acl_header){ | |
841 | for (const auto& c: acl_header_conditionals){ | |
842 | auto hdr = s->info.env->get(c.first); | |
843 | if(hdr) { | |
844 | e[c.second] = hdr; | |
845 | } | |
846 | } | |
847 | } | |
848 | } | |
849 | ||
850 | void rgw_build_iam_environment(RGWRados* store, | |
851 | struct req_state* s) | |
31f18b77 | 852 | { |
31f18b77 FG |
853 | const auto& m = s->info.env->get_map(); |
854 | auto t = ceph::real_clock::now(); | |
11fdf7f2 TL |
855 | s->env.emplace("aws:CurrentTime", std::to_string(ceph::real_clock::to_time_t(t))); |
856 | s->env.emplace("aws:EpochTime", ceph::to_iso_8601(t)); | |
31f18b77 FG |
857 | // TODO: This is fine for now, but once we have STS we'll need to |
858 | // look and see. Also this won't work with the IdentityApplier | |
859 | // model, since we need to know the actual credential. | |
11fdf7f2 | 860 | s->env.emplace("aws:PrincipalType", "User"); |
31f18b77 FG |
861 | |
862 | auto i = m.find("HTTP_REFERER"); | |
863 | if (i != m.end()) { | |
11fdf7f2 | 864 | s->env.emplace("aws:Referer", i->second); |
31f18b77 FG |
865 | } |
866 | ||
f64942e4 | 867 | if (rgw_transport_is_secure(s->cct, *s->info.env)) { |
11fdf7f2 | 868 | s->env.emplace("aws:SecureTransport", "true"); |
31f18b77 FG |
869 | } |
870 | ||
b32b8144 FG |
871 | const auto remote_addr_param = s->cct->_conf->rgw_remote_addr_param; |
872 | if (remote_addr_param.length()) { | |
873 | i = m.find(remote_addr_param); | |
874 | } else { | |
875 | i = m.find("REMOTE_ADDR"); | |
876 | } | |
31f18b77 | 877 | if (i != m.end()) { |
b32b8144 FG |
878 | const string* ip = &(i->second); |
879 | string temp; | |
880 | if (remote_addr_param == "HTTP_X_FORWARDED_FOR") { | |
881 | const auto comma = ip->find(','); | |
882 | if (comma != string::npos) { | |
883 | temp.assign(*ip, 0, comma); | |
884 | ip = &temp; | |
885 | } | |
886 | } | |
11fdf7f2 | 887 | s->env.emplace("aws:SourceIp", *ip); |
31f18b77 FG |
888 | } |
889 | ||
890 | i = m.find("HTTP_USER_AGENT"); { | |
891 | if (i != m.end()) | |
11fdf7f2 | 892 | s->env.emplace("aws:UserAgent", i->second); |
31f18b77 FG |
893 | } |
894 | ||
895 | if (s->user) { | |
896 | // What to do about aws::userid? One can have multiple access | |
897 | // keys so that isn't really suitable. Do we have a durable | |
898 | // identifier that can persist through name changes? | |
11fdf7f2 TL |
899 | s->env.emplace("aws:username", s->user->user_id.id); |
900 | } | |
901 | ||
902 | i = m.find("HTTP_X_AMZ_SECURITY_TOKEN"); | |
903 | if (i != m.end()) { | |
904 | s->env.emplace("sts:authentication", "true"); | |
905 | } else { | |
906 | s->env.emplace("sts:authentication", "false"); | |
31f18b77 | 907 | } |
31f18b77 FG |
908 | } |
909 | ||
910 | void rgw_bucket_object_pre_exec(struct req_state *s) | |
7c673cae FG |
911 | { |
912 | if (s->expect_cont) | |
913 | dump_continue(s); | |
914 | ||
915 | dump_bucket_from_state(s); | |
916 | } | |
917 | ||
b32b8144 FG |
918 | // So! Now and then when we try to update bucket information, the |
919 | // bucket has changed during the course of the operation. (Or we have | |
920 | // a cache consistency problem that Watch/Notify isn't ruling out | |
921 | // completely.) | |
922 | // | |
923 | // When this happens, we need to update the bucket info and try | |
924 | // again. We have, however, to try the right *part* again. We can't | |
925 | // simply re-send, since that will obliterate the previous update. | |
926 | // | |
927 | // Thus, callers of this function should include everything that | |
928 | // merges information to be changed into the bucket information as | |
929 | // well as the call to set it. | |
930 | // | |
931 | // The called function must return an integer, negative on error. In | |
932 | // general, they should just return op_ret. | |
933 | namespace { | |
934 | template<typename F> | |
935 | int retry_raced_bucket_write(RGWRados* g, req_state* s, const F& f) { | |
936 | auto r = f(); | |
937 | for (auto i = 0u; i < 15u && r == -ECANCELED; ++i) { | |
938 | r = g->try_refresh_bucket_info(s->bucket_info, nullptr, | |
939 | &s->bucket_attrs); | |
940 | if (r >= 0) { | |
941 | r = f(); | |
942 | } | |
943 | } | |
944 | return r; | |
945 | } | |
946 | } | |
947 | ||
948 | ||
7c673cae FG |
949 | int RGWGetObj::verify_permission() |
950 | { | |
951 | obj = rgw_obj(s->bucket, s->object); | |
952 | store->set_atomic(s->obj_ctx, obj); | |
953 | if (get_data) { | |
954 | store->set_prefetch_data(s->obj_ctx, obj); | |
955 | } | |
956 | ||
31f18b77 FG |
957 | if (torrent.get_flag()) { |
958 | if (obj.key.instance.empty()) { | |
959 | action = rgw::IAM::s3GetObjectTorrent; | |
960 | } else { | |
961 | action = rgw::IAM::s3GetObjectVersionTorrent; | |
962 | } | |
963 | } else { | |
964 | if (obj.key.instance.empty()) { | |
965 | action = rgw::IAM::s3GetObject; | |
966 | } else { | |
967 | action = rgw::IAM::s3GetObjectVersion; | |
968 | } | |
11fdf7f2 TL |
969 | if (s->iam_policy && s->iam_policy->has_partial_conditional(S3_EXISTING_OBJTAG)) |
970 | rgw_iam_add_existing_objtags(store, s, obj, action); | |
971 | if (! s->iam_user_policies.empty()) { | |
972 | for (auto& user_policy : s->iam_user_policies) { | |
973 | if (user_policy.has_partial_conditional(S3_EXISTING_OBJTAG)) | |
974 | rgw_iam_add_existing_objtags(store, s, obj, action); | |
975 | } | |
976 | } | |
31f18b77 FG |
977 | } |
978 | ||
11fdf7f2 | 979 | if (!verify_object_permission(this, s, action)) { |
7c673cae FG |
980 | return -EACCES; |
981 | } | |
982 | ||
eafe8130 TL |
983 | if (s->bucket_info.obj_lock_enabled()) { |
984 | get_retention = verify_object_permission(this, s, rgw::IAM::s3GetObjectRetention); | |
985 | get_legal_hold = verify_object_permission(this, s, rgw::IAM::s3GetObjectLegalHold); | |
986 | } | |
987 | ||
7c673cae FG |
988 | return 0; |
989 | } | |
990 | ||
991 | ||
992 | int RGWOp::verify_op_mask() | |
993 | { | |
994 | uint32_t required_mask = op_mask(); | |
995 | ||
11fdf7f2 TL |
996 | ldpp_dout(this, 20) << "required_mask= " << required_mask |
997 | << " user.op_mask=" << s->user->op_mask << dendl; | |
7c673cae FG |
998 | |
999 | if ((s->user->op_mask & required_mask) != required_mask) { | |
1000 | return -EPERM; | |
1001 | } | |
1002 | ||
11fdf7f2 TL |
1003 | if (!s->system_request && (required_mask & RGW_OP_TYPE_MODIFY) && !store->svc.zone->zone_is_writeable()) { |
1004 | ldpp_dout(this, 5) << "NOTICE: modify request to a read-only zone by a " | |
1005 | "non-system user, permission denied" << dendl; | |
7c673cae FG |
1006 | return -EPERM; |
1007 | } | |
1008 | ||
1009 | return 0; | |
1010 | } | |
1011 | ||
224ce89b WB |
1012 | int RGWGetObjTags::verify_permission() |
1013 | { | |
11fdf7f2 TL |
1014 | auto iam_action = s->object.instance.empty()? |
1015 | rgw::IAM::s3GetObjectTagging: | |
1016 | rgw::IAM::s3GetObjectVersionTagging; | |
1017 | // TODO since we are parsing the bl now anyway, we probably change | |
1018 | // the send_response function to accept RGWObjTag instead of a bl | |
1019 | if (s->iam_policy && s->iam_policy->has_partial_conditional(S3_EXISTING_OBJTAG)){ | |
1020 | rgw_obj obj = rgw_obj(s->bucket, s->object); | |
1021 | rgw_iam_add_existing_objtags(store, s, obj, iam_action); | |
1022 | } | |
1023 | if (! s->iam_user_policies.empty()) { | |
1024 | for (auto& user_policy : s->iam_user_policies) { | |
1025 | if (user_policy.has_partial_conditional(S3_EXISTING_OBJTAG)) { | |
1026 | rgw_obj obj = rgw_obj(s->bucket, s->object); | |
1027 | rgw_iam_add_existing_objtags(store, s, obj, iam_action); | |
1028 | } | |
1029 | } | |
1030 | } | |
1031 | if (!verify_object_permission(this, s,iam_action)) | |
224ce89b WB |
1032 | return -EACCES; |
1033 | ||
1034 | return 0; | |
1035 | } | |
1036 | ||
1037 | void RGWGetObjTags::pre_exec() | |
1038 | { | |
1039 | rgw_bucket_object_pre_exec(s); | |
1040 | } | |
1041 | ||
1042 | void RGWGetObjTags::execute() | |
1043 | { | |
1044 | rgw_obj obj; | |
1045 | map<string,bufferlist> attrs; | |
1046 | ||
1047 | obj = rgw_obj(s->bucket, s->object); | |
1048 | ||
1049 | store->set_atomic(s->obj_ctx, obj); | |
1050 | ||
1051 | op_ret = get_obj_attrs(store, s, obj, attrs); | |
b5b8bbf5 | 1052 | if (op_ret < 0) { |
11fdf7f2 TL |
1053 | ldpp_dout(this, 0) << "ERROR: failed to get obj attrs, obj=" << obj |
1054 | << " ret=" << op_ret << dendl; | |
b5b8bbf5 FG |
1055 | return; |
1056 | } | |
1057 | ||
224ce89b WB |
1058 | auto tags = attrs.find(RGW_ATTR_TAGS); |
1059 | if(tags != attrs.end()){ | |
1060 | has_tags = true; | |
1061 | tags_bl.append(tags->second); | |
1062 | } | |
1063 | send_response_data(tags_bl); | |
1064 | } | |
1065 | ||
1066 | int RGWPutObjTags::verify_permission() | |
1067 | { | |
11fdf7f2 TL |
1068 | auto iam_action = s->object.instance.empty() ? |
1069 | rgw::IAM::s3PutObjectTagging: | |
1070 | rgw::IAM::s3PutObjectVersionTagging; | |
1071 | ||
1072 | if(s->iam_policy && s->iam_policy->has_partial_conditional(S3_EXISTING_OBJTAG)){ | |
1073 | auto obj = rgw_obj(s->bucket, s->object); | |
1074 | rgw_iam_add_existing_objtags(store, s, obj, iam_action); | |
1075 | } | |
1076 | if (! s->iam_user_policies.empty()) { | |
1077 | for (auto& user_policy : s->iam_user_policies) { | |
1078 | if (user_policy.has_partial_conditional(S3_EXISTING_OBJTAG)) { | |
1079 | rgw_obj obj = rgw_obj(s->bucket, s->object); | |
1080 | rgw_iam_add_existing_objtags(store, s, obj, iam_action); | |
1081 | } | |
1082 | } | |
1083 | } | |
1084 | if (!verify_object_permission(this, s,iam_action)) | |
224ce89b WB |
1085 | return -EACCES; |
1086 | return 0; | |
1087 | } | |
1088 | ||
1089 | void RGWPutObjTags::execute() | |
1090 | { | |
1091 | op_ret = get_params(); | |
1092 | if (op_ret < 0) | |
1093 | return; | |
1094 | ||
1095 | if (s->object.empty()){ | |
1096 | op_ret= -EINVAL; // we only support tagging on existing objects | |
1097 | return; | |
1098 | } | |
1099 | ||
1100 | rgw_obj obj; | |
1101 | obj = rgw_obj(s->bucket, s->object); | |
1102 | store->set_atomic(s->obj_ctx, obj); | |
1103 | op_ret = modify_obj_attr(store, s, obj, RGW_ATTR_TAGS, tags_bl); | |
1104 | if (op_ret == -ECANCELED){ | |
1105 | op_ret = -ERR_TAG_CONFLICT; | |
1106 | } | |
1107 | } | |
1108 | ||
1109 | void RGWDeleteObjTags::pre_exec() | |
1110 | { | |
1111 | rgw_bucket_object_pre_exec(s); | |
1112 | } | |
1113 | ||
1114 | ||
1115 | int RGWDeleteObjTags::verify_permission() | |
1116 | { | |
1117 | if (!s->object.empty()) { | |
11fdf7f2 TL |
1118 | auto iam_action = s->object.instance.empty() ? |
1119 | rgw::IAM::s3DeleteObjectTagging: | |
1120 | rgw::IAM::s3DeleteObjectVersionTagging; | |
1121 | ||
1122 | if (s->iam_policy && s->iam_policy->has_partial_conditional(S3_EXISTING_OBJTAG)){ | |
1123 | auto obj = rgw_obj(s->bucket, s->object); | |
1124 | rgw_iam_add_existing_objtags(store, s, obj, iam_action); | |
1125 | } | |
1126 | if (! s->iam_user_policies.empty()) { | |
1127 | for (auto& user_policy : s->iam_user_policies) { | |
1128 | if (user_policy.has_partial_conditional(S3_EXISTING_OBJTAG)) { | |
1129 | auto obj = rgw_obj(s->bucket, s->object); | |
1130 | rgw_iam_add_existing_objtags(store, s, obj, iam_action); | |
1131 | } | |
1132 | } | |
1133 | } | |
1134 | if (!verify_object_permission(this, s, iam_action)) | |
224ce89b WB |
1135 | return -EACCES; |
1136 | } | |
1137 | return 0; | |
1138 | } | |
1139 | ||
1140 | void RGWDeleteObjTags::execute() | |
1141 | { | |
1142 | if (s->object.empty()) | |
1143 | return; | |
1144 | ||
1145 | rgw_obj obj; | |
1146 | obj = rgw_obj(s->bucket, s->object); | |
1147 | store->set_atomic(s->obj_ctx, obj); | |
1148 | map <string, bufferlist> attrs; | |
1149 | map <string, bufferlist> rmattr; | |
1150 | bufferlist bl; | |
1151 | rmattr[RGW_ATTR_TAGS] = bl; | |
1152 | op_ret = store->set_attrs(s->obj_ctx, s->bucket_info, obj, attrs, &rmattr); | |
1153 | } | |
1154 | ||
7c673cae FG |
1155 | int RGWOp::do_aws4_auth_completion() |
1156 | { | |
11fdf7f2 | 1157 | ldpp_dout(this, 5) << "NOTICE: call to do_aws4_auth_completion" << dendl; |
31f18b77 FG |
1158 | if (s->auth.completer) { |
1159 | if (!s->auth.completer->complete()) { | |
1160 | return -ERR_AMZ_CONTENT_SHA256_MISMATCH; | |
1161 | } else { | |
11fdf7f2 | 1162 | ldpp_dout(this, 10) << "v4 auth ok -- do_aws4_auth_completion" << dendl; |
7c673cae | 1163 | } |
31f18b77 FG |
1164 | |
1165 | /* TODO(rzarzynski): yes, we're really called twice on PUTs. Only first | |
1166 | * call passes, so we disable second one. This is old behaviour, sorry! | |
1167 | * Plan for tomorrow: seek and destroy. */ | |
1168 | s->auth.completer = nullptr; | |
7c673cae FG |
1169 | } |
1170 | ||
1171 | return 0; | |
1172 | } | |
1173 | ||
1174 | int RGWOp::init_quota() | |
1175 | { | |
1176 | /* no quota enforcement for system requests */ | |
1177 | if (s->system_request) | |
1178 | return 0; | |
1179 | ||
1180 | /* init quota related stuff */ | |
1181 | if (!(s->user->op_mask & RGW_OP_TYPE_MODIFY)) { | |
1182 | return 0; | |
1183 | } | |
1184 | ||
1185 | /* only interested in object related ops */ | |
1186 | if (s->object.empty()) { | |
1187 | return 0; | |
1188 | } | |
1189 | ||
1190 | RGWUserInfo owner_info; | |
1191 | RGWUserInfo *uinfo; | |
1192 | ||
1193 | if (s->user->user_id == s->bucket_owner.get_id()) { | |
1194 | uinfo = s->user; | |
1195 | } else { | |
1196 | int r = rgw_get_user_info_by_uid(store, s->bucket_info.owner, owner_info); | |
1197 | if (r < 0) | |
1198 | return r; | |
1199 | uinfo = &owner_info; | |
1200 | } | |
1201 | ||
1202 | if (s->bucket_info.quota.enabled) { | |
1203 | bucket_quota = s->bucket_info.quota; | |
1204 | } else if (uinfo->bucket_quota.enabled) { | |
1205 | bucket_quota = uinfo->bucket_quota; | |
1206 | } else { | |
11fdf7f2 | 1207 | bucket_quota = store->svc.quota->get_bucket_quota(); |
7c673cae FG |
1208 | } |
1209 | ||
1210 | if (uinfo->user_quota.enabled) { | |
1211 | user_quota = uinfo->user_quota; | |
1212 | } else { | |
11fdf7f2 | 1213 | user_quota = store->svc.quota->get_user_quota(); |
7c673cae FG |
1214 | } |
1215 | ||
1216 | return 0; | |
1217 | } | |
1218 | ||
1219 | static bool validate_cors_rule_method(RGWCORSRule *rule, const char *req_meth) { | |
1220 | uint8_t flags = 0; | |
1221 | ||
1222 | if (!req_meth) { | |
1223 | dout(5) << "req_meth is null" << dendl; | |
1224 | return false; | |
1225 | } | |
1226 | ||
1227 | if (strcmp(req_meth, "GET") == 0) flags = RGW_CORS_GET; | |
1228 | else if (strcmp(req_meth, "POST") == 0) flags = RGW_CORS_POST; | |
1229 | else if (strcmp(req_meth, "PUT") == 0) flags = RGW_CORS_PUT; | |
1230 | else if (strcmp(req_meth, "DELETE") == 0) flags = RGW_CORS_DELETE; | |
1231 | else if (strcmp(req_meth, "HEAD") == 0) flags = RGW_CORS_HEAD; | |
1232 | ||
1adf2230 | 1233 | if (rule->get_allowed_methods() & flags) { |
7c673cae FG |
1234 | dout(10) << "Method " << req_meth << " is supported" << dendl; |
1235 | } else { | |
1236 | dout(5) << "Method " << req_meth << " is not supported" << dendl; | |
1237 | return false; | |
1238 | } | |
1239 | ||
1240 | return true; | |
1241 | } | |
1242 | ||
b32b8144 FG |
1243 | static bool validate_cors_rule_header(RGWCORSRule *rule, const char *req_hdrs) { |
1244 | if (req_hdrs) { | |
1245 | vector<string> hdrs; | |
1246 | get_str_vec(req_hdrs, hdrs); | |
1247 | for (const auto& hdr : hdrs) { | |
1248 | if (!rule->is_header_allowed(hdr.c_str(), hdr.length())) { | |
1249 | dout(5) << "Header " << hdr << " is not registered in this rule" << dendl; | |
1250 | return false; | |
1251 | } | |
1252 | } | |
1253 | } | |
1254 | return true; | |
1255 | } | |
1256 | ||
7c673cae FG |
1257 | int RGWOp::read_bucket_cors() |
1258 | { | |
1259 | bufferlist bl; | |
1260 | ||
1261 | map<string, bufferlist>::iterator aiter = s->bucket_attrs.find(RGW_ATTR_CORS); | |
1262 | if (aiter == s->bucket_attrs.end()) { | |
11fdf7f2 | 1263 | ldpp_dout(this, 20) << "no CORS configuration attr found" << dendl; |
7c673cae FG |
1264 | cors_exist = false; |
1265 | return 0; /* no CORS configuration found */ | |
1266 | } | |
1267 | ||
1268 | cors_exist = true; | |
1269 | ||
1270 | bl = aiter->second; | |
1271 | ||
11fdf7f2 | 1272 | auto iter = bl.cbegin(); |
7c673cae FG |
1273 | try { |
1274 | bucket_cors.decode(iter); | |
1275 | } catch (buffer::error& err) { | |
11fdf7f2 | 1276 | ldpp_dout(this, 0) << "ERROR: could not decode policy, caught buffer::error" << dendl; |
7c673cae FG |
1277 | return -EIO; |
1278 | } | |
11fdf7f2 | 1279 | if (s->cct->_conf->subsys.should_gather<ceph_subsys_rgw, 15>()) { |
7c673cae | 1280 | RGWCORSConfiguration_S3 *s3cors = static_cast<RGWCORSConfiguration_S3 *>(&bucket_cors); |
11fdf7f2 | 1281 | ldpp_dout(this, 15) << "Read RGWCORSConfiguration"; |
7c673cae FG |
1282 | s3cors->to_xml(*_dout); |
1283 | *_dout << dendl; | |
1284 | } | |
1285 | return 0; | |
1286 | } | |
1287 | ||
1288 | /** CORS 6.2.6. | |
1289 | * If any of the header field-names is not a ASCII case-insensitive match for | |
1290 | * any of the values in list of headers do not set any additional headers and | |
1291 | * terminate this set of steps. | |
1292 | * */ | |
1293 | static void get_cors_response_headers(RGWCORSRule *rule, const char *req_hdrs, string& hdrs, string& exp_hdrs, unsigned *max_age) { | |
1294 | if (req_hdrs) { | |
1295 | list<string> hl; | |
1296 | get_str_list(req_hdrs, hl); | |
1297 | for(list<string>::iterator it = hl.begin(); it != hl.end(); ++it) { | |
1298 | if (!rule->is_header_allowed((*it).c_str(), (*it).length())) { | |
1299 | dout(5) << "Header " << (*it) << " is not registered in this rule" << dendl; | |
1300 | } else { | |
1301 | if (hdrs.length() > 0) hdrs.append(","); | |
1302 | hdrs.append((*it)); | |
1303 | } | |
1304 | } | |
1305 | } | |
1306 | rule->format_exp_headers(exp_hdrs); | |
1307 | *max_age = rule->get_max_age(); | |
1308 | } | |
1309 | ||
1310 | /** | |
1311 | * Generate the CORS header response | |
1312 | * | |
1313 | * This is described in the CORS standard, section 6.2. | |
1314 | */ | |
1315 | bool RGWOp::generate_cors_headers(string& origin, string& method, string& headers, string& exp_headers, unsigned *max_age) | |
1316 | { | |
1317 | /* CORS 6.2.1. */ | |
1318 | const char *orig = s->info.env->get("HTTP_ORIGIN"); | |
1319 | if (!orig) { | |
1320 | return false; | |
1321 | } | |
1322 | ||
1323 | /* Custom: */ | |
1324 | origin = orig; | |
1325 | op_ret = read_bucket_cors(); | |
1326 | if (op_ret < 0) { | |
1327 | return false; | |
1328 | } | |
1329 | ||
1330 | if (!cors_exist) { | |
11fdf7f2 | 1331 | ldpp_dout(this, 2) << "No CORS configuration set yet for this bucket" << dendl; |
7c673cae FG |
1332 | return false; |
1333 | } | |
1334 | ||
1335 | /* CORS 6.2.2. */ | |
1336 | RGWCORSRule *rule = bucket_cors.host_name_rule(orig); | |
1337 | if (!rule) | |
1338 | return false; | |
1339 | ||
1340 | /* | |
1341 | * Set the Allowed-Origin header to a asterisk if this is allowed in the rule | |
1342 | * and no Authorization was send by the client | |
1343 | * | |
1344 | * The origin parameter specifies a URI that may access the resource. The browser must enforce this. | |
1345 | * For requests without credentials, the server may specify "*" as a wildcard, | |
1346 | * thereby allowing any origin to access the resource. | |
1347 | */ | |
1348 | const char *authorization = s->info.env->get("HTTP_AUTHORIZATION"); | |
1349 | if (!authorization && rule->has_wildcard_origin()) | |
1350 | origin = "*"; | |
1351 | ||
1352 | /* CORS 6.2.3. */ | |
1353 | const char *req_meth = s->info.env->get("HTTP_ACCESS_CONTROL_REQUEST_METHOD"); | |
1354 | if (!req_meth) { | |
1355 | req_meth = s->info.method; | |
1356 | } | |
1357 | ||
1358 | if (req_meth) { | |
1359 | method = req_meth; | |
1360 | /* CORS 6.2.5. */ | |
1361 | if (!validate_cors_rule_method(rule, req_meth)) { | |
1362 | return false; | |
1363 | } | |
1364 | } | |
1365 | ||
1366 | /* CORS 6.2.4. */ | |
1367 | const char *req_hdrs = s->info.env->get("HTTP_ACCESS_CONTROL_REQUEST_HEADERS"); | |
1368 | ||
1369 | /* CORS 6.2.6. */ | |
1370 | get_cors_response_headers(rule, req_hdrs, headers, exp_headers, max_age); | |
1371 | ||
1372 | return true; | |
1373 | } | |
1374 | ||
1375 | int RGWGetObj::read_user_manifest_part(rgw_bucket& bucket, | |
1376 | const rgw_bucket_dir_entry& ent, | |
31f18b77 | 1377 | RGWAccessControlPolicy * const bucket_acl, |
11fdf7f2 | 1378 | const boost::optional<Policy>& bucket_policy, |
7c673cae | 1379 | const off_t start_ofs, |
11fdf7f2 TL |
1380 | const off_t end_ofs, |
1381 | bool swift_slo) | |
7c673cae | 1382 | { |
11fdf7f2 TL |
1383 | ldpp_dout(this, 20) << "user manifest obj=" << ent.key.name |
1384 | << "[" << ent.key.instance << "]" << dendl; | |
7c673cae | 1385 | RGWGetObj_CB cb(this); |
11fdf7f2 | 1386 | RGWGetObj_Filter* filter = &cb; |
7c673cae FG |
1387 | boost::optional<RGWGetObj_Decompress> decompress; |
1388 | ||
1389 | int64_t cur_ofs = start_ofs; | |
1390 | int64_t cur_end = end_ofs; | |
1391 | ||
1392 | rgw_obj part(bucket, ent.key); | |
1393 | ||
1394 | map<string, bufferlist> attrs; | |
1395 | ||
1396 | uint64_t obj_size; | |
1397 | RGWObjectCtx obj_ctx(store); | |
1398 | RGWAccessControlPolicy obj_policy(s->cct); | |
1399 | ||
11fdf7f2 TL |
1400 | ldpp_dout(this, 20) << "reading obj=" << part << " ofs=" << cur_ofs |
1401 | << " end=" << cur_end << dendl; | |
7c673cae | 1402 | |
11fdf7f2 | 1403 | obj_ctx.set_atomic(part); |
7c673cae FG |
1404 | store->set_prefetch_data(&obj_ctx, part); |
1405 | ||
1406 | RGWRados::Object op_target(store, s->bucket_info, obj_ctx, part); | |
1407 | RGWRados::Object::Read read_op(&op_target); | |
1408 | ||
11fdf7f2 TL |
1409 | if (!swift_slo) { |
1410 | /* SLO etag is optional */ | |
1411 | read_op.conds.if_match = ent.meta.etag.c_str(); | |
1412 | } | |
7c673cae FG |
1413 | read_op.params.attrs = &attrs; |
1414 | read_op.params.obj_size = &obj_size; | |
7c673cae FG |
1415 | |
1416 | op_ret = read_op.prepare(); | |
1417 | if (op_ret < 0) | |
1418 | return op_ret; | |
224ce89b | 1419 | op_ret = read_op.range_to_ofs(ent.meta.accounted_size, cur_ofs, cur_end); |
7c673cae FG |
1420 | if (op_ret < 0) |
1421 | return op_ret; | |
1422 | bool need_decompress; | |
1423 | op_ret = rgw_compression_info_from_attrset(attrs, need_decompress, cs_info); | |
1424 | if (op_ret < 0) { | |
11fdf7f2 TL |
1425 | ldpp_dout(this, 0) << "ERROR: failed to decode compression info" << dendl; |
1426 | return -EIO; | |
7c673cae FG |
1427 | } |
1428 | ||
1429 | if (need_decompress) | |
1430 | { | |
224ce89b | 1431 | if (cs_info.orig_size != ent.meta.accounted_size) { |
7c673cae | 1432 | // hmm.. something wrong, object not as expected, abort! |
11fdf7f2 TL |
1433 | ldpp_dout(this, 0) << "ERROR: expected cs_info.orig_size=" << cs_info.orig_size |
1434 | << ", actual read size=" << ent.meta.size << dendl; | |
7c673cae FG |
1435 | return -EIO; |
1436 | } | |
1437 | decompress.emplace(s->cct, &cs_info, partial_content, filter); | |
1438 | filter = &*decompress; | |
1439 | } | |
1440 | else | |
1441 | { | |
1442 | if (obj_size != ent.meta.size) { | |
1443 | // hmm.. something wrong, object not as expected, abort! | |
11fdf7f2 TL |
1444 | ldpp_dout(this, 0) << "ERROR: expected obj_size=" << obj_size |
1445 | << ", actual read size=" << ent.meta.size << dendl; | |
7c673cae FG |
1446 | return -EIO; |
1447 | } | |
1448 | } | |
1449 | ||
1450 | op_ret = rgw_policy_from_attrset(s->cct, attrs, &obj_policy); | |
1451 | if (op_ret < 0) | |
1452 | return op_ret; | |
1453 | ||
1454 | /* We can use global user_acl because LOs cannot have segments | |
1455 | * stored inside different accounts. */ | |
1456 | if (s->system_request) { | |
11fdf7f2 | 1457 | ldpp_dout(this, 2) << "overriding permissions due to system operation" << dendl; |
7c673cae | 1458 | } else if (s->auth.identity->is_admin_of(s->user->user_id)) { |
11fdf7f2 TL |
1459 | ldpp_dout(this, 2) << "overriding permissions due to admin operation" << dendl; |
1460 | } else if (!verify_object_permission(this, s, part, s->user_acl.get(), bucket_acl, | |
1461 | &obj_policy, bucket_policy, s->iam_user_policies, action)) { | |
7c673cae FG |
1462 | return -EPERM; |
1463 | } | |
7c673cae FG |
1464 | if (ent.meta.size == 0) { |
1465 | return 0; | |
1466 | } | |
1467 | ||
1468 | perfcounter->inc(l_rgw_get_b, cur_end - cur_ofs); | |
1469 | filter->fixup_range(cur_ofs, cur_end); | |
1470 | op_ret = read_op.iterate(cur_ofs, cur_end, filter); | |
1471 | if (op_ret >= 0) | |
1472 | op_ret = filter->flush(); | |
1473 | return op_ret; | |
1474 | } | |
1475 | ||
1476 | static int iterate_user_manifest_parts(CephContext * const cct, | |
1477 | RGWRados * const store, | |
1478 | const off_t ofs, | |
1479 | const off_t end, | |
1480 | RGWBucketInfo *pbucket_info, | |
1481 | const string& obj_prefix, | |
31f18b77 | 1482 | RGWAccessControlPolicy * const bucket_acl, |
11fdf7f2 | 1483 | const boost::optional<Policy>& bucket_policy, |
7c673cae FG |
1484 | uint64_t * const ptotal_len, |
1485 | uint64_t * const pobj_size, | |
1486 | string * const pobj_sum, | |
1487 | int (*cb)(rgw_bucket& bucket, | |
1488 | const rgw_bucket_dir_entry& ent, | |
31f18b77 | 1489 | RGWAccessControlPolicy * const bucket_acl, |
11fdf7f2 | 1490 | const boost::optional<Policy>& bucket_policy, |
7c673cae FG |
1491 | off_t start_ofs, |
1492 | off_t end_ofs, | |
11fdf7f2 TL |
1493 | void *param, |
1494 | bool swift_slo), | |
7c673cae FG |
1495 | void * const cb_param) |
1496 | { | |
1497 | rgw_bucket& bucket = pbucket_info->bucket; | |
1498 | uint64_t obj_ofs = 0, len_count = 0; | |
1499 | bool found_start = false, found_end = false, handled_end = false; | |
1500 | string delim; | |
1501 | bool is_truncated; | |
1502 | vector<rgw_bucket_dir_entry> objs; | |
1503 | ||
1504 | utime_t start_time = ceph_clock_now(); | |
1505 | ||
1506 | RGWRados::Bucket target(store, *pbucket_info); | |
1507 | RGWRados::Bucket::List list_op(&target); | |
1508 | ||
1509 | list_op.params.prefix = obj_prefix; | |
1510 | list_op.params.delim = delim; | |
1511 | ||
1512 | MD5 etag_sum; | |
1513 | do { | |
1514 | #define MAX_LIST_OBJS 100 | |
1515 | int r = list_op.list_objects(MAX_LIST_OBJS, &objs, NULL, &is_truncated); | |
1516 | if (r < 0) { | |
1517 | return r; | |
1518 | } | |
1519 | ||
1520 | for (rgw_bucket_dir_entry& ent : objs) { | |
224ce89b WB |
1521 | const uint64_t cur_total_len = obj_ofs; |
1522 | const uint64_t obj_size = ent.meta.accounted_size; | |
1523 | uint64_t start_ofs = 0, end_ofs = obj_size; | |
7c673cae | 1524 | |
224ce89b | 1525 | if ((ptotal_len || cb) && !found_start && cur_total_len + obj_size > (uint64_t)ofs) { |
7c673cae FG |
1526 | start_ofs = ofs - obj_ofs; |
1527 | found_start = true; | |
1528 | } | |
1529 | ||
224ce89b | 1530 | obj_ofs += obj_size; |
7c673cae | 1531 | if (pobj_sum) { |
11fdf7f2 | 1532 | etag_sum.Update((const unsigned char *)ent.meta.etag.c_str(), |
7c673cae FG |
1533 | ent.meta.etag.length()); |
1534 | } | |
1535 | ||
31f18b77 | 1536 | if ((ptotal_len || cb) && !found_end && obj_ofs > (uint64_t)end) { |
7c673cae FG |
1537 | end_ofs = end - cur_total_len + 1; |
1538 | found_end = true; | |
1539 | } | |
1540 | ||
1541 | perfcounter->tinc(l_rgw_get_lat, | |
1542 | (ceph_clock_now() - start_time)); | |
1543 | ||
1544 | if (found_start && !handled_end) { | |
1545 | len_count += end_ofs - start_ofs; | |
1546 | ||
1547 | if (cb) { | |
11fdf7f2 TL |
1548 | r = cb(bucket, ent, bucket_acl, bucket_policy, start_ofs, end_ofs, |
1549 | cb_param, false /* swift_slo */); | |
7c673cae FG |
1550 | if (r < 0) { |
1551 | return r; | |
1552 | } | |
1553 | } | |
1554 | } | |
1555 | ||
1556 | handled_end = found_end; | |
1557 | start_time = ceph_clock_now(); | |
1558 | } | |
1559 | } while (is_truncated); | |
1560 | ||
1561 | if (ptotal_len) { | |
1562 | *ptotal_len = len_count; | |
1563 | } | |
1564 | if (pobj_size) { | |
1565 | *pobj_size = obj_ofs; | |
1566 | } | |
1567 | if (pobj_sum) { | |
1568 | complete_etag(etag_sum, pobj_sum); | |
1569 | } | |
1570 | ||
1571 | return 0; | |
1572 | } | |
1573 | ||
1574 | struct rgw_slo_part { | |
31f18b77 FG |
1575 | RGWAccessControlPolicy *bucket_acl = nullptr; |
1576 | Policy* bucket_policy = nullptr; | |
7c673cae FG |
1577 | rgw_bucket bucket; |
1578 | string obj_name; | |
31f18b77 | 1579 | uint64_t size = 0; |
7c673cae | 1580 | string etag; |
7c673cae FG |
1581 | }; |
1582 | ||
1583 | static int iterate_slo_parts(CephContext *cct, | |
1584 | RGWRados *store, | |
1585 | off_t ofs, | |
1586 | off_t end, | |
1587 | map<uint64_t, rgw_slo_part>& slo_parts, | |
1588 | int (*cb)(rgw_bucket& bucket, | |
1589 | const rgw_bucket_dir_entry& ent, | |
31f18b77 | 1590 | RGWAccessControlPolicy *bucket_acl, |
11fdf7f2 | 1591 | const boost::optional<Policy>& bucket_policy, |
7c673cae FG |
1592 | off_t start_ofs, |
1593 | off_t end_ofs, | |
11fdf7f2 TL |
1594 | void *param, |
1595 | bool swift_slo), | |
7c673cae FG |
1596 | void *cb_param) |
1597 | { | |
1598 | bool found_start = false, found_end = false; | |
1599 | ||
1600 | if (slo_parts.empty()) { | |
1601 | return 0; | |
1602 | } | |
1603 | ||
1604 | utime_t start_time = ceph_clock_now(); | |
1605 | ||
1606 | map<uint64_t, rgw_slo_part>::iterator iter = slo_parts.upper_bound(ofs); | |
1607 | if (iter != slo_parts.begin()) { | |
1608 | --iter; | |
1609 | } | |
1610 | ||
1611 | uint64_t obj_ofs = iter->first; | |
1612 | ||
1613 | for (; iter != slo_parts.end() && !found_end; ++iter) { | |
1614 | rgw_slo_part& part = iter->second; | |
1615 | rgw_bucket_dir_entry ent; | |
1616 | ||
1617 | ent.key.name = part.obj_name; | |
224ce89b | 1618 | ent.meta.accounted_size = ent.meta.size = part.size; |
7c673cae FG |
1619 | ent.meta.etag = part.etag; |
1620 | ||
1621 | uint64_t cur_total_len = obj_ofs; | |
eafe8130 | 1622 | uint64_t start_ofs = 0, end_ofs = ent.meta.size - 1; |
7c673cae FG |
1623 | |
1624 | if (!found_start && cur_total_len + ent.meta.size > (uint64_t)ofs) { | |
1625 | start_ofs = ofs - obj_ofs; | |
1626 | found_start = true; | |
1627 | } | |
1628 | ||
1629 | obj_ofs += ent.meta.size; | |
1630 | ||
1631 | if (!found_end && obj_ofs > (uint64_t)end) { | |
eafe8130 | 1632 | end_ofs = end - cur_total_len; |
7c673cae FG |
1633 | found_end = true; |
1634 | } | |
1635 | ||
1636 | perfcounter->tinc(l_rgw_get_lat, | |
1637 | (ceph_clock_now() - start_time)); | |
1638 | ||
1639 | if (found_start) { | |
1640 | if (cb) { | |
eafe8130 TL |
1641 | dout(20) << "iterate_slo_parts()" |
1642 | << " obj=" << part.obj_name | |
1643 | << " start_ofs=" << start_ofs | |
1644 | << " end_ofs=" << end_ofs | |
1645 | << dendl; | |
1646 | ||
31f18b77 FG |
1647 | // SLO is a Swift thing, and Swift has no knowledge of S3 Policies. |
1648 | int r = cb(part.bucket, ent, part.bucket_acl, | |
1649 | (part.bucket_policy ? | |
11fdf7f2 TL |
1650 | boost::optional<Policy>(*part.bucket_policy) : none), |
1651 | start_ofs, end_ofs, cb_param, true /* swift_slo */); | |
31f18b77 | 1652 | if (r < 0) |
7c673cae FG |
1653 | return r; |
1654 | } | |
1655 | } | |
1656 | ||
1657 | start_time = ceph_clock_now(); | |
1658 | } | |
1659 | ||
1660 | return 0; | |
1661 | } | |
1662 | ||
1663 | static int get_obj_user_manifest_iterate_cb(rgw_bucket& bucket, | |
1664 | const rgw_bucket_dir_entry& ent, | |
31f18b77 | 1665 | RGWAccessControlPolicy * const bucket_acl, |
11fdf7f2 | 1666 | const boost::optional<Policy>& bucket_policy, |
7c673cae FG |
1667 | const off_t start_ofs, |
1668 | const off_t end_ofs, | |
11fdf7f2 TL |
1669 | void * const param, |
1670 | bool swift_slo = false) | |
7c673cae FG |
1671 | { |
1672 | RGWGetObj *op = static_cast<RGWGetObj *>(param); | |
11fdf7f2 TL |
1673 | return op->read_user_manifest_part( |
1674 | bucket, ent, bucket_acl, bucket_policy, start_ofs, end_ofs, swift_slo); | |
7c673cae FG |
1675 | } |
1676 | ||
1677 | int RGWGetObj::handle_user_manifest(const char *prefix) | |
1678 | { | |
31f18b77 | 1679 | const boost::string_view prefix_view(prefix); |
11fdf7f2 | 1680 | ldpp_dout(this, 2) << "RGWGetObj::handle_user_manifest() prefix=" |
31f18b77 | 1681 | << prefix_view << dendl; |
7c673cae | 1682 | |
31f18b77 FG |
1683 | const size_t pos = prefix_view.find('/'); |
1684 | if (pos == string::npos) { | |
7c673cae | 1685 | return -EINVAL; |
31f18b77 | 1686 | } |
7c673cae | 1687 | |
31f18b77 FG |
1688 | const std::string bucket_name = url_decode(prefix_view.substr(0, pos)); |
1689 | const std::string obj_prefix = url_decode(prefix_view.substr(pos + 1)); | |
7c673cae FG |
1690 | |
1691 | rgw_bucket bucket; | |
1692 | ||
31f18b77 FG |
1693 | RGWAccessControlPolicy _bucket_acl(s->cct); |
1694 | RGWAccessControlPolicy *bucket_acl; | |
11fdf7f2 TL |
1695 | boost::optional<Policy> _bucket_policy; |
1696 | boost::optional<Policy>* bucket_policy; | |
7c673cae FG |
1697 | RGWBucketInfo bucket_info; |
1698 | RGWBucketInfo *pbucket_info; | |
1699 | ||
1700 | if (bucket_name.compare(s->bucket.name) != 0) { | |
1701 | map<string, bufferlist> bucket_attrs; | |
11fdf7f2 | 1702 | auto obj_ctx = store->svc.sysobj->init_obj_ctx(); |
7c673cae FG |
1703 | int r = store->get_bucket_info(obj_ctx, s->user->user_id.tenant, |
1704 | bucket_name, bucket_info, NULL, | |
1705 | &bucket_attrs); | |
1706 | if (r < 0) { | |
11fdf7f2 | 1707 | ldpp_dout(this, 0) << "could not get bucket info for bucket=" |
7c673cae FG |
1708 | << bucket_name << dendl; |
1709 | return r; | |
1710 | } | |
1711 | bucket = bucket_info.bucket; | |
1712 | pbucket_info = &bucket_info; | |
31f18b77 FG |
1713 | bucket_acl = &_bucket_acl; |
1714 | r = read_bucket_policy(store, s, bucket_info, bucket_attrs, bucket_acl, bucket); | |
7c673cae | 1715 | if (r < 0) { |
11fdf7f2 | 1716 | ldpp_dout(this, 0) << "failed to read bucket policy" << dendl; |
7c673cae FG |
1717 | return r; |
1718 | } | |
31f18b77 FG |
1719 | _bucket_policy = get_iam_policy_from_attr(s->cct, store, bucket_attrs, |
1720 | bucket_info.bucket.tenant); | |
1721 | bucket_policy = &_bucket_policy; | |
7c673cae FG |
1722 | } else { |
1723 | bucket = s->bucket; | |
1724 | pbucket_info = &s->bucket_info; | |
c07f9fc5 | 1725 | bucket_acl = s->bucket_acl.get(); |
31f18b77 | 1726 | bucket_policy = &s->iam_policy; |
7c673cae FG |
1727 | } |
1728 | ||
1729 | /* dry run to find out: | |
1730 | * - total length (of the parts we are going to send to client), | |
1731 | * - overall DLO's content size, | |
1732 | * - md5 sum of overall DLO's content (for etag of Swift API). */ | |
1733 | int r = iterate_user_manifest_parts(s->cct, store, ofs, end, | |
31f18b77 FG |
1734 | pbucket_info, obj_prefix, bucket_acl, *bucket_policy, |
1735 | nullptr, &s->obj_size, &lo_etag, | |
7c673cae FG |
1736 | nullptr /* cb */, nullptr /* cb arg */); |
1737 | if (r < 0) { | |
1738 | return r; | |
1739 | } | |
1740 | ||
31f18b77 FG |
1741 | r = RGWRados::Object::Read::range_to_ofs(s->obj_size, ofs, end); |
1742 | if (r < 0) { | |
1743 | return r; | |
1744 | } | |
1745 | ||
1746 | r = iterate_user_manifest_parts(s->cct, store, ofs, end, | |
1747 | pbucket_info, obj_prefix, bucket_acl, *bucket_policy, | |
1748 | &total_len, nullptr, nullptr, | |
1749 | nullptr, nullptr); | |
1750 | if (r < 0) { | |
1751 | return r; | |
1752 | } | |
1753 | ||
7c673cae FG |
1754 | if (!get_data) { |
1755 | bufferlist bl; | |
1756 | send_response_data(bl, 0, 0); | |
1757 | return 0; | |
1758 | } | |
1759 | ||
1760 | r = iterate_user_manifest_parts(s->cct, store, ofs, end, | |
31f18b77 | 1761 | pbucket_info, obj_prefix, bucket_acl, *bucket_policy, |
7c673cae FG |
1762 | nullptr, nullptr, nullptr, |
1763 | get_obj_user_manifest_iterate_cb, (void *)this); | |
1764 | if (r < 0) { | |
1765 | return r; | |
1766 | } | |
1767 | ||
1768 | if (!total_len) { | |
1769 | bufferlist bl; | |
1770 | send_response_data(bl, 0, 0); | |
1771 | } | |
1772 | ||
1773 | return 0; | |
1774 | } | |
1775 | ||
1776 | int RGWGetObj::handle_slo_manifest(bufferlist& bl) | |
1777 | { | |
1778 | RGWSLOInfo slo_info; | |
11fdf7f2 | 1779 | auto bliter = bl.cbegin(); |
7c673cae | 1780 | try { |
11fdf7f2 | 1781 | decode(slo_info, bliter); |
7c673cae | 1782 | } catch (buffer::error& err) { |
11fdf7f2 | 1783 | ldpp_dout(this, 0) << "ERROR: failed to decode slo manifest" << dendl; |
7c673cae FG |
1784 | return -EIO; |
1785 | } | |
11fdf7f2 | 1786 | ldpp_dout(this, 2) << "RGWGetObj::handle_slo_manifest()" << dendl; |
7c673cae | 1787 | |
31f18b77 | 1788 | vector<RGWAccessControlPolicy> allocated_acls; |
11fdf7f2 | 1789 | map<string, pair<RGWAccessControlPolicy *, boost::optional<Policy>>> policies; |
7c673cae FG |
1790 | map<string, rgw_bucket> buckets; |
1791 | ||
1792 | map<uint64_t, rgw_slo_part> slo_parts; | |
1793 | ||
1794 | MD5 etag_sum; | |
1795 | total_len = 0; | |
1796 | ||
1797 | for (const auto& entry : slo_info.entries) { | |
1798 | const string& path = entry.path; | |
1799 | ||
1800 | /* If the path starts with slashes, strip them all. */ | |
1801 | const size_t pos_init = path.find_first_not_of('/'); | |
1802 | /* According to the documentation of std::string::find following check | |
1803 | * is not necessary as we should get the std::string::npos propagation | |
1804 | * here. This might be true with the accuracy to implementation's bugs. | |
1805 | * See following question on SO: | |
1806 | * http://stackoverflow.com/questions/1011790/why-does-stdstring-findtext-stdstringnpos-not-return-npos | |
1807 | */ | |
1808 | if (pos_init == string::npos) { | |
1809 | return -EINVAL; | |
1810 | } | |
1811 | ||
1812 | const size_t pos_sep = path.find('/', pos_init); | |
1813 | if (pos_sep == string::npos) { | |
1814 | return -EINVAL; | |
1815 | } | |
1816 | ||
1817 | string bucket_name = path.substr(pos_init, pos_sep - pos_init); | |
1818 | string obj_name = path.substr(pos_sep + 1); | |
1819 | ||
1820 | rgw_bucket bucket; | |
31f18b77 FG |
1821 | RGWAccessControlPolicy *bucket_acl; |
1822 | Policy* bucket_policy; | |
7c673cae FG |
1823 | |
1824 | if (bucket_name.compare(s->bucket.name) != 0) { | |
1825 | const auto& piter = policies.find(bucket_name); | |
1826 | if (piter != policies.end()) { | |
31f18b77 FG |
1827 | bucket_acl = piter->second.first; |
1828 | bucket_policy = piter->second.second.get_ptr(); | |
1829 | bucket = buckets[bucket_name]; | |
7c673cae | 1830 | } else { |
31f18b77 FG |
1831 | allocated_acls.push_back(RGWAccessControlPolicy(s->cct)); |
1832 | RGWAccessControlPolicy& _bucket_acl = allocated_acls.back(); | |
7c673cae FG |
1833 | |
1834 | RGWBucketInfo bucket_info; | |
1835 | map<string, bufferlist> bucket_attrs; | |
11fdf7f2 | 1836 | auto obj_ctx = store->svc.sysobj->init_obj_ctx(); |
7c673cae FG |
1837 | int r = store->get_bucket_info(obj_ctx, s->user->user_id.tenant, |
1838 | bucket_name, bucket_info, nullptr, | |
1839 | &bucket_attrs); | |
1840 | if (r < 0) { | |
11fdf7f2 | 1841 | ldpp_dout(this, 0) << "could not get bucket info for bucket=" |
7c673cae FG |
1842 | << bucket_name << dendl; |
1843 | return r; | |
1844 | } | |
1845 | bucket = bucket_info.bucket; | |
31f18b77 FG |
1846 | bucket_acl = &_bucket_acl; |
1847 | r = read_bucket_policy(store, s, bucket_info, bucket_attrs, bucket_acl, | |
7c673cae FG |
1848 | bucket); |
1849 | if (r < 0) { | |
11fdf7f2 | 1850 | ldpp_dout(this, 0) << "failed to read bucket ACL for bucket " |
7c673cae FG |
1851 | << bucket << dendl; |
1852 | return r; | |
31f18b77 FG |
1853 | } |
1854 | auto _bucket_policy = get_iam_policy_from_attr( | |
1855 | s->cct, store, bucket_attrs, bucket_info.bucket.tenant); | |
1856 | bucket_policy = _bucket_policy.get_ptr(); | |
1857 | buckets[bucket_name] = bucket; | |
1858 | policies[bucket_name] = make_pair(bucket_acl, _bucket_policy); | |
7c673cae FG |
1859 | } |
1860 | } else { | |
1861 | bucket = s->bucket; | |
c07f9fc5 | 1862 | bucket_acl = s->bucket_acl.get(); |
31f18b77 | 1863 | bucket_policy = s->iam_policy.get_ptr(); |
7c673cae FG |
1864 | } |
1865 | ||
1866 | rgw_slo_part part; | |
31f18b77 | 1867 | part.bucket_acl = bucket_acl; |
7c673cae FG |
1868 | part.bucket_policy = bucket_policy; |
1869 | part.bucket = bucket; | |
1870 | part.obj_name = obj_name; | |
1871 | part.size = entry.size_bytes; | |
1872 | part.etag = entry.etag; | |
eafe8130 | 1873 | ldpp_dout(this, 20) << "slo_part: bucket=" << part.bucket |
7c673cae FG |
1874 | << " obj=" << part.obj_name |
1875 | << " size=" << part.size | |
1876 | << " etag=" << part.etag | |
1877 | << dendl; | |
1878 | ||
11fdf7f2 | 1879 | etag_sum.Update((const unsigned char *)entry.etag.c_str(), |
7c673cae FG |
1880 | entry.etag.length()); |
1881 | ||
1882 | slo_parts[total_len] = part; | |
1883 | total_len += part.size; | |
11fdf7f2 | 1884 | } /* foreach entry */ |
7c673cae FG |
1885 | |
1886 | complete_etag(etag_sum, &lo_etag); | |
1887 | ||
1888 | s->obj_size = slo_info.total_size; | |
11fdf7f2 | 1889 | ldpp_dout(this, 20) << "s->obj_size=" << s->obj_size << dendl; |
7c673cae | 1890 | |
31f18b77 FG |
1891 | int r = RGWRados::Object::Read::range_to_ofs(total_len, ofs, end); |
1892 | if (r < 0) { | |
1893 | return r; | |
7c673cae FG |
1894 | } |
1895 | ||
1896 | total_len = end - ofs + 1; | |
eafe8130 TL |
1897 | ldpp_dout(this, 20) << "Requested: ofs=" << ofs |
1898 | << " end=" << end | |
1899 | << " total=" << total_len | |
1900 | << dendl; | |
7c673cae | 1901 | |
31f18b77 | 1902 | r = iterate_slo_parts(s->cct, store, ofs, end, slo_parts, |
7c673cae FG |
1903 | get_obj_user_manifest_iterate_cb, (void *)this); |
1904 | if (r < 0) { | |
1905 | return r; | |
1906 | } | |
1907 | ||
1908 | return 0; | |
1909 | } | |
1910 | ||
1911 | int RGWGetObj::get_data_cb(bufferlist& bl, off_t bl_ofs, off_t bl_len) | |
1912 | { | |
1913 | /* garbage collection related handling */ | |
1914 | utime_t start_time = ceph_clock_now(); | |
1915 | if (start_time > gc_invalidate_time) { | |
1916 | int r = store->defer_gc(s->obj_ctx, s->bucket_info, obj); | |
1917 | if (r < 0) { | |
11fdf7f2 | 1918 | ldpp_dout(this, 0) << "WARNING: could not defer gc entry for obj" << dendl; |
7c673cae FG |
1919 | } |
1920 | gc_invalidate_time = start_time; | |
1921 | gc_invalidate_time += (s->cct->_conf->rgw_gc_obj_min_wait / 2); | |
1922 | } | |
1923 | return send_response_data(bl, bl_ofs, bl_len); | |
1924 | } | |
1925 | ||
1926 | bool RGWGetObj::prefetch_data() | |
1927 | { | |
1928 | /* HEAD request, stop prefetch*/ | |
1929 | if (!get_data) { | |
1930 | return false; | |
1931 | } | |
1932 | ||
1933 | bool prefetch_first_chunk = true; | |
1934 | range_str = s->info.env->get("HTTP_RANGE"); | |
1935 | ||
28e407b8 AA |
1936 | if (range_str) { |
1937 | int r = parse_range(); | |
1938 | /* error on parsing the range, stop prefetch and will fail in execute() */ | |
7c673cae | 1939 | if (r < 0) { |
28e407b8 | 1940 | return false; /* range_parsed==false */ |
7c673cae | 1941 | } |
28e407b8 | 1942 | /* range get goes to shadow objects, stop prefetch */ |
7c673cae FG |
1943 | if (ofs >= s->cct->_conf->rgw_max_chunk_size) { |
1944 | prefetch_first_chunk = false; | |
1945 | } | |
1946 | } | |
1947 | ||
1948 | return get_data && prefetch_first_chunk; | |
1949 | } | |
28e407b8 | 1950 | |
7c673cae FG |
1951 | void RGWGetObj::pre_exec() |
1952 | { | |
1953 | rgw_bucket_object_pre_exec(s); | |
1954 | } | |
1955 | ||
1956 | static bool object_is_expired(map<string, bufferlist>& attrs) { | |
1957 | map<string, bufferlist>::iterator iter = attrs.find(RGW_ATTR_DELETE_AT); | |
1958 | if (iter != attrs.end()) { | |
1959 | utime_t delete_at; | |
1960 | try { | |
11fdf7f2 | 1961 | decode(delete_at, iter->second); |
7c673cae FG |
1962 | } catch (buffer::error& err) { |
1963 | dout(0) << "ERROR: " << __func__ << ": failed to decode " RGW_ATTR_DELETE_AT " attr" << dendl; | |
1964 | return false; | |
1965 | } | |
1966 | ||
1967 | if (delete_at <= ceph_clock_now() && !delete_at.is_zero()) { | |
1968 | return true; | |
1969 | } | |
1970 | } | |
1971 | ||
1972 | return false; | |
1973 | } | |
1974 | ||
1975 | void RGWGetObj::execute() | |
1976 | { | |
7c673cae FG |
1977 | bufferlist bl; |
1978 | gc_invalidate_time = ceph_clock_now(); | |
1979 | gc_invalidate_time += (s->cct->_conf->rgw_gc_obj_min_wait / 2); | |
1980 | ||
1981 | bool need_decompress; | |
1982 | int64_t ofs_x, end_x; | |
1983 | ||
1984 | RGWGetObj_CB cb(this); | |
11fdf7f2 | 1985 | RGWGetObj_Filter* filter = (RGWGetObj_Filter *)&cb; |
7c673cae | 1986 | boost::optional<RGWGetObj_Decompress> decompress; |
11fdf7f2 | 1987 | std::unique_ptr<RGWGetObj_Filter> decrypt; |
7c673cae FG |
1988 | map<string, bufferlist>::iterator attr_iter; |
1989 | ||
1990 | perfcounter->inc(l_rgw_get); | |
1991 | ||
1992 | RGWRados::Object op_target(store, s->bucket_info, *static_cast<RGWObjectCtx *>(s->obj_ctx), obj); | |
1993 | RGWRados::Object::Read read_op(&op_target); | |
1994 | ||
1995 | op_ret = get_params(); | |
1996 | if (op_ret < 0) | |
1997 | goto done_err; | |
1998 | ||
1999 | op_ret = init_common(); | |
2000 | if (op_ret < 0) | |
2001 | goto done_err; | |
2002 | ||
2003 | read_op.conds.mod_ptr = mod_ptr; | |
2004 | read_op.conds.unmod_ptr = unmod_ptr; | |
2005 | read_op.conds.high_precision_time = s->system_request; /* system request need to use high precision time */ | |
2006 | read_op.conds.mod_zone_id = mod_zone_id; | |
2007 | read_op.conds.mod_pg_ver = mod_pg_ver; | |
2008 | read_op.conds.if_match = if_match; | |
2009 | read_op.conds.if_nomatch = if_nomatch; | |
2010 | read_op.params.attrs = &attrs; | |
2011 | read_op.params.lastmod = &lastmod; | |
2012 | read_op.params.obj_size = &s->obj_size; | |
7c673cae FG |
2013 | |
2014 | op_ret = read_op.prepare(); | |
2015 | if (op_ret < 0) | |
2016 | goto done_err; | |
31f18b77 | 2017 | version_id = read_op.state.obj.key.instance; |
7c673cae FG |
2018 | |
2019 | /* STAT ops don't need data, and do no i/o */ | |
2020 | if (get_type() == RGW_OP_STAT_OBJ) { | |
2021 | return; | |
2022 | } | |
2023 | ||
2024 | /* start gettorrent */ | |
2025 | if (torrent.get_flag()) | |
2026 | { | |
3efd9988 FG |
2027 | attr_iter = attrs.find(RGW_ATTR_CRYPT_MODE); |
2028 | if (attr_iter != attrs.end() && attr_iter->second.to_str() == "SSE-C-AES256") { | |
11fdf7f2 | 2029 | ldpp_dout(this, 0) << "ERROR: torrents are not supported for objects " |
28e407b8 AA |
2030 | "encrypted with SSE-C" << dendl; |
2031 | op_ret = -EINVAL; | |
3efd9988 FG |
2032 | goto done_err; |
2033 | } | |
7c673cae | 2034 | torrent.init(s, store); |
3efd9988 | 2035 | op_ret = torrent.get_torrent_file(read_op, total_len, bl, obj); |
7c673cae FG |
2036 | if (op_ret < 0) |
2037 | { | |
11fdf7f2 | 2038 | ldpp_dout(this, 0) << "ERROR: failed to get_torrent_file ret= " << op_ret |
7c673cae FG |
2039 | << dendl; |
2040 | goto done_err; | |
2041 | } | |
2042 | op_ret = send_response_data(bl, 0, total_len); | |
2043 | if (op_ret < 0) | |
2044 | { | |
11fdf7f2 | 2045 | ldpp_dout(this, 0) << "ERROR: failed to send_response_data ret= " << op_ret << dendl; |
7c673cae FG |
2046 | goto done_err; |
2047 | } | |
2048 | return; | |
2049 | } | |
2050 | /* end gettorrent */ | |
2051 | ||
2052 | op_ret = rgw_compression_info_from_attrset(attrs, need_decompress, cs_info); | |
2053 | if (op_ret < 0) { | |
11fdf7f2 | 2054 | ldpp_dout(s, 0) << "ERROR: failed to decode compression info, cannot decompress" << dendl; |
7c673cae FG |
2055 | goto done_err; |
2056 | } | |
2057 | if (need_decompress) { | |
2058 | s->obj_size = cs_info.orig_size; | |
2059 | decompress.emplace(s->cct, &cs_info, partial_content, filter); | |
2060 | filter = &*decompress; | |
2061 | } | |
7c673cae FG |
2062 | |
2063 | attr_iter = attrs.find(RGW_ATTR_USER_MANIFEST); | |
2064 | if (attr_iter != attrs.end() && !skip_manifest) { | |
2065 | op_ret = handle_user_manifest(attr_iter->second.c_str()); | |
2066 | if (op_ret < 0) { | |
11fdf7f2 | 2067 | ldpp_dout(this, 0) << "ERROR: failed to handle user manifest ret=" |
7c673cae FG |
2068 | << op_ret << dendl; |
2069 | goto done_err; | |
2070 | } | |
2071 | return; | |
2072 | } | |
2073 | ||
2074 | attr_iter = attrs.find(RGW_ATTR_SLO_MANIFEST); | |
2075 | if (attr_iter != attrs.end() && !skip_manifest) { | |
2076 | is_slo = true; | |
2077 | op_ret = handle_slo_manifest(attr_iter->second); | |
2078 | if (op_ret < 0) { | |
11fdf7f2 | 2079 | ldpp_dout(this, 0) << "ERROR: failed to handle slo manifest ret=" << op_ret |
7c673cae FG |
2080 | << dendl; |
2081 | goto done_err; | |
2082 | } | |
2083 | return; | |
2084 | } | |
2085 | ||
31f18b77 FG |
2086 | // for range requests with obj size 0 |
2087 | if (range_str && !(s->obj_size)) { | |
2088 | total_len = 0; | |
2089 | op_ret = -ERANGE; | |
2090 | goto done_err; | |
2091 | } | |
2092 | ||
2093 | op_ret = read_op.range_to_ofs(s->obj_size, ofs, end); | |
2094 | if (op_ret < 0) | |
2095 | goto done_err; | |
2096 | total_len = (ofs <= end ? end + 1 - ofs : 0); | |
2097 | ||
7c673cae FG |
2098 | /* Check whether the object has expired. Swift API documentation |
2099 | * stands that we should return 404 Not Found in such case. */ | |
2100 | if (need_object_expiration() && object_is_expired(attrs)) { | |
2101 | op_ret = -ENOENT; | |
2102 | goto done_err; | |
2103 | } | |
2104 | ||
2105 | start = ofs; | |
2106 | ||
7c673cae FG |
2107 | attr_iter = attrs.find(RGW_ATTR_MANIFEST); |
2108 | op_ret = this->get_decrypt_filter(&decrypt, filter, | |
2109 | attr_iter != attrs.end() ? &(attr_iter->second) : nullptr); | |
2110 | if (decrypt != nullptr) { | |
2111 | filter = decrypt.get(); | |
2112 | } | |
2113 | if (op_ret < 0) { | |
2114 | goto done_err; | |
2115 | } | |
2116 | ||
2117 | if (!get_data || ofs > end) { | |
2118 | send_response_data(bl, 0, 0); | |
2119 | return; | |
2120 | } | |
2121 | ||
2122 | perfcounter->inc(l_rgw_get_b, end - ofs); | |
2123 | ||
2124 | ofs_x = ofs; | |
2125 | end_x = end; | |
2126 | filter->fixup_range(ofs_x, end_x); | |
2127 | op_ret = read_op.iterate(ofs_x, end_x, filter); | |
2128 | ||
2129 | if (op_ret >= 0) | |
2130 | op_ret = filter->flush(); | |
2131 | ||
11fdf7f2 | 2132 | perfcounter->tinc(l_rgw_get_lat, s->time_elapsed()); |
7c673cae FG |
2133 | if (op_ret < 0) { |
2134 | goto done_err; | |
2135 | } | |
2136 | ||
2137 | op_ret = send_response_data(bl, 0, 0); | |
2138 | if (op_ret < 0) { | |
2139 | goto done_err; | |
2140 | } | |
2141 | return; | |
2142 | ||
2143 | done_err: | |
2144 | send_response_data_error(); | |
2145 | } | |
2146 | ||
2147 | int RGWGetObj::init_common() | |
2148 | { | |
2149 | if (range_str) { | |
28e407b8 | 2150 | /* range parsed error when prefetch */ |
7c673cae | 2151 | if (!range_parsed) { |
28e407b8 | 2152 | int r = parse_range(); |
7c673cae FG |
2153 | if (r < 0) |
2154 | return r; | |
2155 | } | |
2156 | } | |
2157 | if (if_mod) { | |
2158 | if (parse_time(if_mod, &mod_time) < 0) | |
2159 | return -EINVAL; | |
2160 | mod_ptr = &mod_time; | |
2161 | } | |
2162 | ||
2163 | if (if_unmod) { | |
2164 | if (parse_time(if_unmod, &unmod_time) < 0) | |
2165 | return -EINVAL; | |
2166 | unmod_ptr = &unmod_time; | |
2167 | } | |
2168 | ||
2169 | return 0; | |
2170 | } | |
2171 | ||
2172 | int RGWListBuckets::verify_permission() | |
2173 | { | |
eafe8130 TL |
2174 | rgw::Partition partition = rgw::Partition::aws; |
2175 | rgw::Service service = rgw::Service::s3; | |
11fdf7f2 TL |
2176 | |
2177 | if (!verify_user_permission(this, s, ARN(partition, service, "", s->user->user_id.tenant, "*"), rgw::IAM::s3ListAllMyBuckets)) { | |
7c673cae FG |
2178 | return -EACCES; |
2179 | } | |
2180 | ||
2181 | return 0; | |
2182 | } | |
2183 | ||
2184 | int RGWGetUsage::verify_permission() | |
2185 | { | |
2186 | if (s->auth.identity->is_anonymous()) { | |
2187 | return -EACCES; | |
2188 | } | |
2189 | ||
2190 | return 0; | |
2191 | } | |
2192 | ||
2193 | void RGWListBuckets::execute() | |
2194 | { | |
2195 | bool done; | |
2196 | bool started = false; | |
2197 | uint64_t total_count = 0; | |
2198 | ||
3efd9988 | 2199 | const uint64_t max_buckets = s->cct->_conf->rgw_list_buckets_max_chunk; |
7c673cae FG |
2200 | |
2201 | op_ret = get_params(); | |
2202 | if (op_ret < 0) { | |
2203 | goto send_end; | |
2204 | } | |
2205 | ||
2206 | if (supports_account_metadata()) { | |
2207 | op_ret = rgw_get_user_attrs_by_uid(store, s->user->user_id, attrs); | |
2208 | if (op_ret < 0) { | |
2209 | goto send_end; | |
2210 | } | |
2211 | } | |
2212 | ||
2213 | is_truncated = false; | |
2214 | do { | |
2215 | RGWUserBuckets buckets; | |
2216 | uint64_t read_count; | |
2217 | if (limit >= 0) { | |
11fdf7f2 | 2218 | read_count = min(limit - total_count, max_buckets); |
7c673cae FG |
2219 | } else { |
2220 | read_count = max_buckets; | |
2221 | } | |
2222 | ||
2223 | op_ret = rgw_read_user_buckets(store, s->user->user_id, buckets, | |
2224 | marker, end_marker, read_count, | |
2225 | should_get_stats(), &is_truncated, | |
2226 | get_default_max()); | |
2227 | if (op_ret < 0) { | |
2228 | /* hmm.. something wrong here.. the user was authenticated, so it | |
2229 | should exist */ | |
11fdf7f2 | 2230 | ldpp_dout(this, 10) << "WARNING: failed on rgw_get_user_buckets uid=" |
7c673cae FG |
2231 | << s->user->user_id << dendl; |
2232 | break; | |
2233 | } | |
3efd9988 FG |
2234 | |
2235 | /* We need to have stats for all our policies - even if a given policy | |
2236 | * isn't actually used in a given account. In such situation its usage | |
2237 | * stats would be simply full of zeros. */ | |
11fdf7f2 | 2238 | for (const auto& policy : store->svc.zone->get_zonegroup().placement_targets) { |
3efd9988 FG |
2239 | policies_stats.emplace(policy.second.name, |
2240 | decltype(policies_stats)::mapped_type()); | |
2241 | } | |
2242 | ||
2243 | std::map<std::string, RGWBucketEnt>& m = buckets.get_buckets(); | |
2244 | for (const auto& kv : m) { | |
2245 | const auto& bucket = kv.second; | |
2246 | ||
2247 | global_stats.bytes_used += bucket.size; | |
2248 | global_stats.bytes_used_rounded += bucket.size_rounded; | |
2249 | global_stats.objects_count += bucket.count; | |
2250 | ||
2251 | /* operator[] still can create a new entry for storage policy seen | |
2252 | * for first time. */ | |
11fdf7f2 | 2253 | auto& policy_stats = policies_stats[bucket.placement_rule.to_str()]; |
3efd9988 FG |
2254 | policy_stats.bytes_used += bucket.size; |
2255 | policy_stats.bytes_used_rounded += bucket.size_rounded; | |
2256 | policy_stats.buckets_count++; | |
2257 | policy_stats.objects_count += bucket.count; | |
7c673cae | 2258 | } |
3efd9988 | 2259 | global_stats.buckets_count += m.size(); |
7c673cae FG |
2260 | total_count += m.size(); |
2261 | ||
2262 | done = (m.size() < read_count || (limit >= 0 && total_count >= (uint64_t)limit)); | |
2263 | ||
2264 | if (!started) { | |
2265 | send_response_begin(buckets.count() > 0); | |
2266 | started = true; | |
2267 | } | |
2268 | ||
2269 | if (!m.empty()) { | |
7c673cae FG |
2270 | map<string, RGWBucketEnt>::reverse_iterator riter = m.rbegin(); |
2271 | marker = riter->first; | |
3efd9988 FG |
2272 | |
2273 | handle_listing_chunk(std::move(buckets)); | |
7c673cae FG |
2274 | } |
2275 | } while (is_truncated && !done); | |
2276 | ||
2277 | send_end: | |
2278 | if (!started) { | |
2279 | send_response_begin(false); | |
2280 | } | |
2281 | send_response_end(); | |
2282 | } | |
2283 | ||
2284 | void RGWGetUsage::execute() | |
2285 | { | |
2286 | uint64_t start_epoch = 0; | |
2287 | uint64_t end_epoch = (uint64_t)-1; | |
2288 | op_ret = get_params(); | |
2289 | if (op_ret < 0) | |
2290 | return; | |
2291 | ||
2292 | if (!start_date.empty()) { | |
2293 | op_ret = utime_t::parse_date(start_date, &start_epoch, NULL); | |
2294 | if (op_ret < 0) { | |
11fdf7f2 | 2295 | ldpp_dout(this, 0) << "ERROR: failed to parse start date" << dendl; |
7c673cae FG |
2296 | return; |
2297 | } | |
2298 | } | |
2299 | ||
2300 | if (!end_date.empty()) { | |
2301 | op_ret = utime_t::parse_date(end_date, &end_epoch, NULL); | |
2302 | if (op_ret < 0) { | |
11fdf7f2 | 2303 | ldpp_dout(this, 0) << "ERROR: failed to parse end date" << dendl; |
7c673cae FG |
2304 | return; |
2305 | } | |
2306 | } | |
2307 | ||
2308 | uint32_t max_entries = 1000; | |
2309 | ||
2310 | bool is_truncated = true; | |
2311 | ||
2312 | RGWUsageIter usage_iter; | |
2313 | ||
2314 | while (is_truncated) { | |
11fdf7f2 | 2315 | op_ret = store->read_usage(s->user->user_id, s->bucket_name, start_epoch, end_epoch, max_entries, |
7c673cae FG |
2316 | &is_truncated, usage_iter, usage); |
2317 | ||
2318 | if (op_ret == -ENOENT) { | |
2319 | op_ret = 0; | |
2320 | is_truncated = false; | |
2321 | } | |
2322 | ||
2323 | if (op_ret < 0) { | |
2324 | return; | |
2325 | } | |
2326 | } | |
2327 | ||
2328 | op_ret = rgw_user_sync_all_stats(store, s->user->user_id); | |
2329 | if (op_ret < 0) { | |
11fdf7f2 | 2330 | ldpp_dout(this, 0) << "ERROR: failed to sync user stats" << dendl; |
c07f9fc5 FG |
2331 | return; |
2332 | } | |
2333 | ||
2334 | op_ret = rgw_user_get_all_buckets_stats(store, s->user->user_id, buckets_usage); | |
2335 | if (op_ret < 0) { | |
11fdf7f2 TL |
2336 | ldpp_dout(this, 0) << "ERROR: failed to get user's buckets stats" << dendl; |
2337 | return; | |
7c673cae | 2338 | } |
c07f9fc5 | 2339 | |
7c673cae FG |
2340 | string user_str = s->user->user_id.to_str(); |
2341 | op_ret = store->cls_user_get_header(user_str, &header); | |
2342 | if (op_ret < 0) { | |
11fdf7f2 | 2343 | ldpp_dout(this, 0) << "ERROR: can't read user header" << dendl; |
c07f9fc5 | 2344 | return; |
7c673cae FG |
2345 | } |
2346 | ||
2347 | return; | |
2348 | } | |
2349 | ||
2350 | int RGWStatAccount::verify_permission() | |
2351 | { | |
11fdf7f2 | 2352 | if (!verify_user_permission_no_policy(this, s, RGW_PERM_READ)) { |
7c673cae FG |
2353 | return -EACCES; |
2354 | } | |
2355 | ||
2356 | return 0; | |
2357 | } | |
2358 | ||
2359 | void RGWStatAccount::execute() | |
2360 | { | |
2361 | string marker; | |
2362 | bool is_truncated = false; | |
2363 | uint64_t max_buckets = s->cct->_conf->rgw_list_buckets_max_chunk; | |
2364 | ||
2365 | do { | |
2366 | RGWUserBuckets buckets; | |
2367 | ||
2368 | op_ret = rgw_read_user_buckets(store, s->user->user_id, buckets, marker, | |
2369 | string(), max_buckets, true, &is_truncated); | |
2370 | if (op_ret < 0) { | |
2371 | /* hmm.. something wrong here.. the user was authenticated, so it | |
2372 | should exist */ | |
11fdf7f2 | 2373 | ldpp_dout(this, 10) << "WARNING: failed on rgw_get_user_buckets uid=" |
7c673cae FG |
2374 | << s->user->user_id << dendl; |
2375 | break; | |
2376 | } else { | |
3efd9988 FG |
2377 | /* We need to have stats for all our policies - even if a given policy |
2378 | * isn't actually used in a given account. In such situation its usage | |
2379 | * stats would be simply full of zeros. */ | |
11fdf7f2 | 2380 | for (const auto& policy : store->svc.zone->get_zonegroup().placement_targets) { |
3efd9988 FG |
2381 | policies_stats.emplace(policy.second.name, |
2382 | decltype(policies_stats)::mapped_type()); | |
2383 | } | |
2384 | ||
2385 | std::map<std::string, RGWBucketEnt>& m = buckets.get_buckets(); | |
2386 | for (const auto& kv : m) { | |
2387 | const auto& bucket = kv.second; | |
2388 | ||
2389 | global_stats.bytes_used += bucket.size; | |
2390 | global_stats.bytes_used_rounded += bucket.size_rounded; | |
2391 | global_stats.objects_count += bucket.count; | |
2392 | ||
2393 | /* operator[] still can create a new entry for storage policy seen | |
2394 | * for first time. */ | |
11fdf7f2 | 2395 | auto& policy_stats = policies_stats[bucket.placement_rule.to_str()]; |
3efd9988 FG |
2396 | policy_stats.bytes_used += bucket.size; |
2397 | policy_stats.bytes_used_rounded += bucket.size_rounded; | |
2398 | policy_stats.buckets_count++; | |
2399 | policy_stats.objects_count += bucket.count; | |
7c673cae | 2400 | } |
3efd9988 | 2401 | global_stats.buckets_count += m.size(); |
7c673cae FG |
2402 | |
2403 | } | |
2404 | } while (is_truncated); | |
2405 | } | |
2406 | ||
2407 | int RGWGetBucketVersioning::verify_permission() | |
2408 | { | |
b32b8144 | 2409 | return verify_bucket_owner_or_policy(s, rgw::IAM::s3GetBucketVersioning); |
7c673cae FG |
2410 | } |
2411 | ||
2412 | void RGWGetBucketVersioning::pre_exec() | |
2413 | { | |
2414 | rgw_bucket_object_pre_exec(s); | |
2415 | } | |
2416 | ||
2417 | void RGWGetBucketVersioning::execute() | |
2418 | { | |
2419 | versioned = s->bucket_info.versioned(); | |
2420 | versioning_enabled = s->bucket_info.versioning_enabled(); | |
11fdf7f2 | 2421 | mfa_enabled = s->bucket_info.mfa_enabled(); |
7c673cae FG |
2422 | } |
2423 | ||
2424 | int RGWSetBucketVersioning::verify_permission() | |
2425 | { | |
b32b8144 | 2426 | return verify_bucket_owner_or_policy(s, rgw::IAM::s3PutBucketVersioning); |
7c673cae FG |
2427 | } |
2428 | ||
2429 | void RGWSetBucketVersioning::pre_exec() | |
2430 | { | |
2431 | rgw_bucket_object_pre_exec(s); | |
2432 | } | |
2433 | ||
2434 | void RGWSetBucketVersioning::execute() | |
2435 | { | |
2436 | op_ret = get_params(); | |
2437 | if (op_ret < 0) | |
2438 | return; | |
2439 | ||
eafe8130 TL |
2440 | if (s->bucket_info.obj_lock_enabled() && versioning_status != VersioningEnabled) { |
2441 | op_ret = -ERR_INVALID_BUCKET_STATE; | |
2442 | return; | |
2443 | } | |
2444 | ||
11fdf7f2 TL |
2445 | bool cur_mfa_status = (s->bucket_info.flags & BUCKET_MFA_ENABLED) != 0; |
2446 | ||
2447 | mfa_set_status &= (mfa_status != cur_mfa_status); | |
2448 | ||
2449 | if (mfa_set_status && | |
2450 | !s->mfa_verified) { | |
2451 | op_ret = -ERR_MFA_REQUIRED; | |
2452 | return; | |
2453 | } | |
92f5a8d4 TL |
2454 | //if mfa is enabled for bucket, make sure mfa code is validated in case versioned status gets changed |
2455 | if (cur_mfa_status) { | |
2456 | bool req_versioning_status = false; | |
2457 | //if requested versioning status is not the same as the one set for the bucket, return error | |
2458 | if (versioning_status == VersioningEnabled) { | |
2459 | req_versioning_status = (s->bucket_info.flags & BUCKET_VERSIONS_SUSPENDED) != 0; | |
2460 | } else if (versioning_status == VersioningSuspended) { | |
2461 | req_versioning_status = (s->bucket_info.flags & BUCKET_VERSIONS_SUSPENDED) == 0; | |
2462 | } | |
2463 | if (req_versioning_status && !s->mfa_verified) { | |
2464 | op_ret = -ERR_MFA_REQUIRED; | |
2465 | return; | |
2466 | } | |
2467 | } | |
11fdf7f2 TL |
2468 | |
2469 | if (!store->svc.zone->is_meta_master()) { | |
7c673cae FG |
2470 | op_ret = forward_request_to_master(s, NULL, store, in_data, nullptr); |
2471 | if (op_ret < 0) { | |
11fdf7f2 | 2472 | ldpp_dout(this, 0) << "forward_request_to_master returned ret=" << op_ret << dendl; |
7c673cae FG |
2473 | return; |
2474 | } | |
2475 | } | |
2476 | ||
11fdf7f2 TL |
2477 | bool modified = mfa_set_status; |
2478 | ||
2479 | op_ret = retry_raced_bucket_write(store, s, [&] { | |
2480 | if (mfa_set_status) { | |
2481 | if (mfa_status) { | |
2482 | s->bucket_info.flags |= BUCKET_MFA_ENABLED; | |
2483 | } else { | |
2484 | s->bucket_info.flags &= ~BUCKET_MFA_ENABLED; | |
2485 | } | |
2486 | } | |
2487 | ||
2488 | if (versioning_status == VersioningEnabled) { | |
b32b8144 FG |
2489 | s->bucket_info.flags |= BUCKET_VERSIONED; |
2490 | s->bucket_info.flags &= ~BUCKET_VERSIONS_SUSPENDED; | |
11fdf7f2 TL |
2491 | modified = true; |
2492 | } else if (versioning_status == VersioningSuspended) { | |
b32b8144 | 2493 | s->bucket_info.flags |= (BUCKET_VERSIONED | BUCKET_VERSIONS_SUSPENDED); |
11fdf7f2 TL |
2494 | modified = true; |
2495 | } else { | |
2496 | return op_ret; | |
b32b8144 | 2497 | } |
b32b8144 | 2498 | return store->put_bucket_instance_info(s->bucket_info, false, real_time(), |
11fdf7f2 | 2499 | &s->bucket_attrs); |
b32b8144 | 2500 | }); |
7c673cae | 2501 | |
11fdf7f2 TL |
2502 | if (!modified) { |
2503 | return; | |
2504 | } | |
2505 | ||
7c673cae | 2506 | if (op_ret < 0) { |
11fdf7f2 | 2507 | ldpp_dout(this, 0) << "NOTICE: put_bucket_info on bucket=" << s->bucket.name |
7c673cae FG |
2508 | << " returned err=" << op_ret << dendl; |
2509 | return; | |
2510 | } | |
2511 | } | |
2512 | ||
2513 | int RGWGetBucketWebsite::verify_permission() | |
2514 | { | |
b32b8144 | 2515 | return verify_bucket_owner_or_policy(s, rgw::IAM::s3GetBucketWebsite); |
7c673cae FG |
2516 | } |
2517 | ||
2518 | void RGWGetBucketWebsite::pre_exec() | |
2519 | { | |
2520 | rgw_bucket_object_pre_exec(s); | |
2521 | } | |
2522 | ||
2523 | void RGWGetBucketWebsite::execute() | |
2524 | { | |
2525 | if (!s->bucket_info.has_website) { | |
b32b8144 | 2526 | op_ret = -ERR_NO_SUCH_WEBSITE_CONFIGURATION; |
7c673cae FG |
2527 | } |
2528 | } | |
2529 | ||
2530 | int RGWSetBucketWebsite::verify_permission() | |
2531 | { | |
b32b8144 | 2532 | return verify_bucket_owner_or_policy(s, rgw::IAM::s3PutBucketWebsite); |
7c673cae FG |
2533 | } |
2534 | ||
2535 | void RGWSetBucketWebsite::pre_exec() | |
2536 | { | |
2537 | rgw_bucket_object_pre_exec(s); | |
2538 | } | |
2539 | ||
2540 | void RGWSetBucketWebsite::execute() | |
2541 | { | |
2542 | op_ret = get_params(); | |
2543 | ||
2544 | if (op_ret < 0) | |
2545 | return; | |
2546 | ||
11fdf7f2 | 2547 | if (!store->svc.zone->is_meta_master()) { |
31f18b77 FG |
2548 | op_ret = forward_request_to_master(s, NULL, store, in_data, nullptr); |
2549 | if (op_ret < 0) { | |
11fdf7f2 | 2550 | ldpp_dout(this, 0) << " forward_request_to_master returned ret=" << op_ret << dendl; |
31f18b77 FG |
2551 | return; |
2552 | } | |
2553 | } | |
2554 | ||
b32b8144 FG |
2555 | op_ret = retry_raced_bucket_write(store, s, [this] { |
2556 | s->bucket_info.has_website = true; | |
2557 | s->bucket_info.website_conf = website_conf; | |
2558 | op_ret = store->put_bucket_instance_info(s->bucket_info, false, | |
2559 | real_time(), &s->bucket_attrs); | |
2560 | return op_ret; | |
2561 | }); | |
7c673cae | 2562 | |
7c673cae | 2563 | if (op_ret < 0) { |
11fdf7f2 TL |
2564 | ldpp_dout(this, 0) << "NOTICE: put_bucket_info on bucket=" << s->bucket.name |
2565 | << " returned err=" << op_ret << dendl; | |
7c673cae FG |
2566 | return; |
2567 | } | |
2568 | } | |
2569 | ||
2570 | int RGWDeleteBucketWebsite::verify_permission() | |
2571 | { | |
b32b8144 | 2572 | return verify_bucket_owner_or_policy(s, rgw::IAM::s3DeleteBucketWebsite); |
7c673cae FG |
2573 | } |
2574 | ||
2575 | void RGWDeleteBucketWebsite::pre_exec() | |
2576 | { | |
2577 | rgw_bucket_object_pre_exec(s); | |
2578 | } | |
2579 | ||
2580 | void RGWDeleteBucketWebsite::execute() | |
2581 | { | |
494da23a TL |
2582 | |
2583 | if (!store->svc.zone->is_meta_master()) { | |
2584 | bufferlist in_data; | |
2585 | op_ret = forward_request_to_master(s, nullptr, store, in_data, nullptr); | |
2586 | if (op_ret < 0) { | |
2587 | ldpp_dout(this, 0) << "NOTICE: forward_to_master failed on bucket=" << s->bucket.name | |
2588 | << "returned err=" << op_ret << dendl; | |
2589 | return; | |
2590 | } | |
2591 | } | |
b32b8144 FG |
2592 | op_ret = retry_raced_bucket_write(store, s, [this] { |
2593 | s->bucket_info.has_website = false; | |
2594 | s->bucket_info.website_conf = RGWBucketWebsiteConf(); | |
2595 | op_ret = store->put_bucket_instance_info(s->bucket_info, false, | |
2596 | real_time(), &s->bucket_attrs); | |
2597 | return op_ret; | |
2598 | }); | |
7c673cae | 2599 | if (op_ret < 0) { |
11fdf7f2 TL |
2600 | ldpp_dout(this, 0) << "NOTICE: put_bucket_info on bucket=" << s->bucket.name |
2601 | << " returned err=" << op_ret << dendl; | |
7c673cae FG |
2602 | return; |
2603 | } | |
2604 | } | |
2605 | ||
2606 | int RGWStatBucket::verify_permission() | |
2607 | { | |
31f18b77 | 2608 | // This (a HEAD request on a bucket) is governed by the s3:ListBucket permission. |
11fdf7f2 | 2609 | if (!verify_bucket_permission(this, s, rgw::IAM::s3ListBucket)) { |
7c673cae FG |
2610 | return -EACCES; |
2611 | } | |
2612 | ||
2613 | return 0; | |
2614 | } | |
2615 | ||
2616 | void RGWStatBucket::pre_exec() | |
2617 | { | |
2618 | rgw_bucket_object_pre_exec(s); | |
2619 | } | |
2620 | ||
2621 | void RGWStatBucket::execute() | |
2622 | { | |
2623 | if (!s->bucket_exists) { | |
2624 | op_ret = -ERR_NO_SUCH_BUCKET; | |
2625 | return; | |
2626 | } | |
2627 | ||
2628 | RGWUserBuckets buckets; | |
2629 | bucket.bucket = s->bucket; | |
2630 | buckets.add(bucket); | |
2631 | map<string, RGWBucketEnt>& m = buckets.get_buckets(); | |
2632 | op_ret = store->update_containers_stats(m); | |
2633 | if (! op_ret) | |
2634 | op_ret = -EEXIST; | |
2635 | if (op_ret > 0) { | |
2636 | op_ret = 0; | |
2637 | map<string, RGWBucketEnt>::iterator iter = m.find(bucket.bucket.name); | |
2638 | if (iter != m.end()) { | |
2639 | bucket = iter->second; | |
2640 | } else { | |
2641 | op_ret = -EINVAL; | |
2642 | } | |
2643 | } | |
2644 | } | |
2645 | ||
2646 | int RGWListBucket::verify_permission() | |
2647 | { | |
31f18b77 FG |
2648 | op_ret = get_params(); |
2649 | if (op_ret < 0) { | |
2650 | return op_ret; | |
2651 | } | |
b32b8144 FG |
2652 | if (!prefix.empty()) |
2653 | s->env.emplace("s3:prefix", prefix); | |
2654 | ||
2655 | if (!delimiter.empty()) | |
2656 | s->env.emplace("s3:delimiter", delimiter); | |
2657 | ||
2658 | s->env.emplace("s3:max-keys", std::to_string(max)); | |
31f18b77 | 2659 | |
11fdf7f2 TL |
2660 | if (!verify_bucket_permission(this, |
2661 | s, | |
31f18b77 FG |
2662 | list_versions ? |
2663 | rgw::IAM::s3ListBucketVersions : | |
2664 | rgw::IAM::s3ListBucket)) { | |
7c673cae FG |
2665 | return -EACCES; |
2666 | } | |
2667 | ||
2668 | return 0; | |
2669 | } | |
2670 | ||
2671 | int RGWListBucket::parse_max_keys() | |
2672 | { | |
f64942e4 AA |
2673 | // Bound max value of max-keys to configured value for security |
2674 | // Bound min value of max-keys to '0' | |
2675 | // Some S3 clients explicitly send max-keys=0 to detect if the bucket is | |
2676 | // empty without listing any items. | |
2677 | return parse_value_and_bound(max_keys, max, 0, | |
11fdf7f2 | 2678 | g_conf().get_val<uint64_t>("rgw_max_listing_results"), |
f64942e4 | 2679 | default_max); |
7c673cae FG |
2680 | } |
2681 | ||
2682 | void RGWListBucket::pre_exec() | |
2683 | { | |
2684 | rgw_bucket_object_pre_exec(s); | |
2685 | } | |
2686 | ||
2687 | void RGWListBucket::execute() | |
2688 | { | |
2689 | if (!s->bucket_exists) { | |
2690 | op_ret = -ERR_NO_SUCH_BUCKET; | |
2691 | return; | |
2692 | } | |
2693 | ||
1adf2230 | 2694 | if (allow_unordered && !delimiter.empty()) { |
11fdf7f2 | 2695 | ldpp_dout(this, 0) << |
1adf2230 AA |
2696 | "ERROR: unordered bucket listing requested with a delimiter" << dendl; |
2697 | op_ret = -EINVAL; | |
2698 | return; | |
2699 | } | |
2700 | ||
7c673cae FG |
2701 | if (need_container_stats()) { |
2702 | map<string, RGWBucketEnt> m; | |
2703 | m[s->bucket.name] = RGWBucketEnt(); | |
2704 | m.begin()->second.bucket = s->bucket; | |
2705 | op_ret = store->update_containers_stats(m); | |
2706 | if (op_ret > 0) { | |
2707 | bucket = m.begin()->second; | |
2708 | } | |
2709 | } | |
2710 | ||
2711 | RGWRados::Bucket target(store, s->bucket_info); | |
2712 | if (shard_id >= 0) { | |
2713 | target.set_shard_id(shard_id); | |
2714 | } | |
2715 | RGWRados::Bucket::List list_op(&target); | |
2716 | ||
2717 | list_op.params.prefix = prefix; | |
2718 | list_op.params.delim = delimiter; | |
2719 | list_op.params.marker = marker; | |
2720 | list_op.params.end_marker = end_marker; | |
2721 | list_op.params.list_versions = list_versions; | |
1adf2230 | 2722 | list_op.params.allow_unordered = allow_unordered; |
7c673cae FG |
2723 | |
2724 | op_ret = list_op.list_objects(max, &objs, &common_prefixes, &is_truncated); | |
31f18b77 | 2725 | if (op_ret >= 0) { |
7c673cae FG |
2726 | next_marker = list_op.get_next_marker(); |
2727 | } | |
2728 | } | |
2729 | ||
2730 | int RGWGetBucketLogging::verify_permission() | |
2731 | { | |
b32b8144 | 2732 | return verify_bucket_owner_or_policy(s, rgw::IAM::s3GetBucketLogging); |
7c673cae FG |
2733 | } |
2734 | ||
2735 | int RGWGetBucketLocation::verify_permission() | |
2736 | { | |
b32b8144 | 2737 | return verify_bucket_owner_or_policy(s, rgw::IAM::s3GetBucketLocation); |
7c673cae FG |
2738 | } |
2739 | ||
2740 | int RGWCreateBucket::verify_permission() | |
2741 | { | |
2742 | /* This check is mostly needed for S3 that doesn't support account ACL. | |
2743 | * Swift doesn't allow to delegate any permission to an anonymous user, | |
2744 | * so it will become an early exit in such case. */ | |
2745 | if (s->auth.identity->is_anonymous()) { | |
2746 | return -EACCES; | |
2747 | } | |
2748 | ||
11fdf7f2 TL |
2749 | rgw_bucket bucket; |
2750 | bucket.name = s->bucket_name; | |
2751 | bucket.tenant = s->bucket_tenant; | |
2752 | ARN arn = ARN(bucket); | |
2753 | if (!verify_user_permission(this, s, arn, rgw::IAM::s3CreateBucket)) { | |
7c673cae FG |
2754 | return -EACCES; |
2755 | } | |
2756 | ||
2757 | if (s->user->user_id.tenant != s->bucket_tenant) { | |
11fdf7f2 | 2758 | ldpp_dout(this, 10) << "user cannot create a bucket in a different tenant" |
7c673cae FG |
2759 | << " (user_id.tenant=" << s->user->user_id.tenant |
2760 | << " requested=" << s->bucket_tenant << ")" | |
2761 | << dendl; | |
2762 | return -EACCES; | |
2763 | } | |
2764 | if (s->user->max_buckets < 0) { | |
2765 | return -EPERM; | |
2766 | } | |
2767 | ||
2768 | if (s->user->max_buckets) { | |
2769 | RGWUserBuckets buckets; | |
2770 | string marker; | |
2771 | bool is_truncated = false; | |
2772 | op_ret = rgw_read_user_buckets(store, s->user->user_id, buckets, | |
2773 | marker, string(), s->user->max_buckets, | |
2774 | false, &is_truncated); | |
2775 | if (op_ret < 0) { | |
2776 | return op_ret; | |
2777 | } | |
2778 | ||
2779 | if ((int)buckets.count() >= s->user->max_buckets) { | |
2780 | return -ERR_TOO_MANY_BUCKETS; | |
2781 | } | |
2782 | } | |
2783 | ||
2784 | return 0; | |
2785 | } | |
2786 | ||
2787 | static int forward_request_to_master(struct req_state *s, obj_version *objv, | |
2788 | RGWRados *store, bufferlist& in_data, | |
2789 | JSONParser *jp, req_info *forward_info) | |
2790 | { | |
11fdf7f2 TL |
2791 | if (!store->svc.zone->get_master_conn()) { |
2792 | ldpp_dout(s, 0) << "rest connection is invalid" << dendl; | |
7c673cae FG |
2793 | return -EINVAL; |
2794 | } | |
11fdf7f2 | 2795 | ldpp_dout(s, 0) << "sending request to master zonegroup" << dendl; |
7c673cae FG |
2796 | bufferlist response; |
2797 | string uid_str = s->user->user_id.to_str(); | |
2798 | #define MAX_REST_RESPONSE (128 * 1024) // we expect a very small response | |
11fdf7f2 TL |
2799 | int ret = store->svc.zone->get_master_conn()->forward(uid_str, (forward_info ? *forward_info : s->info), |
2800 | objv, MAX_REST_RESPONSE, &in_data, &response); | |
7c673cae FG |
2801 | if (ret < 0) |
2802 | return ret; | |
2803 | ||
11fdf7f2 | 2804 | ldpp_dout(s, 20) << "response: " << response.c_str() << dendl; |
7c673cae | 2805 | if (jp && !jp->parse(response.c_str(), response.length())) { |
11fdf7f2 | 2806 | ldpp_dout(s, 0) << "failed parsing response from master zonegroup" << dendl; |
7c673cae FG |
2807 | return -EINVAL; |
2808 | } | |
2809 | ||
2810 | return 0; | |
2811 | } | |
2812 | ||
2813 | void RGWCreateBucket::pre_exec() | |
2814 | { | |
2815 | rgw_bucket_object_pre_exec(s); | |
2816 | } | |
2817 | ||
2818 | static void prepare_add_del_attrs(const map<string, bufferlist>& orig_attrs, | |
2819 | map<string, bufferlist>& out_attrs, | |
2820 | map<string, bufferlist>& out_rmattrs) | |
2821 | { | |
2822 | for (const auto& kv : orig_attrs) { | |
2823 | const string& name = kv.first; | |
2824 | ||
2825 | /* Check if the attr is user-defined metadata item. */ | |
2826 | if (name.compare(0, sizeof(RGW_ATTR_META_PREFIX) - 1, | |
2827 | RGW_ATTR_META_PREFIX) == 0) { | |
2828 | /* For the objects all existing meta attrs have to be removed. */ | |
2829 | out_rmattrs[name] = kv.second; | |
2830 | } else if (out_attrs.find(name) == std::end(out_attrs)) { | |
2831 | out_attrs[name] = kv.second; | |
2832 | } | |
2833 | } | |
2834 | } | |
2835 | ||
2836 | /* Fuse resource metadata basing on original attributes in @orig_attrs, set | |
2837 | * of _custom_ attribute names to remove in @rmattr_names and attributes in | |
2838 | * @out_attrs. Place results in @out_attrs. | |
2839 | * | |
2840 | * NOTE: it's supposed that all special attrs already present in @out_attrs | |
2841 | * will be preserved without any change. Special attributes are those which | |
2842 | * names start with RGW_ATTR_META_PREFIX. They're complement to custom ones | |
2843 | * used for X-Account-Meta-*, X-Container-Meta-*, X-Amz-Meta and so on. */ | |
2844 | static void prepare_add_del_attrs(const map<string, bufferlist>& orig_attrs, | |
2845 | const set<string>& rmattr_names, | |
2846 | map<string, bufferlist>& out_attrs) | |
2847 | { | |
2848 | for (const auto& kv : orig_attrs) { | |
2849 | const string& name = kv.first; | |
2850 | ||
2851 | /* Check if the attr is user-defined metadata item. */ | |
2852 | if (name.compare(0, strlen(RGW_ATTR_META_PREFIX), | |
2853 | RGW_ATTR_META_PREFIX) == 0) { | |
2854 | /* For the buckets all existing meta attrs are preserved, | |
2855 | except those that are listed in rmattr_names. */ | |
2856 | if (rmattr_names.find(name) != std::end(rmattr_names)) { | |
2857 | const auto aiter = out_attrs.find(name); | |
2858 | ||
2859 | if (aiter != std::end(out_attrs)) { | |
2860 | out_attrs.erase(aiter); | |
2861 | } | |
2862 | } else { | |
2863 | /* emplace() won't alter the map if the key is already present. | |
2864 | * This behaviour is fully intensional here. */ | |
2865 | out_attrs.emplace(kv); | |
2866 | } | |
2867 | } else if (out_attrs.find(name) == std::end(out_attrs)) { | |
2868 | out_attrs[name] = kv.second; | |
2869 | } | |
2870 | } | |
2871 | } | |
2872 | ||
2873 | ||
2874 | static void populate_with_generic_attrs(const req_state * const s, | |
2875 | map<string, bufferlist>& out_attrs) | |
2876 | { | |
2877 | for (const auto& kv : s->generic_attrs) { | |
2878 | bufferlist& attrbl = out_attrs[kv.first]; | |
2879 | const string& val = kv.second; | |
2880 | attrbl.clear(); | |
2881 | attrbl.append(val.c_str(), val.size() + 1); | |
2882 | } | |
2883 | } | |
2884 | ||
2885 | ||
2886 | static int filter_out_quota_info(std::map<std::string, bufferlist>& add_attrs, | |
2887 | const std::set<std::string>& rmattr_names, | |
2888 | RGWQuotaInfo& quota, | |
2889 | bool * quota_extracted = nullptr) | |
2890 | { | |
2891 | bool extracted = false; | |
2892 | ||
2893 | /* Put new limit on max objects. */ | |
2894 | auto iter = add_attrs.find(RGW_ATTR_QUOTA_NOBJS); | |
2895 | std::string err; | |
2896 | if (std::end(add_attrs) != iter) { | |
2897 | quota.max_objects = | |
2898 | static_cast<int64_t>(strict_strtoll(iter->second.c_str(), 10, &err)); | |
2899 | if (!err.empty()) { | |
2900 | return -EINVAL; | |
2901 | } | |
2902 | add_attrs.erase(iter); | |
2903 | extracted = true; | |
2904 | } | |
2905 | ||
2906 | /* Put new limit on bucket (container) size. */ | |
2907 | iter = add_attrs.find(RGW_ATTR_QUOTA_MSIZE); | |
2908 | if (iter != add_attrs.end()) { | |
2909 | quota.max_size = | |
2910 | static_cast<int64_t>(strict_strtoll(iter->second.c_str(), 10, &err)); | |
2911 | if (!err.empty()) { | |
2912 | return -EINVAL; | |
2913 | } | |
2914 | add_attrs.erase(iter); | |
2915 | extracted = true; | |
2916 | } | |
2917 | ||
2918 | for (const auto& name : rmattr_names) { | |
2919 | /* Remove limit on max objects. */ | |
2920 | if (name.compare(RGW_ATTR_QUOTA_NOBJS) == 0) { | |
2921 | quota.max_objects = -1; | |
2922 | extracted = true; | |
2923 | } | |
2924 | ||
2925 | /* Remove limit on max bucket size. */ | |
2926 | if (name.compare(RGW_ATTR_QUOTA_MSIZE) == 0) { | |
2927 | quota.max_size = -1; | |
2928 | extracted = true; | |
2929 | } | |
2930 | } | |
2931 | ||
2932 | /* Swift requries checking on raw usage instead of the 4 KiB rounded one. */ | |
2933 | quota.check_on_raw = true; | |
2934 | quota.enabled = quota.max_size > 0 || quota.max_objects > 0; | |
2935 | ||
2936 | if (quota_extracted) { | |
2937 | *quota_extracted = extracted; | |
2938 | } | |
2939 | ||
2940 | return 0; | |
2941 | } | |
2942 | ||
2943 | ||
2944 | static void filter_out_website(std::map<std::string, ceph::bufferlist>& add_attrs, | |
2945 | const std::set<std::string>& rmattr_names, | |
2946 | RGWBucketWebsiteConf& ws_conf) | |
2947 | { | |
2948 | std::string lstval; | |
2949 | ||
2950 | /* Let's define a mapping between each custom attribute and the memory where | |
2951 | * attribute's value should be stored. The memory location is expressed by | |
2952 | * a non-const reference. */ | |
2953 | const auto mapping = { | |
2954 | std::make_pair(RGW_ATTR_WEB_INDEX, std::ref(ws_conf.index_doc_suffix)), | |
2955 | std::make_pair(RGW_ATTR_WEB_ERROR, std::ref(ws_conf.error_doc)), | |
2956 | std::make_pair(RGW_ATTR_WEB_LISTINGS, std::ref(lstval)), | |
2957 | std::make_pair(RGW_ATTR_WEB_LIST_CSS, std::ref(ws_conf.listing_css_doc)), | |
2958 | std::make_pair(RGW_ATTR_SUBDIR_MARKER, std::ref(ws_conf.subdir_marker)) | |
2959 | }; | |
2960 | ||
2961 | for (const auto& kv : mapping) { | |
2962 | const char * const key = kv.first; | |
2963 | auto& target = kv.second; | |
2964 | ||
2965 | auto iter = add_attrs.find(key); | |
2966 | ||
2967 | if (std::end(add_attrs) != iter) { | |
2968 | /* The "target" is a reference to ws_conf. */ | |
2969 | target = iter->second.c_str(); | |
2970 | add_attrs.erase(iter); | |
2971 | } | |
2972 | ||
2973 | if (rmattr_names.count(key)) { | |
2974 | target = std::string(); | |
2975 | } | |
2976 | } | |
2977 | ||
2978 | if (! lstval.empty()) { | |
2979 | ws_conf.listing_enabled = boost::algorithm::iequals(lstval, "true"); | |
2980 | } | |
2981 | } | |
2982 | ||
2983 | ||
2984 | void RGWCreateBucket::execute() | |
2985 | { | |
2986 | RGWAccessControlPolicy old_policy(s->cct); | |
2987 | buffer::list aclbl; | |
2988 | buffer::list corsbl; | |
2989 | bool existed; | |
2990 | string bucket_name; | |
2991 | rgw_make_bucket_entry_name(s->bucket_tenant, s->bucket_name, bucket_name); | |
11fdf7f2 | 2992 | rgw_raw_obj obj(store->svc.zone->get_zone_params().domain_root, bucket_name); |
7c673cae FG |
2993 | obj_version objv, *pobjv = NULL; |
2994 | ||
2995 | op_ret = get_params(); | |
2996 | if (op_ret < 0) | |
2997 | return; | |
2998 | ||
11fdf7f2 TL |
2999 | if (!relaxed_region_enforcement && |
3000 | !location_constraint.empty() && | |
3001 | !store->svc.zone->has_zonegroup_api(location_constraint)) { | |
3002 | ldpp_dout(this, 0) << "location constraint (" << location_constraint << ")" | |
31f18b77 FG |
3003 | << " can't be found." << dendl; |
3004 | op_ret = -ERR_INVALID_LOCATION_CONSTRAINT; | |
3005 | s->err.message = "The specified location-constraint is not valid"; | |
3006 | return; | |
3007 | } | |
3008 | ||
11fdf7f2 TL |
3009 | if (!relaxed_region_enforcement && !store->svc.zone->get_zonegroup().is_master_zonegroup() && !location_constraint.empty() && |
3010 | store->svc.zone->get_zonegroup().api_name != location_constraint) { | |
3011 | ldpp_dout(this, 0) << "location constraint (" << location_constraint << ")" | |
3012 | << " doesn't match zonegroup" << " (" << store->svc.zone->get_zonegroup().api_name << ")" | |
31f18b77 FG |
3013 | << dendl; |
3014 | op_ret = -ERR_INVALID_LOCATION_CONSTRAINT; | |
3015 | s->err.message = "The specified location-constraint is not valid"; | |
7c673cae FG |
3016 | return; |
3017 | } | |
3018 | ||
11fdf7f2 TL |
3019 | const auto& zonegroup = store->svc.zone->get_zonegroup(); |
3020 | if (!placement_rule.name.empty() && | |
3021 | !zonegroup.placement_targets.count(placement_rule.name)) { | |
3022 | ldpp_dout(this, 0) << "placement target (" << placement_rule.name << ")" | |
c07f9fc5 | 3023 | << " doesn't exist in the placement targets of zonegroup" |
11fdf7f2 | 3024 | << " (" << store->svc.zone->get_zonegroup().api_name << ")" << dendl; |
c07f9fc5 FG |
3025 | op_ret = -ERR_INVALID_LOCATION_CONSTRAINT; |
3026 | s->err.message = "The specified placement target does not exist"; | |
3027 | return; | |
3028 | } | |
3029 | ||
7c673cae FG |
3030 | /* we need to make sure we read bucket info, it's not read before for this |
3031 | * specific request */ | |
11fdf7f2 TL |
3032 | op_ret = store->get_bucket_info(*s->sysobj_ctx, s->bucket_tenant, s->bucket_name, |
3033 | s->bucket_info, nullptr, &s->bucket_attrs); | |
7c673cae FG |
3034 | if (op_ret < 0 && op_ret != -ENOENT) |
3035 | return; | |
3036 | s->bucket_exists = (op_ret != -ENOENT); | |
3037 | ||
3038 | s->bucket_owner.set_id(s->user->user_id); | |
3039 | s->bucket_owner.set_name(s->user->display_name); | |
3040 | if (s->bucket_exists) { | |
11fdf7f2 TL |
3041 | int r = rgw_op_get_bucket_policy_from_attr(s->cct, store, s->bucket_info, |
3042 | s->bucket_attrs, &old_policy); | |
7c673cae FG |
3043 | if (r >= 0) { |
3044 | if (old_policy.get_owner().get_id().compare(s->user->user_id) != 0) { | |
3045 | op_ret = -EEXIST; | |
3046 | return; | |
3047 | } | |
3048 | } | |
3049 | } | |
3050 | ||
3051 | RGWBucketInfo master_info; | |
3052 | rgw_bucket *pmaster_bucket; | |
3053 | uint32_t *pmaster_num_shards; | |
3054 | real_time creation_time; | |
3055 | ||
11fdf7f2 | 3056 | if (!store->svc.zone->is_meta_master()) { |
7c673cae FG |
3057 | JSONParser jp; |
3058 | op_ret = forward_request_to_master(s, NULL, store, in_data, &jp); | |
3059 | if (op_ret < 0) { | |
3060 | return; | |
3061 | } | |
3062 | ||
3063 | JSONDecoder::decode_json("entry_point_object_ver", ep_objv, &jp); | |
3064 | JSONDecoder::decode_json("object_ver", objv, &jp); | |
3065 | JSONDecoder::decode_json("bucket_info", master_info, &jp); | |
11fdf7f2 TL |
3066 | ldpp_dout(this, 20) << "parsed: objv.tag=" << objv.tag << " objv.ver=" << objv.ver << dendl; |
3067 | ldpp_dout(this, 20) << "got creation time: << " << master_info.creation_time << dendl; | |
7c673cae FG |
3068 | pmaster_bucket= &master_info.bucket; |
3069 | creation_time = master_info.creation_time; | |
3070 | pmaster_num_shards = &master_info.num_shards; | |
3071 | pobjv = &objv; | |
eafe8130 | 3072 | obj_lock_enabled = master_info.obj_lock_enabled(); |
7c673cae FG |
3073 | } else { |
3074 | pmaster_bucket = NULL; | |
3075 | pmaster_num_shards = NULL; | |
3076 | } | |
3077 | ||
3078 | string zonegroup_id; | |
3079 | ||
3080 | if (s->system_request) { | |
3081 | zonegroup_id = s->info.args.get(RGW_SYS_PARAM_PREFIX "zonegroup"); | |
3082 | if (zonegroup_id.empty()) { | |
11fdf7f2 | 3083 | zonegroup_id = store->svc.zone->get_zonegroup().get_id(); |
7c673cae FG |
3084 | } |
3085 | } else { | |
11fdf7f2 | 3086 | zonegroup_id = store->svc.zone->get_zonegroup().get_id(); |
7c673cae FG |
3087 | } |
3088 | ||
3089 | if (s->bucket_exists) { | |
11fdf7f2 | 3090 | rgw_placement_rule selected_placement_rule; |
7c673cae FG |
3091 | rgw_bucket bucket; |
3092 | bucket.tenant = s->bucket_tenant; | |
3093 | bucket.name = s->bucket_name; | |
11fdf7f2 | 3094 | op_ret = store->svc.zone->select_bucket_placement(*(s->user), zonegroup_id, |
7c673cae FG |
3095 | placement_rule, |
3096 | &selected_placement_rule, nullptr); | |
3097 | if (selected_placement_rule != s->bucket_info.placement_rule) { | |
3098 | op_ret = -EEXIST; | |
3099 | return; | |
3100 | } | |
3101 | } | |
3102 | ||
3103 | /* Encode special metadata first as we're using std::map::emplace under | |
3104 | * the hood. This method will add the new items only if the map doesn't | |
3105 | * contain such keys yet. */ | |
3106 | policy.encode(aclbl); | |
3107 | emplace_attr(RGW_ATTR_ACL, std::move(aclbl)); | |
3108 | ||
3109 | if (has_cors) { | |
3110 | cors_config.encode(corsbl); | |
3111 | emplace_attr(RGW_ATTR_CORS, std::move(corsbl)); | |
3112 | } | |
3113 | ||
3114 | RGWQuotaInfo quota_info; | |
3115 | const RGWQuotaInfo * pquota_info = nullptr; | |
3116 | if (need_metadata_upload()) { | |
3117 | /* It's supposed that following functions WILL NOT change any special | |
3118 | * attributes (like RGW_ATTR_ACL) if they are already present in attrs. */ | |
3efd9988 FG |
3119 | op_ret = rgw_get_request_metadata(s->cct, s->info, attrs, false); |
3120 | if (op_ret < 0) { | |
3121 | return; | |
3122 | } | |
7c673cae FG |
3123 | prepare_add_del_attrs(s->bucket_attrs, rmattr_names, attrs); |
3124 | populate_with_generic_attrs(s, attrs); | |
3125 | ||
3126 | op_ret = filter_out_quota_info(attrs, rmattr_names, quota_info); | |
3127 | if (op_ret < 0) { | |
3128 | return; | |
3129 | } else { | |
3130 | pquota_info = "a_info; | |
3131 | } | |
3132 | ||
3133 | /* Web site of Swift API. */ | |
3134 | filter_out_website(attrs, rmattr_names, s->bucket_info.website_conf); | |
3135 | s->bucket_info.has_website = !s->bucket_info.website_conf.is_empty(); | |
3136 | } | |
3137 | ||
3138 | s->bucket.tenant = s->bucket_tenant; /* ignored if bucket exists */ | |
3139 | s->bucket.name = s->bucket_name; | |
3140 | ||
3141 | /* Handle updates of the metadata for Swift's object versioning. */ | |
3142 | if (swift_ver_location) { | |
3143 | s->bucket_info.swift_ver_location = *swift_ver_location; | |
3144 | s->bucket_info.swift_versioning = (! swift_ver_location->empty()); | |
3145 | } | |
eafe8130 TL |
3146 | if (obj_lock_enabled) { |
3147 | info.flags = BUCKET_VERSIONED | BUCKET_OBJ_LOCK_ENABLED; | |
3148 | } | |
3149 | ||
7c673cae FG |
3150 | |
3151 | op_ret = store->create_bucket(*(s->user), s->bucket, zonegroup_id, | |
3152 | placement_rule, s->bucket_info.swift_ver_location, | |
3153 | pquota_info, attrs, | |
3154 | info, pobjv, &ep_objv, creation_time, | |
3155 | pmaster_bucket, pmaster_num_shards, true); | |
3156 | /* continue if EEXIST and create_bucket will fail below. this way we can | |
3157 | * recover from a partial create by retrying it. */ | |
11fdf7f2 | 3158 | ldpp_dout(this, 20) << "rgw_create_bucket returned ret=" << op_ret << " bucket=" << s->bucket << dendl; |
7c673cae FG |
3159 | |
3160 | if (op_ret && op_ret != -EEXIST) | |
3161 | return; | |
3162 | ||
3163 | existed = (op_ret == -EEXIST); | |
3164 | ||
3165 | if (existed) { | |
3166 | /* bucket already existed, might have raced with another bucket creation, or | |
3167 | * might be partial bucket creation that never completed. Read existing bucket | |
3168 | * info, verify that the reported bucket owner is the current user. | |
3169 | * If all is ok then update the user's list of buckets. | |
3170 | * Otherwise inform client about a name conflict. | |
3171 | */ | |
3172 | if (info.owner.compare(s->user->user_id) != 0) { | |
3173 | op_ret = -EEXIST; | |
3174 | return; | |
3175 | } | |
3176 | s->bucket = info.bucket; | |
3177 | } | |
3178 | ||
3179 | op_ret = rgw_link_bucket(store, s->user->user_id, s->bucket, | |
3180 | info.creation_time, false); | |
3181 | if (op_ret && !existed && op_ret != -EEXIST) { | |
3182 | /* if it exists (or previously existed), don't remove it! */ | |
3183 | op_ret = rgw_unlink_bucket(store, s->user->user_id, s->bucket.tenant, | |
3184 | s->bucket.name); | |
3185 | if (op_ret < 0) { | |
11fdf7f2 | 3186 | ldpp_dout(this, 0) << "WARNING: failed to unlink bucket: ret=" << op_ret |
7c673cae FG |
3187 | << dendl; |
3188 | } | |
3189 | } else if (op_ret == -EEXIST || (op_ret == 0 && existed)) { | |
3190 | op_ret = -ERR_BUCKET_EXISTS; | |
3191 | } | |
3192 | ||
3193 | if (need_metadata_upload() && existed) { | |
3194 | /* OK, it looks we lost race with another request. As it's required to | |
3195 | * handle metadata fusion and upload, the whole operation becomes very | |
3196 | * similar in nature to PutMetadataBucket. However, as the attrs may | |
3197 | * changed in the meantime, we have to refresh. */ | |
3198 | short tries = 0; | |
3199 | do { | |
7c673cae FG |
3200 | RGWBucketInfo binfo; |
3201 | map<string, bufferlist> battrs; | |
3202 | ||
11fdf7f2 | 3203 | op_ret = store->get_bucket_info(*s->sysobj_ctx, s->bucket_tenant, s->bucket_name, |
7c673cae FG |
3204 | binfo, nullptr, &battrs); |
3205 | if (op_ret < 0) { | |
3206 | return; | |
3207 | } else if (binfo.owner.compare(s->user->user_id) != 0) { | |
3208 | /* New bucket doesn't belong to the account we're operating on. */ | |
3209 | op_ret = -EEXIST; | |
3210 | return; | |
3211 | } else { | |
3212 | s->bucket_info = binfo; | |
3213 | s->bucket_attrs = battrs; | |
3214 | } | |
3215 | ||
3216 | attrs.clear(); | |
3217 | ||
3efd9988 FG |
3218 | op_ret = rgw_get_request_metadata(s->cct, s->info, attrs, false); |
3219 | if (op_ret < 0) { | |
3220 | return; | |
3221 | } | |
7c673cae FG |
3222 | prepare_add_del_attrs(s->bucket_attrs, rmattr_names, attrs); |
3223 | populate_with_generic_attrs(s, attrs); | |
3224 | op_ret = filter_out_quota_info(attrs, rmattr_names, s->bucket_info.quota); | |
3225 | if (op_ret < 0) { | |
3226 | return; | |
3227 | } | |
3228 | ||
3229 | /* Handle updates of the metadata for Swift's object versioning. */ | |
3230 | if (swift_ver_location) { | |
3231 | s->bucket_info.swift_ver_location = *swift_ver_location; | |
3232 | s->bucket_info.swift_versioning = (! swift_ver_location->empty()); | |
3233 | } | |
3234 | ||
3235 | /* Web site of Swift API. */ | |
3236 | filter_out_website(attrs, rmattr_names, s->bucket_info.website_conf); | |
3237 | s->bucket_info.has_website = !s->bucket_info.website_conf.is_empty(); | |
3238 | ||
3239 | /* This will also set the quota on the bucket. */ | |
3240 | op_ret = rgw_bucket_set_attrs(store, s->bucket_info, attrs, | |
3241 | &s->bucket_info.objv_tracker); | |
3242 | } while (op_ret == -ECANCELED && tries++ < 20); | |
3243 | ||
3244 | /* Restore the proper return code. */ | |
3245 | if (op_ret >= 0) { | |
3246 | op_ret = -ERR_BUCKET_EXISTS; | |
3247 | } | |
3248 | } | |
3249 | } | |
3250 | ||
3251 | int RGWDeleteBucket::verify_permission() | |
3252 | { | |
11fdf7f2 | 3253 | if (!verify_bucket_permission(this, s, rgw::IAM::s3DeleteBucket)) { |
7c673cae FG |
3254 | return -EACCES; |
3255 | } | |
3256 | ||
3257 | return 0; | |
3258 | } | |
3259 | ||
3260 | void RGWDeleteBucket::pre_exec() | |
3261 | { | |
3262 | rgw_bucket_object_pre_exec(s); | |
3263 | } | |
3264 | ||
3265 | void RGWDeleteBucket::execute() | |
3266 | { | |
11fdf7f2 TL |
3267 | if (s->bucket_name.empty()) { |
3268 | op_ret = -EINVAL; | |
7c673cae | 3269 | return; |
11fdf7f2 | 3270 | } |
7c673cae FG |
3271 | |
3272 | if (!s->bucket_exists) { | |
11fdf7f2 | 3273 | ldpp_dout(this, 0) << "ERROR: bucket " << s->bucket_name << " not found" << dendl; |
7c673cae FG |
3274 | op_ret = -ERR_NO_SUCH_BUCKET; |
3275 | return; | |
3276 | } | |
3277 | RGWObjVersionTracker ot; | |
3278 | ot.read_version = s->bucket_info.ep_objv; | |
3279 | ||
3280 | if (s->system_request) { | |
3281 | string tag = s->info.args.get(RGW_SYS_PARAM_PREFIX "tag"); | |
3282 | string ver_str = s->info.args.get(RGW_SYS_PARAM_PREFIX "ver"); | |
3283 | if (!tag.empty()) { | |
3284 | ot.read_version.tag = tag; | |
3285 | uint64_t ver; | |
3286 | string err; | |
3287 | ver = strict_strtol(ver_str.c_str(), 10, &err); | |
3288 | if (!err.empty()) { | |
11fdf7f2 | 3289 | ldpp_dout(this, 0) << "failed to parse ver param" << dendl; |
7c673cae FG |
3290 | op_ret = -EINVAL; |
3291 | return; | |
3292 | } | |
3293 | ot.read_version.ver = ver; | |
3294 | } | |
3295 | } | |
3296 | ||
3297 | op_ret = rgw_bucket_sync_user_stats(store, s->user->user_id, s->bucket_info); | |
3298 | if ( op_ret < 0) { | |
11fdf7f2 | 3299 | ldpp_dout(this, 1) << "WARNING: failed to sync user stats before bucket delete: op_ret= " << op_ret << dendl; |
7c673cae FG |
3300 | } |
3301 | ||
3302 | op_ret = store->check_bucket_empty(s->bucket_info); | |
3303 | if (op_ret < 0) { | |
3304 | return; | |
3305 | } | |
3306 | ||
11fdf7f2 | 3307 | if (!store->svc.zone->is_meta_master()) { |
7c673cae FG |
3308 | bufferlist in_data; |
3309 | op_ret = forward_request_to_master(s, &ot.read_version, store, in_data, | |
3310 | NULL); | |
3311 | if (op_ret < 0) { | |
3312 | if (op_ret == -ENOENT) { | |
3313 | /* adjust error, we want to return with NoSuchBucket and not | |
3314 | * NoSuchKey */ | |
3315 | op_ret = -ERR_NO_SUCH_BUCKET; | |
3316 | } | |
3317 | return; | |
3318 | } | |
3319 | } | |
3320 | ||
224ce89b WB |
3321 | string prefix, delimiter; |
3322 | ||
3323 | if (s->prot_flags & RGW_REST_SWIFT) { | |
3324 | string path_args; | |
3325 | path_args = s->info.args.get("path"); | |
3326 | if (!path_args.empty()) { | |
3327 | if (!delimiter.empty() || !prefix.empty()) { | |
3328 | op_ret = -EINVAL; | |
3329 | return; | |
3330 | } | |
3331 | prefix = path_args; | |
3332 | delimiter="/"; | |
3333 | } | |
3334 | } | |
3335 | ||
3336 | op_ret = abort_bucket_multiparts(store, s->cct, s->bucket_info, prefix, delimiter); | |
3337 | ||
3338 | if (op_ret < 0) { | |
3339 | return; | |
3340 | } | |
3341 | ||
7c673cae FG |
3342 | op_ret = store->delete_bucket(s->bucket_info, ot, false); |
3343 | ||
3344 | if (op_ret == -ECANCELED) { | |
3345 | // lost a race, either with mdlog sync or another delete bucket operation. | |
3346 | // in either case, we've already called rgw_unlink_bucket() | |
3347 | op_ret = 0; | |
3348 | return; | |
3349 | } | |
3350 | ||
3351 | if (op_ret == 0) { | |
b32b8144 | 3352 | op_ret = rgw_unlink_bucket(store, s->bucket_info.owner, s->bucket.tenant, |
7c673cae FG |
3353 | s->bucket.name, false); |
3354 | if (op_ret < 0) { | |
11fdf7f2 | 3355 | ldpp_dout(this, 0) << "WARNING: failed to unlink bucket: ret=" << op_ret |
7c673cae FG |
3356 | << dendl; |
3357 | } | |
3358 | } | |
7c673cae FG |
3359 | } |
3360 | ||
3361 | int RGWPutObj::verify_permission() | |
3362 | { | |
3a9019d9 | 3363 | if (! copy_source.empty()) { |
7c673cae | 3364 | |
31f18b77 | 3365 | RGWAccessControlPolicy cs_acl(s->cct); |
11fdf7f2 | 3366 | boost::optional<Policy> policy; |
7c673cae FG |
3367 | map<string, bufferlist> cs_attrs; |
3368 | rgw_bucket cs_bucket(copy_source_bucket_info.bucket); | |
3369 | rgw_obj_key cs_object(copy_source_object_name, copy_source_version_id); | |
3370 | ||
3371 | rgw_obj obj(cs_bucket, cs_object); | |
3372 | store->set_atomic(s->obj_ctx, obj); | |
3373 | store->set_prefetch_data(s->obj_ctx, obj); | |
3374 | ||
3375 | /* check source object permissions */ | |
11fdf7f2 | 3376 | if (read_obj_policy(store, s, copy_source_bucket_info, cs_attrs, &cs_acl, nullptr, |
3a9019d9 | 3377 | policy, cs_bucket, cs_object) < 0) { |
7c673cae FG |
3378 | return -EACCES; |
3379 | } | |
3380 | ||
3381 | /* admin request overrides permission checks */ | |
31f18b77 | 3382 | if (! s->auth.identity->is_admin_of(cs_acl.get_owner().get_id())) { |
11fdf7f2 TL |
3383 | if (policy || ! s->iam_user_policies.empty()) { |
3384 | auto usr_policy_res = Effect::Pass; | |
3385 | for (auto& user_policy : s->iam_user_policies) { | |
3386 | if (usr_policy_res = user_policy.eval(s->env, *s->auth.identity, | |
3387 | cs_object.instance.empty() ? | |
3388 | rgw::IAM::s3GetObject : | |
3389 | rgw::IAM::s3GetObjectVersion, | |
eafe8130 | 3390 | rgw::ARN(obj)); usr_policy_res == Effect::Deny) |
11fdf7f2 TL |
3391 | return -EACCES; |
3392 | else if (usr_policy_res == Effect::Allow) | |
3393 | break; | |
3394 | } | |
3395 | rgw::IAM::Effect e = Effect::Pass; | |
3396 | if (policy) { | |
3397 | e = policy->eval(s->env, *s->auth.identity, | |
31f18b77 FG |
3398 | cs_object.instance.empty() ? |
3399 | rgw::IAM::s3GetObject : | |
3400 | rgw::IAM::s3GetObjectVersion, | |
eafe8130 | 3401 | rgw::ARN(obj)); |
11fdf7f2 | 3402 | } |
31f18b77 FG |
3403 | if (e == Effect::Deny) { |
3404 | return -EACCES; | |
11fdf7f2 TL |
3405 | } else if (usr_policy_res == Effect::Pass && e == Effect::Pass && |
3406 | !cs_acl.verify_permission(this, *s->auth.identity, s->perm_mask, | |
31f18b77 FG |
3407 | RGW_PERM_READ)) { |
3408 | return -EACCES; | |
3409 | } | |
11fdf7f2 | 3410 | } else if (!cs_acl.verify_permission(this, *s->auth.identity, s->perm_mask, |
31f18b77 FG |
3411 | RGW_PERM_READ)) { |
3412 | return -EACCES; | |
3413 | } | |
7c673cae | 3414 | } |
31f18b77 | 3415 | } |
7c673cae | 3416 | |
11fdf7f2 TL |
3417 | auto op_ret = get_params(); |
3418 | if (op_ret < 0) { | |
3419 | ldpp_dout(this, 20) << "get_params() returned ret=" << op_ret << dendl; | |
3420 | return op_ret; | |
7c673cae FG |
3421 | } |
3422 | ||
11fdf7f2 TL |
3423 | if (s->iam_policy || ! s->iam_user_policies.empty()) { |
3424 | rgw_add_grant_to_iam_environment(s->env, s); | |
7c673cae | 3425 | |
11fdf7f2 | 3426 | rgw_add_to_iam_environment(s->env, "s3:x-amz-acl", s->canned_acl); |
7c673cae | 3427 | |
11fdf7f2 TL |
3428 | if (obj_tags != nullptr && obj_tags->count() > 0){ |
3429 | auto tags = obj_tags->get_tags(); | |
3430 | for (const auto& kv: tags){ | |
3431 | rgw_add_to_iam_environment(s->env, "s3:RequestObjectTag/"+kv.first, kv.second); | |
3432 | } | |
7c673cae | 3433 | } |
7c673cae | 3434 | |
11fdf7f2 TL |
3435 | constexpr auto encrypt_attr = "x-amz-server-side-encryption"; |
3436 | constexpr auto s3_encrypt_attr = "s3:x-amz-server-side-encryption"; | |
3437 | auto enc_header = s->info.x_meta_map.find(encrypt_attr); | |
3438 | if (enc_header != s->info.x_meta_map.end()){ | |
3439 | rgw_add_to_iam_environment(s->env, s3_encrypt_attr, enc_header->second); | |
3440 | } | |
7c673cae | 3441 | |
11fdf7f2 TL |
3442 | constexpr auto kms_attr = "x-amz-server-side-encryption-aws-kms-key-id"; |
3443 | constexpr auto s3_kms_attr = "s3:x-amz-server-side-encryption-aws-kms-key-id"; | |
3444 | auto kms_header = s->info.x_meta_map.find(kms_attr); | |
3445 | if (kms_header != s->info.x_meta_map.end()){ | |
3446 | rgw_add_to_iam_environment(s->env, s3_kms_attr, kms_header->second); | |
3447 | } | |
7c673cae | 3448 | |
11fdf7f2 TL |
3449 | auto usr_policy_res = eval_user_policies(s->iam_user_policies, s->env, |
3450 | boost::none, | |
3451 | rgw::IAM::s3PutObject, | |
3452 | rgw_obj(s->bucket, s->object)); | |
3453 | if (usr_policy_res == Effect::Deny) | |
3454 | return -EACCES; | |
7c673cae | 3455 | |
11fdf7f2 TL |
3456 | rgw::IAM::Effect e = Effect::Pass; |
3457 | if (s->iam_policy) { | |
3458 | e = s->iam_policy->eval(s->env, *s->auth.identity, | |
3459 | rgw::IAM::s3PutObject, | |
3460 | rgw_obj(s->bucket, s->object)); | |
3461 | } | |
3462 | if (e == Effect::Allow) { | |
3463 | return 0; | |
3464 | } else if (e == Effect::Deny) { | |
3465 | return -EACCES; | |
3466 | } else if (usr_policy_res == Effect::Allow) { | |
3467 | return 0; | |
3468 | } | |
7c673cae FG |
3469 | } |
3470 | ||
11fdf7f2 TL |
3471 | if (!verify_bucket_permission_no_policy(this, s, RGW_PERM_WRITE)) { |
3472 | return -EACCES; | |
7c673cae FG |
3473 | } |
3474 | ||
11fdf7f2 | 3475 | return 0; |
7c673cae FG |
3476 | } |
3477 | ||
7c673cae FG |
3478 | |
3479 | void RGWPutObj::pre_exec() | |
3480 | { | |
3481 | rgw_bucket_object_pre_exec(s); | |
3482 | } | |
3483 | ||
11fdf7f2 | 3484 | class RGWPutObj_CB : public RGWGetObj_Filter |
7c673cae FG |
3485 | { |
3486 | RGWPutObj *op; | |
3487 | public: | |
11fdf7f2 | 3488 | explicit RGWPutObj_CB(RGWPutObj *_op) : op(_op) {} |
7c673cae FG |
3489 | ~RGWPutObj_CB() override {} |
3490 | ||
3491 | int handle_data(bufferlist& bl, off_t bl_ofs, off_t bl_len) override { | |
3492 | return op->get_data_cb(bl, bl_ofs, bl_len); | |
3493 | } | |
3494 | }; | |
3495 | ||
3496 | int RGWPutObj::get_data_cb(bufferlist& bl, off_t bl_ofs, off_t bl_len) | |
3497 | { | |
3498 | bufferlist bl_tmp; | |
3499 | bl.copy(bl_ofs, bl_len, bl_tmp); | |
3500 | ||
3501 | bl_aux.append(bl_tmp); | |
3502 | ||
3503 | return bl_len; | |
3504 | } | |
3505 | ||
3506 | int RGWPutObj::get_data(const off_t fst, const off_t lst, bufferlist& bl) | |
3507 | { | |
3508 | RGWPutObj_CB cb(this); | |
11fdf7f2 | 3509 | RGWGetObj_Filter* filter = &cb; |
7c673cae | 3510 | boost::optional<RGWGetObj_Decompress> decompress; |
11fdf7f2 | 3511 | std::unique_ptr<RGWGetObj_Filter> decrypt; |
7c673cae FG |
3512 | RGWCompressionInfo cs_info; |
3513 | map<string, bufferlist> attrs; | |
3514 | map<string, bufferlist>::iterator attr_iter; | |
3515 | int ret = 0; | |
3516 | ||
3517 | uint64_t obj_size; | |
3518 | int64_t new_ofs, new_end; | |
3519 | ||
3520 | new_ofs = fst; | |
3521 | new_end = lst; | |
3522 | ||
3523 | rgw_obj_key obj_key(copy_source_object_name, copy_source_version_id); | |
3524 | rgw_obj obj(copy_source_bucket_info.bucket, obj_key); | |
3525 | ||
3526 | RGWRados::Object op_target(store, copy_source_bucket_info, *static_cast<RGWObjectCtx *>(s->obj_ctx), obj); | |
3527 | RGWRados::Object::Read read_op(&op_target); | |
3528 | read_op.params.obj_size = &obj_size; | |
3529 | read_op.params.attrs = &attrs; | |
3530 | ||
3531 | ret = read_op.prepare(); | |
3532 | if (ret < 0) | |
3533 | return ret; | |
3534 | ||
3535 | bool need_decompress; | |
3536 | op_ret = rgw_compression_info_from_attrset(attrs, need_decompress, cs_info); | |
3537 | if (op_ret < 0) { | |
11fdf7f2 TL |
3538 | ldpp_dout(s, 0) << "ERROR: failed to decode compression info" << dendl; |
3539 | return -EIO; | |
7c673cae FG |
3540 | } |
3541 | ||
3542 | bool partial_content = true; | |
3543 | if (need_decompress) | |
3544 | { | |
3545 | obj_size = cs_info.orig_size; | |
3546 | decompress.emplace(s->cct, &cs_info, partial_content, filter); | |
3547 | filter = &*decompress; | |
3548 | } | |
3549 | ||
3550 | attr_iter = attrs.find(RGW_ATTR_MANIFEST); | |
3551 | op_ret = this->get_decrypt_filter(&decrypt, | |
3552 | filter, | |
3553 | attrs, | |
3554 | attr_iter != attrs.end() ? &(attr_iter->second) : nullptr); | |
3555 | if (decrypt != nullptr) { | |
3556 | filter = decrypt.get(); | |
3557 | } | |
3558 | if (op_ret < 0) { | |
3559 | return ret; | |
3560 | } | |
3561 | ||
3562 | ret = read_op.range_to_ofs(obj_size, new_ofs, new_end); | |
3563 | if (ret < 0) | |
3564 | return ret; | |
3565 | ||
3566 | filter->fixup_range(new_ofs, new_end); | |
3567 | ret = read_op.iterate(new_ofs, new_end, filter); | |
3568 | ||
3569 | if (ret >= 0) | |
3570 | ret = filter->flush(); | |
3571 | ||
3572 | bl.claim_append(bl_aux); | |
3573 | ||
3574 | return ret; | |
3575 | } | |
3576 | ||
3577 | // special handling for compression type = "random" with multipart uploads | |
3578 | static CompressorRef get_compressor_plugin(const req_state *s, | |
3579 | const std::string& compression_type) | |
3580 | { | |
3581 | if (compression_type != "random") { | |
3582 | return Compressor::create(s->cct, compression_type); | |
3583 | } | |
3584 | ||
3585 | bool is_multipart{false}; | |
3586 | const auto& upload_id = s->info.args.get("uploadId", &is_multipart); | |
3587 | ||
3588 | if (!is_multipart) { | |
3589 | return Compressor::create(s->cct, compression_type); | |
3590 | } | |
3591 | ||
3592 | // use a hash of the multipart upload id so all parts use the same plugin | |
3593 | const auto alg = std::hash<std::string>{}(upload_id) % Compressor::COMP_ALG_LAST; | |
3594 | if (alg == Compressor::COMP_ALG_NONE) { | |
3595 | return nullptr; | |
3596 | } | |
3597 | return Compressor::create(s->cct, alg); | |
3598 | } | |
3599 | ||
3600 | void RGWPutObj::execute() | |
3601 | { | |
7c673cae FG |
3602 | char supplied_md5_bin[CEPH_CRYPTO_MD5_DIGESTSIZE + 1]; |
3603 | char supplied_md5[CEPH_CRYPTO_MD5_DIGESTSIZE * 2 + 1]; | |
3604 | char calc_md5[CEPH_CRYPTO_MD5_DIGESTSIZE * 2 + 1]; | |
3605 | unsigned char m[CEPH_CRYPTO_MD5_DIGESTSIZE]; | |
3606 | MD5 hash; | |
3607 | bufferlist bl, aclbl, bs; | |
3608 | int len; | |
7c673cae FG |
3609 | |
3610 | off_t fst; | |
3611 | off_t lst; | |
7c673cae FG |
3612 | |
3613 | bool need_calc_md5 = (dlo_manifest == NULL) && (slo_info == NULL); | |
3614 | perfcounter->inc(l_rgw_put); | |
11fdf7f2 TL |
3615 | // report latency on return |
3616 | auto put_lat = make_scope_guard([&] { | |
3617 | perfcounter->tinc(l_rgw_put_lat, s->time_elapsed()); | |
3618 | }); | |
3619 | ||
7c673cae FG |
3620 | op_ret = -EINVAL; |
3621 | if (s->object.empty()) { | |
11fdf7f2 | 3622 | return; |
7c673cae FG |
3623 | } |
3624 | ||
3625 | if (!s->bucket_exists) { | |
3626 | op_ret = -ERR_NO_SUCH_BUCKET; | |
3627 | return; | |
3628 | } | |
3629 | ||
7c673cae FG |
3630 | |
3631 | op_ret = get_system_versioning_params(s, &olh_epoch, &version_id); | |
3632 | if (op_ret < 0) { | |
11fdf7f2 | 3633 | ldpp_dout(this, 20) << "get_system_versioning_params() returned ret=" |
7c673cae | 3634 | << op_ret << dendl; |
11fdf7f2 | 3635 | return; |
7c673cae FG |
3636 | } |
3637 | ||
3638 | if (supplied_md5_b64) { | |
3639 | need_calc_md5 = true; | |
3640 | ||
11fdf7f2 | 3641 | ldpp_dout(this, 15) << "supplied_md5_b64=" << supplied_md5_b64 << dendl; |
7c673cae FG |
3642 | op_ret = ceph_unarmor(supplied_md5_bin, &supplied_md5_bin[CEPH_CRYPTO_MD5_DIGESTSIZE + 1], |
3643 | supplied_md5_b64, supplied_md5_b64 + strlen(supplied_md5_b64)); | |
11fdf7f2 | 3644 | ldpp_dout(this, 15) << "ceph_armor ret=" << op_ret << dendl; |
7c673cae FG |
3645 | if (op_ret != CEPH_CRYPTO_MD5_DIGESTSIZE) { |
3646 | op_ret = -ERR_INVALID_DIGEST; | |
11fdf7f2 | 3647 | return; |
7c673cae FG |
3648 | } |
3649 | ||
3650 | buf_to_hex((const unsigned char *)supplied_md5_bin, CEPH_CRYPTO_MD5_DIGESTSIZE, supplied_md5); | |
11fdf7f2 | 3651 | ldpp_dout(this, 15) << "supplied_md5=" << supplied_md5 << dendl; |
7c673cae FG |
3652 | } |
3653 | ||
3654 | if (!chunked_upload) { /* with chunked upload we don't know how big is the upload. | |
3655 | we also check sizes at the end anyway */ | |
3656 | op_ret = store->check_quota(s->bucket_owner.get_id(), s->bucket, | |
3657 | user_quota, bucket_quota, s->content_length); | |
3658 | if (op_ret < 0) { | |
11fdf7f2 TL |
3659 | ldpp_dout(this, 20) << "check_quota() returned ret=" << op_ret << dendl; |
3660 | return; | |
7c673cae | 3661 | } |
31f18b77 FG |
3662 | op_ret = store->check_bucket_shards(s->bucket_info, s->bucket, bucket_quota); |
3663 | if (op_ret < 0) { | |
11fdf7f2 TL |
3664 | ldpp_dout(this, 20) << "check_bucket_shards() returned ret=" << op_ret << dendl; |
3665 | return; | |
31f18b77 | 3666 | } |
7c673cae FG |
3667 | } |
3668 | ||
3669 | if (supplied_etag) { | |
3670 | strncpy(supplied_md5, supplied_etag, sizeof(supplied_md5) - 1); | |
3671 | supplied_md5[sizeof(supplied_md5) - 1] = '\0'; | |
3672 | } | |
3673 | ||
11fdf7f2 TL |
3674 | const bool multipart = !multipart_upload_id.empty(); |
3675 | auto& obj_ctx = *static_cast<RGWObjectCtx*>(s->obj_ctx); | |
3676 | rgw_obj obj{s->bucket, s->object}; | |
7c673cae FG |
3677 | |
3678 | /* Handle object versioning of Swift API. */ | |
3679 | if (! multipart) { | |
11fdf7f2 | 3680 | op_ret = store->swift_versioning_copy(obj_ctx, |
7c673cae FG |
3681 | s->bucket_owner.get_id(), |
3682 | s->bucket_info, | |
3683 | obj); | |
3684 | if (op_ret < 0) { | |
11fdf7f2 | 3685 | return; |
7c673cae FG |
3686 | } |
3687 | } | |
3688 | ||
11fdf7f2 TL |
3689 | // create the object processor |
3690 | rgw::AioThrottle aio(store->ctx()->_conf->rgw_put_obj_min_window_size); | |
3691 | using namespace rgw::putobj; | |
3692 | constexpr auto max_processor_size = std::max({sizeof(MultipartObjectProcessor), | |
3693 | sizeof(AtomicObjectProcessor), | |
3694 | sizeof(AppendObjectProcessor)}); | |
3695 | ceph::static_ptr<ObjectProcessor, max_processor_size> processor; | |
3696 | ||
3697 | rgw_placement_rule *pdest_placement; | |
3698 | ||
eafe8130 | 3699 | multipart_upload_info upload_info; |
11fdf7f2 TL |
3700 | if (multipart) { |
3701 | RGWMPObj mp(s->object.name, multipart_upload_id); | |
3702 | ||
11fdf7f2 TL |
3703 | op_ret = get_multipart_info(store, s, mp.get_meta(), nullptr, nullptr, &upload_info); |
3704 | if (op_ret < 0) { | |
3705 | if (op_ret != -ENOENT) { | |
3706 | ldpp_dout(this, 0) << "ERROR: get_multipart_info returned " << op_ret << ": " << cpp_strerror(-op_ret) << dendl; | |
3707 | } else {// -ENOENT: raced with upload complete/cancel, no need to spam log | |
3708 | ldpp_dout(this, 20) << "failed to get multipart info (returned " << op_ret << ": " << cpp_strerror(-op_ret) << "): probably raced with upload complete / cancel" << dendl; | |
3709 | } | |
3710 | return; | |
3711 | } | |
3712 | pdest_placement = &upload_info.dest_placement; | |
3713 | ldpp_dout(this, 20) << "dest_placement for part=" << upload_info.dest_placement << dendl; | |
3714 | processor.emplace<MultipartObjectProcessor>( | |
3715 | &aio, store, s->bucket_info, pdest_placement, | |
3716 | s->owner.get_id(), obj_ctx, obj, | |
3717 | multipart_upload_id, multipart_part_num, multipart_part_str); | |
3718 | } else if(append) { | |
3719 | if (s->bucket_info.versioned()) { | |
3720 | op_ret = -ERR_INVALID_BUCKET_STATE; | |
3721 | return; | |
3722 | } | |
3723 | pdest_placement = &s->dest_placement; | |
3724 | processor.emplace<AppendObjectProcessor>( | |
3725 | &aio, store, s->bucket_info, pdest_placement, s->bucket_owner.get_id(),obj_ctx, obj, | |
3726 | s->req_id, position, &cur_accounted_size); | |
3727 | } else { | |
3728 | if (s->bucket_info.versioning_enabled()) { | |
3729 | if (!version_id.empty()) { | |
3730 | obj.key.set_instance(version_id); | |
3731 | } else { | |
3732 | store->gen_rand_obj_instance_name(&obj); | |
3733 | version_id = obj.key.instance; | |
3734 | } | |
3735 | } | |
3736 | pdest_placement = &s->dest_placement; | |
3737 | processor.emplace<AtomicObjectProcessor>( | |
3738 | &aio, store, s->bucket_info, pdest_placement, | |
3739 | s->bucket_owner.get_id(), obj_ctx, obj, olh_epoch, s->req_id); | |
3740 | } | |
3741 | ||
3742 | op_ret = processor->prepare(); | |
7c673cae | 3743 | if (op_ret < 0) { |
11fdf7f2 | 3744 | ldpp_dout(this, 20) << "processor->prepare() returned ret=" << op_ret |
7c673cae | 3745 | << dendl; |
11fdf7f2 | 3746 | return; |
7c673cae FG |
3747 | } |
3748 | ||
3a9019d9 FG |
3749 | if ((! copy_source.empty()) && !copy_source_range) { |
3750 | rgw_obj_key obj_key(copy_source_object_name, copy_source_version_id); | |
3751 | rgw_obj obj(copy_source_bucket_info.bucket, obj_key.name); | |
3752 | ||
3753 | RGWObjState *astate; | |
11fdf7f2 TL |
3754 | op_ret = store->get_obj_state(&obj_ctx, copy_source_bucket_info, obj, |
3755 | &astate, true, false); | |
3a9019d9 | 3756 | if (op_ret < 0) { |
11fdf7f2 TL |
3757 | ldpp_dout(this, 0) << "ERROR: get copy source obj state returned with error" << op_ret << dendl; |
3758 | return; | |
3a9019d9 FG |
3759 | } |
3760 | if (!astate->exists){ | |
3761 | op_ret = -ENOENT; | |
11fdf7f2 | 3762 | return; |
3a9019d9 | 3763 | } |
28e407b8 | 3764 | lst = astate->accounted_size - 1; |
3a9019d9 FG |
3765 | } else { |
3766 | lst = copy_source_range_lst; | |
3767 | } | |
3768 | ||
7c673cae | 3769 | fst = copy_source_range_fst; |
7c673cae | 3770 | |
11fdf7f2 TL |
3771 | // no filters by default |
3772 | DataProcessor *filter = processor.get(); | |
3773 | ||
3774 | const auto& compression_type = store->svc.zone->get_zone_params().get_compression_type(*pdest_placement); | |
3775 | CompressorRef plugin; | |
3776 | boost::optional<RGWPutObj_Compress> compressor; | |
3777 | ||
3778 | std::unique_ptr<DataProcessor> encrypt; | |
eafe8130 TL |
3779 | |
3780 | if (!append) { // compression and encryption only apply to full object uploads | |
3781 | op_ret = get_encrypt_filter(&encrypt, filter); | |
3782 | if (op_ret < 0) { | |
3783 | return; | |
3784 | } | |
3785 | if (encrypt != nullptr) { | |
3786 | filter = &*encrypt; | |
3787 | } else if (compression_type != "none") { | |
3788 | plugin = get_compressor_plugin(s, compression_type); | |
3789 | if (!plugin) { | |
3790 | ldpp_dout(this, 1) << "Cannot load plugin for compression type " | |
3791 | << compression_type << dendl; | |
3792 | } else { | |
3793 | compressor.emplace(s->cct, plugin, filter); | |
3794 | filter = &*compressor; | |
3795 | } | |
7c673cae FG |
3796 | } |
3797 | } | |
11fdf7f2 | 3798 | tracepoint(rgw_op, before_data_transfer, s->req_id.c_str()); |
7c673cae | 3799 | do { |
31f18b77 | 3800 | bufferlist data; |
7c673cae FG |
3801 | if (fst > lst) |
3802 | break; | |
3a9019d9 | 3803 | if (copy_source.empty()) { |
31f18b77 | 3804 | len = get_data(data); |
7c673cae FG |
3805 | } else { |
3806 | uint64_t cur_lst = min(fst + s->cct->_conf->rgw_max_chunk_size - 1, lst); | |
31f18b77 | 3807 | op_ret = get_data(fst, cur_lst, data); |
7c673cae | 3808 | if (op_ret < 0) |
11fdf7f2 | 3809 | return; |
31f18b77 | 3810 | len = data.length(); |
7c673cae FG |
3811 | s->content_length += len; |
3812 | fst += len; | |
3813 | } | |
3814 | if (len < 0) { | |
3815 | op_ret = len; | |
11fdf7f2 TL |
3816 | ldpp_dout(this, 20) << "get_data() returned ret=" << op_ret << dendl; |
3817 | return; | |
3818 | } else if (len == 0) { | |
3819 | break; | |
7c673cae FG |
3820 | } |
3821 | ||
7c673cae | 3822 | if (need_calc_md5) { |
11fdf7f2 | 3823 | hash.Update((const unsigned char *)data.c_str(), data.length()); |
7c673cae FG |
3824 | } |
3825 | ||
31f18b77 FG |
3826 | /* update torrrent */ |
3827 | torrent.update(data); | |
7c673cae | 3828 | |
11fdf7f2 | 3829 | op_ret = filter->process(std::move(data), ofs); |
7c673cae | 3830 | if (op_ret < 0) { |
11fdf7f2 TL |
3831 | ldpp_dout(this, 20) << "processor->process() returned ret=" |
3832 | << op_ret << dendl; | |
3833 | return; | |
7c673cae FG |
3834 | } |
3835 | ||
3836 | ofs += len; | |
3837 | } while (len > 0); | |
11fdf7f2 | 3838 | tracepoint(rgw_op, after_data_transfer, s->req_id.c_str(), ofs); |
7c673cae | 3839 | |
11fdf7f2 TL |
3840 | // flush any data in filters |
3841 | op_ret = filter->process({}, ofs); | |
3842 | if (op_ret < 0) { | |
3843 | return; | |
7c673cae FG |
3844 | } |
3845 | ||
31f18b77 | 3846 | if (!chunked_upload && ofs != s->content_length) { |
7c673cae | 3847 | op_ret = -ERR_REQUEST_TIMEOUT; |
11fdf7f2 | 3848 | return; |
7c673cae FG |
3849 | } |
3850 | s->obj_size = ofs; | |
3851 | ||
3852 | perfcounter->inc(l_rgw_put_b, s->obj_size); | |
3853 | ||
31f18b77 FG |
3854 | op_ret = do_aws4_auth_completion(); |
3855 | if (op_ret < 0) { | |
11fdf7f2 | 3856 | return; |
7c673cae | 3857 | } |
31f18b77 | 3858 | |
7c673cae FG |
3859 | op_ret = store->check_quota(s->bucket_owner.get_id(), s->bucket, |
3860 | user_quota, bucket_quota, s->obj_size); | |
3861 | if (op_ret < 0) { | |
11fdf7f2 TL |
3862 | ldpp_dout(this, 20) << "second check_quota() returned op_ret=" << op_ret << dendl; |
3863 | return; | |
7c673cae FG |
3864 | } |
3865 | ||
31f18b77 FG |
3866 | op_ret = store->check_bucket_shards(s->bucket_info, s->bucket, bucket_quota); |
3867 | if (op_ret < 0) { | |
11fdf7f2 TL |
3868 | ldpp_dout(this, 20) << "check_bucket_shards() returned ret=" << op_ret << dendl; |
3869 | return; | |
31f18b77 FG |
3870 | } |
3871 | ||
7c673cae FG |
3872 | hash.Final(m); |
3873 | ||
3874 | if (compressor && compressor->is_compressed()) { | |
3875 | bufferlist tmp; | |
3876 | RGWCompressionInfo cs_info; | |
3877 | cs_info.compression_type = plugin->get_type_name(); | |
3878 | cs_info.orig_size = s->obj_size; | |
3879 | cs_info.blocks = move(compressor->get_compression_blocks()); | |
11fdf7f2 | 3880 | encode(cs_info, tmp); |
7c673cae | 3881 | attrs[RGW_ATTR_COMPRESSION] = tmp; |
11fdf7f2 | 3882 | ldpp_dout(this, 20) << "storing " << RGW_ATTR_COMPRESSION |
7c673cae FG |
3883 | << " with type=" << cs_info.compression_type |
3884 | << ", orig_size=" << cs_info.orig_size | |
3885 | << ", blocks=" << cs_info.blocks.size() << dendl; | |
3886 | } | |
3887 | ||
3888 | buf_to_hex(m, CEPH_CRYPTO_MD5_DIGESTSIZE, calc_md5); | |
3889 | ||
3890 | etag = calc_md5; | |
3891 | ||
3892 | if (supplied_md5_b64 && strcmp(calc_md5, supplied_md5)) { | |
3893 | op_ret = -ERR_BAD_DIGEST; | |
11fdf7f2 | 3894 | return; |
7c673cae FG |
3895 | } |
3896 | ||
3897 | policy.encode(aclbl); | |
3898 | emplace_attr(RGW_ATTR_ACL, std::move(aclbl)); | |
3899 | ||
3900 | if (dlo_manifest) { | |
3901 | op_ret = encode_dlo_manifest_attr(dlo_manifest, attrs); | |
3902 | if (op_ret < 0) { | |
11fdf7f2 TL |
3903 | ldpp_dout(this, 0) << "bad user manifest: " << dlo_manifest << dendl; |
3904 | return; | |
7c673cae | 3905 | } |
7c673cae FG |
3906 | } |
3907 | ||
3908 | if (slo_info) { | |
3909 | bufferlist manifest_bl; | |
11fdf7f2 | 3910 | encode(*slo_info, manifest_bl); |
7c673cae | 3911 | emplace_attr(RGW_ATTR_SLO_MANIFEST, std::move(manifest_bl)); |
7c673cae FG |
3912 | } |
3913 | ||
3914 | if (supplied_etag && etag.compare(supplied_etag) != 0) { | |
3915 | op_ret = -ERR_UNPROCESSABLE_ENTITY; | |
11fdf7f2 | 3916 | return; |
7c673cae | 3917 | } |
11fdf7f2 | 3918 | bl.append(etag.c_str(), etag.size()); |
7c673cae FG |
3919 | emplace_attr(RGW_ATTR_ETAG, std::move(bl)); |
3920 | ||
3921 | populate_with_generic_attrs(s, attrs); | |
3efd9988 FG |
3922 | op_ret = rgw_get_request_metadata(s->cct, s->info, attrs); |
3923 | if (op_ret < 0) { | |
11fdf7f2 | 3924 | return; |
3efd9988 | 3925 | } |
7c673cae | 3926 | encode_delete_at_attr(delete_at, attrs); |
224ce89b | 3927 | encode_obj_tags_attr(obj_tags.get(), attrs); |
7c673cae FG |
3928 | |
3929 | /* Add a custom metadata to expose the information whether an object | |
3930 | * is an SLO or not. Appending the attribute must be performed AFTER | |
3931 | * processing any input from user in order to prohibit overwriting. */ | |
3932 | if (slo_info) { | |
3933 | bufferlist slo_userindicator_bl; | |
31f18b77 | 3934 | slo_userindicator_bl.append("True", 4); |
7c673cae FG |
3935 | emplace_attr(RGW_ATTR_SLO_UINDICATOR, std::move(slo_userindicator_bl)); |
3936 | } | |
eafe8130 TL |
3937 | if (obj_legal_hold) { |
3938 | bufferlist obj_legal_hold_bl; | |
3939 | obj_legal_hold->encode(obj_legal_hold_bl); | |
3940 | emplace_attr(RGW_ATTR_OBJECT_LEGAL_HOLD, std::move(obj_legal_hold_bl)); | |
3941 | } | |
3942 | if (obj_retention) { | |
3943 | bufferlist obj_retention_bl; | |
3944 | obj_retention->encode(obj_retention_bl); | |
3945 | emplace_attr(RGW_ATTR_OBJECT_RETENTION, std::move(obj_retention_bl)); | |
3946 | } | |
7c673cae | 3947 | |
11fdf7f2 | 3948 | tracepoint(rgw_op, processor_complete_enter, s->req_id.c_str()); |
7c673cae FG |
3949 | op_ret = processor->complete(s->obj_size, etag, &mtime, real_time(), attrs, |
3950 | (delete_at ? *delete_at : real_time()), if_match, if_nomatch, | |
11fdf7f2 TL |
3951 | (user_data.empty() ? nullptr : &user_data), nullptr, nullptr); |
3952 | tracepoint(rgw_op, processor_complete_exit, s->req_id.c_str()); | |
7c673cae FG |
3953 | |
3954 | /* produce torrent */ | |
3955 | if (s->cct->_conf->rgw_torrent_flag && (ofs == torrent.get_data_len())) | |
3956 | { | |
3957 | torrent.init(s, store); | |
3958 | torrent.set_create_date(mtime); | |
31f18b77 | 3959 | op_ret = torrent.complete(); |
7c673cae FG |
3960 | if (0 != op_ret) |
3961 | { | |
11fdf7f2 TL |
3962 | ldpp_dout(this, 0) << "ERROR: torrent.handle_data() returned " << op_ret << dendl; |
3963 | return; | |
7c673cae FG |
3964 | } |
3965 | } | |
eafe8130 TL |
3966 | |
3967 | // send request to notification manager | |
3968 | const auto ret = rgw::notify::publish(s, mtime, etag, rgw::notify::ObjectCreatedPut, store); | |
3969 | if (ret < 0) { | |
3970 | ldpp_dout(this, 5) << "WARNING: publishing notification failed, with error: " << ret << dendl; | |
3971 | // TODO: we should have conf to make send a blocking coroutine and reply with error in case sending failed | |
3972 | // this should be global conf (probably returnign a different handler) | |
3973 | // so we don't need to read the configured values before we perform it | |
3974 | } | |
7c673cae FG |
3975 | } |
3976 | ||
3977 | int RGWPostObj::verify_permission() | |
3978 | { | |
11fdf7f2 | 3979 | return 0; |
7c673cae | 3980 | } |
11fdf7f2 | 3981 | |
7c673cae FG |
3982 | void RGWPostObj::pre_exec() |
3983 | { | |
3984 | rgw_bucket_object_pre_exec(s); | |
3985 | } | |
3986 | ||
3987 | void RGWPostObj::execute() | |
3988 | { | |
7c673cae FG |
3989 | boost::optional<RGWPutObj_Compress> compressor; |
3990 | CompressorRef plugin; | |
224ce89b | 3991 | char supplied_md5[CEPH_CRYPTO_MD5_DIGESTSIZE * 2 + 1]; |
7c673cae FG |
3992 | |
3993 | /* Read in the data from the POST form. */ | |
3994 | op_ret = get_params(); | |
3995 | if (op_ret < 0) { | |
3996 | return; | |
3997 | } | |
3998 | ||
3999 | op_ret = verify_params(); | |
4000 | if (op_ret < 0) { | |
4001 | return; | |
4002 | } | |
4003 | ||
11fdf7f2 TL |
4004 | if (s->iam_policy || ! s->iam_user_policies.empty()) { |
4005 | auto usr_policy_res = eval_user_policies(s->iam_user_policies, s->env, | |
4006 | boost::none, | |
4007 | rgw::IAM::s3PutObject, | |
4008 | rgw_obj(s->bucket, s->object)); | |
4009 | if (usr_policy_res == Effect::Deny) { | |
4010 | op_ret = -EACCES; | |
4011 | return; | |
4012 | } | |
4013 | ||
4014 | rgw::IAM::Effect e = Effect::Pass; | |
4015 | if (s->iam_policy) { | |
4016 | e = s->iam_policy->eval(s->env, *s->auth.identity, | |
31f18b77 FG |
4017 | rgw::IAM::s3PutObject, |
4018 | rgw_obj(s->bucket, s->object)); | |
11fdf7f2 | 4019 | } |
31f18b77 FG |
4020 | if (e == Effect::Deny) { |
4021 | op_ret = -EACCES; | |
4022 | return; | |
11fdf7f2 | 4023 | } else if (usr_policy_res == Effect::Pass && e == Effect::Pass && !verify_bucket_permission_no_policy(this, s, RGW_PERM_WRITE)) { |
31f18b77 FG |
4024 | op_ret = -EACCES; |
4025 | return; | |
4026 | } | |
11fdf7f2 | 4027 | } else if (!verify_bucket_permission_no_policy(this, s, RGW_PERM_WRITE)) { |
7c673cae FG |
4028 | op_ret = -EACCES; |
4029 | return; | |
4030 | } | |
4031 | ||
4032 | /* Start iteration over data fields. It's necessary as Swift's FormPost | |
4033 | * is capable to handle multiple files in single form. */ | |
4034 | do { | |
7c673cae FG |
4035 | char calc_md5[CEPH_CRYPTO_MD5_DIGESTSIZE * 2 + 1]; |
4036 | unsigned char m[CEPH_CRYPTO_MD5_DIGESTSIZE]; | |
4037 | MD5 hash; | |
4038 | ceph::buffer::list bl, aclbl; | |
4039 | int len = 0; | |
4040 | ||
4041 | op_ret = store->check_quota(s->bucket_owner.get_id(), | |
4042 | s->bucket, | |
4043 | user_quota, | |
4044 | bucket_quota, | |
4045 | s->content_length); | |
4046 | if (op_ret < 0) { | |
4047 | return; | |
4048 | } | |
4049 | ||
31f18b77 FG |
4050 | op_ret = store->check_bucket_shards(s->bucket_info, s->bucket, bucket_quota); |
4051 | if (op_ret < 0) { | |
4052 | return; | |
4053 | } | |
4054 | ||
224ce89b WB |
4055 | if (supplied_md5_b64) { |
4056 | char supplied_md5_bin[CEPH_CRYPTO_MD5_DIGESTSIZE + 1]; | |
11fdf7f2 | 4057 | ldpp_dout(this, 15) << "supplied_md5_b64=" << supplied_md5_b64 << dendl; |
224ce89b WB |
4058 | op_ret = ceph_unarmor(supplied_md5_bin, &supplied_md5_bin[CEPH_CRYPTO_MD5_DIGESTSIZE + 1], |
4059 | supplied_md5_b64, supplied_md5_b64 + strlen(supplied_md5_b64)); | |
11fdf7f2 | 4060 | ldpp_dout(this, 15) << "ceph_armor ret=" << op_ret << dendl; |
224ce89b WB |
4061 | if (op_ret != CEPH_CRYPTO_MD5_DIGESTSIZE) { |
4062 | op_ret = -ERR_INVALID_DIGEST; | |
4063 | return; | |
4064 | } | |
4065 | ||
4066 | buf_to_hex((const unsigned char *)supplied_md5_bin, CEPH_CRYPTO_MD5_DIGESTSIZE, supplied_md5); | |
11fdf7f2 TL |
4067 | ldpp_dout(this, 15) << "supplied_md5=" << supplied_md5 << dendl; |
4068 | } | |
4069 | ||
4070 | rgw_obj obj(s->bucket, get_current_filename()); | |
4071 | if (s->bucket_info.versioning_enabled()) { | |
4072 | store->gen_rand_obj_instance_name(&obj); | |
4073 | } | |
7c673cae | 4074 | |
11fdf7f2 TL |
4075 | rgw::AioThrottle aio(s->cct->_conf->rgw_put_obj_min_window_size); |
4076 | ||
4077 | using namespace rgw::putobj; | |
4078 | AtomicObjectProcessor processor(&aio, store, s->bucket_info, | |
4079 | &s->dest_placement, | |
4080 | s->bucket_owner.get_id(), | |
4081 | *static_cast<RGWObjectCtx*>(s->obj_ctx), | |
4082 | obj, 0, s->req_id); | |
4083 | op_ret = processor.prepare(); | |
7c673cae FG |
4084 | if (op_ret < 0) { |
4085 | return; | |
4086 | } | |
4087 | ||
11fdf7f2 TL |
4088 | /* No filters by default. */ |
4089 | DataProcessor *filter = &processor; | |
4090 | ||
4091 | std::unique_ptr<DataProcessor> encrypt; | |
7c673cae FG |
4092 | op_ret = get_encrypt_filter(&encrypt, filter); |
4093 | if (op_ret < 0) { | |
4094 | return; | |
4095 | } | |
4096 | if (encrypt != nullptr) { | |
4097 | filter = encrypt.get(); | |
4098 | } else { | |
11fdf7f2 TL |
4099 | const auto& compression_type = store->svc.zone->get_zone_params().get_compression_type( |
4100 | s->dest_placement); | |
7c673cae FG |
4101 | if (compression_type != "none") { |
4102 | plugin = Compressor::create(s->cct, compression_type); | |
4103 | if (!plugin) { | |
11fdf7f2 | 4104 | ldpp_dout(this, 1) << "Cannot load plugin for compression type " |
7c673cae FG |
4105 | << compression_type << dendl; |
4106 | } else { | |
4107 | compressor.emplace(s->cct, plugin, filter); | |
4108 | filter = &*compressor; | |
4109 | } | |
4110 | } | |
4111 | } | |
4112 | ||
4113 | bool again; | |
4114 | do { | |
4115 | ceph::bufferlist data; | |
4116 | len = get_data(data, again); | |
4117 | ||
4118 | if (len < 0) { | |
4119 | op_ret = len; | |
4120 | return; | |
4121 | } | |
4122 | ||
4123 | if (!len) { | |
4124 | break; | |
4125 | } | |
4126 | ||
11fdf7f2 TL |
4127 | hash.Update((const unsigned char *)data.c_str(), data.length()); |
4128 | op_ret = filter->process(std::move(data), ofs); | |
7c673cae FG |
4129 | |
4130 | ofs += len; | |
4131 | ||
4132 | if (ofs > max_len) { | |
4133 | op_ret = -ERR_TOO_LARGE; | |
4134 | return; | |
4135 | } | |
4136 | } while (again); | |
4137 | ||
11fdf7f2 TL |
4138 | // flush |
4139 | op_ret = filter->process({}, ofs); | |
4140 | if (op_ret < 0) { | |
4141 | return; | |
7c673cae FG |
4142 | } |
4143 | ||
4144 | if (len < min_len) { | |
4145 | op_ret = -ERR_TOO_SMALL; | |
4146 | return; | |
4147 | } | |
4148 | ||
4149 | s->obj_size = ofs; | |
4150 | ||
224ce89b | 4151 | |
7c673cae FG |
4152 | op_ret = store->check_quota(s->bucket_owner.get_id(), s->bucket, |
4153 | user_quota, bucket_quota, s->obj_size); | |
4154 | if (op_ret < 0) { | |
4155 | return; | |
4156 | } | |
4157 | ||
31f18b77 FG |
4158 | op_ret = store->check_bucket_shards(s->bucket_info, s->bucket, bucket_quota); |
4159 | if (op_ret < 0) { | |
4160 | return; | |
4161 | } | |
4162 | ||
7c673cae FG |
4163 | hash.Final(m); |
4164 | buf_to_hex(m, CEPH_CRYPTO_MD5_DIGESTSIZE, calc_md5); | |
4165 | ||
4166 | etag = calc_md5; | |
11fdf7f2 TL |
4167 | |
4168 | if (supplied_md5_b64 && strcmp(calc_md5, supplied_md5)) { | |
4169 | op_ret = -ERR_BAD_DIGEST; | |
4170 | return; | |
4171 | } | |
4172 | ||
4173 | bl.append(etag.c_str(), etag.size()); | |
7c673cae FG |
4174 | emplace_attr(RGW_ATTR_ETAG, std::move(bl)); |
4175 | ||
4176 | policy.encode(aclbl); | |
4177 | emplace_attr(RGW_ATTR_ACL, std::move(aclbl)); | |
4178 | ||
4179 | const std::string content_type = get_current_content_type(); | |
4180 | if (! content_type.empty()) { | |
4181 | ceph::bufferlist ct_bl; | |
4182 | ct_bl.append(content_type.c_str(), content_type.size() + 1); | |
4183 | emplace_attr(RGW_ATTR_CONTENT_TYPE, std::move(ct_bl)); | |
4184 | } | |
4185 | ||
4186 | if (compressor && compressor->is_compressed()) { | |
4187 | ceph::bufferlist tmp; | |
4188 | RGWCompressionInfo cs_info; | |
4189 | cs_info.compression_type = plugin->get_type_name(); | |
4190 | cs_info.orig_size = s->obj_size; | |
4191 | cs_info.blocks = move(compressor->get_compression_blocks()); | |
11fdf7f2 | 4192 | encode(cs_info, tmp); |
7c673cae FG |
4193 | emplace_attr(RGW_ATTR_COMPRESSION, std::move(tmp)); |
4194 | } | |
4195 | ||
11fdf7f2 TL |
4196 | op_ret = processor.complete(s->obj_size, etag, nullptr, real_time(), attrs, |
4197 | (delete_at ? *delete_at : real_time()), | |
4198 | nullptr, nullptr, nullptr, nullptr, nullptr); | |
4199 | if (op_ret < 0) { | |
4200 | return; | |
4201 | } | |
7c673cae | 4202 | } while (is_next_file_to_upload()); |
eafe8130 TL |
4203 | |
4204 | const auto ret = rgw::notify::publish(s, ceph::real_clock::now(), etag, rgw::notify::ObjectCreatedPost, store); | |
4205 | if (ret < 0) { | |
4206 | ldpp_dout(this, 5) << "WARNING: publishing notification failed, with error: " << ret << dendl; | |
4207 | // TODO: we should have conf to make send a blocking coroutine and reply with error in case sending failed | |
4208 | // this should be global conf (probably returnign a different handler) | |
4209 | // so we don't need to read the configured values before we perform it | |
4210 | } | |
7c673cae FG |
4211 | } |
4212 | ||
4213 | ||
4214 | void RGWPutMetadataAccount::filter_out_temp_url(map<string, bufferlist>& add_attrs, | |
4215 | const set<string>& rmattr_names, | |
4216 | map<int, string>& temp_url_keys) | |
4217 | { | |
4218 | map<string, bufferlist>::iterator iter; | |
4219 | ||
4220 | iter = add_attrs.find(RGW_ATTR_TEMPURL_KEY1); | |
4221 | if (iter != add_attrs.end()) { | |
4222 | temp_url_keys[0] = iter->second.c_str(); | |
4223 | add_attrs.erase(iter); | |
4224 | } | |
4225 | ||
4226 | iter = add_attrs.find(RGW_ATTR_TEMPURL_KEY2); | |
4227 | if (iter != add_attrs.end()) { | |
4228 | temp_url_keys[1] = iter->second.c_str(); | |
4229 | add_attrs.erase(iter); | |
4230 | } | |
4231 | ||
4232 | for (const string& name : rmattr_names) { | |
4233 | if (name.compare(RGW_ATTR_TEMPURL_KEY1) == 0) { | |
4234 | temp_url_keys[0] = string(); | |
4235 | } | |
4236 | if (name.compare(RGW_ATTR_TEMPURL_KEY2) == 0) { | |
4237 | temp_url_keys[1] = string(); | |
4238 | } | |
4239 | } | |
4240 | } | |
4241 | ||
4242 | int RGWPutMetadataAccount::init_processing() | |
4243 | { | |
4244 | /* First, go to the base class. At the time of writing the method was | |
4245 | * responsible only for initializing the quota. This isn't necessary | |
4246 | * here as we are touching metadata only. I'm putting this call only | |
4247 | * for the future. */ | |
4248 | op_ret = RGWOp::init_processing(); | |
4249 | if (op_ret < 0) { | |
4250 | return op_ret; | |
4251 | } | |
4252 | ||
4253 | op_ret = get_params(); | |
4254 | if (op_ret < 0) { | |
4255 | return op_ret; | |
4256 | } | |
4257 | ||
4258 | op_ret = rgw_get_user_attrs_by_uid(store, s->user->user_id, orig_attrs, | |
4259 | &acct_op_tracker); | |
4260 | if (op_ret < 0) { | |
4261 | return op_ret; | |
4262 | } | |
4263 | ||
4264 | if (has_policy) { | |
4265 | bufferlist acl_bl; | |
4266 | policy.encode(acl_bl); | |
4267 | attrs.emplace(RGW_ATTR_ACL, std::move(acl_bl)); | |
4268 | } | |
4269 | ||
3efd9988 FG |
4270 | op_ret = rgw_get_request_metadata(s->cct, s->info, attrs, false); |
4271 | if (op_ret < 0) { | |
4272 | return op_ret; | |
4273 | } | |
7c673cae FG |
4274 | prepare_add_del_attrs(orig_attrs, rmattr_names, attrs); |
4275 | populate_with_generic_attrs(s, attrs); | |
4276 | ||
4277 | /* Try extract the TempURL-related stuff now to allow verify_permission | |
4278 | * evaluate whether we need FULL_CONTROL or not. */ | |
4279 | filter_out_temp_url(attrs, rmattr_names, temp_url_keys); | |
4280 | ||
4281 | /* The same with quota except a client needs to be reseller admin. */ | |
4282 | op_ret = filter_out_quota_info(attrs, rmattr_names, new_quota, | |
4283 | &new_quota_extracted); | |
4284 | if (op_ret < 0) { | |
4285 | return op_ret; | |
4286 | } | |
4287 | ||
4288 | return 0; | |
4289 | } | |
4290 | ||
4291 | int RGWPutMetadataAccount::verify_permission() | |
4292 | { | |
4293 | if (s->auth.identity->is_anonymous()) { | |
4294 | return -EACCES; | |
4295 | } | |
4296 | ||
11fdf7f2 | 4297 | if (!verify_user_permission_no_policy(this, s, RGW_PERM_WRITE)) { |
7c673cae FG |
4298 | return -EACCES; |
4299 | } | |
4300 | ||
4301 | /* Altering TempURL keys requires FULL_CONTROL. */ | |
4302 | if (!temp_url_keys.empty() && s->perm_mask != RGW_PERM_FULL_CONTROL) { | |
4303 | return -EPERM; | |
4304 | } | |
4305 | ||
4306 | /* We are failing this intensionally to allow system user/reseller admin | |
4307 | * override in rgw_process.cc. This is the way to specify a given RGWOp | |
4308 | * expect extra privileges. */ | |
4309 | if (new_quota_extracted) { | |
4310 | return -EACCES; | |
4311 | } | |
4312 | ||
4313 | return 0; | |
4314 | } | |
4315 | ||
4316 | void RGWPutMetadataAccount::execute() | |
4317 | { | |
4318 | /* Params have been extracted earlier. See init_processing(). */ | |
4319 | RGWUserInfo new_uinfo; | |
4320 | op_ret = rgw_get_user_info_by_uid(store, s->user->user_id, new_uinfo, | |
4321 | &acct_op_tracker); | |
4322 | if (op_ret < 0) { | |
4323 | return; | |
4324 | } | |
4325 | ||
4326 | /* Handle the TempURL-related stuff. */ | |
4327 | if (!temp_url_keys.empty()) { | |
4328 | for (auto& pair : temp_url_keys) { | |
4329 | new_uinfo.temp_url_keys[pair.first] = std::move(pair.second); | |
4330 | } | |
4331 | } | |
4332 | ||
4333 | /* Handle the quota extracted at the verify_permission step. */ | |
4334 | if (new_quota_extracted) { | |
4335 | new_uinfo.user_quota = std::move(new_quota); | |
4336 | } | |
4337 | ||
4338 | /* We are passing here the current (old) user info to allow the function | |
4339 | * optimize-out some operations. */ | |
4340 | op_ret = rgw_store_user_info(store, new_uinfo, s->user, | |
4341 | &acct_op_tracker, real_time(), false, &attrs); | |
4342 | } | |
4343 | ||
4344 | int RGWPutMetadataBucket::verify_permission() | |
4345 | { | |
11fdf7f2 | 4346 | if (!verify_bucket_permission_no_policy(this, s, RGW_PERM_WRITE)) { |
7c673cae FG |
4347 | return -EACCES; |
4348 | } | |
4349 | ||
4350 | return 0; | |
4351 | } | |
4352 | ||
4353 | void RGWPutMetadataBucket::pre_exec() | |
4354 | { | |
4355 | rgw_bucket_object_pre_exec(s); | |
4356 | } | |
4357 | ||
4358 | void RGWPutMetadataBucket::execute() | |
4359 | { | |
4360 | op_ret = get_params(); | |
4361 | if (op_ret < 0) { | |
4362 | return; | |
4363 | } | |
4364 | ||
3efd9988 FG |
4365 | op_ret = rgw_get_request_metadata(s->cct, s->info, attrs, false); |
4366 | if (op_ret < 0) { | |
4367 | return; | |
4368 | } | |
7c673cae FG |
4369 | |
4370 | if (!placement_rule.empty() && | |
4371 | placement_rule != s->bucket_info.placement_rule) { | |
4372 | op_ret = -EEXIST; | |
4373 | return; | |
4374 | } | |
4375 | ||
b32b8144 FG |
4376 | op_ret = retry_raced_bucket_write(store, s, [this] { |
4377 | /* Encode special metadata first as we're using std::map::emplace under | |
4378 | * the hood. This method will add the new items only if the map doesn't | |
4379 | * contain such keys yet. */ | |
4380 | if (has_policy) { | |
4381 | if (s->dialect.compare("swift") == 0) { | |
4382 | auto old_policy = \ | |
4383 | static_cast<RGWAccessControlPolicy_SWIFT*>(s->bucket_acl.get()); | |
4384 | auto new_policy = static_cast<RGWAccessControlPolicy_SWIFT*>(&policy); | |
4385 | new_policy->filter_merge(policy_rw_mask, old_policy); | |
4386 | policy = *new_policy; | |
4387 | } | |
4388 | buffer::list bl; | |
4389 | policy.encode(bl); | |
4390 | emplace_attr(RGW_ATTR_ACL, std::move(bl)); | |
4391 | } | |
7c673cae | 4392 | |
b32b8144 FG |
4393 | if (has_cors) { |
4394 | buffer::list bl; | |
4395 | cors_config.encode(bl); | |
4396 | emplace_attr(RGW_ATTR_CORS, std::move(bl)); | |
4397 | } | |
7c673cae | 4398 | |
b32b8144 FG |
4399 | /* It's supposed that following functions WILL NOT change any |
4400 | * special attributes (like RGW_ATTR_ACL) if they are already | |
4401 | * present in attrs. */ | |
4402 | prepare_add_del_attrs(s->bucket_attrs, rmattr_names, attrs); | |
4403 | populate_with_generic_attrs(s, attrs); | |
7c673cae | 4404 | |
b32b8144 FG |
4405 | /* According to the Swift's behaviour and its container_quota |
4406 | * WSGI middleware implementation: anyone with write permissions | |
4407 | * is able to set the bucket quota. This stays in contrast to | |
4408 | * account quotas that can be set only by clients holding | |
4409 | * reseller admin privileges. */ | |
4410 | op_ret = filter_out_quota_info(attrs, rmattr_names, s->bucket_info.quota); | |
4411 | if (op_ret < 0) { | |
4412 | return op_ret; | |
4413 | } | |
7c673cae | 4414 | |
b32b8144 FG |
4415 | if (swift_ver_location) { |
4416 | s->bucket_info.swift_ver_location = *swift_ver_location; | |
4417 | s->bucket_info.swift_versioning = (!swift_ver_location->empty()); | |
4418 | } | |
7c673cae | 4419 | |
b32b8144 FG |
4420 | /* Web site of Swift API. */ |
4421 | filter_out_website(attrs, rmattr_names, s->bucket_info.website_conf); | |
4422 | s->bucket_info.has_website = !s->bucket_info.website_conf.is_empty(); | |
7c673cae | 4423 | |
b32b8144 FG |
4424 | /* Setting attributes also stores the provided bucket info. Due |
4425 | * to this fact, the new quota settings can be serialized with | |
4426 | * the same call. */ | |
4427 | op_ret = rgw_bucket_set_attrs(store, s->bucket_info, attrs, | |
4428 | &s->bucket_info.objv_tracker); | |
4429 | return op_ret; | |
4430 | }); | |
7c673cae FG |
4431 | } |
4432 | ||
4433 | int RGWPutMetadataObject::verify_permission() | |
4434 | { | |
31f18b77 FG |
4435 | // This looks to be something specific to Swift. We could add |
4436 | // operations like swift:PutMetadataObject to the Policy Engine. | |
11fdf7f2 | 4437 | if (!verify_object_permission_no_policy(this, s, RGW_PERM_WRITE)) { |
7c673cae FG |
4438 | return -EACCES; |
4439 | } | |
4440 | ||
4441 | return 0; | |
4442 | } | |
4443 | ||
4444 | void RGWPutMetadataObject::pre_exec() | |
4445 | { | |
4446 | rgw_bucket_object_pre_exec(s); | |
4447 | } | |
4448 | ||
4449 | void RGWPutMetadataObject::execute() | |
4450 | { | |
4451 | rgw_obj obj(s->bucket, s->object); | |
eafe8130 | 4452 | rgw_obj target_obj; |
7c673cae FG |
4453 | map<string, bufferlist> attrs, orig_attrs, rmattrs; |
4454 | ||
4455 | store->set_atomic(s->obj_ctx, obj); | |
4456 | ||
4457 | op_ret = get_params(); | |
4458 | if (op_ret < 0) { | |
4459 | return; | |
4460 | } | |
4461 | ||
3efd9988 FG |
4462 | op_ret = rgw_get_request_metadata(s->cct, s->info, attrs); |
4463 | if (op_ret < 0) { | |
4464 | return; | |
4465 | } | |
4466 | ||
7c673cae | 4467 | /* check if obj exists, read orig attrs */ |
eafe8130 | 4468 | op_ret = get_obj_attrs(store, s, obj, orig_attrs, &target_obj); |
7c673cae FG |
4469 | if (op_ret < 0) { |
4470 | return; | |
4471 | } | |
4472 | ||
4473 | /* Check whether the object has expired. Swift API documentation | |
4474 | * stands that we should return 404 Not Found in such case. */ | |
4475 | if (need_object_expiration() && object_is_expired(orig_attrs)) { | |
4476 | op_ret = -ENOENT; | |
4477 | return; | |
4478 | } | |
4479 | ||
4480 | /* Filter currently existing attributes. */ | |
4481 | prepare_add_del_attrs(orig_attrs, attrs, rmattrs); | |
4482 | populate_with_generic_attrs(s, attrs); | |
4483 | encode_delete_at_attr(delete_at, attrs); | |
4484 | ||
4485 | if (dlo_manifest) { | |
4486 | op_ret = encode_dlo_manifest_attr(dlo_manifest, attrs); | |
4487 | if (op_ret < 0) { | |
11fdf7f2 | 4488 | ldpp_dout(this, 0) << "bad user manifest: " << dlo_manifest << dendl; |
7c673cae FG |
4489 | return; |
4490 | } | |
4491 | } | |
4492 | ||
eafe8130 | 4493 | op_ret = store->set_attrs(s->obj_ctx, s->bucket_info, target_obj, attrs, &rmattrs); |
7c673cae FG |
4494 | } |
4495 | ||
4496 | int RGWDeleteObj::handle_slo_manifest(bufferlist& bl) | |
4497 | { | |
4498 | RGWSLOInfo slo_info; | |
11fdf7f2 | 4499 | auto bliter = bl.cbegin(); |
7c673cae | 4500 | try { |
11fdf7f2 | 4501 | decode(slo_info, bliter); |
7c673cae | 4502 | } catch (buffer::error& err) { |
11fdf7f2 | 4503 | ldpp_dout(this, 0) << "ERROR: failed to decode slo manifest" << dendl; |
7c673cae FG |
4504 | return -EIO; |
4505 | } | |
4506 | ||
4507 | try { | |
4508 | deleter = std::unique_ptr<RGWBulkDelete::Deleter>(\ | |
11fdf7f2 TL |
4509 | new RGWBulkDelete::Deleter(this, store, s)); |
4510 | } catch (const std::bad_alloc&) { | |
7c673cae FG |
4511 | return -ENOMEM; |
4512 | } | |
4513 | ||
4514 | list<RGWBulkDelete::acct_path_t> items; | |
4515 | for (const auto& iter : slo_info.entries) { | |
4516 | const string& path_str = iter.path; | |
4517 | ||
4518 | const size_t sep_pos = path_str.find('/', 1 /* skip first slash */); | |
31f18b77 | 4519 | if (boost::string_view::npos == sep_pos) { |
7c673cae FG |
4520 | return -EINVAL; |
4521 | } | |
4522 | ||
4523 | RGWBulkDelete::acct_path_t path; | |
4524 | ||
31f18b77 FG |
4525 | path.bucket_name = url_decode(path_str.substr(1, sep_pos - 1)); |
4526 | path.obj_key = url_decode(path_str.substr(sep_pos + 1)); | |
7c673cae FG |
4527 | |
4528 | items.push_back(path); | |
4529 | } | |
4530 | ||
4531 | /* Request removal of the manifest object itself. */ | |
4532 | RGWBulkDelete::acct_path_t path; | |
4533 | path.bucket_name = s->bucket_name; | |
4534 | path.obj_key = s->object; | |
4535 | items.push_back(path); | |
4536 | ||
4537 | int ret = deleter->delete_chunk(items); | |
4538 | if (ret < 0) { | |
4539 | return ret; | |
4540 | } | |
4541 | ||
4542 | return 0; | |
4543 | } | |
4544 | ||
4545 | int RGWDeleteObj::verify_permission() | |
4546 | { | |
eafe8130 TL |
4547 | int op_ret = get_params(); |
4548 | if (op_ret) { | |
4549 | return op_ret; | |
4550 | } | |
11fdf7f2 | 4551 | if (s->iam_policy || ! s->iam_user_policies.empty()) { |
eafe8130 TL |
4552 | if (s->bucket_info.obj_lock_enabled() && bypass_governance_mode) { |
4553 | auto r = eval_user_policies(s->iam_user_policies, s->env, boost::none, | |
4554 | rgw::IAM::s3BypassGovernanceRetention, ARN(s->bucket, s->object.name)); | |
4555 | if (r == Effect::Deny) { | |
4556 | bypass_perm = false; | |
4557 | } else if (r == Effect::Pass && s->iam_policy) { | |
4558 | r = s->iam_policy->eval(s->env, *s->auth.identity, rgw::IAM::s3BypassGovernanceRetention, | |
4559 | ARN(s->bucket, s->object.name)); | |
4560 | if (r == Effect::Deny) { | |
4561 | bypass_perm = false; | |
4562 | } | |
4563 | } | |
4564 | } | |
11fdf7f2 TL |
4565 | auto usr_policy_res = eval_user_policies(s->iam_user_policies, s->env, |
4566 | boost::none, | |
4567 | s->object.instance.empty() ? | |
4568 | rgw::IAM::s3DeleteObject : | |
4569 | rgw::IAM::s3DeleteObjectVersion, | |
4570 | ARN(s->bucket, s->object.name)); | |
4571 | if (usr_policy_res == Effect::Deny) { | |
4572 | return -EACCES; | |
4573 | } | |
4574 | ||
4575 | rgw::IAM::Effect r = Effect::Pass; | |
4576 | if (s->iam_policy) { | |
4577 | r = s->iam_policy->eval(s->env, *s->auth.identity, | |
31f18b77 FG |
4578 | s->object.instance.empty() ? |
4579 | rgw::IAM::s3DeleteObject : | |
4580 | rgw::IAM::s3DeleteObjectVersion, | |
4581 | ARN(s->bucket, s->object.name)); | |
11fdf7f2 | 4582 | } |
31f18b77 | 4583 | if (r == Effect::Allow) |
a8e16298 | 4584 | return 0; |
31f18b77 | 4585 | else if (r == Effect::Deny) |
a8e16298 | 4586 | return -EACCES; |
11fdf7f2 TL |
4587 | else if (usr_policy_res == Effect::Allow) |
4588 | return 0; | |
31f18b77 FG |
4589 | } |
4590 | ||
11fdf7f2 | 4591 | if (!verify_bucket_permission_no_policy(this, s, RGW_PERM_WRITE)) { |
7c673cae FG |
4592 | return -EACCES; |
4593 | } | |
4594 | ||
11fdf7f2 TL |
4595 | if (s->bucket_info.mfa_enabled() && |
4596 | !s->object.instance.empty() && | |
4597 | !s->mfa_verified) { | |
4598 | ldpp_dout(this, 5) << "NOTICE: object delete request with a versioned object, mfa auth not provided" << dendl; | |
4599 | return -ERR_MFA_REQUIRED; | |
4600 | } | |
4601 | ||
7c673cae FG |
4602 | return 0; |
4603 | } | |
4604 | ||
4605 | void RGWDeleteObj::pre_exec() | |
4606 | { | |
4607 | rgw_bucket_object_pre_exec(s); | |
4608 | } | |
4609 | ||
4610 | void RGWDeleteObj::execute() | |
4611 | { | |
4612 | if (!s->bucket_exists) { | |
4613 | op_ret = -ERR_NO_SUCH_BUCKET; | |
4614 | return; | |
4615 | } | |
4616 | ||
7c673cae FG |
4617 | rgw_obj obj(s->bucket, s->object); |
4618 | map<string, bufferlist> attrs; | |
4619 | ||
eafe8130 | 4620 | bool check_obj_lock = obj.key.have_instance() && s->bucket_info.obj_lock_enabled(); |
7c673cae FG |
4621 | |
4622 | if (!s->object.empty()) { | |
4623 | if (need_object_expiration() || multipart_delete) { | |
4624 | /* check if obj exists, read orig attrs */ | |
4625 | op_ret = get_obj_attrs(store, s, obj, attrs); | |
4626 | if (op_ret < 0) { | |
4627 | return; | |
4628 | } | |
4629 | } | |
4630 | ||
eafe8130 TL |
4631 | if (check_obj_lock) { |
4632 | /* check if obj exists, read orig attrs */ | |
4633 | op_ret = get_obj_attrs(store, s, obj, attrs); | |
4634 | if (op_ret < 0) { | |
4635 | if (op_ret == -ENOENT) { | |
4636 | /* object maybe delete_marker, skip check_obj_lock*/ | |
4637 | check_obj_lock = false; | |
4638 | } else { | |
4639 | return; | |
4640 | } | |
4641 | } | |
4642 | } | |
4643 | ||
4644 | if (check_obj_lock) { | |
4645 | auto aiter = attrs.find(RGW_ATTR_OBJECT_RETENTION); | |
4646 | if (aiter != attrs.end()) { | |
4647 | RGWObjectRetention obj_retention; | |
4648 | try { | |
4649 | decode(obj_retention, aiter->second); | |
4650 | } catch (buffer::error& err) { | |
4651 | ldpp_dout(this, 0) << "ERROR: failed to decode RGWObjectRetention" << dendl; | |
4652 | op_ret = -EIO; | |
4653 | return; | |
4654 | } | |
4655 | if (ceph::real_clock::to_time_t(obj_retention.get_retain_until_date()) > ceph_clock_now()) { | |
4656 | if (obj_retention.get_mode().compare("GOVERNANCE") != 0 || !bypass_perm || !bypass_governance_mode) { | |
4657 | op_ret = -EACCES; | |
4658 | return; | |
4659 | } | |
4660 | } | |
4661 | } | |
4662 | aiter = attrs.find(RGW_ATTR_OBJECT_LEGAL_HOLD); | |
4663 | if (aiter != attrs.end()) { | |
4664 | RGWObjectLegalHold obj_legal_hold; | |
4665 | try { | |
4666 | decode(obj_legal_hold, aiter->second); | |
4667 | } catch (buffer::error& err) { | |
4668 | ldpp_dout(this, 0) << "ERROR: failed to decode RGWObjectLegalHold" << dendl; | |
4669 | op_ret = -EIO; | |
4670 | return; | |
4671 | } | |
4672 | if (obj_legal_hold.is_enabled()) { | |
4673 | op_ret = -EACCES; | |
4674 | return; | |
4675 | } | |
4676 | } | |
4677 | } | |
4678 | ||
7c673cae FG |
4679 | if (multipart_delete) { |
4680 | const auto slo_attr = attrs.find(RGW_ATTR_SLO_MANIFEST); | |
4681 | ||
4682 | if (slo_attr != attrs.end()) { | |
4683 | op_ret = handle_slo_manifest(slo_attr->second); | |
4684 | if (op_ret < 0) { | |
11fdf7f2 | 4685 | ldpp_dout(this, 0) << "ERROR: failed to handle slo manifest ret=" << op_ret << dendl; |
7c673cae FG |
4686 | } |
4687 | } else { | |
4688 | op_ret = -ERR_NOT_SLO_MANIFEST; | |
4689 | } | |
4690 | ||
4691 | return; | |
4692 | } | |
4693 | ||
4694 | RGWObjectCtx *obj_ctx = static_cast<RGWObjectCtx *>(s->obj_ctx); | |
11fdf7f2 | 4695 | obj_ctx->set_atomic(obj); |
7c673cae FG |
4696 | |
4697 | bool ver_restored = false; | |
11fdf7f2 | 4698 | op_ret = store->swift_versioning_restore(*s->sysobj_ctx, *obj_ctx, s->bucket_owner.get_id(), |
7c673cae FG |
4699 | s->bucket_info, obj, ver_restored); |
4700 | if (op_ret < 0) { | |
4701 | return; | |
4702 | } | |
4703 | ||
4704 | if (!ver_restored) { | |
4705 | /* Swift's versioning mechanism hasn't found any previous version of | |
4706 | * the object that could be restored. This means we should proceed | |
4707 | * with the regular delete path. */ | |
4708 | RGWRados::Object del_target(store, s->bucket_info, *obj_ctx, obj); | |
4709 | RGWRados::Object::Delete del_op(&del_target); | |
4710 | ||
4711 | op_ret = get_system_versioning_params(s, &del_op.params.olh_epoch, | |
4712 | &del_op.params.marker_version_id); | |
4713 | if (op_ret < 0) { | |
4714 | return; | |
4715 | } | |
4716 | ||
4717 | del_op.params.bucket_owner = s->bucket_owner.get_id(); | |
4718 | del_op.params.versioning_status = s->bucket_info.versioning_status(); | |
4719 | del_op.params.obj_owner = s->owner; | |
4720 | del_op.params.unmod_since = unmod_since; | |
4721 | del_op.params.high_precision_time = s->system_request; /* system request uses high precision time */ | |
4722 | ||
4723 | op_ret = del_op.delete_obj(); | |
4724 | if (op_ret >= 0) { | |
4725 | delete_marker = del_op.result.delete_marker; | |
4726 | version_id = del_op.result.version_id; | |
4727 | } | |
4728 | ||
4729 | /* Check whether the object has expired. Swift API documentation | |
4730 | * stands that we should return 404 Not Found in such case. */ | |
4731 | if (need_object_expiration() && object_is_expired(attrs)) { | |
4732 | op_ret = -ENOENT; | |
4733 | return; | |
4734 | } | |
4735 | } | |
4736 | ||
94b18763 FG |
4737 | if (op_ret == -ECANCELED) { |
4738 | op_ret = 0; | |
4739 | } | |
7c673cae FG |
4740 | if (op_ret == -ERR_PRECONDITION_FAILED && no_precondition_error) { |
4741 | op_ret = 0; | |
4742 | } | |
4743 | } else { | |
4744 | op_ret = -EINVAL; | |
4745 | } | |
eafe8130 TL |
4746 | |
4747 | const auto ret = rgw::notify::publish(s, ceph::real_clock::now(), attrs[RGW_ATTR_ETAG].to_str(), | |
4748 | delete_marker && s->object.instance.empty() ? rgw::notify::ObjectRemovedDeleteMarkerCreated : rgw::notify::ObjectRemovedDelete, | |
4749 | store); | |
4750 | if (ret < 0) { | |
4751 | ldpp_dout(this, 5) << "WARNING: publishing notification failed, with error: " << ret << dendl; | |
4752 | // TODO: we should have conf to make send a blocking coroutine and reply with error in case sending failed | |
4753 | // this should be global conf (probably returnign a different handler) | |
4754 | // so we don't need to read the configured values before we perform it | |
4755 | } | |
7c673cae FG |
4756 | } |
4757 | ||
3a9019d9 FG |
4758 | bool RGWCopyObj::parse_copy_location(const boost::string_view& url_src, |
4759 | string& bucket_name, | |
4760 | rgw_obj_key& key) | |
7c673cae | 4761 | { |
3a9019d9 FG |
4762 | boost::string_view name_str; |
4763 | boost::string_view params_str; | |
7c673cae | 4764 | |
494da23a | 4765 | // search for ? before url-decoding so we don't accidentally match %3F |
7c673cae FG |
4766 | size_t pos = url_src.find('?'); |
4767 | if (pos == string::npos) { | |
4768 | name_str = url_src; | |
4769 | } else { | |
4770 | name_str = url_src.substr(0, pos); | |
4771 | params_str = url_src.substr(pos + 1); | |
4772 | } | |
4773 | ||
3a9019d9 FG |
4774 | boost::string_view dec_src{name_str}; |
4775 | if (dec_src[0] == '/') | |
4776 | dec_src.remove_prefix(1); | |
7c673cae | 4777 | |
3a9019d9 | 4778 | pos = dec_src.find('/'); |
494da23a | 4779 | if (pos == string::npos) |
7c673cae FG |
4780 | return false; |
4781 | ||
494da23a TL |
4782 | bucket_name = url_decode(dec_src.substr(0, pos)); |
4783 | key.name = url_decode(dec_src.substr(pos + 1)); | |
7c673cae FG |
4784 | |
4785 | if (key.name.empty()) { | |
4786 | return false; | |
4787 | } | |
4788 | ||
3a9019d9 | 4789 | if (! params_str.empty()) { |
7c673cae | 4790 | RGWHTTPArgs args; |
3a9019d9 | 4791 | args.set(params_str.to_string()); |
7c673cae FG |
4792 | args.parse(); |
4793 | ||
4794 | key.instance = args.get("versionId", NULL); | |
4795 | } | |
4796 | ||
4797 | return true; | |
4798 | } | |
4799 | ||
4800 | int RGWCopyObj::verify_permission() | |
4801 | { | |
31f18b77 | 4802 | RGWAccessControlPolicy src_acl(s->cct); |
11fdf7f2 | 4803 | boost::optional<Policy> src_policy; |
7c673cae FG |
4804 | op_ret = get_params(); |
4805 | if (op_ret < 0) | |
4806 | return op_ret; | |
4807 | ||
4808 | op_ret = get_system_versioning_params(s, &olh_epoch, &version_id); | |
4809 | if (op_ret < 0) { | |
4810 | return op_ret; | |
4811 | } | |
4812 | map<string, bufferlist> src_attrs; | |
4813 | ||
7c673cae | 4814 | if (s->bucket_instance_id.empty()) { |
11fdf7f2 | 4815 | op_ret = store->get_bucket_info(*s->sysobj_ctx, src_tenant_name, src_bucket_name, src_bucket_info, NULL, &src_attrs); |
7c673cae FG |
4816 | } else { |
4817 | /* will only happen in intra region sync where the source and dest bucket is the same */ | |
11fdf7f2 | 4818 | op_ret = store->get_bucket_instance_info(*s->sysobj_ctx, s->bucket_instance_id, src_bucket_info, NULL, &src_attrs); |
7c673cae FG |
4819 | } |
4820 | if (op_ret < 0) { | |
4821 | if (op_ret == -ENOENT) { | |
4822 | op_ret = -ERR_NO_SUCH_BUCKET; | |
4823 | } | |
4824 | return op_ret; | |
4825 | } | |
4826 | ||
4827 | src_bucket = src_bucket_info.bucket; | |
4828 | ||
4829 | /* get buckets info (source and dest) */ | |
4830 | if (s->local_source && source_zone.empty()) { | |
4831 | rgw_obj src_obj(src_bucket, src_object); | |
4832 | store->set_atomic(s->obj_ctx, src_obj); | |
4833 | store->set_prefetch_data(s->obj_ctx, src_obj); | |
4834 | ||
11fdf7f2 TL |
4835 | rgw_placement_rule src_placement; |
4836 | ||
7c673cae | 4837 | /* check source object permissions */ |
11fdf7f2 | 4838 | op_ret = read_obj_policy(store, s, src_bucket_info, src_attrs, &src_acl, &src_placement.storage_class, |
31f18b77 | 4839 | src_policy, src_bucket, src_object); |
7c673cae FG |
4840 | if (op_ret < 0) { |
4841 | return op_ret; | |
4842 | } | |
4843 | ||
11fdf7f2 TL |
4844 | /* follow up on previous checks that required reading source object head */ |
4845 | if (need_to_check_storage_class) { | |
4846 | src_placement.inherit_from(src_bucket_info.placement_rule); | |
4847 | ||
4848 | op_ret = check_storage_class(src_placement); | |
4849 | if (op_ret < 0) { | |
4850 | return op_ret; | |
4851 | } | |
4852 | } | |
4853 | ||
7c673cae | 4854 | /* admin request overrides permission checks */ |
31f18b77 FG |
4855 | if (!s->auth.identity->is_admin_of(src_acl.get_owner().get_id())) { |
4856 | if (src_policy) { | |
4857 | auto e = src_policy->eval(s->env, *s->auth.identity, | |
4858 | src_object.instance.empty() ? | |
4859 | rgw::IAM::s3GetObject : | |
4860 | rgw::IAM::s3GetObjectVersion, | |
4861 | ARN(src_obj)); | |
4862 | if (e == Effect::Deny) { | |
4863 | return -EACCES; | |
4864 | } else if (e == Effect::Pass && | |
11fdf7f2 | 4865 | !src_acl.verify_permission(this, *s->auth.identity, s->perm_mask, |
31f18b77 FG |
4866 | RGW_PERM_READ)) { |
4867 | return -EACCES; | |
4868 | } | |
11fdf7f2 | 4869 | } else if (!src_acl.verify_permission(this, *s->auth.identity, |
31f18b77 FG |
4870 | s->perm_mask, |
4871 | RGW_PERM_READ)) { | |
4872 | return -EACCES; | |
4873 | } | |
7c673cae FG |
4874 | } |
4875 | } | |
4876 | ||
4877 | RGWAccessControlPolicy dest_bucket_policy(s->cct); | |
4878 | map<string, bufferlist> dest_attrs; | |
4879 | ||
4880 | if (src_bucket_name.compare(dest_bucket_name) == 0) { /* will only happen if s->local_source | |
4881 | or intra region sync */ | |
4882 | dest_bucket_info = src_bucket_info; | |
4883 | dest_attrs = src_attrs; | |
4884 | } else { | |
11fdf7f2 | 4885 | op_ret = store->get_bucket_info(*s->sysobj_ctx, dest_tenant_name, dest_bucket_name, |
7c673cae FG |
4886 | dest_bucket_info, nullptr, &dest_attrs); |
4887 | if (op_ret < 0) { | |
4888 | if (op_ret == -ENOENT) { | |
4889 | op_ret = -ERR_NO_SUCH_BUCKET; | |
4890 | } | |
4891 | return op_ret; | |
4892 | } | |
4893 | } | |
4894 | ||
4895 | dest_bucket = dest_bucket_info.bucket; | |
4896 | ||
4897 | rgw_obj dest_obj(dest_bucket, dest_object); | |
4898 | store->set_atomic(s->obj_ctx, dest_obj); | |
4899 | ||
4900 | /* check dest bucket permissions */ | |
4901 | op_ret = read_bucket_policy(store, s, dest_bucket_info, dest_attrs, | |
4902 | &dest_bucket_policy, dest_bucket); | |
4903 | if (op_ret < 0) { | |
4904 | return op_ret; | |
4905 | } | |
11fdf7f2 | 4906 | auto dest_iam_policy = get_iam_policy_from_attr(s->cct, store, dest_attrs, dest_bucket.tenant); |
7c673cae | 4907 | /* admin request overrides permission checks */ |
11fdf7f2 TL |
4908 | if (! s->auth.identity->is_admin_of(dest_policy.get_owner().get_id())){ |
4909 | if (dest_iam_policy != boost::none) { | |
4910 | rgw_add_to_iam_environment(s->env, "s3:x-amz-copy-source", copy_source); | |
494da23a TL |
4911 | if (md_directive) |
4912 | rgw_add_to_iam_environment(s->env, "s3:x-amz-metadata-directive", | |
4913 | *md_directive); | |
11fdf7f2 TL |
4914 | |
4915 | auto e = dest_iam_policy->eval(s->env, *s->auth.identity, | |
4916 | rgw::IAM::s3PutObject, | |
4917 | ARN(dest_obj)); | |
4918 | if (e == Effect::Deny) { | |
4919 | return -EACCES; | |
4920 | } else if (e == Effect::Pass && | |
4921 | ! dest_bucket_policy.verify_permission(this, | |
4922 | *s->auth.identity, | |
4923 | s->perm_mask, | |
4924 | RGW_PERM_WRITE)){ | |
4925 | return -EACCES; | |
4926 | } | |
92f5a8d4 TL |
4927 | } else if (! dest_bucket_policy.verify_permission(this, *s->auth.identity, s->perm_mask, |
4928 | RGW_PERM_WRITE)) { | |
4929 | return -EACCES; | |
11fdf7f2 | 4930 | } |
92f5a8d4 | 4931 | |
7c673cae FG |
4932 | } |
4933 | ||
4934 | op_ret = init_dest_policy(); | |
4935 | if (op_ret < 0) { | |
4936 | return op_ret; | |
4937 | } | |
4938 | ||
4939 | return 0; | |
4940 | } | |
4941 | ||
4942 | ||
4943 | int RGWCopyObj::init_common() | |
4944 | { | |
4945 | if (if_mod) { | |
4946 | if (parse_time(if_mod, &mod_time) < 0) { | |
4947 | op_ret = -EINVAL; | |
4948 | return op_ret; | |
4949 | } | |
4950 | mod_ptr = &mod_time; | |
4951 | } | |
4952 | ||
4953 | if (if_unmod) { | |
4954 | if (parse_time(if_unmod, &unmod_time) < 0) { | |
4955 | op_ret = -EINVAL; | |
4956 | return op_ret; | |
4957 | } | |
4958 | unmod_ptr = &unmod_time; | |
4959 | } | |
4960 | ||
4961 | bufferlist aclbl; | |
4962 | dest_policy.encode(aclbl); | |
4963 | emplace_attr(RGW_ATTR_ACL, std::move(aclbl)); | |
4964 | ||
3efd9988 FG |
4965 | op_ret = rgw_get_request_metadata(s->cct, s->info, attrs); |
4966 | if (op_ret < 0) { | |
4967 | return op_ret; | |
4968 | } | |
7c673cae FG |
4969 | populate_with_generic_attrs(s, attrs); |
4970 | ||
4971 | return 0; | |
4972 | } | |
4973 | ||
4974 | static void copy_obj_progress_cb(off_t ofs, void *param) | |
4975 | { | |
4976 | RGWCopyObj *op = static_cast<RGWCopyObj *>(param); | |
4977 | op->progress_cb(ofs); | |
4978 | } | |
4979 | ||
4980 | void RGWCopyObj::progress_cb(off_t ofs) | |
4981 | { | |
4982 | if (!s->cct->_conf->rgw_copy_obj_progress) | |
4983 | return; | |
4984 | ||
4985 | if (ofs - last_ofs < s->cct->_conf->rgw_copy_obj_progress_every_bytes) | |
4986 | return; | |
4987 | ||
4988 | send_partial_response(ofs); | |
4989 | ||
4990 | last_ofs = ofs; | |
4991 | } | |
4992 | ||
4993 | void RGWCopyObj::pre_exec() | |
4994 | { | |
4995 | rgw_bucket_object_pre_exec(s); | |
4996 | } | |
4997 | ||
4998 | void RGWCopyObj::execute() | |
4999 | { | |
5000 | if (init_common() < 0) | |
5001 | return; | |
5002 | ||
5003 | rgw_obj src_obj(src_bucket, src_object); | |
5004 | rgw_obj dst_obj(dest_bucket, dest_object); | |
5005 | ||
5006 | RGWObjectCtx& obj_ctx = *static_cast<RGWObjectCtx *>(s->obj_ctx); | |
11fdf7f2 TL |
5007 | if ( ! version_id.empty()) { |
5008 | dst_obj.key.set_instance(version_id); | |
5009 | } else if (dest_bucket_info.versioning_enabled()) { | |
5010 | store->gen_rand_obj_instance_name(&dst_obj); | |
5011 | } | |
5012 | ||
5013 | obj_ctx.set_atomic(src_obj); | |
5014 | obj_ctx.set_atomic(dst_obj); | |
7c673cae FG |
5015 | |
5016 | encode_delete_at_attr(delete_at, attrs); | |
5017 | ||
5018 | bool high_precision_time = (s->system_request); | |
5019 | ||
5020 | /* Handle object versioning of Swift API. In case of copying to remote this | |
5021 | * should fail gently (op_ret == 0) as the dst_obj will not exist here. */ | |
5022 | op_ret = store->swift_versioning_copy(obj_ctx, | |
5023 | dest_bucket_info.owner, | |
5024 | dest_bucket_info, | |
5025 | dst_obj); | |
5026 | if (op_ret < 0) { | |
5027 | return; | |
5028 | } | |
5029 | ||
5030 | op_ret = store->copy_obj(obj_ctx, | |
5031 | s->user->user_id, | |
7c673cae FG |
5032 | &s->info, |
5033 | source_zone, | |
5034 | dst_obj, | |
5035 | src_obj, | |
5036 | dest_bucket_info, | |
5037 | src_bucket_info, | |
11fdf7f2 | 5038 | s->dest_placement, |
7c673cae FG |
5039 | &src_mtime, |
5040 | &mtime, | |
5041 | mod_ptr, | |
5042 | unmod_ptr, | |
5043 | high_precision_time, | |
5044 | if_match, | |
5045 | if_nomatch, | |
5046 | attrs_mod, | |
5047 | copy_if_newer, | |
11fdf7f2 | 5048 | attrs, RGWObjCategory::Main, |
7c673cae FG |
5049 | olh_epoch, |
5050 | (delete_at ? *delete_at : real_time()), | |
5051 | (version_id.empty() ? NULL : &version_id), | |
5052 | &s->req_id, /* use req_id as tag */ | |
5053 | &etag, | |
7c673cae FG |
5054 | copy_obj_progress_cb, (void *)this |
5055 | ); | |
eafe8130 TL |
5056 | |
5057 | const auto ret = rgw::notify::publish(s, mtime, etag, rgw::notify::ObjectCreatedCopy, store); | |
5058 | if (ret < 0) { | |
5059 | ldpp_dout(this, 5) << "WARNING: publishing notification failed, with error: " << ret << dendl; | |
5060 | // TODO: we should have conf to make send a blocking coroutine and reply with error in case sending failed | |
5061 | // this should be global conf (probably returnign a different handler) | |
5062 | // so we don't need to read the configured values before we perform it | |
5063 | } | |
7c673cae FG |
5064 | } |
5065 | ||
5066 | int RGWGetACLs::verify_permission() | |
5067 | { | |
5068 | bool perm; | |
5069 | if (!s->object.empty()) { | |
11fdf7f2 TL |
5070 | auto iam_action = s->object.instance.empty() ? |
5071 | rgw::IAM::s3GetObjectAcl : | |
5072 | rgw::IAM::s3GetObjectVersionAcl; | |
5073 | ||
5074 | if (s->iam_policy && s->iam_policy->has_partial_conditional(S3_EXISTING_OBJTAG)){ | |
5075 | rgw_obj obj = rgw_obj(s->bucket, s->object); | |
5076 | rgw_iam_add_existing_objtags(store, s, obj, iam_action); | |
5077 | } | |
5078 | if (! s->iam_user_policies.empty()) { | |
5079 | for (auto& user_policy : s->iam_user_policies) { | |
5080 | if (user_policy.has_partial_conditional(S3_EXISTING_OBJTAG)) { | |
5081 | rgw_obj obj = rgw_obj(s->bucket, s->object); | |
5082 | rgw_iam_add_existing_objtags(store, s, obj, iam_action); | |
5083 | } | |
5084 | } | |
5085 | } | |
5086 | perm = verify_object_permission(this, s, iam_action); | |
7c673cae | 5087 | } else { |
a8e16298 TL |
5088 | if (!s->bucket_exists) { |
5089 | return -ERR_NO_SUCH_BUCKET; | |
5090 | } | |
11fdf7f2 | 5091 | perm = verify_bucket_permission(this, s, rgw::IAM::s3GetBucketAcl); |
7c673cae FG |
5092 | } |
5093 | if (!perm) | |
5094 | return -EACCES; | |
5095 | ||
5096 | return 0; | |
5097 | } | |
5098 | ||
5099 | void RGWGetACLs::pre_exec() | |
5100 | { | |
5101 | rgw_bucket_object_pre_exec(s); | |
5102 | } | |
5103 | ||
5104 | void RGWGetACLs::execute() | |
5105 | { | |
5106 | stringstream ss; | |
c07f9fc5 FG |
5107 | RGWAccessControlPolicy* const acl = \ |
5108 | (!s->object.empty() ? s->object_acl.get() : s->bucket_acl.get()); | |
5109 | RGWAccessControlPolicy_S3* const s3policy = \ | |
5110 | static_cast<RGWAccessControlPolicy_S3*>(acl); | |
7c673cae FG |
5111 | s3policy->to_xml(ss); |
5112 | acls = ss.str(); | |
5113 | } | |
5114 | ||
5115 | ||
5116 | ||
5117 | int RGWPutACLs::verify_permission() | |
5118 | { | |
5119 | bool perm; | |
11fdf7f2 TL |
5120 | |
5121 | rgw_add_to_iam_environment(s->env, "s3:x-amz-acl", s->canned_acl); | |
5122 | ||
5123 | rgw_add_grant_to_iam_environment(s->env, s); | |
7c673cae | 5124 | if (!s->object.empty()) { |
11fdf7f2 TL |
5125 | auto iam_action = s->object.instance.empty() ? rgw::IAM::s3PutObjectAcl : rgw::IAM::s3PutObjectVersionAcl; |
5126 | auto obj = rgw_obj(s->bucket, s->object); | |
5127 | op_ret = rgw_iam_add_existing_objtags(store, s, obj, iam_action); | |
5128 | perm = verify_object_permission(this, s, iam_action); | |
7c673cae | 5129 | } else { |
11fdf7f2 | 5130 | perm = verify_bucket_permission(this, s, rgw::IAM::s3PutBucketAcl); |
7c673cae FG |
5131 | } |
5132 | if (!perm) | |
5133 | return -EACCES; | |
5134 | ||
5135 | return 0; | |
5136 | } | |
5137 | ||
5138 | int RGWGetLC::verify_permission() | |
5139 | { | |
5140 | bool perm; | |
11fdf7f2 | 5141 | perm = verify_bucket_permission(this, s, rgw::IAM::s3GetLifecycleConfiguration); |
7c673cae FG |
5142 | if (!perm) |
5143 | return -EACCES; | |
5144 | ||
5145 | return 0; | |
5146 | } | |
5147 | ||
5148 | int RGWPutLC::verify_permission() | |
5149 | { | |
5150 | bool perm; | |
11fdf7f2 | 5151 | perm = verify_bucket_permission(this, s, rgw::IAM::s3PutLifecycleConfiguration); |
7c673cae FG |
5152 | if (!perm) |
5153 | return -EACCES; | |
5154 | ||
5155 | return 0; | |
5156 | } | |
5157 | ||
5158 | int RGWDeleteLC::verify_permission() | |
5159 | { | |
5160 | bool perm; | |
11fdf7f2 | 5161 | perm = verify_bucket_permission(this, s, rgw::IAM::s3PutLifecycleConfiguration); |
7c673cae FG |
5162 | if (!perm) |
5163 | return -EACCES; | |
5164 | ||
5165 | return 0; | |
5166 | } | |
5167 | ||
5168 | void RGWPutACLs::pre_exec() | |
5169 | { | |
5170 | rgw_bucket_object_pre_exec(s); | |
5171 | } | |
5172 | ||
5173 | void RGWGetLC::pre_exec() | |
5174 | { | |
5175 | rgw_bucket_object_pre_exec(s); | |
5176 | } | |
5177 | ||
5178 | void RGWPutLC::pre_exec() | |
5179 | { | |
5180 | rgw_bucket_object_pre_exec(s); | |
5181 | } | |
5182 | ||
5183 | void RGWDeleteLC::pre_exec() | |
5184 | { | |
5185 | rgw_bucket_object_pre_exec(s); | |
5186 | } | |
5187 | ||
5188 | void RGWPutACLs::execute() | |
5189 | { | |
5190 | bufferlist bl; | |
5191 | ||
5192 | RGWAccessControlPolicy_S3 *policy = NULL; | |
5193 | RGWACLXMLParser_S3 parser(s->cct); | |
5194 | RGWAccessControlPolicy_S3 new_policy(s->cct); | |
5195 | stringstream ss; | |
7c673cae FG |
5196 | rgw_obj obj; |
5197 | ||
5198 | op_ret = 0; /* XXX redundant? */ | |
5199 | ||
5200 | if (!parser.init()) { | |
5201 | op_ret = -EINVAL; | |
5202 | return; | |
5203 | } | |
5204 | ||
5205 | ||
c07f9fc5 FG |
5206 | RGWAccessControlPolicy* const existing_policy = \ |
5207 | (s->object.empty() ? s->bucket_acl.get() : s->object_acl.get()); | |
7c673cae FG |
5208 | |
5209 | owner = existing_policy->get_owner(); | |
5210 | ||
5211 | op_ret = get_params(); | |
c07f9fc5 FG |
5212 | if (op_ret < 0) { |
5213 | if (op_ret == -ERANGE) { | |
11fdf7f2 | 5214 | ldpp_dout(this, 4) << "The size of request xml data is larger than the max limitation, data size = " |
c07f9fc5 FG |
5215 | << s->length << dendl; |
5216 | op_ret = -ERR_MALFORMED_XML; | |
5217 | s->err.message = "The XML you provided was larger than the maximum " + | |
5218 | std::to_string(s->cct->_conf->rgw_max_put_param_size) + | |
5219 | " bytes allowed."; | |
5220 | } | |
7c673cae | 5221 | return; |
c07f9fc5 | 5222 | } |
7c673cae | 5223 | |
11fdf7f2 TL |
5224 | char* buf = data.c_str(); |
5225 | ldpp_dout(this, 15) << "read len=" << data.length() << " data=" << (buf ? buf : "") << dendl; | |
7c673cae | 5226 | |
11fdf7f2 | 5227 | if (!s->canned_acl.empty() && data.length() > 0) { |
7c673cae FG |
5228 | op_ret = -EINVAL; |
5229 | return; | |
5230 | } | |
5231 | ||
5232 | if (!s->canned_acl.empty() || s->has_acl_header) { | |
5233 | op_ret = get_policy_from_state(store, s, ss); | |
5234 | if (op_ret < 0) | |
5235 | return; | |
5236 | ||
11fdf7f2 TL |
5237 | data.clear(); |
5238 | data.append(ss.str()); | |
7c673cae FG |
5239 | } |
5240 | ||
11fdf7f2 | 5241 | if (!parser.parse(data.c_str(), data.length(), 1)) { |
7c673cae FG |
5242 | op_ret = -EINVAL; |
5243 | return; | |
5244 | } | |
5245 | policy = static_cast<RGWAccessControlPolicy_S3 *>(parser.find_first("AccessControlPolicy")); | |
5246 | if (!policy) { | |
5247 | op_ret = -EINVAL; | |
5248 | return; | |
5249 | } | |
5250 | ||
c07f9fc5 FG |
5251 | const RGWAccessControlList& req_acl = policy->get_acl(); |
5252 | const multimap<string, ACLGrant>& req_grant_map = req_acl.get_grant_map(); | |
5253 | #define ACL_GRANTS_MAX_NUM 100 | |
5254 | int max_num = s->cct->_conf->rgw_acl_grants_max_num; | |
5255 | if (max_num < 0) { | |
5256 | max_num = ACL_GRANTS_MAX_NUM; | |
5257 | } | |
5258 | ||
5259 | int grants_num = req_grant_map.size(); | |
5260 | if (grants_num > max_num) { | |
11fdf7f2 TL |
5261 | ldpp_dout(this, 4) << "An acl can have up to " << max_num |
5262 | << " grants, request acl grants num: " << grants_num << dendl; | |
c07f9fc5 FG |
5263 | op_ret = -ERR_MALFORMED_ACL_ERROR; |
5264 | s->err.message = "The request is rejected, because the acl grants number you requested is larger than the maximum " | |
5265 | + std::to_string(max_num) | |
5266 | + " grants allowed in an acl."; | |
5267 | return; | |
5268 | } | |
5269 | ||
7c673cae | 5270 | // forward bucket acl requests to meta master zone |
11fdf7f2 | 5271 | if (s->object.empty() && !store->svc.zone->is_meta_master()) { |
7c673cae FG |
5272 | bufferlist in_data; |
5273 | // include acl data unless it was generated from a canned_acl | |
5274 | if (s->canned_acl.empty()) { | |
11fdf7f2 | 5275 | in_data.append(data); |
7c673cae FG |
5276 | } |
5277 | op_ret = forward_request_to_master(s, NULL, store, in_data, NULL); | |
5278 | if (op_ret < 0) { | |
11fdf7f2 | 5279 | ldpp_dout(this, 0) << "forward_request_to_master returned ret=" << op_ret << dendl; |
7c673cae FG |
5280 | return; |
5281 | } | |
5282 | } | |
5283 | ||
11fdf7f2 TL |
5284 | if (s->cct->_conf->subsys.should_gather<ceph_subsys_rgw, 15>()) { |
5285 | ldpp_dout(this, 15) << "Old AccessControlPolicy"; | |
7c673cae FG |
5286 | policy->to_xml(*_dout); |
5287 | *_dout << dendl; | |
5288 | } | |
5289 | ||
5290 | op_ret = policy->rebuild(store, &owner, new_policy); | |
5291 | if (op_ret < 0) | |
5292 | return; | |
5293 | ||
11fdf7f2 TL |
5294 | if (s->cct->_conf->subsys.should_gather<ceph_subsys_rgw, 15>()) { |
5295 | ldpp_dout(this, 15) << "New AccessControlPolicy:"; | |
7c673cae FG |
5296 | new_policy.to_xml(*_dout); |
5297 | *_dout << dendl; | |
5298 | } | |
5299 | ||
5300 | new_policy.encode(bl); | |
5301 | map<string, bufferlist> attrs; | |
5302 | ||
5303 | if (!s->object.empty()) { | |
5304 | obj = rgw_obj(s->bucket, s->object); | |
5305 | store->set_atomic(s->obj_ctx, obj); | |
5306 | //if instance is empty, we should modify the latest object | |
5307 | op_ret = modify_obj_attr(store, s, obj, RGW_ATTR_ACL, bl); | |
5308 | } else { | |
5309 | attrs = s->bucket_attrs; | |
5310 | attrs[RGW_ATTR_ACL] = bl; | |
5311 | op_ret = rgw_bucket_set_attrs(store, s->bucket_info, attrs, &s->bucket_info.objv_tracker); | |
5312 | } | |
5313 | if (op_ret == -ECANCELED) { | |
5314 | op_ret = 0; /* lost a race, but it's ok because acls are immutable */ | |
5315 | } | |
5316 | } | |
5317 | ||
7c673cae FG |
5318 | void RGWPutLC::execute() |
5319 | { | |
5320 | bufferlist bl; | |
5321 | ||
11fdf7f2 TL |
5322 | RGWLifecycleConfiguration_S3 config(s->cct); |
5323 | RGWXMLParser parser; | |
7c673cae FG |
5324 | RGWLifecycleConfiguration_S3 new_config(s->cct); |
5325 | ||
b32b8144 FG |
5326 | content_md5 = s->info.env->get("HTTP_CONTENT_MD5"); |
5327 | if (content_md5 == nullptr) { | |
5328 | op_ret = -ERR_INVALID_REQUEST; | |
5329 | s->err.message = "Missing required header for this request: Content-MD5"; | |
11fdf7f2 | 5330 | ldpp_dout(this, 5) << s->err.message << dendl; |
b32b8144 FG |
5331 | return; |
5332 | } | |
5333 | ||
5334 | std::string content_md5_bin; | |
5335 | try { | |
5336 | content_md5_bin = rgw::from_base64(boost::string_view(content_md5)); | |
5337 | } catch (...) { | |
5338 | s->err.message = "Request header Content-MD5 contains character " | |
5339 | "that is not base64 encoded."; | |
11fdf7f2 | 5340 | ldpp_dout(this, 5) << s->err.message << dendl; |
b32b8144 FG |
5341 | op_ret = -ERR_BAD_DIGEST; |
5342 | return; | |
5343 | } | |
5344 | ||
7c673cae FG |
5345 | if (!parser.init()) { |
5346 | op_ret = -EINVAL; | |
5347 | return; | |
5348 | } | |
5349 | ||
5350 | op_ret = get_params(); | |
5351 | if (op_ret < 0) | |
5352 | return; | |
5353 | ||
11fdf7f2 TL |
5354 | char* buf = data.c_str(); |
5355 | ldpp_dout(this, 15) << "read len=" << data.length() << " data=" << (buf ? buf : "") << dendl; | |
7c673cae | 5356 | |
b32b8144 FG |
5357 | MD5 data_hash; |
5358 | unsigned char data_hash_res[CEPH_CRYPTO_MD5_DIGESTSIZE]; | |
11fdf7f2 | 5359 | data_hash.Update(reinterpret_cast<const unsigned char*>(buf), data.length()); |
b32b8144 FG |
5360 | data_hash.Final(data_hash_res); |
5361 | ||
5362 | if (memcmp(data_hash_res, content_md5_bin.c_str(), CEPH_CRYPTO_MD5_DIGESTSIZE) != 0) { | |
5363 | op_ret = -ERR_BAD_DIGEST; | |
5364 | s->err.message = "The Content-MD5 you specified did not match what we received."; | |
11fdf7f2 | 5365 | ldpp_dout(this, 5) << s->err.message |
b32b8144 FG |
5366 | << " Specified content md5: " << content_md5 |
5367 | << ", calculated content md5: " << data_hash_res | |
5368 | << dendl; | |
5369 | return; | |
5370 | } | |
5371 | ||
11fdf7f2 | 5372 | if (!parser.parse(buf, data.length(), 1)) { |
7c673cae FG |
5373 | op_ret = -ERR_MALFORMED_XML; |
5374 | return; | |
5375 | } | |
11fdf7f2 TL |
5376 | |
5377 | try { | |
5378 | RGWXMLDecoder::decode_xml("LifecycleConfiguration", config, &parser); | |
5379 | } catch (RGWXMLDecoder::err& err) { | |
5380 | ldpp_dout(this, 5) << "Bad lifecycle configuration: " << err << dendl; | |
7c673cae FG |
5381 | op_ret = -ERR_MALFORMED_XML; |
5382 | return; | |
5383 | } | |
5384 | ||
11fdf7f2 | 5385 | op_ret = config.rebuild(store, new_config); |
7c673cae FG |
5386 | if (op_ret < 0) |
5387 | return; | |
5388 | ||
11fdf7f2 TL |
5389 | if (s->cct->_conf->subsys.should_gather<ceph_subsys_rgw, 15>()) { |
5390 | XMLFormatter xf; | |
5391 | new_config.dump_xml(&xf); | |
5392 | stringstream ss; | |
5393 | xf.flush(ss); | |
5394 | ldpp_dout(this, 15) << "New LifecycleConfiguration:" << ss.str() << dendl; | |
7c673cae | 5395 | } |
11fdf7f2 | 5396 | |
494da23a TL |
5397 | if (!store->svc.zone->is_meta_master()) { |
5398 | op_ret = forward_request_to_master(s, nullptr, store, data, nullptr); | |
5399 | if (op_ret < 0) { | |
5400 | ldpp_dout(this, 0) << "forward_request_to_master returned ret=" << op_ret << dendl; | |
5401 | return; | |
5402 | } | |
5403 | } | |
5404 | ||
11fdf7f2 TL |
5405 | op_ret = store->get_lc()->set_bucket_config(s->bucket_info, s->bucket_attrs, &new_config); |
5406 | if (op_ret < 0) { | |
7c673cae | 5407 | return; |
11fdf7f2 | 5408 | } |
7c673cae FG |
5409 | return; |
5410 | } | |
5411 | ||
5412 | void RGWDeleteLC::execute() | |
5413 | { | |
494da23a TL |
5414 | if (!store->svc.zone->is_meta_master()) { |
5415 | bufferlist data; | |
5416 | op_ret = forward_request_to_master(s, nullptr, store, data, nullptr); | |
5417 | if (op_ret < 0) { | |
5418 | ldpp_dout(this, 0) << "forward_request_to_master returned ret=" << op_ret << dendl; | |
5419 | return; | |
5420 | } | |
5421 | } | |
11fdf7f2 TL |
5422 | map<string, bufferlist> attrs = s->bucket_attrs; |
5423 | attrs.erase(RGW_ATTR_LC); | |
5424 | op_ret = rgw_bucket_set_attrs(store, s->bucket_info, attrs, | |
5425 | &s->bucket_info.objv_tracker); | |
5426 | if (op_ret < 0) { | |
5427 | ldpp_dout(this, 0) << "RGWLC::RGWDeleteLC() failed to set attrs on bucket=" | |
5428 | << s->bucket.name << " returned err=" << op_ret << dendl; | |
7c673cae | 5429 | return; |
11fdf7f2 TL |
5430 | } |
5431 | ||
5432 | op_ret = store->get_lc()->remove_bucket_config(s->bucket_info, s->bucket_attrs); | |
5433 | if (op_ret < 0) { | |
5434 | return; | |
5435 | } | |
7c673cae FG |
5436 | return; |
5437 | } | |
5438 | ||
5439 | int RGWGetCORS::verify_permission() | |
5440 | { | |
b32b8144 | 5441 | return verify_bucket_owner_or_policy(s, rgw::IAM::s3GetBucketCORS); |
7c673cae FG |
5442 | } |
5443 | ||
5444 | void RGWGetCORS::execute() | |
5445 | { | |
5446 | op_ret = read_bucket_cors(); | |
5447 | if (op_ret < 0) | |
5448 | return ; | |
5449 | ||
5450 | if (!cors_exist) { | |
11fdf7f2 TL |
5451 | ldpp_dout(this, 2) << "No CORS configuration set yet for this bucket" << dendl; |
5452 | op_ret = -ERR_NO_CORS_FOUND; | |
7c673cae FG |
5453 | return; |
5454 | } | |
5455 | } | |
5456 | ||
5457 | int RGWPutCORS::verify_permission() | |
5458 | { | |
b32b8144 | 5459 | return verify_bucket_owner_or_policy(s, rgw::IAM::s3PutBucketCORS); |
7c673cae FG |
5460 | } |
5461 | ||
5462 | void RGWPutCORS::execute() | |
5463 | { | |
5464 | rgw_raw_obj obj; | |
5465 | ||
5466 | op_ret = get_params(); | |
5467 | if (op_ret < 0) | |
5468 | return; | |
5469 | ||
11fdf7f2 | 5470 | if (!store->svc.zone->is_meta_master()) { |
31f18b77 FG |
5471 | op_ret = forward_request_to_master(s, NULL, store, in_data, nullptr); |
5472 | if (op_ret < 0) { | |
11fdf7f2 | 5473 | ldpp_dout(this, 0) << "forward_request_to_master returned ret=" << op_ret << dendl; |
31f18b77 FG |
5474 | return; |
5475 | } | |
5476 | } | |
5477 | ||
b32b8144 FG |
5478 | op_ret = retry_raced_bucket_write(store, s, [this] { |
5479 | map<string, bufferlist> attrs = s->bucket_attrs; | |
5480 | attrs[RGW_ATTR_CORS] = cors_bl; | |
5481 | return rgw_bucket_set_attrs(store, s->bucket_info, attrs, &s->bucket_info.objv_tracker); | |
5482 | }); | |
7c673cae FG |
5483 | } |
5484 | ||
5485 | int RGWDeleteCORS::verify_permission() | |
5486 | { | |
b32b8144 FG |
5487 | // No separate delete permission |
5488 | return verify_bucket_owner_or_policy(s, rgw::IAM::s3PutBucketCORS); | |
7c673cae FG |
5489 | } |
5490 | ||
5491 | void RGWDeleteCORS::execute() | |
5492 | { | |
494da23a TL |
5493 | if (!store->svc.zone->is_meta_master()) { |
5494 | bufferlist data; | |
5495 | op_ret = forward_request_to_master(s, nullptr, store, data, nullptr); | |
5496 | if (op_ret < 0) { | |
5497 | ldpp_dout(this, 0) << "forward_request_to_master returned ret=" << op_ret << dendl; | |
5498 | return; | |
5499 | } | |
5500 | } | |
5501 | ||
b32b8144 | 5502 | op_ret = retry_raced_bucket_write(store, s, [this] { |
11fdf7f2 | 5503 | op_ret = read_bucket_cors(); |
b32b8144 FG |
5504 | if (op_ret < 0) |
5505 | return op_ret; | |
5506 | ||
11fdf7f2 TL |
5507 | if (!cors_exist) { |
5508 | ldpp_dout(this, 2) << "No CORS configuration set yet for this bucket" << dendl; | |
5509 | op_ret = -ENOENT; | |
5510 | return op_ret; | |
b32b8144 | 5511 | } |
11fdf7f2 TL |
5512 | |
5513 | map<string, bufferlist> attrs = s->bucket_attrs; | |
5514 | attrs.erase(RGW_ATTR_CORS); | |
5515 | op_ret = rgw_bucket_set_attrs(store, s->bucket_info, attrs, | |
5516 | &s->bucket_info.objv_tracker); | |
5517 | if (op_ret < 0) { | |
5518 | ldpp_dout(this, 0) << "RGWLC::RGWDeleteCORS() failed to set attrs on bucket=" << s->bucket.name | |
5519 | << " returned err=" << op_ret << dendl; | |
5520 | } | |
5521 | return op_ret; | |
b32b8144 | 5522 | }); |
7c673cae FG |
5523 | } |
5524 | ||
5525 | void RGWOptionsCORS::get_response_params(string& hdrs, string& exp_hdrs, unsigned *max_age) { | |
5526 | get_cors_response_headers(rule, req_hdrs, hdrs, exp_hdrs, max_age); | |
5527 | } | |
5528 | ||
5529 | int RGWOptionsCORS::validate_cors_request(RGWCORSConfiguration *cc) { | |
5530 | rule = cc->host_name_rule(origin); | |
5531 | if (!rule) { | |
11fdf7f2 | 5532 | ldpp_dout(this, 10) << "There is no cors rule present for " << origin << dendl; |
7c673cae FG |
5533 | return -ENOENT; |
5534 | } | |
5535 | ||
5536 | if (!validate_cors_rule_method(rule, req_meth)) { | |
5537 | return -ENOENT; | |
5538 | } | |
b32b8144 FG |
5539 | |
5540 | if (!validate_cors_rule_header(rule, req_hdrs)) { | |
5541 | return -ENOENT; | |
5542 | } | |
5543 | ||
7c673cae FG |
5544 | return 0; |
5545 | } | |
5546 | ||
5547 | void RGWOptionsCORS::execute() | |
5548 | { | |
5549 | op_ret = read_bucket_cors(); | |
5550 | if (op_ret < 0) | |
5551 | return; | |
5552 | ||
5553 | origin = s->info.env->get("HTTP_ORIGIN"); | |
5554 | if (!origin) { | |
11fdf7f2 | 5555 | ldpp_dout(this, 0) << "Missing mandatory Origin header" << dendl; |
7c673cae FG |
5556 | op_ret = -EINVAL; |
5557 | return; | |
5558 | } | |
5559 | req_meth = s->info.env->get("HTTP_ACCESS_CONTROL_REQUEST_METHOD"); | |
5560 | if (!req_meth) { | |
11fdf7f2 | 5561 | ldpp_dout(this, 0) << "Missing mandatory Access-control-request-method header" << dendl; |
7c673cae FG |
5562 | op_ret = -EINVAL; |
5563 | return; | |
5564 | } | |
5565 | if (!cors_exist) { | |
11fdf7f2 | 5566 | ldpp_dout(this, 2) << "No CORS configuration set yet for this bucket" << dendl; |
7c673cae FG |
5567 | op_ret = -ENOENT; |
5568 | return; | |
5569 | } | |
5570 | req_hdrs = s->info.env->get("HTTP_ACCESS_CONTROL_REQUEST_HEADERS"); | |
5571 | op_ret = validate_cors_request(&bucket_cors); | |
5572 | if (!rule) { | |
5573 | origin = req_meth = NULL; | |
5574 | return; | |
5575 | } | |
5576 | return; | |
5577 | } | |
5578 | ||
5579 | int RGWGetRequestPayment::verify_permission() | |
5580 | { | |
b32b8144 | 5581 | return verify_bucket_owner_or_policy(s, rgw::IAM::s3GetBucketRequestPayment); |
7c673cae FG |
5582 | } |
5583 | ||
5584 | void RGWGetRequestPayment::pre_exec() | |
5585 | { | |
5586 | rgw_bucket_object_pre_exec(s); | |
5587 | } | |
5588 | ||
5589 | void RGWGetRequestPayment::execute() | |
5590 | { | |
5591 | requester_pays = s->bucket_info.requester_pays; | |
5592 | } | |
5593 | ||
5594 | int RGWSetRequestPayment::verify_permission() | |
5595 | { | |
b32b8144 | 5596 | return verify_bucket_owner_or_policy(s, rgw::IAM::s3PutBucketRequestPayment); |
7c673cae FG |
5597 | } |
5598 | ||
5599 | void RGWSetRequestPayment::pre_exec() | |
5600 | { | |
5601 | rgw_bucket_object_pre_exec(s); | |
5602 | } | |
5603 | ||
5604 | void RGWSetRequestPayment::execute() | |
5605 | { | |
494da23a TL |
5606 | |
5607 | if (!store->svc.zone->is_meta_master()) { | |
5608 | op_ret = forward_request_to_master(s, nullptr, store, in_data, nullptr); | |
5609 | if (op_ret < 0) { | |
5610 | ldpp_dout(this, 0) << "forward_request_to_master returned ret=" << op_ret << dendl; | |
5611 | return; | |
5612 | } | |
5613 | } | |
5614 | ||
7c673cae FG |
5615 | op_ret = get_params(); |
5616 | ||
5617 | if (op_ret < 0) | |
5618 | return; | |
5619 | ||
5620 | s->bucket_info.requester_pays = requester_pays; | |
5621 | op_ret = store->put_bucket_instance_info(s->bucket_info, false, real_time(), | |
5622 | &s->bucket_attrs); | |
5623 | if (op_ret < 0) { | |
11fdf7f2 | 5624 | ldpp_dout(this, 0) << "NOTICE: put_bucket_info on bucket=" << s->bucket.name |
7c673cae FG |
5625 | << " returned err=" << op_ret << dendl; |
5626 | return; | |
5627 | } | |
5628 | } | |
5629 | ||
5630 | int RGWInitMultipart::verify_permission() | |
5631 | { | |
11fdf7f2 TL |
5632 | if (s->iam_policy || ! s->iam_user_policies.empty()) { |
5633 | auto usr_policy_res = eval_user_policies(s->iam_user_policies, s->env, | |
5634 | boost::none, | |
5635 | rgw::IAM::s3PutObject, | |
5636 | rgw_obj(s->bucket, s->object)); | |
5637 | if (usr_policy_res == Effect::Deny) { | |
5638 | return -EACCES; | |
5639 | } | |
5640 | ||
5641 | rgw::IAM::Effect e = Effect::Pass; | |
5642 | if (s->iam_policy) { | |
5643 | e = s->iam_policy->eval(s->env, *s->auth.identity, | |
31f18b77 FG |
5644 | rgw::IAM::s3PutObject, |
5645 | rgw_obj(s->bucket, s->object)); | |
11fdf7f2 | 5646 | } |
31f18b77 FG |
5647 | if (e == Effect::Allow) { |
5648 | return 0; | |
5649 | } else if (e == Effect::Deny) { | |
5650 | return -EACCES; | |
11fdf7f2 TL |
5651 | } else if (usr_policy_res == Effect::Allow) { |
5652 | return 0; | |
31f18b77 FG |
5653 | } |
5654 | } | |
5655 | ||
11fdf7f2 | 5656 | if (!verify_bucket_permission_no_policy(this, s, RGW_PERM_WRITE)) { |
7c673cae | 5657 | return -EACCES; |
31f18b77 | 5658 | } |
7c673cae FG |
5659 | |
5660 | return 0; | |
5661 | } | |
5662 | ||
5663 | void RGWInitMultipart::pre_exec() | |
5664 | { | |
5665 | rgw_bucket_object_pre_exec(s); | |
5666 | } | |
5667 | ||
5668 | void RGWInitMultipart::execute() | |
5669 | { | |
5670 | bufferlist aclbl; | |
5671 | map<string, bufferlist> attrs; | |
5672 | rgw_obj obj; | |
5673 | ||
5674 | if (get_params() < 0) | |
5675 | return; | |
5676 | ||
5677 | if (s->object.empty()) | |
5678 | return; | |
5679 | ||
5680 | policy.encode(aclbl); | |
5681 | attrs[RGW_ATTR_ACL] = aclbl; | |
5682 | ||
5683 | populate_with_generic_attrs(s, attrs); | |
5684 | ||
5685 | /* select encryption mode */ | |
5686 | op_ret = prepare_encryption(attrs); | |
5687 | if (op_ret != 0) | |
5688 | return; | |
5689 | ||
3efd9988 FG |
5690 | op_ret = rgw_get_request_metadata(s->cct, s->info, attrs); |
5691 | if (op_ret < 0) { | |
5692 | return; | |
5693 | } | |
7c673cae FG |
5694 | |
5695 | do { | |
5696 | char buf[33]; | |
5697 | gen_rand_alphanumeric(s->cct, buf, sizeof(buf) - 1); | |
5698 | upload_id = MULTIPART_UPLOAD_ID_PREFIX; /* v2 upload id */ | |
5699 | upload_id.append(buf); | |
5700 | ||
5701 | string tmp_obj_name; | |
5702 | RGWMPObj mp(s->object.name, upload_id); | |
5703 | tmp_obj_name = mp.get_meta(); | |
5704 | ||
5705 | obj.init_ns(s->bucket, tmp_obj_name, mp_ns); | |
5706 | // the meta object will be indexed with 0 size, we c | |
5707 | obj.set_in_extra_data(true); | |
5708 | obj.index_hash_source = s->object.name; | |
5709 | ||
5710 | RGWRados::Object op_target(store, s->bucket_info, *static_cast<RGWObjectCtx *>(s->obj_ctx), obj); | |
5711 | op_target.set_versioning_disabled(true); /* no versioning for multipart meta */ | |
5712 | ||
5713 | RGWRados::Object::Write obj_op(&op_target); | |
5714 | ||
5715 | obj_op.meta.owner = s->owner.get_id(); | |
11fdf7f2 | 5716 | obj_op.meta.category = RGWObjCategory::MultiMeta; |
7c673cae FG |
5717 | obj_op.meta.flags = PUT_OBJ_CREATE_EXCL; |
5718 | ||
11fdf7f2 TL |
5719 | multipart_upload_info upload_info; |
5720 | upload_info.dest_placement = s->dest_placement; | |
7c673cae | 5721 | |
11fdf7f2 TL |
5722 | bufferlist bl; |
5723 | encode(upload_info, bl); | |
5724 | obj_op.meta.data = &bl; | |
7c673cae | 5725 | |
11fdf7f2 TL |
5726 | op_ret = obj_op.write_meta(bl.length(), 0, attrs); |
5727 | } while (op_ret == -EEXIST); | |
eafe8130 TL |
5728 | |
5729 | const auto ret = rgw::notify::publish(s, ceph::real_clock::now(), attrs[RGW_ATTR_ETAG].to_str(), rgw::notify::ObjectCreatedPost, store); | |
5730 | if (ret < 0) { | |
5731 | ldpp_dout(this, 5) << "WARNING: publishing notification failed, with error: " << ret << dendl; | |
5732 | // TODO: we should have conf to make send a blocking coroutine and reply with error in case sending failed | |
5733 | // this should be global conf (probably returnign a different handler) | |
5734 | // so we don't need to read the configured values before we perform it | |
5735 | } | |
7c673cae FG |
5736 | } |
5737 | ||
5738 | int RGWCompleteMultipart::verify_permission() | |
5739 | { | |
11fdf7f2 TL |
5740 | if (s->iam_policy || ! s->iam_user_policies.empty()) { |
5741 | auto usr_policy_res = eval_user_policies(s->iam_user_policies, s->env, | |
5742 | boost::none, | |
5743 | rgw::IAM::s3PutObject, | |
5744 | rgw_obj(s->bucket, s->object)); | |
5745 | if (usr_policy_res == Effect::Deny) { | |
5746 | return -EACCES; | |
5747 | } | |
5748 | ||
5749 | rgw::IAM::Effect e = Effect::Pass; | |
5750 | if (s->iam_policy) { | |
5751 | e = s->iam_policy->eval(s->env, *s->auth.identity, | |
31f18b77 FG |
5752 | rgw::IAM::s3PutObject, |
5753 | rgw_obj(s->bucket, s->object)); | |
11fdf7f2 | 5754 | } |
31f18b77 FG |
5755 | if (e == Effect::Allow) { |
5756 | return 0; | |
5757 | } else if (e == Effect::Deny) { | |
5758 | return -EACCES; | |
11fdf7f2 TL |
5759 | } else if (usr_policy_res == Effect::Allow) { |
5760 | return 0; | |
31f18b77 FG |
5761 | } |
5762 | } | |
5763 | ||
11fdf7f2 | 5764 | if (!verify_bucket_permission_no_policy(this, s, RGW_PERM_WRITE)) { |
7c673cae | 5765 | return -EACCES; |
31f18b77 | 5766 | } |
7c673cae FG |
5767 | |
5768 | return 0; | |
5769 | } | |
5770 | ||
5771 | void RGWCompleteMultipart::pre_exec() | |
5772 | { | |
5773 | rgw_bucket_object_pre_exec(s); | |
5774 | } | |
5775 | ||
5776 | void RGWCompleteMultipart::execute() | |
5777 | { | |
5778 | RGWMultiCompleteUpload *parts; | |
5779 | map<int, string>::iterator iter; | |
5780 | RGWMultiXMLParser parser; | |
5781 | string meta_oid; | |
5782 | map<uint32_t, RGWUploadPartInfo> obj_parts; | |
5783 | map<uint32_t, RGWUploadPartInfo>::iterator obj_iter; | |
5784 | map<string, bufferlist> attrs; | |
5785 | off_t ofs = 0; | |
5786 | MD5 hash; | |
5787 | char final_etag[CEPH_CRYPTO_MD5_DIGESTSIZE]; | |
5788 | char final_etag_str[CEPH_CRYPTO_MD5_DIGESTSIZE * 2 + 16]; | |
5789 | bufferlist etag_bl; | |
5790 | rgw_obj meta_obj; | |
5791 | rgw_obj target_obj; | |
5792 | RGWMPObj mp; | |
5793 | RGWObjManifest manifest; | |
5794 | uint64_t olh_epoch = 0; | |
7c673cae FG |
5795 | |
5796 | op_ret = get_params(); | |
5797 | if (op_ret < 0) | |
5798 | return; | |
5799 | op_ret = get_system_versioning_params(s, &olh_epoch, &version_id); | |
5800 | if (op_ret < 0) { | |
5801 | return; | |
5802 | } | |
5803 | ||
11fdf7f2 | 5804 | if (!data.length()) { |
7c673cae FG |
5805 | op_ret = -ERR_MALFORMED_XML; |
5806 | return; | |
5807 | } | |
5808 | ||
5809 | if (!parser.init()) { | |
5810 | op_ret = -EIO; | |
5811 | return; | |
5812 | } | |
5813 | ||
11fdf7f2 | 5814 | if (!parser.parse(data.c_str(), data.length(), 1)) { |
7c673cae FG |
5815 | op_ret = -ERR_MALFORMED_XML; |
5816 | return; | |
5817 | } | |
5818 | ||
5819 | parts = static_cast<RGWMultiCompleteUpload *>(parser.find_first("CompleteMultipartUpload")); | |
5820 | if (!parts || parts->parts.empty()) { | |
5821 | op_ret = -ERR_MALFORMED_XML; | |
5822 | return; | |
5823 | } | |
5824 | ||
5825 | if ((int)parts->parts.size() > | |
5826 | s->cct->_conf->rgw_multipart_part_upload_limit) { | |
5827 | op_ret = -ERANGE; | |
5828 | return; | |
5829 | } | |
5830 | ||
5831 | mp.init(s->object.name, upload_id); | |
5832 | meta_oid = mp.get_meta(); | |
5833 | ||
5834 | int total_parts = 0; | |
5835 | int handled_parts = 0; | |
5836 | int max_parts = 1000; | |
5837 | int marker = 0; | |
5838 | bool truncated; | |
5839 | RGWCompressionInfo cs_info; | |
5840 | bool compressed = false; | |
5841 | uint64_t accounted_size = 0; | |
5842 | ||
5843 | uint64_t min_part_size = s->cct->_conf->rgw_multipart_min_part_size; | |
5844 | ||
5845 | list<rgw_obj_index_key> remove_objs; /* objects to be removed from index listing */ | |
5846 | ||
5847 | bool versioned_object = s->bucket_info.versioning_enabled(); | |
5848 | ||
5849 | iter = parts->parts.begin(); | |
5850 | ||
5851 | meta_obj.init_ns(s->bucket, meta_oid, mp_ns); | |
5852 | meta_obj.set_in_extra_data(true); | |
5853 | meta_obj.index_hash_source = s->object.name; | |
5854 | ||
d2e6a577 FG |
5855 | /*take a cls lock on meta_obj to prevent racing completions (or retries) |
5856 | from deleting the parts*/ | |
5857 | rgw_pool meta_pool; | |
5858 | rgw_raw_obj raw_obj; | |
3efd9988 | 5859 | int max_lock_secs_mp = |
11fdf7f2 | 5860 | s->cct->_conf.get_val<int64_t>("rgw_mp_lock_max_time"); |
3efd9988 | 5861 | utime_t dur(max_lock_secs_mp, 0); |
d2e6a577 | 5862 | |
d2e6a577 | 5863 | store->obj_to_raw((s->bucket_info).placement_rule, meta_obj, &raw_obj); |
3efd9988 FG |
5864 | store->get_obj_data_pool((s->bucket_info).placement_rule, |
5865 | meta_obj,&meta_pool); | |
494da23a | 5866 | store->open_pool_ctx(meta_pool, serializer.ioctx, true); |
d2e6a577 | 5867 | |
3efd9988 | 5868 | op_ret = serializer.try_lock(raw_obj.oid, dur); |
d2e6a577 | 5869 | if (op_ret < 0) { |
11fdf7f2 | 5870 | ldpp_dout(this, 0) << "failed to acquire lock" << dendl; |
d2e6a577 FG |
5871 | op_ret = -ERR_INTERNAL_ERROR; |
5872 | s->err.message = "This multipart completion is already in progress"; | |
5873 | return; | |
5874 | } | |
5875 | ||
7c673cae FG |
5876 | op_ret = get_obj_attrs(store, s, meta_obj, attrs); |
5877 | ||
5878 | if (op_ret < 0) { | |
11fdf7f2 | 5879 | ldpp_dout(this, 0) << "ERROR: failed to get obj attrs, obj=" << meta_obj |
7c673cae FG |
5880 | << " ret=" << op_ret << dendl; |
5881 | return; | |
5882 | } | |
5883 | ||
5884 | do { | |
5885 | op_ret = list_multipart_parts(store, s, upload_id, meta_oid, max_parts, | |
5886 | marker, obj_parts, &marker, &truncated); | |
5887 | if (op_ret == -ENOENT) { | |
5888 | op_ret = -ERR_NO_SUCH_UPLOAD; | |
5889 | } | |
5890 | if (op_ret < 0) | |
5891 | return; | |
5892 | ||
5893 | total_parts += obj_parts.size(); | |
5894 | if (!truncated && total_parts != (int)parts->parts.size()) { | |
11fdf7f2 | 5895 | ldpp_dout(this, 0) << "NOTICE: total parts mismatch: have: " << total_parts |
7c673cae FG |
5896 | << " expected: " << parts->parts.size() << dendl; |
5897 | op_ret = -ERR_INVALID_PART; | |
5898 | return; | |
5899 | } | |
5900 | ||
5901 | for (obj_iter = obj_parts.begin(); iter != parts->parts.end() && obj_iter != obj_parts.end(); ++iter, ++obj_iter, ++handled_parts) { | |
5902 | uint64_t part_size = obj_iter->second.accounted_size; | |
5903 | if (handled_parts < (int)parts->parts.size() - 1 && | |
5904 | part_size < min_part_size) { | |
5905 | op_ret = -ERR_TOO_SMALL; | |
5906 | return; | |
5907 | } | |
5908 | ||
5909 | char petag[CEPH_CRYPTO_MD5_DIGESTSIZE]; | |
5910 | if (iter->first != (int)obj_iter->first) { | |
11fdf7f2 | 5911 | ldpp_dout(this, 0) << "NOTICE: parts num mismatch: next requested: " |
7c673cae FG |
5912 | << iter->first << " next uploaded: " |
5913 | << obj_iter->first << dendl; | |
5914 | op_ret = -ERR_INVALID_PART; | |
5915 | return; | |
5916 | } | |
5917 | string part_etag = rgw_string_unquote(iter->second); | |
5918 | if (part_etag.compare(obj_iter->second.etag) != 0) { | |
11fdf7f2 | 5919 | ldpp_dout(this, 0) << "NOTICE: etag mismatch: part: " << iter->first |
7c673cae FG |
5920 | << " etag: " << iter->second << dendl; |
5921 | op_ret = -ERR_INVALID_PART; | |
5922 | return; | |
5923 | } | |
5924 | ||
5925 | hex_to_buf(obj_iter->second.etag.c_str(), petag, | |
5926 | CEPH_CRYPTO_MD5_DIGESTSIZE); | |
11fdf7f2 | 5927 | hash.Update((const unsigned char *)petag, sizeof(petag)); |
7c673cae FG |
5928 | |
5929 | RGWUploadPartInfo& obj_part = obj_iter->second; | |
5930 | ||
5931 | /* update manifest for part */ | |
5932 | string oid = mp.get_part(obj_iter->second.num); | |
5933 | rgw_obj src_obj; | |
5934 | src_obj.init_ns(s->bucket, oid, mp_ns); | |
5935 | ||
5936 | if (obj_part.manifest.empty()) { | |
11fdf7f2 | 5937 | ldpp_dout(this, 0) << "ERROR: empty manifest for object part: obj=" |
7c673cae FG |
5938 | << src_obj << dendl; |
5939 | op_ret = -ERR_INVALID_PART; | |
5940 | return; | |
5941 | } else { | |
11fdf7f2 | 5942 | manifest.append(obj_part.manifest, store->svc.zone); |
7c673cae FG |
5943 | } |
5944 | ||
11fdf7f2 TL |
5945 | bool part_compressed = (obj_part.cs_info.compression_type != "none"); |
5946 | if ((obj_iter != obj_parts.begin()) && | |
5947 | ((part_compressed != compressed) || | |
5948 | (cs_info.compression_type != obj_part.cs_info.compression_type))) { | |
5949 | ldpp_dout(this, 0) << "ERROR: compression type was changed during multipart upload (" | |
7c673cae FG |
5950 | << cs_info.compression_type << ">>" << obj_part.cs_info.compression_type << ")" << dendl; |
5951 | op_ret = -ERR_INVALID_PART; | |
11fdf7f2 TL |
5952 | return; |
5953 | } | |
5954 | ||
5955 | if (part_compressed) { | |
224ce89b | 5956 | int64_t new_ofs; // offset in compression data for new part |
7c673cae FG |
5957 | if (cs_info.blocks.size() > 0) |
5958 | new_ofs = cs_info.blocks.back().new_ofs + cs_info.blocks.back().len; | |
5959 | else | |
5960 | new_ofs = 0; | |
5961 | for (const auto& block : obj_part.cs_info.blocks) { | |
5962 | compression_block cb; | |
5963 | cb.old_ofs = block.old_ofs + cs_info.orig_size; | |
5964 | cb.new_ofs = new_ofs; | |
5965 | cb.len = block.len; | |
5966 | cs_info.blocks.push_back(cb); | |
5967 | new_ofs = cb.new_ofs + cb.len; | |
5968 | } | |
5969 | if (!compressed) | |
5970 | cs_info.compression_type = obj_part.cs_info.compression_type; | |
5971 | cs_info.orig_size += obj_part.cs_info.orig_size; | |
5972 | compressed = true; | |
5973 | } | |
5974 | ||
5975 | rgw_obj_index_key remove_key; | |
5976 | src_obj.key.get_index_key(&remove_key); | |
5977 | ||
5978 | remove_objs.push_back(remove_key); | |
5979 | ||
5980 | ofs += obj_part.size; | |
5981 | accounted_size += obj_part.accounted_size; | |
5982 | } | |
5983 | } while (truncated); | |
11fdf7f2 | 5984 | hash.Final((unsigned char *)final_etag); |
7c673cae FG |
5985 | |
5986 | buf_to_hex((unsigned char *)final_etag, sizeof(final_etag), final_etag_str); | |
5987 | snprintf(&final_etag_str[CEPH_CRYPTO_MD5_DIGESTSIZE * 2], sizeof(final_etag_str) - CEPH_CRYPTO_MD5_DIGESTSIZE * 2, | |
5988 | "-%lld", (long long)parts->parts.size()); | |
5989 | etag = final_etag_str; | |
11fdf7f2 | 5990 | ldpp_dout(this, 10) << "calculated etag: " << final_etag_str << dendl; |
7c673cae | 5991 | |
11fdf7f2 | 5992 | etag_bl.append(final_etag_str, strlen(final_etag_str)); |
7c673cae FG |
5993 | |
5994 | attrs[RGW_ATTR_ETAG] = etag_bl; | |
5995 | ||
5996 | if (compressed) { | |
5997 | // write compression attribute to full object | |
5998 | bufferlist tmp; | |
11fdf7f2 | 5999 | encode(cs_info, tmp); |
7c673cae FG |
6000 | attrs[RGW_ATTR_COMPRESSION] = tmp; |
6001 | } | |
6002 | ||
6003 | target_obj.init(s->bucket, s->object.name); | |
6004 | if (versioned_object) { | |
11fdf7f2 TL |
6005 | if (!version_id.empty()) { |
6006 | target_obj.key.set_instance(version_id); | |
6007 | } else { | |
6008 | store->gen_rand_obj_instance_name(&target_obj); | |
6009 | version_id = target_obj.key.get_instance(); | |
6010 | } | |
7c673cae FG |
6011 | } |
6012 | ||
6013 | RGWObjectCtx& obj_ctx = *static_cast<RGWObjectCtx *>(s->obj_ctx); | |
6014 | ||
11fdf7f2 | 6015 | obj_ctx.set_atomic(target_obj); |
7c673cae FG |
6016 | |
6017 | RGWRados::Object op_target(store, s->bucket_info, *static_cast<RGWObjectCtx *>(s->obj_ctx), target_obj); | |
6018 | RGWRados::Object::Write obj_op(&op_target); | |
6019 | ||
6020 | obj_op.meta.manifest = &manifest; | |
6021 | obj_op.meta.remove_objs = &remove_objs; | |
6022 | ||
6023 | obj_op.meta.ptag = &s->req_id; /* use req_id as operation tag */ | |
6024 | obj_op.meta.owner = s->owner.get_id(); | |
6025 | obj_op.meta.flags = PUT_OBJ_CREATE; | |
181888fb | 6026 | obj_op.meta.modify_tail = true; |
3efd9988 | 6027 | obj_op.meta.completeMultipart = true; |
91327a77 | 6028 | obj_op.meta.olh_epoch = olh_epoch; |
7c673cae FG |
6029 | op_ret = obj_op.write_meta(ofs, accounted_size, attrs); |
6030 | if (op_ret < 0) | |
6031 | return; | |
6032 | ||
6033 | // remove the upload obj | |
6034 | int r = store->delete_obj(*static_cast<RGWObjectCtx *>(s->obj_ctx), | |
6035 | s->bucket_info, meta_obj, 0); | |
3efd9988 FG |
6036 | if (r >= 0) { |
6037 | /* serializer's exclusive lock is released */ | |
6038 | serializer.clear_locked(); | |
6039 | } else { | |
11fdf7f2 | 6040 | ldpp_dout(this, 0) << "WARNING: failed to remove object " << meta_obj << dendl; |
3efd9988 | 6041 | } |
eafe8130 TL |
6042 | |
6043 | const auto ret = rgw::notify::publish(s, ceph::real_clock::now(), etag, rgw::notify::ObjectCreatedCompleteMultipartUpload, store); | |
6044 | if (ret < 0) { | |
6045 | ldpp_dout(this, 5) << "WARNING: publishing notification failed, with error: " << ret << dendl; | |
6046 | // TODO: we should have conf to make send a blocking coroutine and reply with error in case sending failed | |
6047 | // this should be global conf (probably returnign a different handler) | |
6048 | // so we don't need to read the configured values before we perform it | |
6049 | } | |
3efd9988 FG |
6050 | } |
6051 | ||
6052 | int RGWCompleteMultipart::MPSerializer::try_lock( | |
6053 | const std::string& _oid, | |
6054 | utime_t dur) | |
6055 | { | |
6056 | oid = _oid; | |
6057 | op.assert_exists(); | |
6058 | lock.set_duration(dur); | |
6059 | lock.lock_exclusive(&op); | |
6060 | int ret = ioctx.operate(oid, &op); | |
6061 | if (! ret) { | |
6062 | locked = true; | |
6063 | } | |
6064 | return ret; | |
6065 | } | |
6066 | ||
6067 | void RGWCompleteMultipart::complete() | |
6068 | { | |
6069 | /* release exclusive lock iff not already */ | |
6070 | if (unlikely(serializer.locked)) { | |
6071 | int r = serializer.unlock(); | |
d2e6a577 | 6072 | if (r < 0) { |
11fdf7f2 | 6073 | ldpp_dout(this, 0) << "WARNING: failed to unlock " << serializer.oid << dendl; |
d2e6a577 | 6074 | } |
7c673cae | 6075 | } |
3efd9988 | 6076 | send_response(); |
7c673cae FG |
6077 | } |
6078 | ||
6079 | int RGWAbortMultipart::verify_permission() | |
6080 | { | |
11fdf7f2 TL |
6081 | if (s->iam_policy || ! s->iam_user_policies.empty()) { |
6082 | auto usr_policy_res = eval_user_policies(s->iam_user_policies, s->env, | |
6083 | boost::none, | |
6084 | rgw::IAM::s3AbortMultipartUpload, | |
6085 | rgw_obj(s->bucket, s->object)); | |
6086 | if (usr_policy_res == Effect::Deny) { | |
6087 | return -EACCES; | |
6088 | } | |
6089 | ||
6090 | rgw::IAM::Effect e = Effect::Pass; | |
6091 | if (s->iam_policy) { | |
6092 | e = s->iam_policy->eval(s->env, *s->auth.identity, | |
31f18b77 FG |
6093 | rgw::IAM::s3AbortMultipartUpload, |
6094 | rgw_obj(s->bucket, s->object)); | |
11fdf7f2 | 6095 | } |
31f18b77 FG |
6096 | if (e == Effect::Allow) { |
6097 | return 0; | |
6098 | } else if (e == Effect::Deny) { | |
6099 | return -EACCES; | |
11fdf7f2 TL |
6100 | } else if (usr_policy_res == Effect::Allow) |
6101 | return 0; | |
31f18b77 FG |
6102 | } |
6103 | ||
11fdf7f2 | 6104 | if (!verify_bucket_permission_no_policy(this, s, RGW_PERM_WRITE)) { |
7c673cae | 6105 | return -EACCES; |
31f18b77 | 6106 | } |
7c673cae FG |
6107 | |
6108 | return 0; | |
6109 | } | |
6110 | ||
6111 | void RGWAbortMultipart::pre_exec() | |
6112 | { | |
6113 | rgw_bucket_object_pre_exec(s); | |
6114 | } | |
6115 | ||
6116 | void RGWAbortMultipart::execute() | |
6117 | { | |
6118 | op_ret = -EINVAL; | |
6119 | string upload_id; | |
6120 | string meta_oid; | |
6121 | upload_id = s->info.args.get("uploadId"); | |
7c673cae FG |
6122 | rgw_obj meta_obj; |
6123 | RGWMPObj mp; | |
6124 | ||
6125 | if (upload_id.empty() || s->object.empty()) | |
6126 | return; | |
6127 | ||
6128 | mp.init(s->object.name, upload_id); | |
6129 | meta_oid = mp.get_meta(); | |
6130 | ||
11fdf7f2 | 6131 | op_ret = get_multipart_info(store, s, meta_oid, nullptr, nullptr, nullptr); |
7c673cae FG |
6132 | if (op_ret < 0) |
6133 | return; | |
6134 | ||
6135 | RGWObjectCtx *obj_ctx = static_cast<RGWObjectCtx *>(s->obj_ctx); | |
6136 | op_ret = abort_multipart_upload(store, s->cct, obj_ctx, s->bucket_info, mp); | |
6137 | } | |
6138 | ||
6139 | int RGWListMultipart::verify_permission() | |
6140 | { | |
11fdf7f2 | 6141 | if (!verify_object_permission(this, s, rgw::IAM::s3ListMultipartUploadParts)) |
7c673cae FG |
6142 | return -EACCES; |
6143 | ||
6144 | return 0; | |
6145 | } | |
6146 | ||
6147 | void RGWListMultipart::pre_exec() | |
6148 | { | |
6149 | rgw_bucket_object_pre_exec(s); | |
6150 | } | |
6151 | ||
6152 | void RGWListMultipart::execute() | |
6153 | { | |
7c673cae FG |
6154 | string meta_oid; |
6155 | RGWMPObj mp; | |
6156 | ||
6157 | op_ret = get_params(); | |
6158 | if (op_ret < 0) | |
6159 | return; | |
6160 | ||
6161 | mp.init(s->object.name, upload_id); | |
6162 | meta_oid = mp.get_meta(); | |
6163 | ||
11fdf7f2 | 6164 | op_ret = get_multipart_info(store, s, meta_oid, &policy, nullptr, nullptr); |
7c673cae FG |
6165 | if (op_ret < 0) |
6166 | return; | |
6167 | ||
6168 | op_ret = list_multipart_parts(store, s, upload_id, meta_oid, max_parts, | |
6169 | marker, parts, NULL, &truncated); | |
6170 | } | |
6171 | ||
6172 | int RGWListBucketMultiparts::verify_permission() | |
6173 | { | |
11fdf7f2 TL |
6174 | if (!verify_bucket_permission(this, |
6175 | s, | |
28e407b8 | 6176 | rgw::IAM::s3ListBucketMultipartUploads)) |
7c673cae FG |
6177 | return -EACCES; |
6178 | ||
6179 | return 0; | |
6180 | } | |
6181 | ||
6182 | void RGWListBucketMultiparts::pre_exec() | |
6183 | { | |
6184 | rgw_bucket_object_pre_exec(s); | |
6185 | } | |
6186 | ||
6187 | void RGWListBucketMultiparts::execute() | |
6188 | { | |
6189 | vector<rgw_bucket_dir_entry> objs; | |
6190 | string marker_meta; | |
6191 | ||
6192 | op_ret = get_params(); | |
6193 | if (op_ret < 0) | |
6194 | return; | |
6195 | ||
6196 | if (s->prot_flags & RGW_REST_SWIFT) { | |
6197 | string path_args; | |
6198 | path_args = s->info.args.get("path"); | |
6199 | if (!path_args.empty()) { | |
6200 | if (!delimiter.empty() || !prefix.empty()) { | |
6201 | op_ret = -EINVAL; | |
6202 | return; | |
6203 | } | |
6204 | prefix = path_args; | |
6205 | delimiter="/"; | |
6206 | } | |
6207 | } | |
6208 | marker_meta = marker.get_meta(); | |
6209 | ||
224ce89b WB |
6210 | op_ret = list_bucket_multiparts(store, s->bucket_info, prefix, marker_meta, delimiter, |
6211 | max_uploads, &objs, &common_prefixes, &is_truncated); | |
6212 | if (op_ret < 0) { | |
6213 | return; | |
6214 | } | |
7c673cae | 6215 | |
7c673cae FG |
6216 | if (!objs.empty()) { |
6217 | vector<rgw_bucket_dir_entry>::iterator iter; | |
6218 | RGWMultipartUploadEntry entry; | |
6219 | for (iter = objs.begin(); iter != objs.end(); ++iter) { | |
6220 | rgw_obj_key key(iter->key); | |
6221 | if (!entry.mp.from_meta(key.name)) | |
6222 | continue; | |
6223 | entry.obj = *iter; | |
6224 | uploads.push_back(entry); | |
6225 | } | |
6226 | next_marker = entry; | |
6227 | } | |
6228 | } | |
6229 | ||
6230 | void RGWGetHealthCheck::execute() | |
6231 | { | |
11fdf7f2 TL |
6232 | if (!g_conf()->rgw_healthcheck_disabling_path.empty() && |
6233 | (::access(g_conf()->rgw_healthcheck_disabling_path.c_str(), F_OK) == 0)) { | |
7c673cae FG |
6234 | /* Disabling path specified & existent in the filesystem. */ |
6235 | op_ret = -ERR_SERVICE_UNAVAILABLE; /* 503 */ | |
6236 | } else { | |
6237 | op_ret = 0; /* 200 OK */ | |
6238 | } | |
6239 | } | |
6240 | ||
6241 | int RGWDeleteMultiObj::verify_permission() | |
6242 | { | |
11fdf7f2 TL |
6243 | if (s->iam_policy || ! s->iam_user_policies.empty()) { |
6244 | auto usr_policy_res = eval_user_policies(s->iam_user_policies, s->env, | |
6245 | boost::none, | |
6246 | s->object.instance.empty() ? | |
6247 | rgw::IAM::s3DeleteObject : | |
6248 | rgw::IAM::s3DeleteObjectVersion, | |
6249 | ARN(s->bucket)); | |
6250 | if (usr_policy_res == Effect::Deny) { | |
6251 | return -EACCES; | |
6252 | } | |
6253 | ||
6254 | rgw::IAM::Effect r = Effect::Pass; | |
6255 | if (s->iam_policy) { | |
6256 | r = s->iam_policy->eval(s->env, *s->auth.identity, | |
6257 | s->object.instance.empty() ? | |
6258 | rgw::IAM::s3DeleteObject : | |
6259 | rgw::IAM::s3DeleteObjectVersion, | |
6260 | ARN(s->bucket)); | |
6261 | } | |
6262 | if (r == Effect::Allow) | |
6263 | return 0; | |
6264 | else if (r == Effect::Deny) | |
6265 | return -EACCES; | |
6266 | else if (usr_policy_res == Effect::Allow) | |
6267 | return 0; | |
6268 | } | |
6269 | ||
6270 | acl_allowed = verify_bucket_permission_no_policy(this, s, RGW_PERM_WRITE); | |
6271 | if (!acl_allowed) | |
7c673cae FG |
6272 | return -EACCES; |
6273 | ||
6274 | return 0; | |
6275 | } | |
6276 | ||
6277 | void RGWDeleteMultiObj::pre_exec() | |
6278 | { | |
6279 | rgw_bucket_object_pre_exec(s); | |
6280 | } | |
6281 | ||
6282 | void RGWDeleteMultiObj::execute() | |
6283 | { | |
6284 | RGWMultiDelDelete *multi_delete; | |
6285 | vector<rgw_obj_key>::iterator iter; | |
6286 | RGWMultiDelXMLParser parser; | |
7c673cae | 6287 | RGWObjectCtx *obj_ctx = static_cast<RGWObjectCtx *>(s->obj_ctx); |
11fdf7f2 | 6288 | char* buf; |
7c673cae FG |
6289 | |
6290 | op_ret = get_params(); | |
6291 | if (op_ret < 0) { | |
6292 | goto error; | |
6293 | } | |
6294 | ||
11fdf7f2 TL |
6295 | buf = data.c_str(); |
6296 | if (!buf) { | |
7c673cae FG |
6297 | op_ret = -EINVAL; |
6298 | goto error; | |
6299 | } | |
6300 | ||
6301 | if (!parser.init()) { | |
6302 | op_ret = -EINVAL; | |
6303 | goto error; | |
6304 | } | |
6305 | ||
11fdf7f2 | 6306 | if (!parser.parse(buf, data.length(), 1)) { |
7c673cae FG |
6307 | op_ret = -EINVAL; |
6308 | goto error; | |
6309 | } | |
6310 | ||
6311 | multi_delete = static_cast<RGWMultiDelDelete *>(parser.find_first("Delete")); | |
6312 | if (!multi_delete) { | |
6313 | op_ret = -EINVAL; | |
6314 | goto error; | |
11fdf7f2 TL |
6315 | } else { |
6316 | #define DELETE_MULTI_OBJ_MAX_NUM 1000 | |
6317 | int max_num = s->cct->_conf->rgw_delete_multi_obj_max_num; | |
6318 | if (max_num < 0) { | |
6319 | max_num = DELETE_MULTI_OBJ_MAX_NUM; | |
6320 | } | |
6321 | int multi_delete_object_num = multi_delete->objects.size(); | |
6322 | if (multi_delete_object_num > max_num) { | |
6323 | op_ret = -ERR_MALFORMED_XML; | |
6324 | goto error; | |
6325 | } | |
7c673cae FG |
6326 | } |
6327 | ||
6328 | if (multi_delete->is_quiet()) | |
6329 | quiet = true; | |
6330 | ||
11fdf7f2 TL |
6331 | if (s->bucket_info.mfa_enabled()) { |
6332 | bool has_versioned = false; | |
6333 | for (auto i : multi_delete->objects) { | |
6334 | if (!i.instance.empty()) { | |
6335 | has_versioned = true; | |
6336 | break; | |
6337 | } | |
6338 | } | |
6339 | if (has_versioned && !s->mfa_verified) { | |
6340 | ldpp_dout(this, 5) << "NOTICE: multi-object delete request with a versioned object, mfa auth not provided" << dendl; | |
6341 | op_ret = -ERR_MFA_REQUIRED; | |
6342 | goto error; | |
6343 | } | |
6344 | } | |
6345 | ||
7c673cae FG |
6346 | begin_response(); |
6347 | if (multi_delete->objects.empty()) { | |
6348 | goto done; | |
6349 | } | |
6350 | ||
6351 | for (iter = multi_delete->objects.begin(); | |
11fdf7f2 TL |
6352 | iter != multi_delete->objects.end(); |
6353 | ++iter) { | |
7c673cae | 6354 | rgw_obj obj(bucket, *iter); |
11fdf7f2 TL |
6355 | if (s->iam_policy || ! s->iam_user_policies.empty()) { |
6356 | auto usr_policy_res = eval_user_policies(s->iam_user_policies, s->env, | |
6357 | boost::none, | |
6358 | iter->instance.empty() ? | |
6359 | rgw::IAM::s3DeleteObject : | |
6360 | rgw::IAM::s3DeleteObjectVersion, | |
6361 | ARN(obj)); | |
6362 | if (usr_policy_res == Effect::Deny) { | |
6363 | send_partial_response(*iter, false, "", -EACCES); | |
6364 | continue; | |
6365 | } | |
6366 | ||
6367 | rgw::IAM::Effect e = Effect::Pass; | |
6368 | if (s->iam_policy) { | |
6369 | e = s->iam_policy->eval(s->env, | |
31f18b77 FG |
6370 | *s->auth.identity, |
6371 | iter->instance.empty() ? | |
6372 | rgw::IAM::s3DeleteObject : | |
6373 | rgw::IAM::s3DeleteObjectVersion, | |
11fdf7f2 TL |
6374 | ARN(obj)); |
6375 | } | |
31f18b77 | 6376 | if ((e == Effect::Deny) || |
11fdf7f2 | 6377 | (usr_policy_res == Effect::Pass && e == Effect::Pass && !acl_allowed)) { |
31f18b77 FG |
6378 | send_partial_response(*iter, false, "", -EACCES); |
6379 | continue; | |
6380 | } | |
6381 | } | |
7c673cae | 6382 | |
11fdf7f2 | 6383 | obj_ctx->set_atomic(obj); |
7c673cae FG |
6384 | |
6385 | RGWRados::Object del_target(store, s->bucket_info, *obj_ctx, obj); | |
6386 | RGWRados::Object::Delete del_op(&del_target); | |
6387 | ||
6388 | del_op.params.bucket_owner = s->bucket_owner.get_id(); | |
6389 | del_op.params.versioning_status = s->bucket_info.versioning_status(); | |
6390 | del_op.params.obj_owner = s->owner; | |
6391 | ||
6392 | op_ret = del_op.delete_obj(); | |
6393 | if (op_ret == -ENOENT) { | |
6394 | op_ret = 0; | |
6395 | } | |
6396 | ||
6397 | send_partial_response(*iter, del_op.result.delete_marker, | |
6398 | del_op.result.version_id, op_ret); | |
6399 | } | |
6400 | ||
6401 | /* set the return code to zero, errors at this point will be | |
6402 | dumped to the response */ | |
6403 | op_ret = 0; | |
6404 | ||
6405 | done: | |
6406 | // will likely segfault if begin_response() has not been called | |
6407 | end_response(); | |
7c673cae FG |
6408 | return; |
6409 | ||
6410 | error: | |
6411 | send_status(); | |
7c673cae FG |
6412 | return; |
6413 | ||
6414 | } | |
6415 | ||
6416 | bool RGWBulkDelete::Deleter::verify_permission(RGWBucketInfo& binfo, | |
6417 | map<string, bufferlist>& battrs, | |
6418 | ACLOwner& bucket_owner /* out */) | |
6419 | { | |
6420 | RGWAccessControlPolicy bacl(store->ctx()); | |
6421 | int ret = read_bucket_policy(store, s, binfo, battrs, &bacl, binfo.bucket); | |
6422 | if (ret < 0) { | |
6423 | return false; | |
6424 | } | |
6425 | ||
31f18b77 FG |
6426 | auto policy = get_iam_policy_from_attr(s->cct, store, battrs, binfo.bucket.tenant); |
6427 | ||
7c673cae FG |
6428 | bucket_owner = bacl.get_owner(); |
6429 | ||
6430 | /* We can use global user_acl because each BulkDelete request is allowed | |
6431 | * to work on entities from a single account only. */ | |
11fdf7f2 TL |
6432 | return verify_bucket_permission(dpp, s, binfo.bucket, s->user_acl.get(), |
6433 | &bacl, policy, s->iam_user_policies, rgw::IAM::s3DeleteBucket); | |
7c673cae FG |
6434 | } |
6435 | ||
6436 | bool RGWBulkDelete::Deleter::delete_single(const acct_path_t& path) | |
6437 | { | |
6438 | auto& obj_ctx = *static_cast<RGWObjectCtx *>(s->obj_ctx); | |
6439 | ||
6440 | RGWBucketInfo binfo; | |
6441 | map<string, bufferlist> battrs; | |
6442 | ACLOwner bowner; | |
6443 | ||
11fdf7f2 | 6444 | int ret = store->get_bucket_info(*s->sysobj_ctx, s->user->user_id.tenant, |
7c673cae FG |
6445 | path.bucket_name, binfo, nullptr, |
6446 | &battrs); | |
6447 | if (ret < 0) { | |
6448 | goto binfo_fail; | |
6449 | } | |
6450 | ||
6451 | if (!verify_permission(binfo, battrs, bowner)) { | |
6452 | ret = -EACCES; | |
6453 | goto auth_fail; | |
6454 | } | |
6455 | ||
6456 | if (!path.obj_key.empty()) { | |
6457 | rgw_obj obj(binfo.bucket, path.obj_key); | |
11fdf7f2 | 6458 | obj_ctx.set_atomic(obj); |
7c673cae FG |
6459 | |
6460 | RGWRados::Object del_target(store, binfo, obj_ctx, obj); | |
6461 | RGWRados::Object::Delete del_op(&del_target); | |
6462 | ||
6463 | del_op.params.bucket_owner = binfo.owner; | |
6464 | del_op.params.versioning_status = binfo.versioning_status(); | |
6465 | del_op.params.obj_owner = bowner; | |
6466 | ||
6467 | ret = del_op.delete_obj(); | |
6468 | if (ret < 0) { | |
6469 | goto delop_fail; | |
6470 | } | |
6471 | } else { | |
6472 | RGWObjVersionTracker ot; | |
6473 | ot.read_version = binfo.ep_objv; | |
6474 | ||
6475 | ret = store->delete_bucket(binfo, ot); | |
6476 | if (0 == ret) { | |
6477 | ret = rgw_unlink_bucket(store, binfo.owner, binfo.bucket.tenant, | |
6478 | binfo.bucket.name, false); | |
6479 | if (ret < 0) { | |
11fdf7f2 | 6480 | ldpp_dout(s, 0) << "WARNING: failed to unlink bucket: ret=" << ret << dendl; |
7c673cae FG |
6481 | } |
6482 | } | |
6483 | if (ret < 0) { | |
6484 | goto delop_fail; | |
6485 | } | |
6486 | ||
11fdf7f2 | 6487 | if (!store->svc.zone->is_meta_master()) { |
7c673cae FG |
6488 | bufferlist in_data; |
6489 | ret = forward_request_to_master(s, &ot.read_version, store, in_data, | |
6490 | nullptr); | |
6491 | if (ret < 0) { | |
6492 | if (ret == -ENOENT) { | |
6493 | /* adjust error, we want to return with NoSuchBucket and not | |
6494 | * NoSuchKey */ | |
6495 | ret = -ERR_NO_SUCH_BUCKET; | |
6496 | } | |
6497 | goto delop_fail; | |
6498 | } | |
6499 | } | |
6500 | } | |
6501 | ||
6502 | num_deleted++; | |
6503 | return true; | |
6504 | ||
6505 | ||
6506 | binfo_fail: | |
6507 | if (-ENOENT == ret) { | |
11fdf7f2 | 6508 | ldpp_dout(s, 20) << "cannot find bucket = " << path.bucket_name << dendl; |
7c673cae FG |
6509 | num_unfound++; |
6510 | } else { | |
11fdf7f2 | 6511 | ldpp_dout(s, 20) << "cannot get bucket info, ret = " << ret << dendl; |
7c673cae FG |
6512 | |
6513 | fail_desc_t failed_item = { | |
6514 | .err = ret, | |
6515 | .path = path | |
6516 | }; | |
6517 | failures.push_back(failed_item); | |
6518 | } | |
6519 | return false; | |
6520 | ||
6521 | auth_fail: | |
11fdf7f2 | 6522 | ldpp_dout(s, 20) << "wrong auth for " << path << dendl; |
7c673cae FG |
6523 | { |
6524 | fail_desc_t failed_item = { | |
6525 | .err = ret, | |
6526 | .path = path | |
6527 | }; | |
6528 | failures.push_back(failed_item); | |
6529 | } | |
6530 | return false; | |
6531 | ||
6532 | delop_fail: | |
6533 | if (-ENOENT == ret) { | |
11fdf7f2 | 6534 | ldpp_dout(s, 20) << "cannot find entry " << path << dendl; |
7c673cae FG |
6535 | num_unfound++; |
6536 | } else { | |
6537 | fail_desc_t failed_item = { | |
6538 | .err = ret, | |
6539 | .path = path | |
6540 | }; | |
6541 | failures.push_back(failed_item); | |
6542 | } | |
6543 | return false; | |
6544 | } | |
6545 | ||
6546 | bool RGWBulkDelete::Deleter::delete_chunk(const std::list<acct_path_t>& paths) | |
6547 | { | |
11fdf7f2 | 6548 | ldpp_dout(s, 20) << "in delete_chunk" << dendl; |
7c673cae | 6549 | for (auto path : paths) { |
11fdf7f2 | 6550 | ldpp_dout(s, 20) << "bulk deleting path: " << path << dendl; |
7c673cae FG |
6551 | delete_single(path); |
6552 | } | |
6553 | ||
6554 | return true; | |
6555 | } | |
6556 | ||
6557 | int RGWBulkDelete::verify_permission() | |
6558 | { | |
6559 | return 0; | |
6560 | } | |
6561 | ||
6562 | void RGWBulkDelete::pre_exec() | |
6563 | { | |
6564 | rgw_bucket_object_pre_exec(s); | |
6565 | } | |
6566 | ||
6567 | void RGWBulkDelete::execute() | |
6568 | { | |
11fdf7f2 | 6569 | deleter = std::unique_ptr<Deleter>(new Deleter(this, store, s)); |
7c673cae FG |
6570 | |
6571 | bool is_truncated = false; | |
6572 | do { | |
6573 | list<RGWBulkDelete::acct_path_t> items; | |
6574 | ||
6575 | int ret = get_data(items, &is_truncated); | |
6576 | if (ret < 0) { | |
6577 | return; | |
6578 | } | |
6579 | ||
6580 | ret = deleter->delete_chunk(items); | |
6581 | } while (!op_ret && is_truncated); | |
6582 | ||
6583 | return; | |
6584 | } | |
6585 | ||
6586 | ||
6587 | constexpr std::array<int, 2> RGWBulkUploadOp::terminal_errors; | |
6588 | ||
6589 | int RGWBulkUploadOp::verify_permission() | |
6590 | { | |
6591 | if (s->auth.identity->is_anonymous()) { | |
6592 | return -EACCES; | |
6593 | } | |
6594 | ||
11fdf7f2 | 6595 | if (! verify_user_permission_no_policy(this, s, RGW_PERM_WRITE)) { |
7c673cae FG |
6596 | return -EACCES; |
6597 | } | |
6598 | ||
6599 | if (s->user->user_id.tenant != s->bucket_tenant) { | |
11fdf7f2 TL |
6600 | ldpp_dout(this, 10) << "user cannot create a bucket in a different tenant" |
6601 | << " (user_id.tenant=" << s->user->user_id.tenant | |
6602 | << " requested=" << s->bucket_tenant << ")" << dendl; | |
7c673cae FG |
6603 | return -EACCES; |
6604 | } | |
6605 | ||
6606 | if (s->user->max_buckets < 0) { | |
6607 | return -EPERM; | |
6608 | } | |
6609 | ||
6610 | return 0; | |
6611 | } | |
6612 | ||
6613 | void RGWBulkUploadOp::pre_exec() | |
6614 | { | |
6615 | rgw_bucket_object_pre_exec(s); | |
6616 | } | |
6617 | ||
6618 | boost::optional<std::pair<std::string, rgw_obj_key>> | |
6619 | RGWBulkUploadOp::parse_path(const boost::string_ref& path) | |
6620 | { | |
6621 | /* We need to skip all slashes at the beginning in order to preserve | |
6622 | * compliance with Swift. */ | |
6623 | const size_t start_pos = path.find_first_not_of('/'); | |
6624 | ||
6625 | if (boost::string_ref::npos != start_pos) { | |
6626 | /* Seperator is the first slash after the leading ones. */ | |
6627 | const size_t sep_pos = path.substr(start_pos).find('/'); | |
6628 | ||
6629 | if (boost::string_ref::npos != sep_pos) { | |
6630 | const auto bucket_name = path.substr(start_pos, sep_pos - start_pos); | |
6631 | const auto obj_name = path.substr(sep_pos + 1); | |
6632 | ||
6633 | return std::make_pair(bucket_name.to_string(), | |
6634 | rgw_obj_key(obj_name.to_string())); | |
6635 | } else { | |
6636 | /* It's guaranteed here that bucket name is at least one character | |
6637 | * long and is different than slash. */ | |
6638 | return std::make_pair(path.substr(start_pos).to_string(), | |
6639 | rgw_obj_key()); | |
6640 | } | |
6641 | } | |
6642 | ||
31f18b77 | 6643 | return none; |
7c673cae FG |
6644 | } |
6645 | ||
6646 | std::pair<std::string, std::string> | |
6647 | RGWBulkUploadOp::handle_upload_path(struct req_state *s) | |
6648 | { | |
6649 | std::string bucket_path, file_prefix; | |
6650 | if (! s->init_state.url_bucket.empty()) { | |
6651 | file_prefix = bucket_path = s->init_state.url_bucket + "/"; | |
6652 | if (! s->object.empty()) { | |
6653 | std::string& object_name = s->object.name; | |
6654 | ||
6655 | /* As rgw_obj_key::empty() already verified emptiness of s->object.name, | |
6656 | * we can safely examine its last element. */ | |
6657 | if (object_name.back() == '/') { | |
6658 | file_prefix.append(object_name); | |
6659 | } else { | |
6660 | file_prefix.append(object_name).append("/"); | |
6661 | } | |
6662 | } | |
6663 | } | |
6664 | return std::make_pair(bucket_path, file_prefix); | |
6665 | } | |
6666 | ||
6667 | int RGWBulkUploadOp::handle_dir_verify_permission() | |
6668 | { | |
6669 | if (s->user->max_buckets > 0) { | |
6670 | RGWUserBuckets buckets; | |
6671 | std::string marker; | |
6672 | bool is_truncated = false; | |
6673 | op_ret = rgw_read_user_buckets(store, s->user->user_id, buckets, | |
6674 | marker, std::string(), s->user->max_buckets, | |
6675 | false, &is_truncated); | |
6676 | if (op_ret < 0) { | |
6677 | return op_ret; | |
6678 | } | |
6679 | ||
6680 | if (buckets.count() >= static_cast<size_t>(s->user->max_buckets)) { | |
6681 | return -ERR_TOO_MANY_BUCKETS; | |
6682 | } | |
6683 | } | |
6684 | ||
6685 | return 0; | |
6686 | } | |
6687 | ||
6688 | static void forward_req_info(CephContext *cct, req_info& info, const std::string& bucket_name) | |
6689 | { | |
6690 | /* the request of container or object level will contain bucket name. | |
6691 | * only at account level need to append the bucket name */ | |
6692 | if (info.script_uri.find(bucket_name) != std::string::npos) { | |
6693 | return; | |
6694 | } | |
6695 | ||
6696 | ldout(cct, 20) << "append the bucket: "<< bucket_name << " to req_info" << dendl; | |
6697 | info.script_uri.append("/").append(bucket_name); | |
6698 | info.request_uri_aws4 = info.request_uri = info.script_uri; | |
6699 | info.effective_uri = "/" + bucket_name; | |
6700 | } | |
6701 | ||
11fdf7f2 TL |
6702 | void RGWBulkUploadOp::init(RGWRados* const store, |
6703 | struct req_state* const s, | |
6704 | RGWHandler* const h) | |
6705 | { | |
6706 | RGWOp::init(store, s, h); | |
6707 | dir_ctx.emplace(store->svc.sysobj->init_obj_ctx()); | |
6708 | } | |
6709 | ||
7c673cae FG |
6710 | int RGWBulkUploadOp::handle_dir(const boost::string_ref path) |
6711 | { | |
11fdf7f2 | 6712 | ldpp_dout(this, 20) << "got directory=" << path << dendl; |
7c673cae FG |
6713 | |
6714 | op_ret = handle_dir_verify_permission(); | |
6715 | if (op_ret < 0) { | |
6716 | return op_ret; | |
6717 | } | |
6718 | ||
6719 | std::string bucket_name; | |
6720 | rgw_obj_key object_junk; | |
6721 | std::tie(bucket_name, object_junk) = *parse_path(path); | |
6722 | ||
11fdf7f2 | 6723 | rgw_raw_obj obj(store->svc.zone->get_zone_params().domain_root, |
7c673cae FG |
6724 | rgw_make_bucket_entry_name(s->bucket_tenant, bucket_name)); |
6725 | ||
6726 | /* we need to make sure we read bucket info, it's not read before for this | |
6727 | * specific request */ | |
6728 | RGWBucketInfo binfo; | |
6729 | std::map<std::string, ceph::bufferlist> battrs; | |
6730 | op_ret = store->get_bucket_info(*dir_ctx, s->bucket_tenant, bucket_name, | |
11fdf7f2 | 6731 | binfo, nullptr, &battrs); |
7c673cae FG |
6732 | if (op_ret < 0 && op_ret != -ENOENT) { |
6733 | return op_ret; | |
6734 | } | |
6735 | const bool bucket_exists = (op_ret != -ENOENT); | |
6736 | ||
6737 | if (bucket_exists) { | |
6738 | RGWAccessControlPolicy old_policy(s->cct); | |
11fdf7f2 TL |
6739 | int r = rgw_op_get_bucket_policy_from_attr(s->cct, store, binfo, |
6740 | battrs, &old_policy); | |
7c673cae FG |
6741 | if (r >= 0) { |
6742 | if (old_policy.get_owner().get_id().compare(s->user->user_id) != 0) { | |
6743 | op_ret = -EEXIST; | |
6744 | return op_ret; | |
6745 | } | |
6746 | } | |
6747 | } | |
6748 | ||
6749 | RGWBucketInfo master_info; | |
6750 | rgw_bucket *pmaster_bucket = nullptr; | |
6751 | uint32_t *pmaster_num_shards = nullptr; | |
6752 | real_time creation_time; | |
6753 | obj_version objv, ep_objv, *pobjv = nullptr; | |
6754 | ||
11fdf7f2 | 6755 | if (! store->svc.zone->is_meta_master()) { |
7c673cae FG |
6756 | JSONParser jp; |
6757 | ceph::bufferlist in_data; | |
6758 | req_info info = s->info; | |
6759 | forward_req_info(s->cct, info, bucket_name); | |
6760 | op_ret = forward_request_to_master(s, nullptr, store, in_data, &jp, &info); | |
6761 | if (op_ret < 0) { | |
6762 | return op_ret; | |
6763 | } | |
6764 | ||
6765 | JSONDecoder::decode_json("entry_point_object_ver", ep_objv, &jp); | |
6766 | JSONDecoder::decode_json("object_ver", objv, &jp); | |
6767 | JSONDecoder::decode_json("bucket_info", master_info, &jp); | |
6768 | ||
11fdf7f2 TL |
6769 | ldpp_dout(this, 20) << "parsed: objv.tag=" << objv.tag << " objv.ver=" << objv.ver << dendl; |
6770 | ldpp_dout(this, 20) << "got creation_time="<< master_info.creation_time << dendl; | |
7c673cae FG |
6771 | |
6772 | pmaster_bucket= &master_info.bucket; | |
6773 | creation_time = master_info.creation_time; | |
6774 | pmaster_num_shards = &master_info.num_shards; | |
6775 | pobjv = &objv; | |
6776 | } else { | |
6777 | pmaster_bucket = nullptr; | |
6778 | pmaster_num_shards = nullptr; | |
6779 | } | |
6780 | ||
11fdf7f2 | 6781 | rgw_placement_rule placement_rule(binfo.placement_rule, s->info.storage_class); |
7c673cae | 6782 | |
7c673cae | 6783 | if (bucket_exists) { |
11fdf7f2 | 6784 | rgw_placement_rule selected_placement_rule; |
7c673cae FG |
6785 | rgw_bucket bucket; |
6786 | bucket.tenant = s->bucket_tenant; | |
6787 | bucket.name = s->bucket_name; | |
11fdf7f2 TL |
6788 | op_ret = store->svc.zone->select_bucket_placement(*(s->user), |
6789 | store->svc.zone->get_zonegroup().get_id(), | |
7c673cae FG |
6790 | placement_rule, |
6791 | &selected_placement_rule, | |
6792 | nullptr); | |
6793 | if (selected_placement_rule != binfo.placement_rule) { | |
6794 | op_ret = -EEXIST; | |
11fdf7f2 | 6795 | ldpp_dout(this, 20) << "non-coherent placement rule" << dendl; |
7c673cae FG |
6796 | return op_ret; |
6797 | } | |
6798 | } | |
6799 | ||
6800 | /* Create metadata: ACLs. */ | |
6801 | std::map<std::string, ceph::bufferlist> attrs; | |
6802 | RGWAccessControlPolicy policy; | |
6803 | policy.create_default(s->user->user_id, s->user->display_name); | |
6804 | ceph::bufferlist aclbl; | |
6805 | policy.encode(aclbl); | |
6806 | attrs.emplace(RGW_ATTR_ACL, std::move(aclbl)); | |
6807 | ||
6808 | RGWQuotaInfo quota_info; | |
6809 | const RGWQuotaInfo * pquota_info = nullptr; | |
6810 | ||
6811 | rgw_bucket bucket; | |
6812 | bucket.tenant = s->bucket_tenant; /* ignored if bucket exists */ | |
6813 | bucket.name = bucket_name; | |
6814 | ||
6815 | ||
6816 | RGWBucketInfo out_info; | |
6817 | op_ret = store->create_bucket(*(s->user), | |
6818 | bucket, | |
11fdf7f2 | 6819 | store->svc.zone->get_zonegroup().get_id(), |
7c673cae FG |
6820 | placement_rule, binfo.swift_ver_location, |
6821 | pquota_info, attrs, | |
6822 | out_info, pobjv, &ep_objv, creation_time, | |
6823 | pmaster_bucket, pmaster_num_shards, true); | |
6824 | /* continue if EEXIST and create_bucket will fail below. this way we can | |
6825 | * recover from a partial create by retrying it. */ | |
11fdf7f2 TL |
6826 | ldpp_dout(this, 20) << "rgw_create_bucket returned ret=" << op_ret |
6827 | << ", bucket=" << bucket << dendl; | |
7c673cae FG |
6828 | |
6829 | if (op_ret && op_ret != -EEXIST) { | |
6830 | return op_ret; | |
6831 | } | |
6832 | ||
6833 | const bool existed = (op_ret == -EEXIST); | |
6834 | if (existed) { | |
6835 | /* bucket already existed, might have raced with another bucket creation, or | |
6836 | * might be partial bucket creation that never completed. Read existing bucket | |
6837 | * info, verify that the reported bucket owner is the current user. | |
6838 | * If all is ok then update the user's list of buckets. | |
6839 | * Otherwise inform client about a name conflict. | |
6840 | */ | |
6841 | if (out_info.owner.compare(s->user->user_id) != 0) { | |
6842 | op_ret = -EEXIST; | |
11fdf7f2 | 6843 | ldpp_dout(this, 20) << "conflicting bucket name" << dendl; |
7c673cae FG |
6844 | return op_ret; |
6845 | } | |
6846 | bucket = out_info.bucket; | |
6847 | } | |
6848 | ||
6849 | op_ret = rgw_link_bucket(store, s->user->user_id, bucket, | |
6850 | out_info.creation_time, false); | |
6851 | if (op_ret && !existed && op_ret != -EEXIST) { | |
6852 | /* if it exists (or previously existed), don't remove it! */ | |
6853 | op_ret = rgw_unlink_bucket(store, s->user->user_id, | |
6854 | bucket.tenant, bucket.name); | |
6855 | if (op_ret < 0) { | |
11fdf7f2 | 6856 | ldpp_dout(this, 0) << "WARNING: failed to unlink bucket: ret=" << op_ret << dendl; |
7c673cae FG |
6857 | } |
6858 | } else if (op_ret == -EEXIST || (op_ret == 0 && existed)) { | |
11fdf7f2 | 6859 | ldpp_dout(this, 20) << "containers already exists" << dendl; |
7c673cae FG |
6860 | op_ret = -ERR_BUCKET_EXISTS; |
6861 | } | |
6862 | ||
6863 | return op_ret; | |
6864 | } | |
6865 | ||
6866 | ||
6867 | bool RGWBulkUploadOp::handle_file_verify_permission(RGWBucketInfo& binfo, | |
31f18b77 | 6868 | const rgw_obj& obj, |
7c673cae FG |
6869 | std::map<std::string, ceph::bufferlist>& battrs, |
6870 | ACLOwner& bucket_owner /* out */) | |
6871 | { | |
6872 | RGWAccessControlPolicy bacl(store->ctx()); | |
6873 | op_ret = read_bucket_policy(store, s, binfo, battrs, &bacl, binfo.bucket); | |
6874 | if (op_ret < 0) { | |
11fdf7f2 | 6875 | ldpp_dout(this, 20) << "cannot read_policy() for bucket" << dendl; |
7c673cae FG |
6876 | return false; |
6877 | } | |
6878 | ||
31f18b77 FG |
6879 | auto policy = get_iam_policy_from_attr(s->cct, store, battrs, binfo.bucket.tenant); |
6880 | ||
7c673cae | 6881 | bucket_owner = bacl.get_owner(); |
11fdf7f2 TL |
6882 | if (policy || ! s->iam_user_policies.empty()) { |
6883 | auto usr_policy_res = eval_user_policies(s->iam_user_policies, s->env, | |
6884 | boost::none, | |
6885 | rgw::IAM::s3PutObject, obj); | |
6886 | if (usr_policy_res == Effect::Deny) { | |
6887 | return false; | |
6888 | } | |
31f18b77 FG |
6889 | auto e = policy->eval(s->env, *s->auth.identity, |
6890 | rgw::IAM::s3PutObject, obj); | |
6891 | if (e == Effect::Allow) { | |
6892 | return true; | |
6893 | } else if (e == Effect::Deny) { | |
6894 | return false; | |
11fdf7f2 TL |
6895 | } else if (usr_policy_res == Effect::Allow) { |
6896 | return true; | |
31f18b77 FG |
6897 | } |
6898 | } | |
6899 | ||
11fdf7f2 | 6900 | return verify_bucket_permission_no_policy(this, s, s->user_acl.get(), |
31f18b77 | 6901 | &bacl, RGW_PERM_WRITE); |
7c673cae FG |
6902 | } |
6903 | ||
6904 | int RGWBulkUploadOp::handle_file(const boost::string_ref path, | |
6905 | const size_t size, | |
6906 | AlignedStreamGetter& body) | |
6907 | { | |
6908 | ||
11fdf7f2 | 6909 | ldpp_dout(this, 20) << "got file=" << path << ", size=" << size << dendl; |
7c673cae | 6910 | |
11fdf7f2 | 6911 | if (size > static_cast<size_t>(s->cct->_conf->rgw_max_put_size)) { |
7c673cae FG |
6912 | op_ret = -ERR_TOO_LARGE; |
6913 | return op_ret; | |
6914 | } | |
6915 | ||
6916 | std::string bucket_name; | |
6917 | rgw_obj_key object; | |
6918 | std::tie(bucket_name, object) = *parse_path(path); | |
6919 | ||
6920 | auto& obj_ctx = *static_cast<RGWObjectCtx *>(s->obj_ctx); | |
6921 | RGWBucketInfo binfo; | |
6922 | std::map<std::string, ceph::bufferlist> battrs; | |
6923 | ACLOwner bowner; | |
11fdf7f2 | 6924 | op_ret = store->get_bucket_info(*s->sysobj_ctx, s->user->user_id.tenant, |
7c673cae FG |
6925 | bucket_name, binfo, nullptr, &battrs); |
6926 | if (op_ret == -ENOENT) { | |
11fdf7f2 | 6927 | ldpp_dout(this, 20) << "non existent directory=" << bucket_name << dendl; |
7c673cae FG |
6928 | } else if (op_ret < 0) { |
6929 | return op_ret; | |
6930 | } | |
6931 | ||
31f18b77 FG |
6932 | if (! handle_file_verify_permission(binfo, |
6933 | rgw_obj(binfo.bucket, object), | |
6934 | battrs, bowner)) { | |
11fdf7f2 | 6935 | ldpp_dout(this, 20) << "object creation unauthorized" << dendl; |
7c673cae FG |
6936 | op_ret = -EACCES; |
6937 | return op_ret; | |
6938 | } | |
6939 | ||
6940 | op_ret = store->check_quota(bowner.get_id(), binfo.bucket, | |
6941 | user_quota, bucket_quota, size); | |
6942 | if (op_ret < 0) { | |
6943 | return op_ret; | |
6944 | } | |
6945 | ||
31f18b77 FG |
6946 | op_ret = store->check_bucket_shards(s->bucket_info, s->bucket, bucket_quota); |
6947 | if (op_ret < 0) { | |
6948 | return op_ret; | |
6949 | } | |
6950 | ||
11fdf7f2 TL |
6951 | rgw_obj obj(binfo.bucket, object); |
6952 | if (s->bucket_info.versioning_enabled()) { | |
6953 | store->gen_rand_obj_instance_name(&obj); | |
6954 | } | |
7c673cae | 6955 | |
11fdf7f2 TL |
6956 | rgw_placement_rule dest_placement = s->dest_placement; |
6957 | dest_placement.inherit_from(binfo.placement_rule); | |
7c673cae | 6958 | |
11fdf7f2 TL |
6959 | rgw::AioThrottle aio(store->ctx()->_conf->rgw_put_obj_min_window_size); |
6960 | ||
6961 | using namespace rgw::putobj; | |
6962 | ||
6963 | AtomicObjectProcessor processor(&aio, store, binfo, &s->dest_placement, bowner.get_id(), | |
6964 | obj_ctx, obj, 0, s->req_id); | |
6965 | ||
6966 | op_ret = processor.prepare(); | |
7c673cae | 6967 | if (op_ret < 0) { |
11fdf7f2 | 6968 | ldpp_dout(this, 20) << "cannot prepare processor due to ret=" << op_ret << dendl; |
7c673cae FG |
6969 | return op_ret; |
6970 | } | |
6971 | ||
11fdf7f2 TL |
6972 | /* No filters by default. */ |
6973 | DataProcessor *filter = &processor; | |
6974 | ||
6975 | const auto& compression_type = store->svc.zone->get_zone_params().get_compression_type( | |
6976 | dest_placement); | |
7c673cae | 6977 | CompressorRef plugin; |
11fdf7f2 | 6978 | boost::optional<RGWPutObj_Compress> compressor; |
7c673cae FG |
6979 | if (compression_type != "none") { |
6980 | plugin = Compressor::create(s->cct, compression_type); | |
6981 | if (! plugin) { | |
11fdf7f2 TL |
6982 | ldpp_dout(this, 1) << "Cannot load plugin for rgw_compression_type " |
6983 | << compression_type << dendl; | |
7c673cae FG |
6984 | } else { |
6985 | compressor.emplace(s->cct, plugin, filter); | |
6986 | filter = &*compressor; | |
6987 | } | |
6988 | } | |
6989 | ||
6990 | /* Upload file content. */ | |
6991 | ssize_t len = 0; | |
6992 | size_t ofs = 0; | |
6993 | MD5 hash; | |
6994 | do { | |
6995 | ceph::bufferlist data; | |
6996 | len = body.get_at_most(s->cct->_conf->rgw_max_chunk_size, data); | |
6997 | ||
11fdf7f2 | 6998 | ldpp_dout(this, 20) << "body=" << data.c_str() << dendl; |
7c673cae FG |
6999 | if (len < 0) { |
7000 | op_ret = len; | |
7001 | return op_ret; | |
7002 | } else if (len > 0) { | |
11fdf7f2 TL |
7003 | hash.Update((const unsigned char *)data.c_str(), data.length()); |
7004 | op_ret = filter->process(std::move(data), ofs); | |
7c673cae | 7005 | if (op_ret < 0) { |
11fdf7f2 | 7006 | ldpp_dout(this, 20) << "filter->process() returned ret=" << op_ret << dendl; |
7c673cae FG |
7007 | return op_ret; |
7008 | } | |
7009 | ||
7010 | ofs += len; | |
7011 | } | |
7012 | ||
7013 | } while (len > 0); | |
7014 | ||
11fdf7f2 TL |
7015 | // flush |
7016 | op_ret = filter->process({}, ofs); | |
7017 | if (op_ret < 0) { | |
7018 | return op_ret; | |
7019 | } | |
7020 | ||
7c673cae | 7021 | if (ofs != size) { |
11fdf7f2 | 7022 | ldpp_dout(this, 10) << "real file size different from declared" << dendl; |
7c673cae | 7023 | op_ret = -EINVAL; |
11fdf7f2 | 7024 | return op_ret; |
7c673cae FG |
7025 | } |
7026 | ||
7027 | op_ret = store->check_quota(bowner.get_id(), binfo.bucket, | |
7028 | user_quota, bucket_quota, size); | |
7029 | if (op_ret < 0) { | |
11fdf7f2 | 7030 | ldpp_dout(this, 20) << "quota exceeded for path=" << path << dendl; |
7c673cae FG |
7031 | return op_ret; |
7032 | } | |
7033 | ||
31f18b77 FG |
7034 | op_ret = store->check_bucket_shards(s->bucket_info, s->bucket, bucket_quota); |
7035 | if (op_ret < 0) { | |
7036 | return op_ret; | |
7037 | } | |
7038 | ||
7c673cae FG |
7039 | char calc_md5[CEPH_CRYPTO_MD5_DIGESTSIZE * 2 + 1]; |
7040 | unsigned char m[CEPH_CRYPTO_MD5_DIGESTSIZE]; | |
7041 | hash.Final(m); | |
7042 | buf_to_hex(m, CEPH_CRYPTO_MD5_DIGESTSIZE, calc_md5); | |
7043 | ||
7044 | /* Create metadata: ETAG. */ | |
7045 | std::map<std::string, ceph::bufferlist> attrs; | |
7046 | std::string etag = calc_md5; | |
7047 | ceph::bufferlist etag_bl; | |
7048 | etag_bl.append(etag.c_str(), etag.size() + 1); | |
7049 | attrs.emplace(RGW_ATTR_ETAG, std::move(etag_bl)); | |
7050 | ||
7051 | /* Create metadata: ACLs. */ | |
7052 | RGWAccessControlPolicy policy; | |
7053 | policy.create_default(s->user->user_id, s->user->display_name); | |
7054 | ceph::bufferlist aclbl; | |
7055 | policy.encode(aclbl); | |
7056 | attrs.emplace(RGW_ATTR_ACL, std::move(aclbl)); | |
7057 | ||
7058 | /* Create metadata: compression info. */ | |
7059 | if (compressor && compressor->is_compressed()) { | |
7060 | ceph::bufferlist tmp; | |
7061 | RGWCompressionInfo cs_info; | |
7062 | cs_info.compression_type = plugin->get_type_name(); | |
7063 | cs_info.orig_size = s->obj_size; | |
7064 | cs_info.blocks = std::move(compressor->get_compression_blocks()); | |
11fdf7f2 | 7065 | encode(cs_info, tmp); |
7c673cae FG |
7066 | attrs.emplace(RGW_ATTR_COMPRESSION, std::move(tmp)); |
7067 | } | |
7068 | ||
7069 | /* Complete the transaction. */ | |
11fdf7f2 TL |
7070 | op_ret = processor.complete(size, etag, nullptr, ceph::real_time(), |
7071 | attrs, ceph::real_time() /* delete_at */, | |
7072 | nullptr, nullptr, nullptr, nullptr, nullptr); | |
7c673cae | 7073 | if (op_ret < 0) { |
11fdf7f2 | 7074 | ldpp_dout(this, 20) << "processor::complete returned op_ret=" << op_ret << dendl; |
7c673cae FG |
7075 | } |
7076 | ||
7077 | return op_ret; | |
7078 | } | |
7079 | ||
7080 | void RGWBulkUploadOp::execute() | |
7081 | { | |
7082 | ceph::bufferlist buffer(64 * 1024); | |
7083 | ||
11fdf7f2 | 7084 | ldpp_dout(this, 20) << "start" << dendl; |
7c673cae FG |
7085 | |
7086 | /* Create an instance of stream-abstracting class. Having this indirection | |
7087 | * allows for easy introduction of decompressors like gzip and bzip2. */ | |
7088 | auto stream = create_stream(); | |
7089 | if (! stream) { | |
7090 | return; | |
7091 | } | |
7092 | ||
7093 | /* Handling the $UPLOAD_PATH accordingly to the Swift's Bulk middleware. See: | |
7094 | * https://github.com/openstack/swift/blob/2.13.0/swift/common/middleware/bulk.py#L31-L41 */ | |
7095 | std::string bucket_path, file_prefix; | |
7096 | std::tie(bucket_path, file_prefix) = handle_upload_path(s); | |
7097 | ||
7098 | auto status = rgw::tar::StatusIndicator::create(); | |
7099 | do { | |
7100 | op_ret = stream->get_exactly(rgw::tar::BLOCK_SIZE, buffer); | |
7101 | if (op_ret < 0) { | |
11fdf7f2 | 7102 | ldpp_dout(this, 2) << "cannot read header" << dendl; |
7c673cae FG |
7103 | return; |
7104 | } | |
7105 | ||
7106 | /* We need to re-interpret the buffer as a TAR block. Exactly two blocks | |
7107 | * must be tracked to detect out end-of-archive. It occurs when both of | |
7108 | * them are empty (zeroed). Tracing this particular inter-block dependency | |
7109 | * is responsibility of the rgw::tar::StatusIndicator class. */ | |
7110 | boost::optional<rgw::tar::HeaderView> header; | |
7111 | std::tie(status, header) = rgw::tar::interpret_block(status, buffer); | |
7112 | ||
7113 | if (! status.empty() && header) { | |
7114 | /* This specific block isn't empty (entirely zeroed), so we can parse | |
7115 | * it as a TAR header and dispatch. At the moment we do support only | |
7116 | * regular files and directories. Everything else (symlinks, devices) | |
7117 | * will be ignored but won't cease the whole upload. */ | |
7118 | switch (header->get_filetype()) { | |
7119 | case rgw::tar::FileType::NORMAL_FILE: { | |
11fdf7f2 | 7120 | ldpp_dout(this, 2) << "handling regular file" << dendl; |
7c673cae FG |
7121 | |
7122 | boost::string_ref filename = bucket_path.empty() ? header->get_filename() : \ | |
7123 | file_prefix + header->get_filename().to_string(); | |
7124 | auto body = AlignedStreamGetter(0, header->get_filesize(), | |
7125 | rgw::tar::BLOCK_SIZE, *stream); | |
7126 | op_ret = handle_file(filename, | |
7127 | header->get_filesize(), | |
7128 | body); | |
7129 | if (! op_ret) { | |
7130 | /* Only regular files counts. */ | |
7131 | num_created++; | |
7132 | } else { | |
7133 | failures.emplace_back(op_ret, filename.to_string()); | |
7134 | } | |
7135 | break; | |
7136 | } | |
7137 | case rgw::tar::FileType::DIRECTORY: { | |
11fdf7f2 | 7138 | ldpp_dout(this, 2) << "handling regular directory" << dendl; |
7c673cae FG |
7139 | |
7140 | boost::string_ref dirname = bucket_path.empty() ? header->get_filename() : bucket_path; | |
7141 | op_ret = handle_dir(dirname); | |
7142 | if (op_ret < 0 && op_ret != -ERR_BUCKET_EXISTS) { | |
7143 | failures.emplace_back(op_ret, dirname.to_string()); | |
7144 | } | |
7145 | break; | |
7146 | } | |
7147 | default: { | |
7148 | /* Not recognized. Skip. */ | |
7149 | op_ret = 0; | |
7150 | break; | |
7151 | } | |
7152 | } | |
7153 | ||
7154 | /* In case of any problems with sub-request authorization Swift simply | |
7155 | * terminates whole upload immediately. */ | |
7156 | if (boost::algorithm::contains(std::initializer_list<int>{ op_ret }, | |
7157 | terminal_errors)) { | |
11fdf7f2 | 7158 | ldpp_dout(this, 2) << "terminating due to ret=" << op_ret << dendl; |
7c673cae FG |
7159 | break; |
7160 | } | |
7161 | } else { | |
11fdf7f2 | 7162 | ldpp_dout(this, 2) << "an empty block" << dendl; |
7c673cae FG |
7163 | op_ret = 0; |
7164 | } | |
7165 | ||
7166 | buffer.clear(); | |
7167 | } while (! status.eof()); | |
7168 | ||
7169 | return; | |
7170 | } | |
7171 | ||
7172 | RGWBulkUploadOp::AlignedStreamGetter::~AlignedStreamGetter() | |
7173 | { | |
7174 | const size_t aligned_legnth = length + (-length % alignment); | |
7175 | ceph::bufferlist junk; | |
7176 | ||
7177 | DecoratedStreamGetter::get_exactly(aligned_legnth - position, junk); | |
7178 | } | |
7179 | ||
7180 | ssize_t RGWBulkUploadOp::AlignedStreamGetter::get_at_most(const size_t want, | |
7181 | ceph::bufferlist& dst) | |
7182 | { | |
7183 | const size_t max_to_read = std::min(want, length - position); | |
7184 | const auto len = DecoratedStreamGetter::get_at_most(max_to_read, dst); | |
7185 | if (len > 0) { | |
7186 | position += len; | |
7187 | } | |
7188 | return len; | |
7189 | } | |
7190 | ||
7191 | ssize_t RGWBulkUploadOp::AlignedStreamGetter::get_exactly(const size_t want, | |
7192 | ceph::bufferlist& dst) | |
7193 | { | |
7194 | const auto len = DecoratedStreamGetter::get_exactly(want, dst); | |
7195 | if (len > 0) { | |
7196 | position += len; | |
7197 | } | |
7198 | return len; | |
7199 | } | |
7200 | ||
7201 | int RGWSetAttrs::verify_permission() | |
7202 | { | |
31f18b77 FG |
7203 | // This looks to be part of the RGW-NFS machinery and has no S3 or |
7204 | // Swift equivalent. | |
7c673cae FG |
7205 | bool perm; |
7206 | if (!s->object.empty()) { | |
11fdf7f2 | 7207 | perm = verify_object_permission_no_policy(this, s, RGW_PERM_WRITE); |
7c673cae | 7208 | } else { |
11fdf7f2 | 7209 | perm = verify_bucket_permission_no_policy(this, s, RGW_PERM_WRITE); |
7c673cae FG |
7210 | } |
7211 | if (!perm) | |
7212 | return -EACCES; | |
7213 | ||
7214 | return 0; | |
7215 | } | |
7216 | ||
7217 | void RGWSetAttrs::pre_exec() | |
7218 | { | |
7219 | rgw_bucket_object_pre_exec(s); | |
7220 | } | |
7221 | ||
7222 | void RGWSetAttrs::execute() | |
7223 | { | |
7224 | op_ret = get_params(); | |
7225 | if (op_ret < 0) | |
7226 | return; | |
7227 | ||
7228 | rgw_obj obj(s->bucket, s->object); | |
7229 | ||
7c673cae | 7230 | if (!s->object.empty()) { |
31f18b77 | 7231 | store->set_atomic(s->obj_ctx, obj); |
7c673cae FG |
7232 | op_ret = store->set_attrs(s->obj_ctx, s->bucket_info, obj, attrs, nullptr); |
7233 | } else { | |
7234 | for (auto& iter : attrs) { | |
7235 | s->bucket_attrs[iter.first] = std::move(iter.second); | |
7236 | } | |
7237 | op_ret = rgw_bucket_set_attrs(store, s->bucket_info, s->bucket_attrs, | |
7238 | &s->bucket_info.objv_tracker); | |
7239 | } | |
7240 | } | |
7241 | ||
7242 | void RGWGetObjLayout::pre_exec() | |
7243 | { | |
7244 | rgw_bucket_object_pre_exec(s); | |
7245 | } | |
7246 | ||
7247 | void RGWGetObjLayout::execute() | |
7248 | { | |
7249 | rgw_obj obj(s->bucket, s->object); | |
7250 | RGWRados::Object target(store, | |
7251 | s->bucket_info, | |
7252 | *static_cast<RGWObjectCtx *>(s->obj_ctx), | |
7253 | rgw_obj(s->bucket, s->object)); | |
7254 | RGWRados::Object::Read stat_op(&target); | |
7255 | ||
7256 | op_ret = stat_op.prepare(); | |
7257 | if (op_ret < 0) { | |
7258 | return; | |
7259 | } | |
7260 | ||
7261 | head_obj = stat_op.state.head_obj; | |
7262 | ||
7263 | op_ret = target.get_manifest(&manifest); | |
7264 | } | |
7265 | ||
7266 | ||
31f18b77 FG |
7267 | int RGWConfigBucketMetaSearch::verify_permission() |
7268 | { | |
7269 | if (!s->auth.identity->is_owner_of(s->bucket_owner.get_id())) { | |
7270 | return -EACCES; | |
7271 | } | |
7272 | ||
7273 | return 0; | |
7274 | } | |
7275 | ||
7276 | void RGWConfigBucketMetaSearch::pre_exec() | |
7277 | { | |
7278 | rgw_bucket_object_pre_exec(s); | |
7279 | } | |
7280 | ||
7281 | void RGWConfigBucketMetaSearch::execute() | |
7282 | { | |
7283 | op_ret = get_params(); | |
7284 | if (op_ret < 0) { | |
11fdf7f2 | 7285 | ldpp_dout(this, 20) << "NOTICE: get_params() returned ret=" << op_ret << dendl; |
31f18b77 FG |
7286 | return; |
7287 | } | |
7288 | ||
7289 | s->bucket_info.mdsearch_config = mdsearch_config; | |
7290 | ||
7291 | op_ret = store->put_bucket_instance_info(s->bucket_info, false, real_time(), &s->bucket_attrs); | |
7292 | if (op_ret < 0) { | |
11fdf7f2 TL |
7293 | ldpp_dout(this, 0) << "NOTICE: put_bucket_info on bucket=" << s->bucket.name |
7294 | << " returned err=" << op_ret << dendl; | |
31f18b77 FG |
7295 | return; |
7296 | } | |
7297 | } | |
7298 | ||
7299 | int RGWGetBucketMetaSearch::verify_permission() | |
7300 | { | |
7301 | if (!s->auth.identity->is_owner_of(s->bucket_owner.get_id())) { | |
7302 | return -EACCES; | |
7303 | } | |
7304 | ||
7305 | return 0; | |
7306 | } | |
7307 | ||
7308 | void RGWGetBucketMetaSearch::pre_exec() | |
7309 | { | |
7310 | rgw_bucket_object_pre_exec(s); | |
7311 | } | |
7312 | ||
7313 | int RGWDelBucketMetaSearch::verify_permission() | |
7314 | { | |
7315 | if (!s->auth.identity->is_owner_of(s->bucket_owner.get_id())) { | |
7316 | return -EACCES; | |
7317 | } | |
7318 | ||
7319 | return 0; | |
7320 | } | |
7321 | ||
7322 | void RGWDelBucketMetaSearch::pre_exec() | |
7323 | { | |
7324 | rgw_bucket_object_pre_exec(s); | |
7325 | } | |
7326 | ||
7327 | void RGWDelBucketMetaSearch::execute() | |
7328 | { | |
7329 | s->bucket_info.mdsearch_config.clear(); | |
7330 | ||
7331 | op_ret = store->put_bucket_instance_info(s->bucket_info, false, real_time(), &s->bucket_attrs); | |
7332 | if (op_ret < 0) { | |
11fdf7f2 TL |
7333 | ldpp_dout(this, 0) << "NOTICE: put_bucket_info on bucket=" << s->bucket.name |
7334 | << " returned err=" << op_ret << dendl; | |
31f18b77 FG |
7335 | return; |
7336 | } | |
7337 | } | |
7338 | ||
7339 | ||
7c673cae FG |
7340 | RGWHandler::~RGWHandler() |
7341 | { | |
7342 | } | |
7343 | ||
7344 | int RGWHandler::init(RGWRados *_store, | |
7345 | struct req_state *_s, | |
7346 | rgw::io::BasicClient *cio) | |
7347 | { | |
7348 | store = _store; | |
7349 | s = _s; | |
7350 | ||
7351 | return 0; | |
7352 | } | |
7353 | ||
7354 | int RGWHandler::do_init_permissions() | |
7355 | { | |
7356 | int ret = rgw_build_bucket_policies(store, s); | |
7c673cae | 7357 | if (ret < 0) { |
11fdf7f2 TL |
7358 | ldpp_dout(s, 10) << "init_permissions on " << s->bucket |
7359 | << " failed, ret=" << ret << dendl; | |
7360 | return ret==-ENODATA ? -EACCES : ret; | |
7c673cae FG |
7361 | } |
7362 | ||
11fdf7f2 | 7363 | rgw_build_iam_environment(store, s); |
7c673cae FG |
7364 | return ret; |
7365 | } | |
7366 | ||
7367 | int RGWHandler::do_read_permissions(RGWOp *op, bool only_bucket) | |
7368 | { | |
7369 | if (only_bucket) { | |
7370 | /* already read bucket info */ | |
7371 | return 0; | |
7372 | } | |
7373 | int ret = rgw_build_object_policies(store, s, op->prefetch_data()); | |
7374 | ||
7375 | if (ret < 0) { | |
11fdf7f2 | 7376 | ldpp_dout(op, 10) << "read_permissions on " << s->bucket << ":" |
7c673cae FG |
7377 | << s->object << " only_bucket=" << only_bucket |
7378 | << " ret=" << ret << dendl; | |
7379 | if (ret == -ENODATA) | |
7380 | ret = -EACCES; | |
7381 | } | |
7382 | ||
7383 | return ret; | |
7384 | } | |
7385 | ||
7386 | int RGWOp::error_handler(int err_no, string *error_content) { | |
7387 | return dialect_handler->error_handler(err_no, error_content); | |
7388 | } | |
7389 | ||
7390 | int RGWHandler::error_handler(int err_no, string *error_content) { | |
7391 | // This is the do-nothing error handler | |
7392 | return err_no; | |
7393 | } | |
31f18b77 | 7394 | |
11fdf7f2 TL |
7395 | std::ostream& RGWOp::gen_prefix(std::ostream& out) const |
7396 | { | |
7397 | // append <dialect>:<op name> to the prefix | |
7398 | return s->gen_prefix(out) << s->dialect << ':' << name() << ' '; | |
7399 | } | |
7400 | ||
7401 | void RGWDefaultResponseOp::send_response() { | |
7402 | if (op_ret) { | |
7403 | set_req_state_err(s, op_ret); | |
7404 | } | |
7405 | dump_errno(s); | |
7406 | end_header(s); | |
7407 | } | |
31f18b77 FG |
7408 | |
7409 | void RGWPutBucketPolicy::send_response() | |
7410 | { | |
7411 | if (op_ret) { | |
7412 | set_req_state_err(s, op_ret); | |
7413 | } | |
7414 | dump_errno(s); | |
7415 | end_header(s); | |
7416 | } | |
7417 | ||
7418 | int RGWPutBucketPolicy::verify_permission() | |
7419 | { | |
11fdf7f2 | 7420 | if (!verify_bucket_permission(this, s, rgw::IAM::s3PutBucketPolicy)) { |
31f18b77 FG |
7421 | return -EACCES; |
7422 | } | |
7423 | ||
7424 | return 0; | |
7425 | } | |
7426 | ||
7427 | int RGWPutBucketPolicy::get_params() | |
7428 | { | |
7429 | const auto max_size = s->cct->_conf->rgw_max_put_param_size; | |
7430 | // At some point when I have more time I want to make a version of | |
7431 | // rgw_rest_read_all_input that doesn't use malloc. | |
11fdf7f2 TL |
7432 | std::tie(op_ret, data) = rgw_rest_read_all_input(s, max_size, false); |
7433 | ||
31f18b77 FG |
7434 | // And throws exceptions. |
7435 | return op_ret; | |
7436 | } | |
7437 | ||
7438 | void RGWPutBucketPolicy::execute() | |
7439 | { | |
7440 | op_ret = get_params(); | |
7441 | if (op_ret < 0) { | |
7442 | return; | |
7443 | } | |
7444 | ||
11fdf7f2 TL |
7445 | if (!store->svc.zone->is_meta_master()) { |
7446 | op_ret = forward_request_to_master(s, NULL, store, data, nullptr); | |
31f18b77 | 7447 | if (op_ret < 0) { |
11fdf7f2 | 7448 | ldpp_dout(this, 20) << "forward_request_to_master returned ret=" << op_ret << dendl; |
31f18b77 FG |
7449 | return; |
7450 | } | |
7451 | } | |
7452 | ||
7453 | try { | |
11fdf7f2 | 7454 | const Policy p(s->cct, s->bucket_tenant, data); |
b32b8144 FG |
7455 | op_ret = retry_raced_bucket_write(store, s, [&p, this] { |
7456 | auto attrs = s->bucket_attrs; | |
7457 | attrs[RGW_ATTR_IAM_POLICY].clear(); | |
7458 | attrs[RGW_ATTR_IAM_POLICY].append(p.text); | |
7459 | op_ret = rgw_bucket_set_attrs(store, s->bucket_info, attrs, | |
7460 | &s->bucket_info.objv_tracker); | |
7461 | return op_ret; | |
7462 | }); | |
31f18b77 | 7463 | } catch (rgw::IAM::PolicyParseException& e) { |
11fdf7f2 | 7464 | ldpp_dout(this, 20) << "failed to parse policy: " << e.what() << dendl; |
31f18b77 FG |
7465 | op_ret = -EINVAL; |
7466 | } | |
7467 | } | |
7468 | ||
7469 | void RGWGetBucketPolicy::send_response() | |
7470 | { | |
7471 | if (op_ret) { | |
7472 | set_req_state_err(s, op_ret); | |
7473 | } | |
7474 | dump_errno(s); | |
7475 | end_header(s, this, "application/json"); | |
7476 | dump_body(s, policy); | |
7477 | } | |
7478 | ||
7479 | int RGWGetBucketPolicy::verify_permission() | |
7480 | { | |
11fdf7f2 | 7481 | if (!verify_bucket_permission(this, s, rgw::IAM::s3GetBucketPolicy)) { |
31f18b77 FG |
7482 | return -EACCES; |
7483 | } | |
7484 | ||
7485 | return 0; | |
7486 | } | |
7487 | ||
7488 | void RGWGetBucketPolicy::execute() | |
7489 | { | |
7490 | auto attrs = s->bucket_attrs; | |
7491 | map<string, bufferlist>::iterator aiter = attrs.find(RGW_ATTR_IAM_POLICY); | |
7492 | if (aiter == attrs.end()) { | |
11fdf7f2 TL |
7493 | ldpp_dout(this, 0) << "can't find bucket IAM POLICY attr bucket_name = " |
7494 | << s->bucket_name << dendl; | |
31f18b77 FG |
7495 | op_ret = -ERR_NO_SUCH_BUCKET_POLICY; |
7496 | s->err.message = "The bucket policy does not exist"; | |
7497 | return; | |
7498 | } else { | |
7499 | policy = attrs[RGW_ATTR_IAM_POLICY]; | |
7500 | ||
7501 | if (policy.length() == 0) { | |
11fdf7f2 TL |
7502 | ldpp_dout(this, 10) << "The bucket policy does not exist, bucket: " |
7503 | << s->bucket_name << dendl; | |
31f18b77 FG |
7504 | op_ret = -ERR_NO_SUCH_BUCKET_POLICY; |
7505 | s->err.message = "The bucket policy does not exist"; | |
7506 | return; | |
7507 | } | |
7508 | } | |
7509 | } | |
7510 | ||
7511 | void RGWDeleteBucketPolicy::send_response() | |
7512 | { | |
7513 | if (op_ret) { | |
7514 | set_req_state_err(s, op_ret); | |
7515 | } | |
7516 | dump_errno(s); | |
7517 | end_header(s); | |
7518 | } | |
7519 | ||
7520 | int RGWDeleteBucketPolicy::verify_permission() | |
7521 | { | |
11fdf7f2 | 7522 | if (!verify_bucket_permission(this, s, rgw::IAM::s3DeleteBucketPolicy)) { |
31f18b77 FG |
7523 | return -EACCES; |
7524 | } | |
7525 | ||
7526 | return 0; | |
7527 | } | |
7528 | ||
7529 | void RGWDeleteBucketPolicy::execute() | |
7530 | { | |
b32b8144 FG |
7531 | op_ret = retry_raced_bucket_write(store, s, [this] { |
7532 | auto attrs = s->bucket_attrs; | |
7533 | attrs.erase(RGW_ATTR_IAM_POLICY); | |
7534 | op_ret = rgw_bucket_set_attrs(store, s->bucket_info, attrs, | |
7535 | &s->bucket_info.objv_tracker); | |
7536 | return op_ret; | |
7537 | }); | |
31f18b77 | 7538 | } |
28e407b8 | 7539 | |
eafe8130 TL |
7540 | void RGWPutBucketObjectLock::pre_exec() |
7541 | { | |
7542 | rgw_bucket_object_pre_exec(s); | |
7543 | } | |
7544 | ||
7545 | int RGWPutBucketObjectLock::verify_permission() | |
7546 | { | |
7547 | return verify_bucket_owner_or_policy(s, rgw::IAM::s3PutBucketObjectLockConfiguration); | |
7548 | } | |
7549 | ||
7550 | void RGWPutBucketObjectLock::execute() | |
7551 | { | |
7552 | if (!s->bucket_info.obj_lock_enabled()) { | |
7553 | ldpp_dout(this, 0) << "ERROR: object Lock configuration cannot be enabled on existing buckets" << dendl; | |
7554 | op_ret = -ERR_INVALID_BUCKET_STATE; | |
7555 | return; | |
7556 | } | |
7557 | ||
7558 | RGWXMLDecoder::XMLParser parser; | |
7559 | if (!parser.init()) { | |
7560 | ldpp_dout(this, 0) << "ERROR: failed to initialize parser" << dendl; | |
7561 | op_ret = -EINVAL; | |
7562 | return; | |
7563 | } | |
7564 | op_ret = get_params(); | |
7565 | if (op_ret < 0) { | |
7566 | return; | |
7567 | } | |
7568 | if (!parser.parse(data.c_str(), data.length(), 1)) { | |
7569 | op_ret = -ERR_MALFORMED_XML; | |
7570 | return; | |
7571 | } | |
7572 | ||
7573 | try { | |
7574 | RGWXMLDecoder::decode_xml("ObjectLockConfiguration", obj_lock, &parser, true); | |
7575 | } catch (RGWXMLDecoder::err& err) { | |
7576 | ldout(s->cct, 5) << "unexpected xml:" << err << dendl; | |
7577 | op_ret = -ERR_MALFORMED_XML; | |
7578 | return; | |
7579 | } | |
7580 | if (obj_lock.has_rule() && !obj_lock.retention_period_valid()) { | |
7581 | ldpp_dout(this, 0) << "ERROR: retention period must be a positive integer value" << dendl; | |
7582 | op_ret = -ERR_INVALID_RETENTION_PERIOD; | |
7583 | return; | |
7584 | } | |
7585 | ||
7586 | if (!store->svc.zone->is_meta_master()) { | |
7587 | op_ret = forward_request_to_master(s, NULL, store, data, nullptr); | |
7588 | if (op_ret < 0) { | |
7589 | ldout(s->cct, 20) << __func__ << "forward_request_to_master returned ret=" << op_ret << dendl; | |
7590 | return; | |
7591 | } | |
7592 | } | |
7593 | ||
7594 | op_ret = retry_raced_bucket_write(store, s, [this] { | |
7595 | s->bucket_info.obj_lock = obj_lock; | |
7596 | op_ret = store->put_bucket_instance_info(s->bucket_info, false, | |
7597 | real_time(), &s->bucket_attrs); | |
7598 | return op_ret; | |
7599 | }); | |
7600 | return; | |
7601 | } | |
7602 | ||
7603 | void RGWGetBucketObjectLock::pre_exec() | |
7604 | { | |
7605 | rgw_bucket_object_pre_exec(s); | |
7606 | } | |
7607 | ||
7608 | int RGWGetBucketObjectLock::verify_permission() | |
7609 | { | |
7610 | return verify_bucket_owner_or_policy(s, rgw::IAM::s3GetBucketObjectLockConfiguration); | |
7611 | } | |
7612 | ||
7613 | void RGWGetBucketObjectLock::execute() | |
7614 | { | |
7615 | if (!s->bucket_info.obj_lock_enabled()) { | |
7616 | op_ret = -ERR_NO_SUCH_OBJECT_LOCK_CONFIGURATION; | |
7617 | return; | |
7618 | } | |
7619 | } | |
7620 | ||
7621 | int RGWPutObjRetention::verify_permission() | |
7622 | { | |
7623 | if (!verify_object_permission(this, s, rgw::IAM::s3PutObjectRetention)) { | |
7624 | return -EACCES; | |
7625 | } | |
7626 | op_ret = get_params(); | |
7627 | if (op_ret) { | |
7628 | return op_ret; | |
7629 | } | |
7630 | if (bypass_governance_mode) { | |
7631 | bypass_perm = verify_object_permission(this, s, rgw::IAM::s3BypassGovernanceRetention); | |
7632 | } | |
7633 | return 0; | |
7634 | } | |
7635 | ||
7636 | void RGWPutObjRetention::pre_exec() | |
7637 | { | |
7638 | rgw_bucket_object_pre_exec(s); | |
7639 | } | |
7640 | ||
7641 | void RGWPutObjRetention::execute() | |
7642 | { | |
7643 | if (!s->bucket_info.obj_lock_enabled()) { | |
7644 | ldpp_dout(this, 0) << "ERROR: object retention can't be set if bucket object lock not configured" << dendl; | |
7645 | op_ret = -ERR_INVALID_REQUEST; | |
7646 | return; | |
7647 | } | |
7648 | ||
7649 | RGWXMLDecoder::XMLParser parser; | |
7650 | if (!parser.init()) { | |
7651 | ldpp_dout(this, 0) << "ERROR: failed to initialize parser" << dendl; | |
7652 | op_ret = -EINVAL; | |
7653 | return; | |
7654 | } | |
7655 | ||
7656 | if (!parser.parse(data.c_str(), data.length(), 1)) { | |
7657 | op_ret = -ERR_MALFORMED_XML; | |
7658 | return; | |
7659 | } | |
7660 | ||
7661 | try { | |
7662 | RGWXMLDecoder::decode_xml("Retention", obj_retention, &parser, true); | |
7663 | } catch (RGWXMLDecoder::err& err) { | |
7664 | ldpp_dout(this, 5) << "unexpected xml:" << err << dendl; | |
7665 | op_ret = -ERR_MALFORMED_XML; | |
7666 | return; | |
7667 | } | |
7668 | ||
7669 | if (ceph::real_clock::to_time_t(obj_retention.get_retain_until_date()) < ceph_clock_now()) { | |
7670 | ldpp_dout(this, 0) << "ERROR: the retain until date must be in the future" << dendl; | |
7671 | op_ret = -EINVAL; | |
7672 | return; | |
7673 | } | |
7674 | bufferlist bl; | |
7675 | obj_retention.encode(bl); | |
7676 | rgw_obj obj(s->bucket, s->object); | |
7677 | ||
7678 | //check old retention | |
7679 | map<string, bufferlist> attrs; | |
7680 | op_ret = get_obj_attrs(store, s, obj, attrs); | |
7681 | if (op_ret < 0) { | |
7682 | ldpp_dout(this, 0) << "ERROR: get obj attr error"<< dendl; | |
7683 | return; | |
7684 | } | |
7685 | auto aiter = attrs.find(RGW_ATTR_OBJECT_RETENTION); | |
7686 | if (aiter != attrs.end()) { | |
7687 | RGWObjectRetention old_obj_retention; | |
7688 | try { | |
7689 | decode(old_obj_retention, aiter->second); | |
7690 | } catch (buffer::error& err) { | |
7691 | ldpp_dout(this, 0) << "ERROR: failed to decode RGWObjectRetention" << dendl; | |
7692 | op_ret = -EIO; | |
7693 | return; | |
7694 | } | |
7695 | if (ceph::real_clock::to_time_t(obj_retention.get_retain_until_date()) < ceph::real_clock::to_time_t(old_obj_retention.get_retain_until_date())) { | |
7696 | if (old_obj_retention.get_mode().compare("GOVERNANCE") != 0 || !bypass_perm || !bypass_governance_mode) { | |
7697 | op_ret = -EACCES; | |
7698 | return; | |
7699 | } | |
7700 | } | |
7701 | } | |
7702 | ||
7703 | op_ret = modify_obj_attr(store, s, obj, RGW_ATTR_OBJECT_RETENTION, bl); | |
7704 | ||
7705 | return; | |
7706 | } | |
7707 | ||
7708 | int RGWGetObjRetention::verify_permission() | |
7709 | { | |
7710 | if (!verify_object_permission(this, s, rgw::IAM::s3GetObjectRetention)) { | |
7711 | return -EACCES; | |
7712 | } | |
7713 | return 0; | |
7714 | } | |
7715 | ||
7716 | void RGWGetObjRetention::pre_exec() | |
7717 | { | |
7718 | rgw_bucket_object_pre_exec(s); | |
7719 | } | |
7720 | ||
7721 | void RGWGetObjRetention::execute() | |
7722 | { | |
7723 | if (!s->bucket_info.obj_lock_enabled()) { | |
7724 | ldpp_dout(this, 0) << "ERROR: bucket object lock not configured" << dendl; | |
7725 | op_ret = -ERR_INVALID_REQUEST; | |
7726 | return; | |
7727 | } | |
7728 | rgw_obj obj(s->bucket, s->object); | |
7729 | map<string, bufferlist> attrs; | |
7730 | op_ret = get_obj_attrs(store, s, obj, attrs); | |
7731 | if (op_ret < 0) { | |
7732 | ldpp_dout(this, 0) << "ERROR: failed to get obj attrs, obj=" << obj | |
7733 | << " ret=" << op_ret << dendl; | |
7734 | return; | |
7735 | } | |
7736 | auto aiter = attrs.find(RGW_ATTR_OBJECT_RETENTION); | |
7737 | if (aiter == attrs.end()) { | |
7738 | op_ret = -ERR_NO_SUCH_OBJECT_LOCK_CONFIGURATION; | |
7739 | return; | |
7740 | } | |
7741 | ||
7742 | bufferlist::const_iterator iter{&aiter->second}; | |
7743 | try { | |
7744 | obj_retention.decode(iter); | |
7745 | } catch (const buffer::error& e) { | |
7746 | ldout(s->cct, 0) << __func__ << "decode object retention config failed" << dendl; | |
7747 | op_ret = -EIO; | |
7748 | return; | |
7749 | } | |
7750 | return; | |
7751 | } | |
7752 | ||
7753 | int RGWPutObjLegalHold::verify_permission() | |
7754 | { | |
7755 | if (!verify_object_permission(this, s, rgw::IAM::s3PutObjectLegalHold)) { | |
7756 | return -EACCES; | |
7757 | } | |
7758 | return 0; | |
7759 | } | |
7760 | ||
7761 | void RGWPutObjLegalHold::pre_exec() | |
7762 | { | |
7763 | rgw_bucket_object_pre_exec(s); | |
7764 | } | |
7765 | ||
7766 | void RGWPutObjLegalHold::execute() { | |
7767 | if (!s->bucket_info.obj_lock_enabled()) { | |
7768 | ldpp_dout(this, 0) << "ERROR: object legal hold can't be set if bucket object lock not configured" << dendl; | |
7769 | op_ret = -ERR_INVALID_REQUEST; | |
7770 | return; | |
7771 | } | |
7772 | ||
7773 | RGWXMLDecoder::XMLParser parser; | |
7774 | if (!parser.init()) { | |
7775 | ldpp_dout(this, 0) << "ERROR: failed to initialize parser" << dendl; | |
7776 | op_ret = -EINVAL; | |
7777 | return; | |
7778 | } | |
7779 | ||
7780 | op_ret = get_params(); | |
7781 | if (op_ret < 0) | |
7782 | return; | |
7783 | ||
7784 | if (!parser.parse(data.c_str(), data.length(), 1)) { | |
7785 | op_ret = -ERR_MALFORMED_XML; | |
7786 | return; | |
7787 | } | |
7788 | ||
7789 | try { | |
7790 | RGWXMLDecoder::decode_xml("LegalHold", obj_legal_hold, &parser, true); | |
7791 | } catch (RGWXMLDecoder::err &err) { | |
7792 | ldout(s->cct, 5) << "unexpected xml:" << err << dendl; | |
7793 | op_ret = -ERR_MALFORMED_XML; | |
7794 | return; | |
7795 | } | |
7796 | bufferlist bl; | |
7797 | obj_legal_hold.encode(bl); | |
7798 | rgw_obj obj(s->bucket, s->object); | |
7799 | //if instance is empty, we should modify the latest object | |
7800 | op_ret = modify_obj_attr(store, s, obj, RGW_ATTR_OBJECT_LEGAL_HOLD, bl); | |
7801 | return; | |
7802 | } | |
7803 | ||
7804 | int RGWGetObjLegalHold::verify_permission() | |
7805 | { | |
7806 | if (!verify_object_permission(this, s, rgw::IAM::s3GetObjectLegalHold)) { | |
7807 | return -EACCES; | |
7808 | } | |
7809 | return 0; | |
7810 | } | |
7811 | ||
7812 | void RGWGetObjLegalHold::pre_exec() | |
7813 | { | |
7814 | rgw_bucket_object_pre_exec(s); | |
7815 | } | |
7816 | ||
7817 | void RGWGetObjLegalHold::execute() | |
7818 | { | |
7819 | if (!s->bucket_info.obj_lock_enabled()) { | |
7820 | ldpp_dout(this, 0) << "ERROR: bucket object lock not configured" << dendl; | |
7821 | op_ret = -ERR_INVALID_REQUEST; | |
7822 | return; | |
7823 | } | |
7824 | rgw_obj obj(s->bucket, s->object); | |
7825 | map<string, bufferlist> attrs; | |
7826 | op_ret = get_obj_attrs(store, s, obj, attrs); | |
7827 | if (op_ret < 0) { | |
7828 | ldpp_dout(this, 0) << "ERROR: failed to get obj attrs, obj=" << obj | |
7829 | << " ret=" << op_ret << dendl; | |
7830 | return; | |
7831 | } | |
7832 | auto aiter = attrs.find(RGW_ATTR_OBJECT_LEGAL_HOLD); | |
7833 | if (aiter == attrs.end()) { | |
7834 | op_ret = -ERR_NO_SUCH_OBJECT_LOCK_CONFIGURATION; | |
7835 | return; | |
7836 | } | |
7837 | ||
7838 | bufferlist::const_iterator iter{&aiter->second}; | |
7839 | try { | |
7840 | obj_legal_hold.decode(iter); | |
7841 | } catch (const buffer::error& e) { | |
7842 | ldout(s->cct, 0) << __func__ << "decode object legal hold config failed" << dendl; | |
7843 | op_ret = -EIO; | |
7844 | return; | |
7845 | } | |
7846 | return; | |
7847 | } | |
7848 | ||
28e407b8 AA |
7849 | void RGWGetClusterStat::execute() |
7850 | { | |
7851 | op_ret = this->store->get_rados_handle()->cluster_stat(stats_op); | |
7852 | } | |
7853 | ||
7854 |