From 93e77b39d9ebf79a38a48373d8f59c78084e1276 Mon Sep 17 00:00:00 2001 From: Mixficsol <838844609@qq.com> Date: Wed, 6 Mar 2024 21:07:42 +0800 Subject: [PATCH] add rename-command config (#2455) add rename-command config --- conf/pika.conf | 15 +++++++++++++++ include/pika_cmd_table_manager.h | 1 + include/pika_conf.h | 2 +- src/pika.cc | 4 ++-- src/pika_cmd_table_manager.cc | 12 ++++++++++++ src/pika_conf.cc | 22 ++++++++++++++++++++-- 6 files changed, 51 insertions(+), 5 deletions(-) diff --git a/conf/pika.conf b/conf/pika.conf index 44c2de9067..398f99eb8b 100644 --- a/conf/pika.conf +++ b/conf/pika.conf @@ -511,3 +511,18 @@ cache-lfu-decay-time: 1 # # aclfile : ../conf/users.acl +# It is possible to change the name of dangerous commands in a shared environment. +# For instance the CONFIG command may be renamed into something Warning: To prevent +# data inconsistency caused by different configuration files, do not use the rename +# command to modify write commands on the primary and secondary servers. If necessary, +# ensure that the configuration files of the primary and secondary servers are consistent +# In addition, when using the command rename, you must not use "" to modify the command, +# for example, rename-command: FLUSHALL "360flushall" is incorrect; instead, use +# rename-command: FLUSHALL 360flushall is correct. After the rename command is executed, +# it is most appropriate to use a numeric string with uppercase or lowercase letters +# for example: rename-command : FLUSHALL joYAPNXRPmcarcR4ZDgC81TbdkSmLAzRPmcarcR +# +# Example: +# +# rename-command : FLUSHALL 360flushall +# rename-command : FLUSHDB 360flushdb diff --git a/include/pika_cmd_table_manager.h b/include/pika_cmd_table_manager.h index 1b0c162807..27393da4ec 100644 --- a/include/pika_cmd_table_manager.h +++ b/include/pika_cmd_table_manager.h @@ -30,6 +30,7 @@ class PikaCmdTableManager { PikaCmdTableManager(); virtual ~PikaCmdTableManager() = default; void InitCmdTable(void); + void RenameCommand(const std::string before, const std::string after); std::shared_ptr GetCmd(const std::string& opt); bool CmdExist(const std::string& cmd) const; CmdTable* GetCmdTable(); diff --git a/include/pika_conf.h b/include/pika_conf.h index 6b0917ee76..0bf1d3972f 100644 --- a/include/pika_conf.h +++ b/include/pika_conf.h @@ -765,7 +765,7 @@ class PikaConf : public pstd::BaseConf { std::vector users_; // acl user rules std::string aclFile_; - + std::vector cmds_; std::atomic acl_pubsub_default_ = 0; // default channel pub/sub permission std::atomic acl_Log_max_len_ = 0; // default acl log max len diff --git a/src/pika.cc b/src/pika.cc index 89f344cb76..760c962745 100644 --- a/src/pika.cc +++ b/src/pika.cc @@ -175,6 +175,8 @@ int main(int argc, char* argv[]) { usage(); exit(-1); } + g_pika_cmd_table_manager = std::make_unique(); + g_pika_cmd_table_manager->InitCmdTable(); PikaConfInit(path); rlimit limit; @@ -205,8 +207,6 @@ int main(int argc, char* argv[]) { PikaSignalSetup(); LOG(INFO) << "Server at: " << path; - g_pika_cmd_table_manager = std::make_unique(); - g_pika_cmd_table_manager->InitCmdTable(); g_pika_server = new PikaServer(); g_pika_rm = std::make_unique(); g_network_statistic = std::make_unique(); diff --git a/src/pika_cmd_table_manager.cc b/src/pika_cmd_table_manager.cc index 567c120a18..2be143a84e 100644 --- a/src/pika_cmd_table_manager.cc +++ b/src/pika_cmd_table_manager.cc @@ -50,6 +50,18 @@ void PikaCmdTableManager::InitCmdTable(void) { } } +void PikaCmdTableManager::RenameCommand(const std::string before, const std::string after) { + auto it = cmds_->find(before); + if (it != cmds_->end()) { + if (after.length() > 0) { + cmds_->insert(std::pair>(after, std::move(it->second))); + } else { + LOG(ERROR) << "The value of rename-command is null"; + } + cmds_->erase(it); + } +} + std::unordered_map* PikaCmdTableManager::GetCommandStatMap() { return &cmdstat_map_; } diff --git a/src/pika_conf.cc b/src/pika_conf.cc index 09ae68996e..2c5ac2ef7f 100644 --- a/src/pika_conf.cc +++ b/src/pika_conf.cc @@ -10,10 +10,12 @@ #include "cache/include/config.h" #include "include/acl.h" -#include "include/pika_define.h" +#include "include/pika_cmd_table_manager.h" #include "include/pika_conf.h" +#include "include/pika_define.h" using pstd::Status; +extern std::unique_ptr g_pika_cmd_table_manager; PikaConf::PikaConf(const std::string& path) : pstd::BaseConf(path), conf_path_(path) {} @@ -465,7 +467,23 @@ int PikaConf::Load() { GetConfStrMulti("user", &users_); GetConfStr("aclfile", &aclFile_); - + GetConfStrMulti("rename-command", &cmds_); + for (const auto & i : cmds_) { + std::string before, after; + std::istringstream iss(i); + iss >> before; + if (iss) { + iss >> after; + pstd::StringToLower(before); + pstd::StringToLower(after); + std::shared_ptr c_ptr = g_pika_cmd_table_manager->GetCmd(before); + if (!c_ptr) { + LOG(ERROR) << "No such " << before << " command in pika-command"; + return -1; + } + g_pika_cmd_table_manager->RenameCommand(before, after); + } + } std::string acl_pubsub_default; GetConfStr("acl-pubsub-default", &acl_pubsub_default); if (acl_pubsub_default == "allchannels") {