]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
rgw: Added a globbing method for AWS Policies. 12445/head
authorPritha Srivastava <prsrivas@redhat.com>
Mon, 12 Dec 2016 15:38:48 +0000 (21:08 +0530)
committerPritha Srivastava <prsrivas@redhat.com>
Tue, 27 Dec 2016 07:47:05 +0000 (13:17 +0530)
Signed-off-by: Pritha Srivastava <prsrivas@redhat.com>
src/rgw/rgw_common.cc
src/rgw/rgw_common.h

index 21ee4a06bbfb32fd9240e82bd05313bc8a3c5c72..676f96c104938af85f4509a6f7e281cdd8cc0eb2 100644 (file)
@@ -2,6 +2,10 @@
 // vim: ts=8 sw=2 smarttab
 
 #include <errno.h>
+#include <vector>
+#include <algorithm>
+#include <string>
+#include <boost/tokenizer.hpp>
 #include <boost/algorithm/string.hpp>
 
 #include "json_spirit/json_spirit.h"
 
 #define dout_subsys ceph_subsys_rgw
 
+#define POLICY_ACTION           0x01
+#define POLICY_RESOURCE         0x02
+#define POLICY_ARN              0x04
+#define POLICY_STRING           0x08
+
 PerfCounters *perfcounter = NULL;
 
 const uint32_t RGWBucketInfo::NUM_SHARDS_BLIND_BUCKET(UINT32_MAX);
@@ -1467,3 +1476,77 @@ int rgw_parse_op_type_list(const string& str, uint32_t *perm)
   return parse_list_of_flags(op_type_mapping, str, perm);
 }
 
+static int match_internal(boost::string_ref pattern, boost::string_ref input, int (*function)(const char&, const char&))
+{
+  boost::string_ref::iterator it1 = pattern.begin();
+  boost::string_ref::iterator it2 = input.begin();
+  while(true) {
+    if (it1 == pattern.end() && it2 == input.end())
+        return 1;
+    if (it1 == pattern.end() || it2 == input.end())
+        return 0;
+    if (*it1 == '*' && (it1 + 1) == pattern.end() && it2 != input.end())
+      return 1;
+    if (*it1 == '*' && (it1 + 1) == pattern.end() && it2 == input.end())
+      return 0;
+    if (function(*it1, *it2) || *it1 == '?') {
+      it1++;
+      it2++;
+      continue;
+    }
+    if (*it1 == '*') {
+      if (function(*(it1 + 1), *it2))
+        it1++;
+      else
+        it2++;
+      continue;
+    }
+    return 0;
+  }
+  return 0;
+}
+
+static int matchcase(const char& c1, const char& c2)
+{
+  if (c1 == c2)
+      return 1;
+  return 0;
+}
+
+static int matchignorecase(const char& c1, const char& c2)
+{
+  if (tolower(c1) == tolower(c2))
+      return 1;
+  return 0;
+}
+
+int match(const string& pattern, const string& input, int flag)
+{
+  auto last_pos_input = 0, last_pos_pattern = 0;
+
+  while(true) {
+    auto cur_pos_input = input.find(":", last_pos_input);
+    auto cur_pos_pattern = pattern.find(":", last_pos_pattern);
+
+    string substr_input = input.substr(last_pos_input, cur_pos_input);
+    string substr_pattern = pattern.substr(last_pos_pattern, cur_pos_pattern);
+
+    int res;
+    if (flag & POLICY_ACTION || flag & POLICY_ARN) {
+      res = match_internal(substr_pattern, substr_input, &matchignorecase);
+    } else {
+      res = match_internal(substr_pattern, substr_input, &matchcase);
+    }
+    if (res == 0)
+      return 0;
+
+    if (cur_pos_pattern == string::npos && cur_pos_input == string::npos)
+      return 1;
+    else if ((cur_pos_pattern == string::npos && cur_pos_input != string::npos) ||
+             (cur_pos_pattern != string::npos && cur_pos_input == string::npos))
+      return 0;
+
+    last_pos_pattern = cur_pos_pattern + 1;
+    last_pos_input = cur_pos_input + 1;
+  }
+}
\ No newline at end of file
index b852520e4c6ecb7b1f34486b1aae4b7d0f5910e8..c5aa61daf78961752ff0aa85a98fa72a2714427c 100644 (file)
@@ -1974,4 +1974,5 @@ extern string  calc_hash_sha256_close_stream(SHA256 **hash);
 
 extern int rgw_parse_op_type_list(const string& str, uint32_t *perm);
 
+int match(const string& pattern, const string& input, int flag);
 #endif