Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add nameserver auth #3835

Merged
merged 62 commits into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
62 commits
Select commit Hold shift + click to select a range
f7fc985
add brpc authenticator
oh2024 Mar 20, 2024
9950ffa
fix: add auth library
oh2024 Mar 21, 2024
c4ab632
fix: make authenticator lifetime same as channel
oh2024 Mar 21, 2024
d402fd3
refactor: use global variables to pass auth tokens
oh2024 Mar 22, 2024
3769917
feat: added basic server auth backwards compatible with user = root a…
oh2024 Mar 26, 2024
7f19ade
chore: remove debug log
oh2024 Mar 26, 2024
5569b90
chore: apply lint style fixes
oh2024 Mar 26, 2024
466a5c4
feat: encrypt password before sending
oh2024 Mar 26, 2024
269220e
fix: add namespace for Authenticator class
oh2024 Mar 26, 2024
8f2a779
chore: add newline
oh2024 Mar 26, 2024
f210a9b
feat: add user verification logic
oh2024 Mar 27, 2024
3fb9819
feat: add nameserver server side auth
zhangziheng01233 Apr 2, 2024
cb76d88
chore: remove unneccessary logs
zhangziheng01233 Apr 2, 2024
e21fb53
chore: lint
zhangziheng01233 Apr 2, 2024
d368ab0
chore: make refreshable map header only
zhangziheng01233 Apr 2, 2024
bdf8af6
chore: remove old refreshable map file
zhangziheng01233 Apr 2, 2024
b84d32b
chore: lint
zhangziheng01233 Apr 2, 2024
faf6f08
chore: lint
zhangziheng01233 Apr 2, 2024
816409f
chore: lint
zhangziheng01233 Apr 2, 2024
e11bdb4
sync with db in user access manager constructor
zhangziheng01233 Apr 2, 2024
a4c9b30
chore: add license
oh2024 Apr 7, 2024
51dd42b
chore: add license
oh2024 Apr 7, 2024
70621a2
feat: default g_auth_token user to root if not provided
oh2024 Apr 7, 2024
64ed7bd
fix: user table start in cluster mode
oh2024 Apr 7, 2024
c98b20b
feat: sync user data every 100ms
oh2024 Apr 7, 2024
93d38d1
fix: add auth library for linking
oh2024 Apr 7, 2024
ad01ac9
fix: refreshable map test
oh2024 Apr 7, 2024
197450c
Merge remote-tracking branch 'upstream/main' into feat/server-auth
oh2024 Apr 7, 2024
007c237
fix: add main method to refreshable map test
oh2024 Apr 8, 2024
fb75477
fix: add auth to sdk
oh2024 Apr 8, 2024
bb2a332
eaat: treat omitted host as localhost
oh2024 Apr 8, 2024
ae7dc86
feat: treat % as all hosts
oh2024 Apr 8, 2024
0d2c030
fix: minicluster auth
oh2024 Apr 9, 2024
bc17c64
fix: remove api server auth
oh2024 Apr 9, 2024
fb69095
Merge remote-tracking branch 'upstream/main' into feat/server-auth
oh2024 Apr 10, 2024
e61751f
fix: change scala test password
oh2024 Apr 10, 2024
fe88685
fix: default root as user in python sdk
oh2024 Apr 10, 2024
44aec00
fix: wait for nameservers to pick up auth data before starting api se…
oh2024 Apr 10, 2024
1b00199
fix: wait 5 seconds after nameserver launch for auth data to be picke…
oh2024 Apr 10, 2024
6af6756
fix: minicluster options scope
oh2024 Apr 10, 2024
d926903
test: cat host file
oh2024 Apr 10, 2024
3de4089
fix: remove cat
oh2024 Apr 11, 2024
335b55e
fix: Init should finish after user table is available
oh2024 Apr 11, 2024
36f362c
fix: always create user table
oh2024 Apr 11, 2024
a6aab82
fix: create user table after internal db create
oh2024 Apr 11, 2024
47c1e53
fix: add partition info setting for user table create
oh2024 Apr 12, 2024
e5abb0c
Merge remote-tracking branch 'upstream/main' into feat/server-auth
oh2024 Apr 15, 2024
2270869
fix: auth related tests
oh2024 Apr 15, 2024
78e2c3f
fix: support multiple nameservers
oh2024 Apr 16, 2024
35b1167
fix: recover db and table only for cluster mode
oh2024 Apr 16, 2024
806051f
fix: remote test ns tablet start order
oh2024 Apr 16, 2024
1e07e5d
fix: set root as default user in python sdk
oh2024 Apr 16, 2024
cb17484
fix: SyncTableReplicaCluster and AddAndRemoveReplicaCluster tests
oh2024 Apr 17, 2024
93317c8
fix: remove user root
oh2024 Apr 17, 2024
0a817a5
Merge remote-tracking branch 'upstream/main' into feat/server-auth
oh2024 Apr 17, 2024
790094e
fix: add erver stops to name_server_test cases and spilt sql availabi…
oh2024 Apr 17, 2024
9307f12
fix: nameserver test scope
oh2024 Apr 17, 2024
c3cb35a
fix: brpc authenticator destruction before nameserver
oh2024 Apr 17, 2024
951afbf
Merge remote-tracking branch 'upstream/main' into feat/server-auth
oh2024 Apr 18, 2024
5f79ed9
Merge remote-tracking branch 'upstream/main' into feat/server-auth
oh2024 Apr 18, 2024
18c0e2e
Merge branch 'main' into feat/server-auth
oh2024 Apr 18, 2024
5debf64
test: change test names
oh2024 Apr 18, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ class OpenmldbBatchConfig extends Serializable {
var openmldbUser = "root"

@ConfigOption(name = "openmldb.password", doc = "The password of OpenMLDB")
var openmldbPassword = "root"
var openmldbPassword = ""

@ConfigOption(name = "openmldb.default.db", doc = "The default database for OpenMLDB SQL")
var defaultDb = "default_db"
Expand Down
7 changes: 4 additions & 3 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ endfunction(compile_lib)

set(TEST_LIBS
openmldb_test_base apiserver nameserver tablet query_response_time openmldb_sdk
openmldb_catalog client zk_client storage schema replica openmldb_codec base openmldb_proto log
openmldb_catalog client zk_client storage schema replica openmldb_codec base auth openmldb_proto log
common zookeeper_mt tcmalloc_minimal ${RocksDB_LIB} ${VM_LIBS} ${LLVM_LIBS} ${ZETASQL_LIBS} ${BRPC_LIBS})
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS "9.1")
# GNU implementation prior to 9.1 requires linking with -lstdc++fs
Expand Down Expand Up @@ -123,7 +123,7 @@ set_property(
)

add_library(openmldb_flags flags.cc)

compile_lib(auth auth "")
compile_lib(openmldb_codec codec "")
compile_lib(openmldb_catalog catalog "")
compile_lib(schema schema "")
Expand All @@ -141,7 +141,7 @@ compile_lib(apiserver apiserver "")
find_package(yaml-cpp REQUIRED)
set(yaml_libs yaml-cpp)

set(BUILTIN_LIBS apiserver nameserver tablet query_response_time openmldb_sdk openmldb_catalog client zk_client replica base storage openmldb_codec schema openmldb_proto log ${RocksDB_LIB})
set(BUILTIN_LIBS apiserver nameserver tablet query_response_time openmldb_sdk openmldb_catalog client zk_client replica base storage openmldb_codec schema openmldb_proto log auth ${RocksDB_LIB})
set(BIN_LIBS ${BUILTIN_LIBS}
common zookeeper_mt tcmalloc_minimal
${VM_LIBS}
Expand All @@ -152,6 +152,7 @@ ${BRPC_LIBS})
if(TESTING_ENABLE)
add_subdirectory(test)
compile_test(cmd)
compile_test(auth)
compile_test(base)
compile_test(codec)
compile_test(zk)
Expand Down
21 changes: 21 additions & 0 deletions src/auth/auth_utils.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright 2021 4Paradigm
*
* Licensed 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 "auth_utils.h"

namespace openmldb::auth {
std::string FormUserHost(const std::string& username, const std::string& host) { return username + "@" + host; }
} // namespace openmldb::auth
26 changes: 26 additions & 0 deletions src/auth/auth_utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright 2021 4Paradigm
*
* Licensed 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.
*/

#ifndef SRC_AUTH_AUTH_UTILS_H_
#define SRC_AUTH_AUTH_UTILS_H_

#include <string>

namespace openmldb::auth {
std::string FormUserHost(const std::string& username, const std::string& host);
} // namespace openmldb::auth

#endif // SRC_AUTH_AUTH_UTILS_H_
70 changes: 70 additions & 0 deletions src/auth/brpc_authenticator.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright 2021 4Paradigm
*
* Licensed 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 "brpc_authenticator.h"

#include "auth_utils.h"
#include "butil/endpoint.h"

namespace openmldb::authn {

int BRPCAuthenticator::GenerateCredential(std::string* auth_str) const {
std::visit(
[auth_str](const auto& s) {
using T = std::decay_t<decltype(s)>;
if constexpr (std::is_same_v<T, UserToken>) {
*auth_str = "u" + s.user + ":" + s.password;
} else if constexpr (std::is_same_v<T, ServiceToken>) {
*auth_str = "s" + s.token;
}
},
auth_token_);
return 0;
}

int BRPCAuthenticator::VerifyCredential(const std::string& auth_str, const butil::EndPoint& client_addr,
brpc::AuthContext* out_ctx) const {
if (auth_str.length() < 2) {
return -1;
}

char auth_type = auth_str[0];
std::string credential = auth_str.substr(1);
if (auth_type == 'u') {
size_t pos = credential.find(':');
if (pos == std::string::npos) {
return -1;
}
auto host = butil::ip2str(client_addr.ip).c_str();
std::string username = credential.substr(0, pos);
std::string password = credential.substr(pos + 1);
if (is_authenticated_(host, username, password)) {
out_ctx->set_user(auth::FormUserHost(username, host));
out_ctx->set_is_service(false);
return 0;
}
} else if (auth_type == 's') {
if (VerifyToken(credential)) {
out_ctx->set_is_service(true);
return 0;
}
}
return -1;
}

bool BRPCAuthenticator::VerifyToken(const std::string& token) const { return token == "default"; }

} // namespace openmldb::authn
63 changes: 63 additions & 0 deletions src/auth/brpc_authenticator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright 2021 4Paradigm
*
* Licensed 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.
*/

#ifndef SRC_AUTH_BRPC_AUTHENTICATOR_H_
#define SRC_AUTH_BRPC_AUTHENTICATOR_H_
#include <functional>
#include <string>
#include <utility>
#include <variant>

#include "brpc/authenticator.h"

namespace openmldb::authn {

struct ServiceToken {
std::string token;
};

struct UserToken {
std::string user, password;
};

using AuthToken = std::variant<ServiceToken, UserToken>;

class BRPCAuthenticator : public brpc::Authenticator {
public:
using IsAuthenticatedFunc = std::function<bool(const std::string&, const std::string&, const std::string&)>;

BRPCAuthenticator() {
is_authenticated_ = [](const std::string& host, const std::string& username, const std::string& password) {
return true;
};
}

explicit BRPCAuthenticator(const AuthToken auth_token) : auth_token_(auth_token) {}

explicit BRPCAuthenticator(IsAuthenticatedFunc is_authenticated) : is_authenticated_(std::move(is_authenticated)) {}

int GenerateCredential(std::string* auth_str) const override;
int VerifyCredential(const std::string& auth_str, const butil::EndPoint& client_addr,
brpc::AuthContext* out_ctx) const override;

private:
AuthToken auth_token_ = openmldb::authn::ServiceToken{"default"};
IsAuthenticatedFunc is_authenticated_;
bool VerifyToken(const std::string& token) const;
};

} // namespace openmldb::authn
#endif // SRC_AUTH_BRPC_AUTHENTICATOR_H_
52 changes: 52 additions & 0 deletions src/auth/refreshable_map.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright 2021 4Paradigm
*
* Licensed 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.
*/

#ifndef SRC_AUTH_REFRESHABLE_MAP_H_
#define SRC_AUTH_REFRESHABLE_MAP_H_

#include <memory>
#include <mutex>
#include <optional>
#include <shared_mutex>
#include <unordered_map>
#include <utility>

namespace openmldb::auth {

template <typename Key, typename Value>
class RefreshableMap {
public:
std::optional<Value> Get(const Key& key) const {
std::shared_lock<std::shared_mutex> lock(mutex_);
if (auto it = map_->find(key); it != map_->end()) {
return it->second;
}
return std::nullopt;
}

void Refresh(std::unique_ptr<std::unordered_map<Key, Value>> new_map) {
std::unique_lock<std::shared_mutex> lock(mutex_);
map_ = std::move(new_map);
}

private:
mutable std::shared_mutex mutex_;
std::shared_ptr<std::unordered_map<Key, Value>> map_;
};

} // namespace openmldb::auth

#endif // SRC_AUTH_REFRESHABLE_MAP_H_
Loading
Loading