acelyc111 commented on code in PR #1360: URL: https://github.com/apache/incubator-pegasus/pull/1360#discussion_r1115283603
########## src/runtime/ranger/ranger_resource_policy.h: ########## @@ -0,0 +1,109 @@ +// 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. + +#pragma once + +#include <map> +#include <string> +#include <set> +#include <vector> + +#include <rapidjson/document.h> + +#include "common/json_helper.h" +#include "utils/fmt_logging.h" + +namespace dsn { +namespace ranger { + +enum access_type Review Comment: In header files, it's necessary to add required comments to describe enums, structs/classes, member functions and variables of structs/classes. ########## src/runtime/test/ranger_resource_policy_test.cpp: ########## @@ -0,0 +1,81 @@ +// 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" + +namespace dsn { +namespace ranger { + +TEST(ranger_resource_policy_test, policy_item_match) +{ + policy_item item = { + {access_type::READ, access_type::WRITE, access_type::CREATE}, {"user1", "user2"}, {}, {}}; + struct test_case + { + access_type ac_type; + std::string user_name; + bool expected_result; + } tests[] = {{access_type::INVALID, "", false}, + {access_type::INVALID, "user", false}, + {access_type::READ, "user", false}, + {access_type::LIST, "user1", false}, + {access_type::READ, "user1", true}, + {access_type::WRITE, "user2", true}}; + for (const auto &test : tests) { + auto actual_result = item.match(test.ac_type, test.user_name); + EXPECT_EQ(actual_result, test.expected_result); Review Comment: it would be better to swap the order of "expect" value and "actual" value, i.e. `EXPECT_EQ(test.expected_result, actual_result);` ########## src/runtime/ranger/ranger_resource_policy.h: ########## @@ -0,0 +1,109 @@ +// 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. + +#pragma once + +#include <map> +#include <string> +#include <set> +#include <vector> + +#include <rapidjson/document.h> + +#include "common/json_helper.h" +#include "utils/fmt_logging.h" + +namespace dsn { +namespace ranger { + +enum access_type +{ + READ = 0, + WRITE, + CREATE, + DROP, + LIST, + METADATA, + CONTROL, + ALL, + INVALID, +}; + +ENUM_BEGIN(access_type, INVALID) +ENUM_REG(READ) +ENUM_REG(WRITE) +ENUM_REG(CREATE) +ENUM_REG(DROP) +ENUM_REG(LIST) +ENUM_REG(METADATA) +ENUM_REG(CONTROL) +ENUM_REG(ALL) +ENUM_END(access_type) + +ENUM_TYPE_SERIALIZATION(access_type, INVALID) + +struct policy_item +{ + std::set<access_type> access_types; + std::set<std::string> users; + std::set<std::string> groups; // not use + std::set<std::string> roles; // not use Review Comment: If they are no used now, can we remove them? ########## src/runtime/test/ranger_resource_policy_test.cpp: ########## @@ -0,0 +1,81 @@ +// 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" + +namespace dsn { +namespace ranger { + +TEST(ranger_resource_policy_test, policy_item_match) +{ + policy_item item = { + {access_type::READ, access_type::WRITE, access_type::CREATE}, {"user1", "user2"}, {}, {}}; + struct test_case + { + access_type ac_type; + std::string user_name; + bool expected_result; + } tests[] = {{access_type::INVALID, "", false}, + {access_type::INVALID, "user", false}, + {access_type::READ, "user", false}, + {access_type::LIST, "user1", false}, + {access_type::READ, "user1", true}, + {access_type::WRITE, "user2", true}}; Review Comment: How about check all access types for user1 and user2? ########## src/runtime/ranger/ranger_resource_policy.h: ########## @@ -0,0 +1,109 @@ +// 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. + +#pragma once + +#include <map> +#include <string> +#include <set> +#include <vector> + +#include <rapidjson/document.h> + +#include "common/json_helper.h" +#include "utils/fmt_logging.h" + +namespace dsn { +namespace ranger { + +enum access_type +{ + READ = 0, + WRITE, + CREATE, + DROP, + LIST, + METADATA, + CONTROL, + ALL, + INVALID, +}; + +ENUM_BEGIN(access_type, INVALID) +ENUM_REG(READ) +ENUM_REG(WRITE) +ENUM_REG(CREATE) +ENUM_REG(DROP) +ENUM_REG(LIST) +ENUM_REG(METADATA) +ENUM_REG(CONTROL) +ENUM_REG(ALL) +ENUM_END(access_type) + +ENUM_TYPE_SERIALIZATION(access_type, INVALID) + +struct policy_item +{ + std::set<access_type> access_types; + std::set<std::string> users; + std::set<std::string> groups; // not use + std::set<std::string> roles; // not use + + DEFINE_JSON_SERIALIZATION(access_types, users, groups, roles); + + bool match(const access_type &ac_type, const std::string &user_name) const; +}; + +struct acl_policies +{ + // policy priority: deny_policies_exclude > deny_policies > allow_policies_exclude > + // allow_policies + std::vector<policy_item> allow_policies; + std::vector<policy_item> allow_policies_exclude; + std::vector<policy_item> deny_policies; + std::vector<policy_item> deny_policies_exclude; + + DEFINE_JSON_SERIALIZATION(allow_policies, + allow_policies_exclude, + deny_policies, + deny_policies_exclude); + + // Check whether the user is allowed to access the resource. + bool allowed(const access_type &ac_type, const std::string &user_name) const; +}; + +struct ranger_resource_policy +{ +public: + ranger_resource_policy() = default; + ~ranger_resource_policy() = default; + + // Create the default policy to adapt legacy tables. + static void create_default_database_policy(ranger_resource_policy &acl); + +public: + std::string name; + std::set<std::string> global_names; // TODO(yingchun): not in use Review Comment: can we remove it? ########## src/runtime/test/ranger_resource_policy_test.cpp: ########## @@ -0,0 +1,81 @@ +// 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" + +namespace dsn { +namespace ranger { + +TEST(ranger_resource_policy_test, policy_item_match) +{ + policy_item item = { + {access_type::READ, access_type::WRITE, access_type::CREATE}, {"user1", "user2"}, {}, {}}; + struct test_case + { + access_type ac_type; + std::string user_name; + bool expected_result; + } tests[] = {{access_type::INVALID, "", false}, + {access_type::INVALID, "user", false}, + {access_type::READ, "user", false}, + {access_type::LIST, "user1", false}, + {access_type::READ, "user1", true}, + {access_type::WRITE, "user2", true}}; + for (const auto &test : tests) { + auto actual_result = item.match(test.ac_type, test.user_name); + EXPECT_EQ(actual_result, test.expected_result); + } +} + +TEST(ranger_resource_policy_test, acl_policies_allowed) +{ + acl_policies policy; + policy.allow_policies = {{{access_type::READ, access_type::WRITE, access_type::CREATE}, + {"user1", "user2", "user3", "user4"}, + {}, + {}}}; + policy.allow_policies_exclude = { + {{access_type::WRITE, access_type::CREATE}, {"user2"}, {}, {}}}; + policy.deny_policies = {{{access_type::READ, access_type::WRITE}, {"user3", "user4"}, {}, {}}}; + policy.deny_policies_exclude = {{{access_type::READ}, {"user4"}, {}, {}}}; + struct test_case + { + access_type ac_type; + std::string user_name; + bool expected_result; + } tests[] = {{access_type::READ, "user", false}, + {access_type::READ, "user1", true}, + {access_type::LIST, "user1", false}, + {access_type::READ, "user2", true}, + {access_type::WRITE, "user2", false}, + {access_type::LIST, "user2", false}, + {access_type::READ, "user3", false}, + {access_type::CREATE, "user3", true}, + {access_type::LIST, "user3", false}, + {access_type::READ, "user4", true}, + {access_type::WRITE, "user4", false}, + {access_type::CREATE, "user4", true}, + {access_type::LIST, "user4", false}}; + for (const auto &test : tests) { + auto actual_result = policy.allowed(test.ac_type, test.user_name); + EXPECT_EQ(actual_result, test.expected_result); Review Comment: same -- 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]
