Skip to content

Commit 4cd40c7

Browse files
committed
rgw/iam: simplify match_policy() for colon-delimited use only
remove unused MATCH_POLICY_STRING ARN::match() was the only caller for MATCH_POLICY_ARN, but it was used to match the 'region' and 'account' components that were already parsed out of the colon-separated ARN string. for that use, we don't need the loop-over-colons behavior of match_policy() so can call match_wildcards() directly after doing the same for MATCH_POLICY_RESOURCE, we no longer have any non-looping callers of match_policy() so can treat 'bool colonblocks' as unconditionally true Signed-off-by: Casey Bodley <[email protected]>
1 parent e56665c commit 4cd40c7

File tree

4 files changed

+9
-33
lines changed

4 files changed

+9
-33
lines changed

src/rgw/rgw_arn.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -328,15 +328,15 @@ bool ARN::match(const ARN& candidate) const {
328328
return false;
329329
}
330330

331-
if (!match_policy(region, candidate.region, MATCH_POLICY_ARN)) {
331+
if (!match_wildcards(region, candidate.region, MATCH_CASE_INSENSITIVE)) {
332332
return false;
333333
}
334334

335-
if (!match_policy(account, candidate.account, MATCH_POLICY_ARN)) {
335+
if (!match_wildcards(account, candidate.account, MATCH_CASE_INSENSITIVE)) {
336336
return false;
337337
}
338338

339-
if (!match_policy(resource, candidate.resource, MATCH_POLICY_RESOURCE)) {
339+
if (!match_wildcards(resource, candidate.resource, 0)) {
340340
return false;
341341
}
342342

src/rgw/rgw_common.cc

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2188,25 +2188,21 @@ bool match_policy(const std::string& pattern, const std::string& input,
21882188
{
21892189
const uint32_t flag2 = flag & (MATCH_POLICY_ACTION|MATCH_POLICY_ARN) ?
21902190
MATCH_CASE_INSENSITIVE : 0;
2191-
const bool colonblocks = !(flag & (MATCH_POLICY_RESOURCE |
2192-
MATCH_POLICY_STRING));
21932191

2194-
const auto npos = std::string_view::npos;
21952192
std::string_view::size_type last_pos_input = 0, last_pos_pattern = 0;
21962193
while (true) {
2197-
auto cur_pos_input = colonblocks ? input.find(":", last_pos_input) : npos;
2198-
auto cur_pos_pattern =
2199-
colonblocks ? pattern.find(":", last_pos_pattern) : npos;
2194+
auto cur_pos_input = input.find(":", last_pos_input);
2195+
auto cur_pos_pattern = pattern.find(":", last_pos_pattern);
22002196

22012197
auto substr_input = input.substr(last_pos_input, cur_pos_input);
22022198
auto substr_pattern = pattern.substr(last_pos_pattern, cur_pos_pattern);
22032199

22042200
if (!match_wildcards(substr_pattern, substr_input, flag2))
22052201
return false;
22062202

2207-
if (cur_pos_pattern == npos)
2208-
return cur_pos_input == npos;
2209-
if (cur_pos_input == npos)
2203+
if (cur_pos_pattern == pattern.npos)
2204+
return cur_pos_input == input.npos;
2205+
if (cur_pos_input == input.npos)
22102206
return false;
22112207

22122208
last_pos_pattern = cur_pos_pattern + 1;

src/rgw/rgw_common.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1909,9 +1909,7 @@ extern std::string calc_hash_sha256_restart_stream(ceph::crypto::SHA256** phash)
19091909
extern int rgw_parse_op_type_list(const std::string& str, uint32_t *perm);
19101910

19111911
static constexpr uint32_t MATCH_POLICY_ACTION = 0x01;
1912-
static constexpr uint32_t MATCH_POLICY_RESOURCE = 0x02;
1913-
static constexpr uint32_t MATCH_POLICY_ARN = 0x04;
1914-
static constexpr uint32_t MATCH_POLICY_STRING = 0x08;
1912+
static constexpr uint32_t MATCH_POLICY_ARN = 0x02;
19151913

19161914
extern bool match_policy(const std::string& pattern, const std::string& input,
19171915
uint32_t flag);

src/test/rgw/test_rgw_iam_policy.cc

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1464,15 +1464,6 @@ TEST(MatchPolicy, Action)
14641464
EXPECT_FALSE(match_policy("a:*", "a:b:c", flag)); // cannot span segments
14651465
}
14661466

1467-
TEST(MatchPolicy, Resource)
1468-
{
1469-
constexpr auto flag = MATCH_POLICY_RESOURCE;
1470-
EXPECT_TRUE(match_policy("a:b:c", "a:b:c", flag));
1471-
EXPECT_FALSE(match_policy("a:b:c", "A:B:C", flag)); // case sensitive
1472-
EXPECT_TRUE(match_policy("a:*:e", "a:bcd:e", flag));
1473-
EXPECT_TRUE(match_policy("a:*", "a:b:c", flag)); // can span segments
1474-
}
1475-
14761467
TEST(MatchPolicy, ARN)
14771468
{
14781469
constexpr auto flag = MATCH_POLICY_ARN;
@@ -1482,15 +1473,6 @@ TEST(MatchPolicy, ARN)
14821473
EXPECT_FALSE(match_policy("a:*", "a:b:c", flag)); // cannot span segments
14831474
}
14841475

1485-
TEST(MatchPolicy, String)
1486-
{
1487-
constexpr auto flag = MATCH_POLICY_STRING;
1488-
EXPECT_TRUE(match_policy("a:b:c", "a:b:c", flag));
1489-
EXPECT_FALSE(match_policy("a:b:c", "A:B:C", flag)); // case sensitive
1490-
EXPECT_TRUE(match_policy("a:*:e", "a:bcd:e", flag));
1491-
EXPECT_TRUE(match_policy("a:*", "a:b:c", flag)); // can span segments
1492-
}
1493-
14941476
Action_t set_range_bits(std::uint64_t start, std::uint64_t end)
14951477
{
14961478
Action_t result;

0 commit comments

Comments
 (0)