From a1333f547e04b3776d3808808c3f727d6fad7362 Mon Sep 17 00:00:00 2001 From: Dunrong Huang Date: Wed, 3 Feb 2016 15:43:40 +0800 Subject: [PATCH] rgw: support json format for admin policy API Fixes: #14090, #14091 - before this patch: $ curl -s -X GET http://radosrgw:9090/admin/bucket?format=json&policy&bucket=m8x | xmllint --format - got XML response with error format - after this patch: got correct response with json format Signed-off-by: Dunrong Huang --- src/rgw/rgw_bucket.cc | 30 ++++++++++++++++++++++-------- src/rgw/rgw_bucket.h | 4 ++-- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/src/rgw/rgw_bucket.cc b/src/rgw/rgw_bucket.cc index e77ecfbd174..9b2294c15a2 100644 --- a/src/rgw/rgw_bucket.cc +++ b/src/rgw/rgw_bucket.cc @@ -923,7 +923,19 @@ int RGWBucket::policy_bl_to_stream(bufferlist& bl, ostream& o) return 0; } -int RGWBucket::get_policy(RGWBucketAdminOpState& op_state, ostream& o) +static int policy_decode(RGWRados *store, bufferlist& bl, RGWAccessControlPolicy& policy) +{ + bufferlist::iterator iter = bl.begin(); + try { + policy.decode(iter); + } catch (buffer::error& err) { + ldout(store->ctx(), 0) << "ERROR: caught buffer::error, could not decode policy" << dendl; + return -EIO; + } + return 0; +} + +int RGWBucket::get_policy(RGWBucketAdminOpState& op_state, RGWAccessControlPolicy& policy) { std::string object_name = op_state.get_object_name(); rgw_bucket bucket = op_state.get_bucket(); @@ -947,7 +959,7 @@ int RGWBucket::get_policy(RGWBucketAdminOpState& op_state, ostream& o) if (ret < 0) return ret; - return policy_bl_to_stream(bl, o); + return policy_decode(store, bl, policy); } map::iterator aiter = attrs.find(RGW_ATTR_ACL); @@ -955,12 +967,12 @@ int RGWBucket::get_policy(RGWBucketAdminOpState& op_state, ostream& o) return -ENOENT; } - return policy_bl_to_stream(aiter->second, o); + return policy_decode(store, aiter->second, policy); } int RGWBucketAdminOp::get_policy(RGWRados *store, RGWBucketAdminOpState& op_state, - ostream& os) + RGWAccessControlPolicy& policy) { RGWBucket bucket; @@ -968,7 +980,7 @@ int RGWBucketAdminOp::get_policy(RGWRados *store, RGWBucketAdminOpState& op_stat if (ret < 0) return ret; - ret = bucket.get_policy(op_state, os); + ret = bucket.get_policy(op_state, policy); if (ret < 0) return ret; @@ -981,9 +993,9 @@ int RGWBucketAdminOp::get_policy(RGWRados *store, RGWBucketAdminOpState& op_stat int RGWBucketAdminOp::get_policy(RGWRados *store, RGWBucketAdminOpState& op_state, RGWFormatterFlusher& flusher) { - std::ostringstream policy_stream; + RGWAccessControlPolicy policy(store->ctx()); - int ret = get_policy(store, op_state, policy_stream); + int ret = get_policy(store, op_state, policy); if (ret < 0) return ret; @@ -991,7 +1003,9 @@ int RGWBucketAdminOp::get_policy(RGWRados *store, RGWBucketAdminOpState& op_stat flusher.start(0); - formatter->dump_string("policy", policy_stream.str()); + formatter->open_object_section("policy"); + policy.dump(formatter); + formatter->close_section(); flusher.flush(); diff --git a/src/rgw/rgw_bucket.h b/src/rgw/rgw_bucket.h index 9bece1ad0c1..924c636021f 100644 --- a/src/rgw/rgw_bucket.h +++ b/src/rgw/rgw_bucket.h @@ -280,7 +280,7 @@ public: int remove_object(RGWBucketAdminOpState& op_state, std::string *err_msg = NULL); int policy_bl_to_stream(bufferlist& bl, ostream& o); - int get_policy(RGWBucketAdminOpState& op_state, ostream& o); + int get_policy(RGWBucketAdminOpState& op_state, RGWAccessControlPolicy& policy); void clear_failure() { failure = false; } }; @@ -291,7 +291,7 @@ public: static int get_policy(RGWRados *store, RGWBucketAdminOpState& op_state, RGWFormatterFlusher& flusher); static int get_policy(RGWRados *store, RGWBucketAdminOpState& op_state, - ostream& os); + RGWAccessControlPolicy& policy); static int unlink(RGWRados *store, RGWBucketAdminOpState& op_state);