]> git.proxmox.com Git - ceph.git/blame - ceph/src/rgw/rgw_basic_types.h
import quincy beta 17.1.0
[ceph.git] / ceph / src / rgw / rgw_basic_types.h
CommitLineData
31f18b77 1// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
9f95a23c 2// vim: ts=8 sw=2 smarttab ft=cpp
11fdf7f2 3
7c673cae
FG
4#ifndef CEPH_RGW_BASIC_TYPES_H
5#define CEPH_RGW_BASIC_TYPES_H
6
7#include <string>
8
9#include "include/types.h"
10
9f95a23c
TL
11class JSONObj;
12class cls_user_bucket;
13
7c673cae
FG
14struct rgw_user {
15 std::string tenant;
16 std::string id;
f67539c2 17 std::string ns;
7c673cae
FG
18
19 rgw_user() {}
9f95a23c 20 explicit rgw_user(const std::string& s) {
7c673cae
FG
21 from_str(s);
22 }
f67539c2 23 rgw_user(const std::string& tenant, const std::string& id, const std::string& ns="")
7c673cae 24 : tenant(tenant),
f67539c2
TL
25 id(id),
26 ns(ns) {
7c673cae 27 }
f67539c2 28 rgw_user(std::string&& tenant, std::string&& id, std::string&& ns="")
31f18b77 29 : tenant(std::move(tenant)),
f67539c2
TL
30 id(std::move(id)),
31 ns(std::move(ns)) {
31f18b77 32 }
7c673cae 33
f67539c2
TL
34 void encode(ceph::buffer::list& bl) const {
35 ENCODE_START(2, 1, bl);
11fdf7f2
TL
36 encode(tenant, bl);
37 encode(id, bl);
f67539c2 38 encode(ns, bl);
7c673cae
FG
39 ENCODE_FINISH(bl);
40 }
f67539c2
TL
41 void decode(ceph::buffer::list::const_iterator& bl) {
42 DECODE_START(2, bl);
11fdf7f2
TL
43 decode(tenant, bl);
44 decode(id, bl);
f67539c2
TL
45 if (struct_v >= 2) {
46 decode(ns, bl);
47 }
7c673cae
FG
48 DECODE_FINISH(bl);
49 }
50
51 void to_str(std::string& str) const {
52 if (!tenant.empty()) {
f67539c2
TL
53 if (!ns.empty()) {
54 str = tenant + '$' + ns + '$' + id;
55 } else {
56 str = tenant + '$' + id;
57 }
58 } else if (!ns.empty()) {
59 str = '$' + ns + '$' + id;
7c673cae
FG
60 } else {
61 str = id;
62 }
63 }
64
65 void clear() {
66 tenant.clear();
67 id.clear();
f67539c2 68 ns.clear();
7c673cae
FG
69 }
70
71 bool empty() const {
72 return id.empty();
73 }
74
f67539c2
TL
75 std::string to_str() const {
76 std::string s;
7c673cae
FG
77 to_str(s);
78 return s;
79 }
80
81 void from_str(const std::string& str) {
82 size_t pos = str.find('$');
83 if (pos != std::string::npos) {
84 tenant = str.substr(0, pos);
20effc67
TL
85 std::string_view sv = str;
86 std::string_view ns_id = sv.substr(pos + 1);
f67539c2
TL
87 size_t ns_pos = ns_id.find('$');
88 if (ns_pos != std::string::npos) {
20effc67
TL
89 ns = std::string(ns_id.substr(0, ns_pos));
90 id = std::string(ns_id.substr(ns_pos + 1));
f67539c2
TL
91 } else {
92 ns.clear();
20effc67 93 id = std::string(ns_id);
f67539c2 94 }
7c673cae
FG
95 } else {
96 tenant.clear();
f67539c2 97 ns.clear();
7c673cae
FG
98 id = str;
99 }
100 }
101
f67539c2 102 rgw_user& operator=(const std::string& str) {
7c673cae
FG
103 from_str(str);
104 return *this;
105 }
106
107 int compare(const rgw_user& u) const {
108 int r = tenant.compare(u.tenant);
109 if (r != 0)
110 return r;
f67539c2
TL
111 r = ns.compare(u.ns);
112 if (r != 0) {
113 return r;
114 }
7c673cae
FG
115 return id.compare(u.id);
116 }
f67539c2 117 int compare(const std::string& str) const {
7c673cae
FG
118 rgw_user u(str);
119 return compare(u);
120 }
121
122 bool operator!=(const rgw_user& rhs) const {
123 return (compare(rhs) != 0);
124 }
125 bool operator==(const rgw_user& rhs) const {
126 return (compare(rhs) == 0);
127 }
128 bool operator<(const rgw_user& rhs) const {
129 if (tenant < rhs.tenant) {
130 return true;
131 } else if (tenant > rhs.tenant) {
132 return false;
133 }
f67539c2
TL
134 if (ns < rhs.ns) {
135 return true;
136 } else if (ns > rhs.ns) {
137 return false;
138 }
7c673cae
FG
139 return (id < rhs.id);
140 }
f67539c2
TL
141 void dump(ceph::Formatter *f) const;
142 static void generate_test_instances(std::list<rgw_user*>& o);
7c673cae
FG
143};
144WRITE_CLASS_ENCODER(rgw_user)
145
9f95a23c
TL
146struct rgw_pool {
147 std::string name;
148 std::string ns;
149
150 rgw_pool() = default;
151 rgw_pool(const rgw_pool& _p) : name(_p.name), ns(_p.ns) {}
152 rgw_pool(rgw_pool&&) = default;
f67539c2 153 rgw_pool(const std::string& _s) {
9f95a23c
TL
154 from_str(_s);
155 }
f67539c2 156 rgw_pool(const std::string& _name, const std::string& _ns) : name(_name), ns(_ns) {}
9f95a23c 157
f67539c2
TL
158 std::string to_str() const;
159 void from_str(const std::string& s);
9f95a23c 160
f67539c2 161 void init(const std::string& _s) {
9f95a23c
TL
162 from_str(_s);
163 }
164
165 bool empty() const {
166 return name.empty();
167 }
168
169 int compare(const rgw_pool& p) const {
170 int r = name.compare(p.name);
171 if (r != 0) {
172 return r;
173 }
174 return ns.compare(p.ns);
175 }
176
f67539c2 177 void encode(ceph::buffer::list& bl) const {
9f95a23c
TL
178 ENCODE_START(10, 10, bl);
179 encode(name, bl);
180 encode(ns, bl);
181 ENCODE_FINISH(bl);
182 }
183
f67539c2 184 void decode_from_bucket(ceph::buffer::list::const_iterator& bl);
9f95a23c 185
f67539c2 186 void decode(ceph::buffer::list::const_iterator& bl) {
9f95a23c
TL
187 DECODE_START_LEGACY_COMPAT_LEN(10, 3, 3, bl);
188
189 decode(name, bl);
190
191 if (struct_v < 10) {
192
193 /*
194 * note that rgw_pool can be used where rgw_bucket was used before
195 * therefore we inherit rgw_bucket's old versions. However, we only
196 * need the first field from rgw_bucket. unless we add more fields
197 * in which case we'll need to look at struct_v, and check the actual
198 * version. Anything older than 10 needs to be treated as old rgw_bucket
199 */
200
201 } else {
202 decode(ns, bl);
203 }
204
205 DECODE_FINISH(bl);
206 }
207
208 rgw_pool& operator=(const rgw_pool&) = default;
209
210 bool operator==(const rgw_pool& p) const {
211 return (compare(p) == 0);
212 }
213 bool operator!=(const rgw_pool& p) const {
214 return !(*this == p);
215 }
216 bool operator<(const rgw_pool& p) const {
217 int r = name.compare(p.name);
218 if (r == 0) {
219 return (ns.compare(p.ns) < 0);
220 }
221 return (r < 0);
222 }
223};
224WRITE_CLASS_ENCODER(rgw_pool)
225
f67539c2 226inline std::ostream& operator<<(std::ostream& out, const rgw_pool& p) {
9f95a23c
TL
227 out << p.to_str();
228 return out;
229}
230
231struct rgw_data_placement_target {
232 rgw_pool data_pool;
233 rgw_pool data_extra_pool;
234 rgw_pool index_pool;
235
236 rgw_data_placement_target() = default;
237 rgw_data_placement_target(const rgw_data_placement_target&) = default;
238 rgw_data_placement_target(rgw_data_placement_target&&) = default;
239
240 rgw_data_placement_target(const rgw_pool& data_pool,
241 const rgw_pool& data_extra_pool,
242 const rgw_pool& index_pool)
243 : data_pool(data_pool),
244 data_extra_pool(data_extra_pool),
245 index_pool(index_pool) {
246 }
247
248 rgw_data_placement_target&
249 operator=(const rgw_data_placement_target&) = default;
250
251 const rgw_pool& get_data_extra_pool() const {
252 if (data_extra_pool.empty()) {
253 return data_pool;
254 }
255 return data_extra_pool;
256 }
257
258 int compare(const rgw_data_placement_target& t) {
259 int c = data_pool.compare(t.data_pool);
260 if (c != 0) {
261 return c;
262 }
263 c = data_extra_pool.compare(t.data_extra_pool);
264 if (c != 0) {
265 return c;
266 }
267 return index_pool.compare(t.index_pool);
268 };
269
f67539c2 270 void dump(ceph::Formatter *f) const;
9f95a23c
TL
271 void decode_json(JSONObj *obj);
272};
273
274struct rgw_bucket_key {
275 std::string tenant;
276 std::string name;
277 std::string bucket_id;
278
279 rgw_bucket_key(const std::string& _tenant,
280 const std::string& _name,
281 const std::string& _bucket_id) : tenant(_tenant),
282 name(_name),
283 bucket_id(_bucket_id) {}
284 rgw_bucket_key(const std::string& _tenant,
285 const std::string& _name) : tenant(_tenant),
286 name(_name) {}
287};
288
289struct rgw_bucket {
290 std::string tenant;
291 std::string name;
292 std::string marker;
293 std::string bucket_id;
294 rgw_data_placement_target explicit_placement;
295
296 rgw_bucket() { }
297 // cppcheck-suppress noExplicitConstructor
298 explicit rgw_bucket(const rgw_user& u, const cls_user_bucket& b);
299
20effc67
TL
300 rgw_bucket(const std::string& _tenant,
301 const std::string& _name,
302 const std::string& _bucket_id) : tenant(_tenant),
303 name(_name),
304 bucket_id(_bucket_id) {}
9f95a23c
TL
305 rgw_bucket(const rgw_bucket_key& bk) : tenant(bk.tenant),
306 name(bk.name),
307 bucket_id(bk.bucket_id) {}
308 rgw_bucket(const rgw_bucket&) = default;
309 rgw_bucket(rgw_bucket&&) = default;
310
311 bool match(const rgw_bucket& b) const {
312 return (tenant == b.tenant &&
313 name == b.name &&
314 (bucket_id == b.bucket_id ||
315 bucket_id.empty() ||
316 b.bucket_id.empty()));
317 }
318
319 void convert(cls_user_bucket *b) const;
320
f67539c2 321 void encode(ceph::buffer::list& bl) const {
9f95a23c
TL
322 ENCODE_START(10, 10, bl);
323 encode(name, bl);
324 encode(marker, bl);
325 encode(bucket_id, bl);
326 encode(tenant, bl);
327 bool encode_explicit = !explicit_placement.data_pool.empty();
328 encode(encode_explicit, bl);
329 if (encode_explicit) {
330 encode(explicit_placement.data_pool, bl);
331 encode(explicit_placement.data_extra_pool, bl);
332 encode(explicit_placement.index_pool, bl);
333 }
334 ENCODE_FINISH(bl);
335 }
f67539c2 336 void decode(ceph::buffer::list::const_iterator& bl) {
9f95a23c
TL
337 DECODE_START_LEGACY_COMPAT_LEN(10, 3, 3, bl);
338 decode(name, bl);
339 if (struct_v < 10) {
340 decode(explicit_placement.data_pool.name, bl);
341 }
342 if (struct_v >= 2) {
343 decode(marker, bl);
344 if (struct_v <= 3) {
345 uint64_t id;
346 decode(id, bl);
347 char buf[16];
348 snprintf(buf, sizeof(buf), "%" PRIu64, id);
349 bucket_id = buf;
350 } else {
351 decode(bucket_id, bl);
352 }
353 }
354 if (struct_v < 10) {
355 if (struct_v >= 5) {
356 decode(explicit_placement.index_pool.name, bl);
357 } else {
358 explicit_placement.index_pool = explicit_placement.data_pool;
359 }
360 if (struct_v >= 7) {
361 decode(explicit_placement.data_extra_pool.name, bl);
362 }
363 }
364 if (struct_v >= 8) {
365 decode(tenant, bl);
366 }
367 if (struct_v >= 10) {
368 bool decode_explicit = !explicit_placement.data_pool.empty();
369 decode(decode_explicit, bl);
370 if (decode_explicit) {
371 decode(explicit_placement.data_pool, bl);
372 decode(explicit_placement.data_extra_pool, bl);
373 decode(explicit_placement.index_pool, bl);
374 }
375 }
376 DECODE_FINISH(bl);
377 }
378
f67539c2 379 void update_bucket_id(const std::string& new_bucket_id) {
9f95a23c
TL
380 bucket_id = new_bucket_id;
381 }
382
383 // format a key for the bucket/instance. pass delim=0 to skip a field
384 std::string get_key(char tenant_delim = '/',
385 char id_delim = ':',
386 size_t reserve = 0) const;
387
388 const rgw_pool& get_data_extra_pool() const {
389 return explicit_placement.get_data_extra_pool();
390 }
391
f67539c2 392 void dump(ceph::Formatter *f) const;
9f95a23c 393 void decode_json(JSONObj *obj);
f67539c2 394 static void generate_test_instances(std::list<rgw_bucket*>& o);
9f95a23c
TL
395
396 rgw_bucket& operator=(const rgw_bucket&) = default;
397
398 bool operator<(const rgw_bucket& b) const {
f67539c2 399 if (tenant < b.tenant) {
9f95a23c 400 return true;
f67539c2 401 } else if (tenant > b.tenant) {
9f95a23c
TL
402 return false;
403 }
404
f67539c2 405 if (name < b.name) {
9f95a23c 406 return true;
f67539c2 407 } else if (name > b.name) {
9f95a23c
TL
408 return false;
409 }
410
f67539c2 411 return (bucket_id < b.bucket_id);
9f95a23c
TL
412 }
413
414 bool operator==(const rgw_bucket& b) const {
415 return (tenant == b.tenant) && (name == b.name) && \
416 (bucket_id == b.bucket_id);
417 }
418 bool operator!=(const rgw_bucket& b) const {
419 return (tenant != b.tenant) || (name != b.name) ||
420 (bucket_id != b.bucket_id);
421 }
422};
423WRITE_CLASS_ENCODER(rgw_bucket)
424
f67539c2 425inline std::ostream& operator<<(std::ostream& out, const rgw_bucket &b) {
9f95a23c
TL
426 out << b.tenant << ":" << b.name << "[" << b.bucket_id << "])";
427 return out;
428}
429
430struct rgw_bucket_shard {
431 rgw_bucket bucket;
432 int shard_id;
433
434 rgw_bucket_shard() : shard_id(-1) {}
435 rgw_bucket_shard(const rgw_bucket& _b, int _sid) : bucket(_b), shard_id(_sid) {}
436
437 std::string get_key(char tenant_delim = '/', char id_delim = ':',
438 char shard_delim = ':') const;
439
440 bool operator<(const rgw_bucket_shard& b) const {
441 if (bucket < b.bucket) {
442 return true;
443 }
444 if (b.bucket < bucket) {
445 return false;
446 }
447 return shard_id < b.shard_id;
448 }
449
450 bool operator==(const rgw_bucket_shard& b) const {
451 return (bucket == b.bucket &&
452 shard_id == b.shard_id);
453 }
454};
455
f67539c2 456inline std::ostream& operator<<(std::ostream& out, const rgw_bucket_shard& bs) {
9f95a23c
TL
457 if (bs.shard_id <= 0) {
458 return out << bs.bucket;
459 }
460
461 return out << bs.bucket << ":" << bs.shard_id;
462}
463
464
465struct rgw_zone_id {
f67539c2 466 std::string id;
9f95a23c
TL
467
468 rgw_zone_id() {}
f67539c2
TL
469 rgw_zone_id(const std::string& _id) : id(_id) {}
470 rgw_zone_id(std::string&& _id) : id(std::move(_id)) {}
9f95a23c 471
f67539c2 472 void encode(ceph::buffer::list& bl) const {
9f95a23c
TL
473 /* backward compatiblity, not using ENCODE_{START,END} macros */
474 ceph::encode(id, bl);
475 }
476
f67539c2 477 void decode(ceph::buffer::list::const_iterator& bl) {
9f95a23c
TL
478 /* backward compatiblity, not using DECODE_{START,END} macros */
479 ceph::decode(id, bl);
480 }
481
482 void clear() {
483 id.clear();
484 }
485
f67539c2 486 bool operator==(const std::string& _id) const {
9f95a23c
TL
487 return (id == _id);
488 }
489 bool operator==(const rgw_zone_id& zid) const {
490 return (id == zid.id);
491 }
492 bool operator!=(const rgw_zone_id& zid) const {
493 return (id != zid.id);
494 }
495 bool operator<(const rgw_zone_id& zid) const {
496 return (id < zid.id);
497 }
498 bool operator>(const rgw_zone_id& zid) const {
499 return (id > zid.id);
500 }
501
502 bool empty() const {
503 return id.empty();
504 }
505};
506WRITE_CLASS_ENCODER(rgw_zone_id)
507
f67539c2 508inline std::ostream& operator<<(std::ostream& os, const rgw_zone_id& zid) {
9f95a23c
TL
509 os << zid.id;
510 return os;
511}
512
20effc67
TL
513struct obj_version;
514struct rgw_placement_rule;
515struct RGWAccessKey;
516class RGWUserCaps;
9f95a23c 517
20effc67
TL
518extern void encode_json(const char *name, const obj_version& v, Formatter *f);
519extern void encode_json(const char *name, const RGWUserCaps& val, Formatter *f);
520extern void encode_json(const char *name, const rgw_pool& pool, Formatter *f);
521extern void encode_json(const char *name, const rgw_placement_rule& r, Formatter *f);
522extern void encode_json_impl(const char *name, const rgw_zone_id& zid, ceph::Formatter *f);
523extern void encode_json_plain(const char *name, const RGWAccessKey& val, Formatter *f);
524
525extern void decode_json_obj(obj_version& v, JSONObj *obj);
526extern void decode_json_obj(rgw_zone_id& zid, JSONObj *obj);
527extern void decode_json_obj(rgw_pool& pool, JSONObj *obj);
528extern void decode_json_obj(rgw_placement_rule& v, JSONObj *obj);
9f95a23c 529
31f18b77
FG
530// Represents an identity. This is more wide-ranging than a
531// 'User'. Its purposes is to be matched against by an
532// IdentityApplier. The internal representation will doubtless change as
533// more types are added. We may want to expose the type enum and make
534// the member public so people can switch/case on it.
535
536namespace rgw {
537namespace auth {
538class Principal {
f91f0fd5 539 enum types { User, Role, Tenant, Wildcard, OidcProvider, AssumedRole };
31f18b77
FG
540 types t;
541 rgw_user u;
f67539c2 542 std::string idp_url;
31f18b77 543
11fdf7f2 544 explicit Principal(types t)
31f18b77
FG
545 : t(t) {}
546
547 Principal(types t, std::string&& n, std::string i)
548 : t(t), u(std::move(n), std::move(i)) {}
549
f67539c2 550 Principal(std::string&& idp_url)
11fdf7f2
TL
551 : t(OidcProvider), idp_url(std::move(idp_url)) {}
552
31f18b77
FG
553public:
554
555 static Principal wildcard() {
556 return Principal(Wildcard);
557 }
558
559 static Principal user(std::string&& t, std::string&& u) {
560 return Principal(User, std::move(t), std::move(u));
561 }
562
563 static Principal role(std::string&& t, std::string&& u) {
564 return Principal(Role, std::move(t), std::move(u));
565 }
566
567 static Principal tenant(std::string&& t) {
568 return Principal(Tenant, std::move(t), {});
569 }
570
f67539c2 571 static Principal oidc_provider(std::string&& idp_url) {
11fdf7f2
TL
572 return Principal(std::move(idp_url));
573 }
574
f91f0fd5
TL
575 static Principal assumed_role(std::string&& t, std::string&& u) {
576 return Principal(AssumedRole, std::move(t), std::move(u));
577 }
578
31f18b77
FG
579 bool is_wildcard() const {
580 return t == Wildcard;
581 }
582
583 bool is_user() const {
584 return t == User;
585 }
586
587 bool is_role() const {
588 return t == Role;
589 }
590
591 bool is_tenant() const {
592 return t == Tenant;
593 }
594
11fdf7f2
TL
595 bool is_oidc_provider() const {
596 return t == OidcProvider;
597 }
598
f91f0fd5
TL
599 bool is_assumed_role() const {
600 return t == AssumedRole;
601 }
602
31f18b77 603 const std::string& get_tenant() const {
31f18b77
FG
604 return u.tenant;
605 }
606
607 const std::string& get_id() const {
31f18b77
FG
608 return u.id;
609 }
610
f67539c2 611 const std::string& get_idp_url() const {
11fdf7f2
TL
612 return idp_url;
613 }
614
20effc67 615 const std::string& get_role_session() const {
f91f0fd5
TL
616 return u.id;
617 }
618
20effc67 619 const std::string& get_role() const {
f91f0fd5
TL
620 return u.id;
621 }
622
31f18b77
FG
623 bool operator ==(const Principal& o) const {
624 return (t == o.t) && (u == o.u);
625 }
626
627 bool operator <(const Principal& o) const {
628 return (t < o.t) || ((t == o.t) && (u < o.u));
629 }
630};
631
632std::ostream& operator <<(std::ostream& m, const Principal& p);
31f18b77
FG
633}
634}
7c673cae
FG
635
636class JSONObj;
637
638void decode_json_obj(rgw_user& val, JSONObj *obj);
f67539c2
TL
639void encode_json(const char *name, const rgw_user& val, ceph::Formatter *f);
640void encode_xml(const char *name, const rgw_user& val, ceph::Formatter *f);
7c673cae 641
f67539c2
TL
642inline std::ostream& operator<<(std::ostream& out, const rgw_user &u) {
643 std::string s;
7c673cae
FG
644 u.to_str(s);
645 return out << s;
646}
647
648
649#endif