From a9b1f63cf88e45fe54e7015a742201dedd716978 Mon Sep 17 00:00:00 2001 From: huerni <47264950+huerni@users.noreply.github.com> Date: Thu, 21 Nov 2024 14:43:47 +0800 Subject: [PATCH] refactor: login --- protos/Crane.proto | 16 ++++-- protos/PublicDefs.proto | 61 +++++++++++---------- src/CraneCtld/AccountManager.cpp | 45 +++------------ src/CraneCtld/AccountManager.h | 2 +- src/CraneCtld/RpcService/CtldGrpcServer.cpp | 12 ++-- src/CraneCtld/TaskScheduler.cpp | 4 +- 6 files changed, 59 insertions(+), 81 deletions(-) diff --git a/protos/Crane.proto b/protos/Crane.proto index ae2c0780..2d87e9fa 100644 --- a/protos/Crane.proto +++ b/protos/Crane.proto @@ -754,7 +754,7 @@ message LoginRequest { message LoginReply { bool ok = 1; string token = 2; - string reason = 3; + ErrCode reason = 3; } // Todo: Divide service into two parts: one for Craned and one for Crun @@ -779,10 +779,18 @@ service CraneCtld { rpc AddUser(AddUserRequest) returns (AddUserReply); rpc AddQos(AddQosRequest) returns (AddQosReply); - rpc DeleteEntity(DeleteEntityRequest) returns (DeleteEntityReply); + rpc DeleteAccount(DeleteAccountRequest) returns (DeleteAccountReply); + rpc DeleteUser(DeleteUserRequest) returns (DeleteUserReply); + rpc DeleteQos(DeleteQosRequest) returns (DeleteQosReply); + + rpc QueryAccountInfo(QueryAccountInfoRequest) returns (QueryAccountInfoReply); + rpc QueryUserInfo(QueryUserInfoRequest) returns (QueryUserInfoReply); + rpc QueryQosInfo(QueryQosInfoRequest) returns (QueryQosInfoReply); - rpc QueryEntityInfo(QueryEntityInfoRequest) returns (QueryEntityInfoReply); - rpc ModifyEntity(ModifyEntityRequest) returns (ModifyEntityReply); + rpc ModifyAccount(ModifyAccountRequest) returns (ModifyAccountReply); + rpc ModifyUser(ModifyUserRequest) returns (ModifyUserReply); + rpc ModifyQos(ModifyQosRequest) returns (ModifyQosReply); + rpc BlockAccountOrUser(BlockAccountOrUserRequest) returns (BlockAccountOrUserReply); /* RPCs called from cinfo */ diff --git a/protos/PublicDefs.proto b/protos/PublicDefs.proto index 0dbbce55..a422e3c2 100644 --- a/protos/PublicDefs.proto +++ b/protos/PublicDefs.proto @@ -329,36 +329,37 @@ enum ErrCode { ERR_INVALID_ADMIN_LEVEL = 10007; ERR_USER_ACCOUNT_MISMATCH = 10008; ERR_NO_ACCOUNT_SPECIFIED = 10009; - - ERR_INVALID_ACCOUNT = 10010; - ERR_DUPLICATE_ACCOUNT = 10011; - ERR_INVALID_PARENTACCOUNT = 10012; - ERR_DELETE_ACCOUNT = 10013; - - ERR_INVALID_PARTITION = 10014; - ERR_ALLOWED_PARTITION = 10015; - ERR_DUPLICATE_PARTITION = 10016; - ERR_PARENT_ALLOWED_PARTITION = 10017; - ERR_USER_EMPTY_PARTITION = 10018; - ERR_CHILD_HAS_PARTITION = 10019; - - ERR_INVALID_QOS = 10020; - ERR_DB_DUPLICATE_QOS = 10021; - ERR_DELETE_QOS = 10022; - ERR_CONVERT_TO_INTERGER = 10023; - ERR_TIME_LIMIT = 10024; - ERR_ALLOWED_QOS = 10025; - ERR_DUPLICATE_QOS = 10026; - ERR_PARENT_ALLOWED_QOS = 10027; - ERR_SET_ALLOWED_QOS = 10028; - ERR_ALLOWED_DEFAULT_QOS = 10029; - ERR_DUPLICATE_DEFAULT_QOS = 10030; - ERR_CHILD_HAS_DEFAULT_QOS = 10031; - ERR_SET_ACCOUNT_QOS = 10032; - ERR_SET_DEFAULT_QOS = 10033; - ERR_IS_DEFAULT_QOS = 10034; - - ERR_UPDATE_DATABASE = 10035; + ERR_PASSWORD_MISMATCH = 100010; + + ERR_INVALID_ACCOUNT = 10011; + ERR_DUPLICATE_ACCOUNT = 10012; + ERR_INVALID_PARENTACCOUNT = 10013; + ERR_DELETE_ACCOUNT = 10014; + + ERR_INVALID_PARTITION = 10015; + ERR_ALLOWED_PARTITION = 10016; + ERR_DUPLICATE_PARTITION = 10017; + ERR_PARENT_ALLOWED_PARTITION = 10018; + ERR_USER_EMPTY_PARTITION = 10019; + ERR_CHILD_HAS_PARTITION = 10020; + + ERR_INVALID_QOS = 10021; + ERR_DB_DUPLICATE_QOS = 10022; + ERR_DELETE_QOS = 10023; + ERR_CONVERT_TO_INTERGER = 10024; + ERR_TIME_LIMIT = 10025; + ERR_ALLOWED_QOS = 10026; + ERR_DUPLICATE_QOS = 10027; + ERR_PARENT_ALLOWED_QOS = 10028; + ERR_SET_ALLOWED_QOS = 10029; + ERR_ALLOWED_DEFAULT_QOS = 10030; + ERR_DUPLICATE_DEFAULT_QOS = 10031; + ERR_CHILD_HAS_DEFAULT_QOS = 10032; + ERR_SET_ACCOUNT_QOS = 10033; + ERR_SET_DEFAULT_QOS = 10034; + ERR_IS_DEFAULT_QOS = 10035; + + ERR_UPDATE_DATABASE = 10036; ERR_GENERIC_FAILURE = 10100; ERR_NO_RESOURCE = 10101; diff --git a/src/CraneCtld/AccountManager.cpp b/src/CraneCtld/AccountManager.cpp index 81987f78..f92f6852 100644 --- a/src/CraneCtld/AccountManager.cpp +++ b/src/CraneCtld/AccountManager.cpp @@ -18,7 +18,6 @@ #include "AccountManager.h" -#include "CtldPublicDefs.h" #include "crane/Jwt.h" #include "crane/PasswordEntry.h" #include "protos/PublicDefs.pb.h" @@ -28,53 +27,23 @@ namespace Ctld { AccountManager::AccountManager() { InitDataMap_(); } -AccountManager::Result AccountManager::Login(uint32_t uid, - const std::string& password) { +AccountManager::CraneExpected AccountManager::Login( + uint32_t uid, const std::string& password) { util::read_lock_guard user_guard(m_rw_user_mutex_); - PasswordEntry entry(uid); - if (!entry.Valid()) { - return Result{false, fmt::format("Uid {} not existed", uid)}; - } + auto user_result = GetUserInfoByUidNoLock_(uid); + if (!user_result) return std::unexpected(user_result.error()); + const User* user = user_result.value(); - const User* user = GetExistedUserInfoNoLock_(entry.Username()); - if (!user) { - return Result{false, "user not existed"}; - } if (password != user->password) { - return Result{false, "Incorrect password"}; + return std::unexpected(CraneErrCode::ERR_PASSWORD_MISMATCH); } std::unordered_map claims{ {"UID", std::to_string(uid)}}; const std::string& token = util::GenerateToken(g_config.ListenConf.JwtSecretContent, claims); - return Result{true, token}; -} - -AccountManager::Result AccountManager::Login(uint32_t uid, - const std::string& password) { - util::read_lock_guard user_guard(m_rw_user_mutex_); - - PasswordEntry entry(uid); - if (!entry.Valid()) { - return Result{false, fmt::format("Uid {} not existed", uid)}; - } - - const User* user = GetExistedUserInfoNoLock_(entry.Username()); - if (!user) { - return Result{false, "user not existed"}; - } - - if (password != user->password) { - return Result{false, "Incorrect password"}; - } - std::unordered_map claims{ - {"UID", std::to_string(uid)}}; - const std::string& token = - util::GenerateToken(g_config.JwtSecretContent, claims); - - return Result{true, token}; + return token; } AccountManager::CraneExpected AccountManager::AddUser( diff --git a/src/CraneCtld/AccountManager.h b/src/CraneCtld/AccountManager.h index 015906a4..df3ef9a3 100644 --- a/src/CraneCtld/AccountManager.h +++ b/src/CraneCtld/AccountManager.h @@ -49,7 +49,7 @@ class AccountManager { ~AccountManager() = default; - Result Login(uint32_t uid, const std::string& password); + CraneExpected Login(uint32_t uid, const std::string& password); CraneExpected AddUser(uint32_t uid, const User& new_user); diff --git a/src/CraneCtld/RpcService/CtldGrpcServer.cpp b/src/CraneCtld/RpcService/CtldGrpcServer.cpp index 7392b7c1..c050d6af 100644 --- a/src/CraneCtld/RpcService/CtldGrpcServer.cpp +++ b/src/CraneCtld/RpcService/CtldGrpcServer.cpp @@ -252,14 +252,14 @@ grpc::Status CraneCtldServiceImpl::QueryTasksInfo( grpc::Status CraneCtldServiceImpl::Login( grpc::ServerContext *context, const crane::grpc::LoginRequest *request, crane::grpc::LoginReply *response) { - AccountManager::Result result = - g_account_manager->Login(request->uid(), request->password()); + auto result = g_account_manager->Login(request->uid(), request->password()); - response->set_ok(result.ok); - if (result.ok) { - response->set_token(result.reason); + if (result) { + response->set_ok(true); + response->set_token(result.value()); } else { - response->set_reason(result.reason); + response->set_ok(false); + response->set_reason(result.error()); } return grpc::Status::OK; } diff --git a/src/CraneCtld/TaskScheduler.cpp b/src/CraneCtld/TaskScheduler.cpp index ed556c53..dade95ef 100644 --- a/src/CraneCtld/TaskScheduler.cpp +++ b/src/CraneCtld/TaskScheduler.cpp @@ -1068,8 +1068,8 @@ TaskScheduler::SubmitTaskToScheduler(std::unique_ptr task) { task->Username(), task->partition_id, task->account)); } - auto enable_res = - g_account_manager->CheckEnableState(task->account, task->Username()); + auto enable_res = g_account_manager->CheckIfUserOfAccountIsEnabled( + task->Username(), task->account); if (enable_res.has_error()) { return result::fail(enable_res.error()); }