From 548d71d1ea9ae084d4dcbbc5ba1f3ae11bd8ba7f Mon Sep 17 00:00:00 2001 From: Casey Bodley Date: Thu, 2 Nov 2023 20:28:03 -0400 Subject: [PATCH] rgw: add rgw_owner variant with json encoding existing buckets and objects use `struct rgw_user` for the owner. with the addition of accounts, we need to be able to represent ownership by the account rather than its users add a `rgw_owner` variant that preserves the existing json encoding of `rgw_user` while adding a new representation for account ids Signed-off-by: Casey Bodley (cherry picked from commit 87a74f8603bdeccdea3fdfa7b4e0fd344fba8aae) --- src/rgw/rgw_basic_types.cc | 42 ++++++++++++++++++++++++++++++++++++++ src/rgw/rgw_user_types.h | 18 ++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/src/rgw/rgw_basic_types.cc b/src/rgw/rgw_basic_types.cc index 88a510175d4..e6e94f48dc0 100644 --- a/src/rgw/rgw_basic_types.cc +++ b/src/rgw/rgw_basic_types.cc @@ -7,6 +7,7 @@ #include "cls/user/cls_user_types.h" +#include "rgw_account.h" #include "rgw_basic_types.h" #include "rgw_bucket.h" #include "rgw_xml.h" @@ -189,3 +190,44 @@ void decode_json_obj(rgw_account_id& id, JSONObj* obj) { decode_json_obj(static_cast(id), obj); } + +// rgw_owner variant +rgw_owner parse_owner(const std::string& str) +{ + if (rgw::account::validate_id(str)) { + return rgw_account_id{str}; + } else { + return rgw_user{str}; + } +} + +std::string to_string(const rgw_owner& o) +{ + struct visitor { + std::string operator()(const rgw_account_id& a) { return a; } + std::string operator()(const rgw_user& u) { return u.to_str(); } + }; + return std::visit(visitor{}, o); +} + +std::ostream& operator<<(std::ostream& out, const rgw_owner& o) +{ + struct visitor { + std::ostream& out; + std::ostream& operator()(const rgw_account_id& a) { return out << a; } + std::ostream& operator()(const rgw_user& u) { return out << u; } + }; + return std::visit(visitor{out}, o); +} + +void encode_json_impl(const char *name, const rgw_owner& o, ceph::Formatter *f) +{ + encode_json(name, to_string(o), f); +} + +void decode_json_obj(rgw_owner& o, JSONObj *obj) +{ + std::string str; + decode_json_obj(str, obj); + o = parse_owner(str); +} diff --git a/src/rgw/rgw_user_types.h b/src/rgw/rgw_user_types.h index afd0d930a67..2329eca3d60 100644 --- a/src/rgw/rgw_user_types.h +++ b/src/rgw/rgw_user_types.h @@ -19,7 +19,9 @@ #pragma once +#include #include +#include #include #include "common/dout.h" @@ -134,3 +136,19 @@ struct rgw_user { static void generate_test_instances(std::list& o); }; WRITE_CLASS_ENCODER(rgw_user) + + +/// Resources are either owned by accounts, or by users or roles (represented as +/// rgw_user) that don't belong to an account. +/// +/// This variant is present in binary encoding formats, so existing types cannot +/// be changed or removed. New types can only be added to the end. +using rgw_owner = std::variant; + +rgw_owner parse_owner(const std::string& str); +std::string to_string(const rgw_owner& o); + +std::ostream& operator<<(std::ostream& out, const rgw_owner& o); + +void encode_json_impl(const char *name, const rgw_owner& o, ceph::Formatter *f); +void decode_json_obj(rgw_owner& o, JSONObj *obj); -- 2.39.5