Skip to content

Commit 866054f

Browse files
authored
Merge pull request ceph#63945 from ArbitCode/wip-raja-get-account-summary
rgw/iam: getAccountSummary API
2 parents 03e22c5 + 75ff229 commit 866054f

File tree

10 files changed

+107
-0
lines changed

10 files changed

+107
-0
lines changed

doc/radosgw/iam.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,15 @@ OpenIDConnectProvider
153153
| **ListOpenIDConnectProviders** | |
154154
+---------------------------------+------------------------------------------+
155155

156+
Accounts
157+
--------
158+
159+
+---------------------------------+------------------------------------------+
160+
| Action | Remarks |
161+
+=================================+==========================================+
162+
| **GetAccountSummary** | |
163+
+---------------------------------+------------------------------------------+
164+
156165
Managed Policies
157166
----------------
158167

src/rgw/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ set(librgw_common_srcs
9797
rgw_rest_metadata.cc
9898
rgw_rest_ratelimit.cc
9999
rgw_rest_role.cc
100+
rgw_rest_iam_account.cc
100101
rgw_rest_iam_group.cc
101102
rgw_rest_iam_user.cc
102103
rgw_rest_s3.cc

src/rgw/rgw_auth_s3.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,7 @@ bool is_non_s3_op(RGWOpType op_type)
511511
case RGW_OP_LIST_ROLE_TAGS:
512512
case RGW_OP_UNTAG_ROLE:
513513
case RGW_OP_UPDATE_ROLE:
514+
case RGW_OP_GET_ACCOUNT_SUMMARY:
514515

515516
case RGW_OP_CREATE_USER:
516517
case RGW_OP_GET_USER:

src/rgw/rgw_iam_policy.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ static const actpair actpairs[] =
202202
{ "iam:GenerateServiceLastAccessedDetails", iamGenerateServiceLastAccessedDetails},
203203
{ "iam:SimulateCustomPolicy", iamSimulateCustomPolicy},
204204
{ "iam:SimulatePrincipalPolicy", iamSimulatePrincipalPolicy},
205+
{ "iam:GetAccountSummary", iamGetAccountSummary},
205206
{ "sts:AssumeRole", stsAssumeRole},
206207
{ "sts:AssumeRoleWithWebIdentity", stsAssumeRoleWithWebIdentity},
207208
{ "sts:GetSessionToken", stsGetSessionToken},
@@ -1704,6 +1705,9 @@ const char* action_bit_string(uint64_t action) {
17041705
case iamSimulatePrincipalPolicy:
17051706
return "iam:SimulatePrincipalPolicy";
17061707

1708+
case iamGetAccountSummary:
1709+
return "iam:GetAccountSummary";
1710+
17071711
case stsAssumeRole:
17081712
return "sts:AssumeRole";
17091713

src/rgw/rgw_iam_policy.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ enum {
185185
iamGenerateServiceLastAccessedDetails,
186186
iamSimulateCustomPolicy,
187187
iamSimulatePrincipalPolicy,
188+
iamGetAccountSummary,
188189
iamAll,
189190

190191
stsAssumeRole,

src/rgw/rgw_op_type.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ enum RGWOpType {
119119
RGW_OP_LIST_ATTACHED_GROUP_POLICIES,
120120
RGW_OP_PUT_BUCKET_LOGGING,
121121
RGW_OP_POST_BUCKET_LOGGING,
122+
RGW_OP_GET_ACCOUNT_SUMMARY,
122123
/* rgw specific */
123124
RGW_OP_ADMIN_SET_METADATA,
124125
RGW_OP_GET_OBJ_LAYOUT,

src/rgw/rgw_rest_iam.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "rgw_rest_iam_user.h"
1515
#include "rgw_rest_conn.h"
1616
#include "rgw_zone.h"
17+
#include "rgw_rest_iam_account.h"
1718

1819
#define dout_context g_ceph_context
1920
#define dout_subsys ceph_subsys_rgw
@@ -34,6 +35,7 @@ static const std::unordered_map<std::string_view, op_generator> op_generators =
3435
{"AttachRolePolicy", make_iam_attach_role_policy_op},
3536
{"DetachRolePolicy", make_iam_detach_role_policy_op},
3637
{"ListAttachedRolePolicies", make_iam_list_attached_role_policies_op},
38+
{"GetAccountSummary", [](const bufferlist& bl_post_body) -> RGWOp* {return new RGWGetAccountSummary;}},
3739
{"PutUserPolicy", [](const bufferlist& bl_post_body) -> RGWOp* {return new RGWPutUserPolicy(bl_post_body);}},
3840
{"GetUserPolicy", [](const bufferlist& bl_post_body) -> RGWOp* {return new RGWGetUserPolicy;}},
3941
{"ListUserPolicies", [](const bufferlist& bl_post_body) -> RGWOp* {return new RGWListUserPolicies;}},

src/rgw/rgw_rest_iam_account.cc

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2+
// vim: ts=8 sw=2 smarttab ft=cpp
3+
4+
#include "rgw_process_env.h"
5+
#include "rgw_rest_iam_account.h"
6+
7+
int RGWGetAccountSummary::verify_permission(optional_yield y)
8+
{
9+
std::string account_id;
10+
if (const auto& account = s->auth.identity->get_account(); account) {
11+
account_id = account->id;
12+
} else {
13+
return -ERR_METHOD_NOT_ALLOWED;
14+
}
15+
const rgw::ARN arn{"", "root", account_id, true};
16+
if (verify_user_permission(this, s, arn, rgw::IAM::iamGetAccountSummary)) {
17+
return 0;
18+
}
19+
return -EACCES;
20+
}
21+
22+
void RGWGetAccountSummary::add_entry(const std::string& type, int64_t value)
23+
{
24+
s->formatter->open_object_section("entry");
25+
s->formatter->dump_string("key", type);
26+
s->formatter->dump_int("value", value);
27+
s->formatter->close_section();
28+
}
29+
30+
void RGWGetAccountSummary::execute(optional_yield y)
31+
{
32+
const auto& info = s->user->get_info();
33+
const auto& account = s->auth.identity->get_account();
34+
uint32_t users_count = 0;
35+
uint32_t groups_count = 0;
36+
37+
if (account->max_users >= 0) {
38+
op_ret = driver->count_account_users(this, y, info.account_id, users_count);
39+
if (op_ret < 0) {
40+
ldpp_dout(this, 4) << "failed to count users for iam account "
41+
<< info.account_id << ": " << op_ret << dendl;
42+
return;
43+
}
44+
}
45+
46+
if (account->max_groups >= 0) {
47+
op_ret = driver->count_account_groups(this, y, info.account_id, groups_count);
48+
if (op_ret < 0) {
49+
ldpp_dout(this, 4) << "failed to count groups for iam account "
50+
<< info.account_id << ": " << op_ret << dendl;
51+
return;
52+
}
53+
}
54+
55+
s->formatter->open_object_section("GetAccountSummaryResponse");
56+
s->formatter->open_object_section("ResponseMetadata");
57+
s->formatter->dump_string("RequestId", s->trans_id);
58+
s->formatter->close_section();
59+
s->formatter->open_object_section("GetAccountSummaryResult");
60+
s->formatter->open_object_section("SummaryMap");
61+
add_entry("Users", users_count);
62+
add_entry("Groups", groups_count);
63+
add_entry("UsersQuota", account->max_users);
64+
add_entry("GroupsQuota", account->max_groups);
65+
add_entry("AccessKeysPerUserQuota", account->max_access_keys);
66+
s->formatter->close_section();
67+
s->formatter->close_section();
68+
s->formatter->close_section();
69+
}
70+

src/rgw/rgw_rest_iam_account.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2+
// vim: ts=8 sw=2 smarttab ft=cpp
3+
4+
#pragma once
5+
6+
#include "rgw_rest.h"
7+
#include "rgw_op_type.h"
8+
9+
class RGWGetAccountSummary : public RGWRESTOp {
10+
void add_entry(const std::string& key, int64_t value);
11+
public:
12+
int verify_permission(optional_yield y) override;
13+
void execute(optional_yield y) override;
14+
const char* name() const override { return "get_account_summary"; }
15+
RGWOpType get_type() override { return RGW_OP_GET_ACCOUNT_SUMMARY; }
16+
};

src/test/rgw/test_rgw_iam_policy.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ using rgw::IAM::iamListGroupPolicies;
119119
using rgw::IAM::iamListAttachedGroupPolicies;
120120
using rgw::IAM::iamSimulateCustomPolicy;
121121
using rgw::IAM::iamSimulatePrincipalPolicy;
122+
using rgw::IAM::iamGetAccountSummary;
122123
using rgw::IAM::snsGetTopicAttributes;
123124
using rgw::IAM::snsListTopics;
124125
using rgw::Service;
@@ -850,6 +851,7 @@ TEST_F(ManagedPolicyTest, IAMReadOnlyAccess)
850851
act[iamListAttachedGroupPolicies] = 1;
851852
act[iamSimulateCustomPolicy] = 1;
852853
act[iamSimulatePrincipalPolicy] = 1;
854+
act[iamGetAccountSummary] = 1;
853855

854856
EXPECT_EQ(act, p->statements[0].action);
855857
}

0 commit comments

Comments
 (0)