Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
huerni committed Jan 18, 2025
1 parent d27f33b commit e7314d1
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 18 deletions.
2 changes: 2 additions & 0 deletions src/CraneCtld/AccountManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1014,6 +1014,8 @@ AccountManager::CraneExpected<void> AccountManager::CheckIfUidHasPermOnUser(
auto user_result = GetUserInfoByUidNoLock_(uid);
if (!user_result) return std::unexpected(user_result.error());
const User* op_user = user_result.value();
result = CheckIfUserHasHigherPrivThan_(*op_user, User::None);
if (!result) return result;

for (const auto& account_name : accounts) {
const Account* account = GetAccountInfoNoLock_(account_name);
Expand Down
8 changes: 5 additions & 3 deletions src/CraneCtld/CraneCtld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -460,15 +460,17 @@ void ParseConfig(int argc, char** argv) {

if (partition["AllowedAccounts"] &&
!partition["AllowedAccounts"].IsNull()) {
std::string allowed_accounts_str =
auto allowed_accounts_str =
partition["AllowedAccounts"].as<std::string>();
std::vector<std::string> allowed_accounts =
absl::StrSplit(absl::StripAsciiWhitespace(allowed_accounts_str).data(), ",");
for (const auto& account_name : allowed_accounts) {
part.allowed_accounts.insert(account_name);
}
} else if (partition["DeniedAccounts"] && !partition["DeniedAccounts"].IsNull()) {
std::string denied_accounts_str = partition["DeniedAccounts"].as<std::string>();
}

if (partition["DeniedAccounts"] && !partition["DeniedAccounts"].IsNull()) {
auto denied_accounts_str = partition["DeniedAccounts"].as<std::string>();
std::vector<std::string> denied_accounts = absl::StrSplit(absl::StripAsciiWhitespace(denied_accounts_str).data(), ",");
for (const auto& account_name : denied_accounts) {
part.denied_accounts.insert(account_name);
Expand Down
18 changes: 10 additions & 8 deletions src/CraneCtld/CranedMetaContainer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -608,33 +608,35 @@ crane::grpc::ModifyCranedStateReply CranedMetaContainer::ChangeNodeState(

if (is_modify_allowed) {
allowed_accounts = accounts;
denied_accounts.clear();
} else if (allowed_accounts.empty()) {
// When allowed_accounts is in use, denied_accounts cannot be modified.
} else {
denied_accounts = accounts;
}

return result;
}

bool CranedMetaContainer::CheckIfAccountIsAllowedInPartition(
std::expected<void, std::string> CranedMetaContainer::CheckIfAccountIsAllowedInPartition(
const std::string& partition_name, const std::string& account_name) {
auto part_metas_map = partition_metas_map_.GetMapSharedPtr();

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

auto part_meta = part_metas_map->at(partition_name).GetExclusivePtr();
const auto& allowed_accounts = part_meta->partition_global_meta.allowed_accounts;
if (!allowed_accounts.empty()) {
if (!allowed_accounts.contains(account_name))
return false;
return std::unexpected(
"The account is not in the AllowedAccounts of the partition "
"specified for the task, submission of the task is prohibited.");
} else {
const auto& denied_accounts = part_meta->partition_global_meta.denied_accounts;
if (denied_accounts.contains(account_name))
return false;
return std::unexpected(
"The account is in the DeniedAccounts of the partition "
"specified for the task, submission of the task is prohibited.");
}

return true;
return {};
}

void CranedMetaContainer::AddDedicatedResource(
Expand Down
2 changes: 1 addition & 1 deletion src/CraneCtld/CranedMetaContainer.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class CranedMetaContainer final {
bool is_modify_allowed,
const std::unordered_set<std::string>& accounts);

bool CheckIfAccountIsAllowedInPartition(const std::string& partition_name,
std::expected<void, std::string> CheckIfAccountIsAllowedInPartition(const std::string& partition_name,
const std::string& account_name);

void CranedUp(const CranedId& craned_id);
Expand Down
10 changes: 4 additions & 6 deletions src/CraneCtld/CtldGrpcServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1014,12 +1014,10 @@ CtldServer::SubmitTaskToScheduler(std::unique_ptr<TaskInCtld> task) {
return std::unexpected(enable_res.error());
}

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

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

Expand Down

0 comments on commit e7314d1

Please sign in to comment.