]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
rgw: add list user admin OP API
authorsongshuangyang <songshuangyang@baidu.com>
Tue, 13 Nov 2018 09:32:41 +0000 (17:32 +0800)
committerKonstantin Shalygin <k0ste@k0ste.ru>
Fri, 18 Oct 2019 05:11:40 +0000 (12:11 +0700)
The radosgw-admin tool support the `user list` subcommand to list radosgw users, but there is no user listing function for the admin OP API. It needs to support this API.

Signed-off-by: Oshyn Song <dualyangsong@gmail.com>
(cherry picked from commit 97e1ff50e57a51744a6d864cef15f9afe0e84b66)

- path: src/rgw/rgw_user.h
  comment:
  - master 'set_access_key' is "const std::string", luminous is "std::string".
  - 'mfa_ids_specified' is not exists on luminous.

src/rgw/rgw_common.cc
src/rgw/rgw_rest_user.cc
src/rgw/rgw_user.cc
src/rgw/rgw_user.h

index 6be06af322b2fb210b6c25921eab48fd2d755fd7..33907aa6991e48536ee81e1d4a107d0142c32704 100644 (file)
@@ -997,6 +997,7 @@ void RGWHTTPArgs::append(const string& name, const string& val)
               (name.compare("index") == 0) ||
               (name.compare("policy") == 0) ||
               (name.compare("quota") == 0) ||
+              (name.compare("list") == 0) ||
               (name.compare("object") == 0)) {
 
     if (!admin_subresource_added) {
index bd561d0c470cf6183b512398cb996051f5a34918..c9de0d50be6aed4e431e073d731c4e13c6c14d85 100644 (file)
 
 #define dout_subsys ceph_subsys_rgw
 
+class RGWOp_User_List : public RGWRESTOp {
+
+public:
+  RGWOp_User_List() {}
+
+  int check_caps(RGWUserCaps& caps) override {
+    return caps.check_cap("users", RGW_CAP_READ);
+  }
+
+  void execute() override;
+
+  const string name() override { return "list_user"; }
+};
+
+void RGWOp_User_List::execute()
+{
+  RGWUserAdminOpState op_state;
+
+  uint32_t max_entries;
+  std::string marker;
+  RESTArgs::get_uint32(s, "max-entries", 1000, &max_entries);
+  RESTArgs::get_string(s, "marker", marker, &marker);
+
+  op_state.max_entries = max_entries;
+  op_state.marker = marker;
+  http_ret = RGWUserAdminOp_User::list(store, op_state, flusher);
+}
+
 class RGWOp_User_Info : public RGWRESTOp {
 
 public:
@@ -321,7 +349,7 @@ void RGWOp_Subuser_Create::execute()
   //RESTArgs::get_bool(s, "generate-subuser", false, &gen_subuser);
   RESTArgs::get_bool(s, "generate-secret", false, &gen_secret);
   RESTArgs::get_bool(s, "gen-access-key", false, &gen_access);
-  
+
   perm_mask = rgw_str_to_perm(perm_str.c_str());
   op_state.set_perm(perm_mask);
 
@@ -619,7 +647,7 @@ struct UserQuotas {
 
   UserQuotas() {}
 
-  explicit UserQuotas(RGWUserInfo& info) : bucket_quota(info.bucket_quota), 
+  explicit UserQuotas(RGWUserInfo& info) : bucket_quota(info.bucket_quota),
                                  user_quota(info.user_quota) {}
 
   void dump(Formatter *f) const {
@@ -895,6 +923,9 @@ RGWOp *RGWHandler_User::op_get()
   if (s->info.args.sub_resource_exists("quota"))
     return new RGWOp_Quota_Info;
 
+  if (s->info.args.sub_resource_exists("list"))
+    return new RGWOp_User_List;
+
   return new RGWOp_User_Info;
 }
 
@@ -936,4 +967,3 @@ RGWOp *RGWHandler_User::op_delete()
 
   return new RGWOp_User_Remove;
 }
-
index 9169f19205c5c7d6315db0615d2167d132777823..f6371c6cdadf087bc7d2d0461ab6b1ba2dd61f47 100644 (file)
@@ -2336,6 +2336,77 @@ int RGWUser::info(RGWUserInfo& fetched_info, std::string *err_msg)
   return 0;
 }
 
+int RGWUser::list(RGWUserAdminOpState& op_state, RGWFormatterFlusher& flusher)
+{
+  Formatter *formatter = flusher.get_formatter();
+  void *handle = nullptr;
+  std::string metadata_key = "user";
+  if (op_state.max_entries > 1000) {
+    op_state.max_entries = 1000;
+  }
+
+  int ret = store->meta_mgr->list_keys_init(metadata_key, op_state.marker, &handle);
+  if (ret < 0) {
+    return ret;
+  }
+
+  bool truncated = false;
+  uint64_t count = 0;
+  uint64_t left = 0;
+  flusher.start(0);
+
+  // open the result object section
+  formatter->open_object_section("result");
+
+  // open the user id list array section
+  formatter->open_array_section("keys");
+  do {
+    std::list<std::string> keys;
+    left = op_state.max_entries - count;
+    ret = store->meta_mgr->list_keys_next(handle, left, keys, &truncated);
+    if (ret < 0 && ret != -ENOENT) {
+      return ret;
+    } if (ret != -ENOENT) {
+      for (std::list<std::string>::iterator iter = keys.begin(); iter != keys.end(); ++iter) {
+      formatter->dump_string("key", *iter);
+        ++count;
+      }
+    }
+  } while (truncated && left > 0);
+  // close user id list section
+  formatter->close_section();
+
+  formatter->dump_bool("truncated", truncated);
+  formatter->dump_int("count", count);
+  if (truncated) {
+    formatter->dump_string("marker", store->meta_mgr->get_marker(handle));
+  }
+
+  // close result object section
+  formatter->close_section();
+
+  store->meta_mgr->list_keys_complete(handle);
+
+  flusher.flush();
+  return 0;
+}
+
+int RGWUserAdminOp_User::list(RGWRados *store, RGWUserAdminOpState& op_state,
+                  RGWFormatterFlusher& flusher)
+{
+  RGWUser user;
+
+  int ret = user.init_storage(store);
+  if (ret < 0)
+    return ret;
+
+  ret = user.list(op_state, flusher);
+  if (ret < 0)
+    return ret;
+
+  return 0;
+}
+
 int RGWUserAdminOp_User::info(RGWRados *store, RGWUserAdminOpState& op_state,
                   RGWFormatterFlusher& flusher)
 {
index 516617a295574c985bcee22c3bbbca60de174569..0b2028c83e0e3bdb1e4ddd51b081d9c9549f88a3 100644 (file)
@@ -204,10 +204,10 @@ struct RGWUserAdminOpState {
   bool system_specified;
   bool key_op;
   bool temp_url_key_specified;
-  bool found_by_uid; 
-  bool found_by_email;  
+  bool found_by_uid;
+  bool found_by_email;
   bool found_by_key;
+
   // req parameters
   bool populated;
   bool initialized;
@@ -221,6 +221,10 @@ struct RGWUserAdminOpState {
   RGWQuotaInfo bucket_quota;
   RGWQuotaInfo user_quota;
 
+  // req parameters for listing user
+  std::string marker;
+  uint32_t max_entries;
+
   void set_access_key(std::string& access_key) {
     if (access_key.empty())
       return;
@@ -523,6 +527,8 @@ struct RGWUserAdminOpState {
     found_by_uid = false;
     found_by_email = false;
     found_by_key = false;
+    max_entries = 1000;
+    marker = "";
   }
 };
 
@@ -688,6 +694,9 @@ public:
   /* info from an already populated RGWUser */
   int info (RGWUserInfo& fetched_info, std::string *err_msg = NULL);
 
+  /* list the existing users */
+  int list(RGWUserAdminOpState& op_state, RGWFormatterFlusher& flusher);
+
   friend class RGWAccessKeyPool;
   friend class RGWSubUserPool;
   friend class RGWUserCapPool;
@@ -698,6 +707,9 @@ public:
 class RGWUserAdminOp_User
 {
 public:
+  static int list(RGWRados *store,
+                  RGWUserAdminOpState& op_state, RGWFormatterFlusher& flusher);
+
   static int info(RGWRados *store,
                   RGWUserAdminOpState& op_state, RGWFormatterFlusher& flusher);