Skip to content

Commit 7377133

Browse files
committed
fix acl permission
1 parent 4206f66 commit 7377133

6 files changed

Lines changed: 42 additions & 44 deletions

File tree

conf/pika.conf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,12 @@ requirepass :
6868
# [NOTICE] The value of this parameter must match the "requirepass" setting on the master.
6969
masterauth :
7070

71-
# The [password of user], which is empty by default.
71+
# The [password of user], which is empty by default.(Deprecated)
7272
# [NOTICE] If this user password is the same as admin password (including both being empty),
7373
# the value of this parameter will be ignored and all users are considered as administrators,
7474
# in this scenario, users are not subject to the restrictions imposed by the userblacklist.
7575
# PS: "admin password" refers to value of the parameter above: requirepass.
76-
userpass :
76+
# userpass :
7777

7878
# The blacklist of commands for users that logged in by userpass,
7979
# the commands that added to this list will not be available for users except for administrator.
@@ -446,5 +446,5 @@ max-rsync-parallel-num : 4
446446
# The format of the external ACL user file is exactly the same as the
447447
# format that is used inside pika.conf to describe users.
448448
#
449-
#aclfile : ../conf/users.acl
449+
# aclfile : ../conf/users.acl
450450

include/acl.h

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
#include "pika_command.h"
2020
#include "pstd_status.h"
2121

22-
#define USER_COMMAND_BITS_COUNT 1024
22+
static const int USER_COMMAND_BITS_COUNT = 1024;
2323

2424
enum class AclSelectorFlag {
2525
ROOT = (1 << 0), // This is the root user permission selector
@@ -119,13 +119,13 @@ class AclSelector {
119119
void ChangeSelector(const std::shared_ptr<Cmd>& cmd, bool allow);
120120
pstd::Status ChangeSelector(const std::shared_ptr<Cmd>& cmd, const std::string& subCmd, bool allow);
121121

122-
void SetSubCommand(const uint32_t cmdId);
123-
void SetSubCommand(const uint32_t cmdId, const uint32_t subCmdIndex);
122+
void SetSubCommand(uint32_t cmdId);
123+
void SetSubCommand(uint32_t cmdId, uint32_t subCmdIndex);
124124
void ResetSubCommand();
125-
void ResetSubCommand(const uint32_t cmdId);
126-
void ResetSubCommand(const uint32_t cmdId, const uint32_t subCmdIndex);
125+
void ResetSubCommand(uint32_t cmdId);
126+
void ResetSubCommand(uint32_t cmdId, uint32_t subCmdIndex);
127127

128-
bool CheckSubCommand(const uint32_t cmdId, const uint32_t subCmdIndex);
128+
bool CheckSubCommand(uint32_t cmdId, uint32_t subCmdIndex);
129129

130130
void DescribeSelectorCommandRules(std::string* str);
131131

@@ -172,7 +172,7 @@ class AclSelector {
172172
class User {
173173
public:
174174
User() = delete;
175-
explicit User(const std::string& name);
175+
explicit User(std::string name);
176176

177177
std::string Name() const;
178178

@@ -226,17 +226,17 @@ class User {
226226
private:
227227
mutable std::shared_mutex mutex_;
228228

229-
std::string name_; // The username
229+
std::string name_; // The username
230230

231231
std::atomic<uint32_t> flags_ = static_cast<uint32_t>(AclUserFlag::DISABLED); // See USER_FLAG_*
232232

233-
std::set<std::string> passwords_; // passwords for this user
233+
std::set<std::string> passwords_; // passwords for this user
234234

235235
std::list<std::shared_ptr<AclSelector>> selectors_; /* A set of selectors this user validates commands
236236
against. This list will always contain at least
237237
one selector for backwards compatibility. */
238238

239-
std::string aclString_; /* cached string represent of ACLs */
239+
std::string aclString_; /* cached string represent of ACLs */
240240
};
241241

242242
class Acl {
@@ -300,10 +300,10 @@ class Acl {
300300
void UpdateDefaultUserPassword(const std::string& pass);
301301

302302
// check the user can be exec the command, after exec command
303-
bool CheckUserCanExec(const std::shared_ptr<Cmd>& cmd, const PikaCmdArgsType& argv);
303+
// bool CheckUserCanExec(const std::shared_ptr<Cmd>& cmd, const PikaCmdArgsType& argv);
304304

305305
// 根据 cmd 分类名 获取分类的值
306-
inline static uint32_t GetCommandCategoryFlagByName(const std::string& name);
306+
static uint32_t GetCommandCategoryFlagByName(const std::string& name);
307307

308308
// 根据 category获取对应的name
309309
static std::string GetCommandCategoryFlagByName(const uint32_t category);

src/acl.cc

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ extern std::unique_ptr<PikaCmdTableManager> g_pika_cmd_table_manager;
1818

1919
// class User
2020

21-
User::User(const std::string& name) : name_(name) {
21+
User::User( std::string name) : name_(std::move(name)) {
2222
selectors_.emplace_back(std::make_shared<AclSelector>(static_cast<uint32_t>(AclSelectorFlag::ROOT)));
2323
}
2424

@@ -157,6 +157,7 @@ pstd::Status User::CreateSelectorFromOpSet(const std::string& opSet) {
157157
return status;
158158
}
159159
AddSelector(selector);
160+
return status;
160161
}
161162

162163
std::shared_ptr<AclSelector> User::GetRootSelector() {
@@ -255,12 +256,8 @@ void User::GetUserDescribe(CmdRes* res) {
255256
AclDeniedCmd User::CheckUserPermission(std::shared_ptr<Cmd>& cmd, const PikaCmdArgsType& argv) {
256257
std::shared_lock l(mutex_);
257258

258-
if (!cmd->CheckArg(argv.size())) {
259-
return AclDeniedCmd::NUMBER;
260-
}
261259
std::string subCmd = "";
262-
std::vector<std::string> keys;
263-
260+
auto keys = cmd->current_key();
264261
AclDeniedCmd res = AclDeniedCmd::OK;
265262
for (const auto& selector : selectors_) {
266263
res = selector->CheckCanExecCmd(cmd, subCmd, keys);
@@ -453,7 +450,7 @@ void Acl::UpdateDefaultUserPassword(const std::string& pass) {
453450
}
454451
}
455452

456-
bool Acl::CheckUserCanExec(const std::shared_ptr<Cmd>& cmd, const PikaCmdArgsType& argv) { cmd->name(); }
453+
// bool Acl::CheckUserCanExec(const std::shared_ptr<Cmd>& cmd, const PikaCmdArgsType& argv) { cmd->name(); }
457454

458455
std::shared_ptr<User> Acl::CreateDefaultUser() {
459456
auto defaultUser = std::make_shared<User>(DefaultUser);
@@ -545,7 +542,7 @@ std::shared_ptr<User> Acl::Auth(const std::string& userName, const std::string&
545542
if (!user) {
546543
return nullptr;
547544
}
548-
if (user->MatchPassword(password)) {
545+
if (user->MatchPassword(pstd::sha256(password))) {
549546
return user;
550547
}
551548
return nullptr;
@@ -858,21 +855,17 @@ pstd::Status AclSelector::ChangeSelector(const std::shared_ptr<Cmd>& cmd, const
858855
return pstd::Status::OK();
859856
}
860857

861-
void AclSelector::SetSubCommand(const uint32_t cmdId) { subCommand_[cmdId] = ~0; }
858+
void AclSelector::SetSubCommand(uint32_t cmdId) { subCommand_[cmdId] = ~0; }
862859

863-
void AclSelector::SetSubCommand(const uint32_t cmdId, const uint32_t subCmdIndex) {
864-
subCommand_[cmdId] = (1 << subCmdIndex);
865-
}
860+
void AclSelector::SetSubCommand(uint32_t cmdId, uint32_t subCmdIndex) { subCommand_[cmdId] = (1 << subCmdIndex); }
866861

867862
void AclSelector::ResetSubCommand() { subCommand_.clear(); }
868863

869-
void AclSelector::ResetSubCommand(const uint32_t cmdId) { subCommand_[cmdId] = 0; }
864+
void AclSelector::ResetSubCommand(uint32_t cmdId) { subCommand_[cmdId] = 0; }
870865

871-
void AclSelector::ResetSubCommand(const uint32_t cmdId, const uint32_t subCmdIndex) {
872-
subCommand_[cmdId] = ~(1 << subCmdIndex);
873-
}
866+
void AclSelector::ResetSubCommand(uint32_t cmdId, uint32_t subCmdIndex) { subCommand_[cmdId] = ~(1 << subCmdIndex); }
874867

875-
bool AclSelector::CheckSubCommand(const uint32_t cmdId, const uint32_t subCmdIndex) {
868+
bool AclSelector::CheckSubCommand(uint32_t cmdId, uint32_t subCmdIndex) {
876869
if (subCmdIndex < 0) {
877870
return false;
878871
}
@@ -1061,7 +1054,7 @@ void AclSelector::RemoveCommonRule(const std::string& rule) {
10611054

10621055
if (start > 0) { // the rule not included '-'/'+', but need delete need
10631056
--start;
1064-
++delNum; // star position moved one forward So delNum takes +1
1057+
++delNum; // star position moved one forward So delNum takes +1
10651058
}
10661059

10671060
commandRules_.erase(start, delNum);

src/pika_acl.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ void PikaAclCmd::Cat() {
8080
res().AppendStringVector(Acl::GetAllCategoryName());
8181
return;
8282
}
83-
auto category = g_pika_server->Acl()->GetCommandCategoryFlagByName(argv_[2]);
83+
auto category = Acl::GetCommandCategoryFlagByName(argv_[2]);
8484
if (category == 0) {
8585
res().SetRes(CmdRes::kErrOther, fmt::format("Unknown category '{}'", argv_[2]));
8686
return;
@@ -207,9 +207,9 @@ void PikaAclCmd::WhoAmI() {
207207
auto name = conn->UserName();
208208

209209
if (name.empty()) {
210-
res().AppendString(name);
210+
res().AppendString(Acl::DefaultUser);
211211
} else {
212-
res().SetRes(CmdRes::kNone);
212+
res().AppendString(name);
213213
}
214214
}
215215

src/pika_admin.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ enum AuthResult {
6262
static AuthResult AuthenticateUser(const std::string& userName, const std::string& pwd,
6363
const std::shared_ptr<net::NetConn>& conn) {
6464
std::string root_password(g_pika_conf->requirepass());
65-
if (root_password.empty()) {
65+
if (userName == Acl::DefaultUser && root_password.empty()) {
6666
return AuthResult::NO_REQUIRE_PASS;
6767
}
6868

src/pika_client_conn.cc

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,17 @@ std::shared_ptr<Cmd> PikaClientConn::DoCmd(const PikaCmdArgsType& argv, const st
5353
}
5454
}
5555

56+
uint64_t start_us = 0;
57+
if (g_pika_conf->slowlog_slower_than() >= 0) {
58+
start_us = pstd::NowMicros();
59+
}
60+
61+
// Initial
62+
c_ptr->Initial(argv, current_db_);
63+
if (!c_ptr->res().ok()) {
64+
return c_ptr;
65+
}
66+
5667
auto checkRes = user_->CheckUserPermission(c_ptr, argv);
5768
switch (checkRes) {
5869
case AclDeniedCmd::CMD:
@@ -65,7 +76,7 @@ std::shared_ptr<Cmd> PikaClientConn::DoCmd(const PikaCmdArgsType& argv, const st
6576
return c_ptr;
6677
case AclDeniedCmd::CHANNEL:
6778
c_ptr->res().SetRes(CmdRes::kErrOther,
68-
"-NOPERM this user has no permissions to access one of the keys used as arguments");
79+
"-NOPERM this user has no permissions to access one of the channel used as arguments");
6980
return c_ptr;
7081
default:
7182
break;
@@ -76,12 +87,6 @@ std::shared_ptr<Cmd> PikaClientConn::DoCmd(const PikaCmdArgsType& argv, const st
7687
ProcessMonitor(argv);
7788
}
7889

79-
// Initial
80-
c_ptr->Initial(argv, current_db_);
81-
if (!c_ptr->res().ok()) {
82-
return c_ptr;
83-
}
84-
8590
g_pika_server->UpdateQueryNumAndExecCountDB(current_db_, opt, c_ptr->is_write());
8691

8792
// PubSub connection

0 commit comments

Comments
 (0)