acelyc111 commented on code in PR #1360: URL: https://github.com/apache/incubator-pegasus/pull/1360#discussion_r1116564295
########## src/runtime/ranger/ranger_resource_policy.h: ########## @@ -0,0 +1,112 @@ +// 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 { + +// ACL type defined in Range service for RPC matching policy +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) + +// Ranger policy data structure +struct policy_item +{ + std::set<access_type> access_types; + std::set<std::string> users; + + DEFINE_JSON_SERIALIZATION(access_types, users); + + // Match a policy according to acl_type and user_name Review Comment: ```suggestion // Check if the 'acl_type' - 'user_name' pair is matched to the policy. // Return true if it is matched, otherwise return false. ``` ########## src/runtime/ranger/ranger_resource_policy.h: ########## @@ -0,0 +1,112 @@ +// 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 { + +// ACL type defined in Range service for RPC matching policy +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) + +// Ranger policy data structure +struct policy_item +{ + std::set<access_type> access_types; + std::set<std::string> users; + + DEFINE_JSON_SERIALIZATION(access_types, users); + + // Match a policy according to acl_type and user_name + bool match(const access_type &ac_type, const std::string &user_name) const; +}; + +// Data structure of policies with different priorities +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; +}; + +// A policy data structure definition of ranger resources +struct ranger_resource_policy +{ +public: + ranger_resource_policy() = default; + ~ranger_resource_policy() = default; + + // Create the default policy to adapt legacy tables. Review Comment: ```suggestion // Create the default policy to adapt legacy tables. // In Ranger policy, database prefix is required to identify the database it belongs to, but for the tables which are created before Ranger policy is introduced to Pegasus, they don't have such a prefix. So we create a default database policy for them. ``` ########## src/runtime/ranger/ranger_resource_policy.cpp: ########## @@ -0,0 +1,79 @@ +// 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 "ranger_resource_policy.h" + +namespace dsn { +namespace ranger { + +bool policy_item::match(const access_type &ac_type, const std::string &user_name) const +{ + return access_types.count(ac_type) != 0 && users.count(user_name) != 0; +} + +bool acl_policies::allowed(const access_type &ac_type, const std::string &user_name) const +{ + // 1. Check if it is not allowed. + for (const auto &deny_policy : deny_policies) { + // 1.1. In 'deny_policies'. + if (!deny_policy.match(ac_type, user_name)) { + continue; + } + bool in_deny_policies_exclude = false; + for (const auto &deny_policy_exclude : deny_policies_exclude) { + if (deny_policy_exclude.match(ac_type, user_name)) { + in_deny_policies_exclude = true; + break; + } + } + // 1.2. Not in any 'deny_policies_exclude', it's not allowed. + if (!in_deny_policies_exclude) { + return false; + } + } + + // 2. Check if it is allowed. + for (const auto &allow_policy : allow_policies) { + // 2.1. In 'allow_policies'. + if (!allow_policy.match(ac_type, user_name)) { + continue; + } + for (const auto &allow_policy_exclude : allow_policies_exclude) { + // 2.2. In some 'allow_policies_exclude', it's not allowed. + if (allow_policy_exclude.match(ac_type, user_name)) { + return false; + } + } + // 2.3. Not in any 'allow_policies_exclude', it's allowed. + return true; + } + + // 3. Otherwise, it's not allowed. + return false; +} + +void ranger_resource_policy::create_default_database_policy(ranger_resource_policy &acl) +{ + acl.name = "default database policy"; + acl.database_names = {"*"}; + policy_item item; + item.access_types.insert(access_type::ALL); Review Comment: `users` field of policy_item is left as empty, is it what we expect? ########## src/runtime/ranger/ranger_resource_policy.h: ########## @@ -0,0 +1,112 @@ +// 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 { + +// ACL type defined in Range service for RPC matching policy +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) + +// Ranger policy data structure +struct policy_item +{ + std::set<access_type> access_types; Review Comment: Use bitmap instead of std::set, `match` is a very frequently called function in hot path, we must ensure it's performance. It's recommend to add a simple benchmark test for `match`. ########## src/runtime/ranger/ranger_resource_policy.h: ########## @@ -0,0 +1,112 @@ +// 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 { + +// ACL type defined in Range service for RPC matching policy +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) + +// Ranger policy data structure +struct policy_item +{ + std::set<access_type> access_types; + std::set<std::string> users; + + DEFINE_JSON_SERIALIZATION(access_types, users); + + // Match a policy according to acl_type and user_name + bool match(const access_type &ac_type, const std::string &user_name) const; +}; + +// Data structure of policies with different priorities +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. Review Comment: ```suggestion // Check whether the 'user_name' is allowed to access the resource by type of 'ac_type'. ``` -- 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]
