acelyc111 commented on code in PR #1378:
URL: 
https://github.com/apache/incubator-pegasus/pull/1378#discussion_r1122567795


##########
src/runtime/ranger/ranger_resource_policy_manager.cpp:
##########
@@ -36,6 +64,39 @@ void register_rpc_access_type(access_type ac_type,
         ac_type_of_rpc.emplace(code, ac_type);
     }
 }
+
+// Used to map access_type matched resources policies json string.
+std::map<std::string, access_type> access_type_maping({{"READ", 
access_type::KRead},
+                                                       {"WRITE", 
access_type::KWrite},
+                                                       {"CREATE", 
access_type::KCreate},
+                                                       {"DROP", 
access_type::KDrop},
+                                                       {"LIST", 
access_type::KList},
+                                                       {"METADATA", 
access_type::KMetadata},
+                                                       {"CONTROL", 
access_type::KControl}});
+
+// Parse Ranger ACL policies in JSON format 'data' into 'policies'.
+void parse_policies_from_json(const rapidjson::Value &data, 
std::vector<policy_item> &policies)
+{
+    CHECK(policies.empty(), "Ranger policy list should not be empty.");
+    RETURN_VOID_IF_NOT_ARRAY(data);
+    for (auto &item : data.GetArray()) {

Review Comment:
   ```suggestion
       for (const auto &item : data.GetArray()) {
   ```



##########
src/runtime/ranger/ranger_resource_policy_manager.h:
##########
@@ -32,8 +32,28 @@ namespace replication {
 class meta_service;
 }
 
+enum class resource_type
+{
+    KGlobal = 0,
+    Kdatabase,
+    KDatabase_table,

Review Comment:
   ```suggestion
       KDatabaseTable,
   ```



##########
src/runtime/ranger/ranger_resource_policy_manager.cpp:
##########
@@ -36,6 +64,39 @@ void register_rpc_access_type(access_type ac_type,
         ac_type_of_rpc.emplace(code, ac_type);
     }
 }
+
+// Used to map access_type matched resources policies json string.
+std::map<std::string, access_type> access_type_maping({{"READ", 
access_type::KRead},
+                                                       {"WRITE", 
access_type::KWrite},
+                                                       {"CREATE", 
access_type::KCreate},
+                                                       {"DROP", 
access_type::KDrop},
+                                                       {"LIST", 
access_type::KList},
+                                                       {"METADATA", 
access_type::KMetadata},
+                                                       {"CONTROL", 
access_type::KControl}});
+
+// Parse Ranger ACL policies in JSON format 'data' into 'policies'.
+void parse_policies_from_json(const rapidjson::Value &data, 
std::vector<policy_item> &policies)
+{
+    CHECK(policies.empty(), "Ranger policy list should not be empty.");
+    RETURN_VOID_IF_NOT_ARRAY(data);
+    for (auto &item : data.GetArray()) {
+        policy_item pi;

Review Comment:
   In `struct policy_item`, add the default value `access_type::KInvalid` to 
access_types like:
   ```
   struct policy_item
   {
       access_type access_types = access_type::KInvalid;
       ...
   ```
   Then the assignment here can be omitted.



##########
src/runtime/ranger/ranger_resource_policy_manager.cpp:
##########
@@ -36,6 +64,39 @@ void register_rpc_access_type(access_type ac_type,
         ac_type_of_rpc.emplace(code, ac_type);
     }
 }
+
+// Used to map access_type matched resources policies json string.
+std::map<std::string, access_type> access_type_maping({{"READ", 
access_type::KRead},
+                                                       {"WRITE", 
access_type::KWrite},
+                                                       {"CREATE", 
access_type::KCreate},
+                                                       {"DROP", 
access_type::KDrop},
+                                                       {"LIST", 
access_type::KList},
+                                                       {"METADATA", 
access_type::KMetadata},
+                                                       {"CONTROL", 
access_type::KControl}});
+
+// Parse Ranger ACL policies in JSON format 'data' into 'policies'.
+void parse_policies_from_json(const rapidjson::Value &data, 
std::vector<policy_item> &policies)
+{
+    CHECK(policies.empty(), "Ranger policy list should not be empty.");
+    RETURN_VOID_IF_NOT_ARRAY(data);
+    for (auto &item : data.GetArray()) {
+        policy_item pi;
+        pi.access_types = access_type::KInvalid;
+        CONTINUE_IF_MISSING_MEMBER(item, "accesses");
+        for (const auto &access : item["accesses"].GetArray()) {
+            if (access["isAllowed"].GetBool()) {

Review Comment:
   Need check `access` has "isAllowed" at first?



##########
src/runtime/test/ranger_resource_policy_manager_test.cpp:
##########
@@ -0,0 +1,92 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#include <gtest/gtest.h>
+
+#include "runtime/ranger/ranger_resource_policy.h"
+#include "runtime/ranger/ranger_resource_policy_manager.h"
+
+namespace dsn {
+namespace ranger {
+
+TEST(ranger_resource_policy_manager_test, parse_policies_from_json_for_test)

Review Comment:
   It's needed to add tests to check that the objects can be serialized 
correctly too.



##########
src/runtime/ranger/ranger_resource_policy_manager.cpp:
##########
@@ -36,6 +64,39 @@ void register_rpc_access_type(access_type ac_type,
         ac_type_of_rpc.emplace(code, ac_type);
     }
 }
+
+// Used to map access_type matched resources policies json string.
+std::map<std::string, access_type> access_type_maping({{"READ", 
access_type::KRead},
+                                                       {"WRITE", 
access_type::KWrite},
+                                                       {"CREATE", 
access_type::KCreate},
+                                                       {"DROP", 
access_type::KDrop},
+                                                       {"LIST", 
access_type::KList},
+                                                       {"METADATA", 
access_type::KMetadata},
+                                                       {"CONTROL", 
access_type::KControl}});
+
+// Parse Ranger ACL policies in JSON format 'data' into 'policies'.
+void parse_policies_from_json(const rapidjson::Value &data, 
std::vector<policy_item> &policies)
+{
+    CHECK(policies.empty(), "Ranger policy list should not be empty.");
+    RETURN_VOID_IF_NOT_ARRAY(data);
+    for (auto &item : data.GetArray()) {
+        policy_item pi;
+        pi.access_types = access_type::KInvalid;
+        CONTINUE_IF_MISSING_MEMBER(item, "accesses");
+        for (const auto &access : item["accesses"].GetArray()) {
+            if (access["isAllowed"].GetBool()) {
+                std::string type = access["type"].GetString();

Review Comment:
   Check has "type" at first?



##########
src/runtime/ranger/ranger_resource_policy_manager.cpp:
##########
@@ -97,5 +158,11 @@ 
ranger_resource_policy_manager::ranger_resource_policy_manager(
                               "RPC_CM_RENAME_APP"},
                              _ac_type_of_database_rpcs);
 }
+
+void parse_policies_from_json_for_test(const rapidjson::Value &data,
+                                       std::vector<policy_item> &policies)
+{
+    parse_policies_from_json(data, policies);

Review Comment:
   It's a bit of strenge, how about define `parse_policies_from_json` as a 
static private function instead?



##########
src/runtime/ranger/ranger_resource_policy_manager.cpp:
##########
@@ -36,6 +64,39 @@ void register_rpc_access_type(access_type ac_type,
         ac_type_of_rpc.emplace(code, ac_type);
     }
 }
+
+// Used to map access_type matched resources policies json string.
+std::map<std::string, access_type> access_type_maping({{"READ", 
access_type::KRead},
+                                                       {"WRITE", 
access_type::KWrite},
+                                                       {"CREATE", 
access_type::KCreate},
+                                                       {"DROP", 
access_type::KDrop},
+                                                       {"LIST", 
access_type::KList},
+                                                       {"METADATA", 
access_type::KMetadata},
+                                                       {"CONTROL", 
access_type::KControl}});
+
+// Parse Ranger ACL policies in JSON format 'data' into 'policies'.
+void parse_policies_from_json(const rapidjson::Value &data, 
std::vector<policy_item> &policies)
+{
+    CHECK(policies.empty(), "Ranger policy list should not be empty.");
+    RETURN_VOID_IF_NOT_ARRAY(data);
+    for (auto &item : data.GetArray()) {
+        policy_item pi;
+        pi.access_types = access_type::KInvalid;
+        CONTINUE_IF_MISSING_MEMBER(item, "accesses");
+        for (const auto &access : item["accesses"].GetArray()) {
+            if (access["isAllowed"].GetBool()) {
+                std::string type = access["type"].GetString();
+                std::transform(type.begin(), type.end(), type.begin(), 
toupper);
+                pi.access_types = pi.access_types | access_type_maping[type];

Review Comment:
   ```suggestion
                   pi.access_types |= access_type_maping[type];
   ```



##########
src/runtime/test/ranger_resource_policy_manager_test.cpp:
##########
@@ -0,0 +1,92 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#include <gtest/gtest.h>
+
+#include "runtime/ranger/ranger_resource_policy.h"
+#include "runtime/ranger/ranger_resource_policy_manager.h"
+
+namespace dsn {
+namespace ranger {
+
+TEST(ranger_resource_policy_manager_test, parse_policies_from_json_for_test)
+{
+    std::string data =
+        
"[{\"accesses\":[{\"type\":\"create\",\"isAllowed\":true},{\"type\":\"drop\",\"isAllowed\":"
+        
"true},{\"type\":\"control\",\"isAllowed\":true},{\"type\":\"metadata\",\"isAllowed\":true}"
+        
",{\"type\":\"list\",\"isAllowed\":true}],\"users\":[\"user1\",\"user2\"],\"groups\":[],"
+        
"\"roles\":[],\"conditions\":[],\"delegateAdmin\":true},{\"accesses\":[{\"type\":\"read\","
+        
"\"isAllowed\":true},{\"type\":\"write\",\"isAllowed\":true}],\"users\":[\"user2\"],"
+        
"\"groups\":[],\"roles\":[],\"conditions\":[],\"delegateAdmin\":true}]";

Review Comment:
   Use string literal, ref: 
https://en.cppreference.com/w/c/language/string_literal, 
src/meta/test/meta_http_service_test.cpp:L55



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to