Skip to content

Commit

Permalink
Improve code style for auth in ExecuteCommands (apache#2096)
Browse files Browse the repository at this point in the history
  • Loading branch information
PragmaTwice authored Feb 10, 2024
1 parent 58d177f commit af18420
Showing 1 changed file with 12 additions and 14 deletions.
26 changes: 12 additions & 14 deletions src/server/redis_connection.cc
Original file line number Diff line number Diff line change
Expand Up @@ -428,30 +428,29 @@ void Connection::ExecuteCommands(std::deque<CommandTokens> *to_process_cmds) {
}
auto current_cmd = std::move(*cmd_s);

if (GetNamespace().empty()) {
if (!password.empty() && util::ToLower(cmd_tokens.front()) != "auth" &&
util::ToLower(cmd_tokens.front()) != "hello") {
Reply(redis::Error("NOAUTH Authentication required."));
continue;
}
const auto attributes = current_cmd->GetAttributes();
auto cmd_name = attributes->name;
auto cmd_flags = attributes->GenerateFlags(cmd_tokens);

if (password.empty()) {
if (GetNamespace().empty()) {
if (!password.empty()) {
if (cmd_name != "auth" && cmd_name != "hello") {
Reply(redis::Error("NOAUTH Authentication required."));
continue;
}
} else {
BecomeAdmin();
SetNamespace(kDefaultNamespace);
}
}

const auto attributes = current_cmd->GetAttributes();
auto cmd_name = attributes->name;
auto cmd_flags = attributes->GenerateFlags(cmd_tokens);

std::shared_lock<std::shared_mutex> concurrency; // Allow concurrency
std::unique_lock<std::shared_mutex> exclusivity; // Need exclusivity
// If the command needs to process exclusively, we need to get 'ExclusivityGuard'
// that can guarantee other threads can't come into critical zone, such as DEBUG,
// CLUSTER subcommand, CONFIG SET, MULTI, LUA (in the immediate future).
// Otherwise, we just use 'ConcurrencyGuard' to allow all workers to execute commands at the same time.
if (is_multi_exec && attributes->name != "exec") {
if (is_multi_exec && cmd_name != "exec") {
// No lock guard, because 'exec' command has acquired 'WorkExclusivityGuard'
} else if (cmd_flags & kCmdExclusive) {
exclusivity = srv_->WorkExclusivityGuard();
Expand Down Expand Up @@ -490,8 +489,7 @@ void Connection::ExecuteCommands(std::deque<CommandTokens> *to_process_cmds) {
}

if (is_multi_exec && (cmd_flags & kCmdNoMulti)) {
std::string no_multi_err = "ERR Can't execute " + attributes->name + " in MULTI";
Reply(redis::Error(no_multi_err));
Reply(redis::Error("ERR Can't execute " + cmd_name + " in MULTI"));
multi_error_ = true;
continue;
}
Expand Down

0 comments on commit af18420

Please sign in to comment.