This repository has been archived by the owner on Jun 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(security): implement meta server access controller (#655)
- Loading branch information
Showing
18 changed files
with
430 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// 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 "access_controller.h" | ||
|
||
#include <dsn/utility/flags.h> | ||
#include <dsn/utility/strings.h> | ||
#include <dsn/utility/smart_pointers.h> | ||
#include "meta_access_controller.h" | ||
|
||
namespace dsn { | ||
namespace security { | ||
DSN_DEFINE_bool("security", enable_acl, false, "whether enable access controller or not"); | ||
DSN_DEFINE_string("security", super_users, "", "super user for access controller"); | ||
|
||
access_controller::access_controller() { utils::split_args(FLAGS_super_users, _super_users, ','); } | ||
|
||
access_controller::~access_controller() {} | ||
|
||
bool access_controller::pre_check(const std::string &user_name) | ||
{ | ||
if (!FLAGS_enable_acl || _super_users.find(user_name) != _super_users.end()) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
std::unique_ptr<access_controller> create_meta_access_controller() | ||
{ | ||
return make_unique<meta_access_controller>(); | ||
} | ||
} // namespace security | ||
} // namespace dsn |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// 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 <memory> | ||
#include <unordered_set> | ||
|
||
namespace dsn { | ||
class message_ex; | ||
namespace security { | ||
|
||
class access_controller | ||
{ | ||
public: | ||
access_controller(); | ||
virtual ~access_controller() = 0; | ||
|
||
/** | ||
* reset the access controller | ||
* acls - the new acls to reset | ||
**/ | ||
virtual void reset(const std::string &acls){}; | ||
|
||
/** | ||
* check if the message received is allowd to do something. | ||
* msg - the message received | ||
**/ | ||
virtual bool allowed(message_ex *msg) = 0; | ||
|
||
protected: | ||
bool pre_check(const std::string &user_name); | ||
friend class meta_access_controller_test; | ||
|
||
std::unordered_set<std::string> _super_users; | ||
}; | ||
|
||
std::unique_ptr<access_controller> create_meta_access_controller(); | ||
} // namespace security | ||
} // namespace dsn |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// 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 "meta_access_controller.h" | ||
|
||
#include <dsn/tool-api/rpc_message.h> | ||
#include <dsn/utility/flags.h> | ||
#include <dsn/tool-api/network.h> | ||
#include <dsn/dist/fmt_logging.h> | ||
|
||
namespace dsn { | ||
namespace security { | ||
DSN_DEFINE_string("security", | ||
meta_acl_rpc_allow_list, | ||
"", | ||
"allowed list of rpc codes for meta_access_controller"); | ||
|
||
meta_access_controller::meta_access_controller() | ||
{ | ||
// MetaServer serves the allow-list RPC from all users. RPCs unincluded are accessible to only | ||
// superusers. | ||
if (strlen(FLAGS_meta_acl_rpc_allow_list) == 0) { | ||
register_allowed_list("RPC_CM_LIST_APPS"); | ||
register_allowed_list("RPC_CM_LIST_NODES"); | ||
register_allowed_list("RPC_CM_CLUSTER_INFO"); | ||
register_allowed_list("RPC_CM_QUERY_PARTITION_CONFIG_BY_INDEX"); | ||
} else { | ||
std::vector<std::string> rpc_code_white_list; | ||
utils::split_args(FLAGS_meta_acl_rpc_allow_list, rpc_code_white_list, ','); | ||
for (const auto &rpc_code : rpc_code_white_list) { | ||
register_allowed_list(rpc_code); | ||
} | ||
} | ||
} | ||
|
||
bool meta_access_controller::allowed(message_ex *msg) | ||
{ | ||
if (pre_check(msg->io_session->get_client_username()) || | ||
_allowed_rpc_code_list.find(msg->rpc_code().code()) != _allowed_rpc_code_list.end()) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
void meta_access_controller::register_allowed_list(const std::string &rpc_code) | ||
{ | ||
auto code = task_code::try_get(rpc_code, TASK_CODE_INVALID); | ||
dassert_f(code != TASK_CODE_INVALID, | ||
"invalid task code({}) in rpc_code_white_list of security section", | ||
rpc_code); | ||
|
||
_allowed_rpc_code_list.insert(code); | ||
} | ||
} // namespace security | ||
} // namespace dsn |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// 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 "access_controller.h" | ||
|
||
#include <unordered_set> | ||
|
||
namespace dsn { | ||
class message_ex; | ||
namespace security { | ||
|
||
class meta_access_controller : public access_controller | ||
{ | ||
public: | ||
meta_access_controller(); | ||
bool allowed(message_ex *msg) override; | ||
|
||
private: | ||
void register_allowed_list(const std::string &rpc_code); | ||
|
||
std::unordered_set<int> _allowed_rpc_code_list; | ||
}; | ||
} // namespace security | ||
} // namespace dsn |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.