Skip to content

Commit 60d444f

Browse files
committed
refactor
1 parent e52fcbe commit 60d444f

5 files changed

+22
-18
lines changed

src/CraneCtld/AccountManager.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -1014,6 +1014,8 @@ AccountManager::CraneExpected<void> AccountManager::CheckIfUidHasPermOnUser(
10141014
auto user_result = GetUserInfoByUidNoLock_(uid);
10151015
if (!user_result) return std::unexpected(user_result.error());
10161016
const User* op_user = user_result.value();
1017+
result = CheckIfUserHasHigherPrivThan_(*op_user, User::None);
1018+
if (!result) return result;
10171019

10181020
for (const auto& account_name : accounts) {
10191021
const Account* account = GetAccountInfoNoLock_(account_name);

src/CraneCtld/CraneCtld.cpp

+5-3
Original file line numberDiff line numberDiff line change
@@ -460,15 +460,17 @@ void ParseConfig(int argc, char** argv) {
460460

461461
if (partition["AllowedAccounts"] &&
462462
!partition["AllowedAccounts"].IsNull()) {
463-
std::string allowed_accounts_str =
463+
auto allowed_accounts_str =
464464
partition["AllowedAccounts"].as<std::string>();
465465
std::vector<std::string> allowed_accounts =
466466
absl::StrSplit(absl::StripAsciiWhitespace(allowed_accounts_str).data(), ",");
467467
for (const auto& account_name : allowed_accounts) {
468468
part.allowed_accounts.insert(account_name);
469469
}
470-
} else if (partition["DeniedAccounts"] && !partition["DeniedAccounts"].IsNull()) {
471-
std::string denied_accounts_str = partition["DeniedAccounts"].as<std::string>();
470+
}
471+
472+
if (partition["DeniedAccounts"] && !partition["DeniedAccounts"].IsNull()) {
473+
auto denied_accounts_str = partition["DeniedAccounts"].as<std::string>();
472474
std::vector<std::string> denied_accounts = absl::StrSplit(absl::StripAsciiWhitespace(denied_accounts_str).data(), ",");
473475
for (const auto& account_name : denied_accounts) {
474476
part.denied_accounts.insert(account_name);

src/CraneCtld/CranedMetaContainer.cpp

+10-8
Original file line numberDiff line numberDiff line change
@@ -608,33 +608,35 @@ crane::grpc::ModifyCranedStateReply CranedMetaContainer::ChangeNodeState(
608608

609609
if (is_modify_allowed) {
610610
allowed_accounts = accounts;
611-
denied_accounts.clear();
612-
} else if (allowed_accounts.empty()) {
613-
// When allowed_accounts is in use, denied_accounts cannot be modified.
611+
} else {
614612
denied_accounts = accounts;
615613
}
616614

617615
return result;
618616
}
619617

620-
bool CranedMetaContainer::CheckIfAccountIsAllowedInPartition(
618+
std::expected<void, std::string> CranedMetaContainer::CheckIfAccountIsAllowedInPartition(
621619
const std::string& partition_name, const std::string& account_name) {
622620
auto part_metas_map = partition_metas_map_.GetMapSharedPtr();
623621

624-
if (!part_metas_map->contains(partition_name)) return false;
622+
if (!part_metas_map->contains(partition_name)) return std::unexpected("Partition does not exist.");
625623

626624
auto part_meta = part_metas_map->at(partition_name).GetExclusivePtr();
627625
const auto& allowed_accounts = part_meta->partition_global_meta.allowed_accounts;
628626
if (!allowed_accounts.empty()) {
629627
if (!allowed_accounts.contains(account_name))
630-
return false;
628+
return std::unexpected(
629+
"The account is not in the AllowedAccounts of the partition "
630+
"specified for the task, submission of the task is prohibited.");
631631
} else {
632632
const auto& denied_accounts = part_meta->partition_global_meta.denied_accounts;
633633
if (denied_accounts.contains(account_name))
634-
return false;
634+
return std::unexpected(
635+
"The account is in the DeniedAccounts of the partition "
636+
"specified for the task, submission of the task is prohibited.");
635637
}
636638

637-
return true;
639+
return {};
638640
}
639641

640642
void CranedMetaContainer::AddDedicatedResource(

src/CraneCtld/CranedMetaContainer.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ class CranedMetaContainer final {
9595
bool is_modify_allowed,
9696
const std::unordered_set<std::string>& accounts);
9797

98-
bool CheckIfAccountIsAllowedInPartition(const std::string& partition_name,
98+
std::expected<void, std::string> CheckIfAccountIsAllowedInPartition(const std::string& partition_name,
9999
const std::string& account_name);
100100

101101
void CranedUp(const CranedId& craned_id);

src/CraneCtld/CtldGrpcServer.cpp

+4-6
Original file line numberDiff line numberDiff line change
@@ -1014,12 +1014,10 @@ CtldServer::SubmitTaskToScheduler(std::unique_ptr<TaskInCtld> task) {
10141014
return std::unexpected(enable_res.error());
10151015
}
10161016

1017-
if (!g_meta_container->CheckIfAccountIsAllowedInPartition(task->partition_id,
1018-
task->account))
1019-
return std::unexpected(
1020-
"The account is not in the AllowedAccounts of the partition "
1021-
"specified "
1022-
"for the task, submission of the task is prohibited.");
1017+
auto result = g_meta_container->CheckIfAccountIsAllowedInPartition(task->partition_id,
1018+
task->account);
1019+
if (!result)
1020+
return std::unexpected(result.error());
10231021

10241022
err = g_task_scheduler->AcquireTaskAttributes(task.get());
10251023

0 commit comments

Comments
 (0)