Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid requirepass and masterauth conflicts with the namespace tokens #507

Merged
merged 4 commits into from
Mar 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 35 additions & 6 deletions src/config.cc
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,15 @@ void Config::initFieldValidator() {
if (v.empty() && !tokens.empty()) {
return Status(Status::NotOK, "requirepass empty not allowed while the namespace exists");
}
if (tokens.find(v) != tokens.end()) {
git-hulk marked this conversation as resolved.
Show resolved Hide resolved
return Status(Status::NotOK, "requirepass is duplicated with namespace tokens");
}
return Status::OK();
}},
{"masterauth", [this](const std::string& k, const std::string& v)->Status {
if (tokens.find(v) != tokens.end()) {
return Status(Status::NotOK, "masterauth is duplicated with namespace tokens");
}
return Status::OK();
}},
{"compact-cron", [this](const std::string& k, const std::string& v)->Status {
Expand Down Expand Up @@ -448,7 +457,7 @@ void Config::ClearMaster() {
}
}

Status Config::parseConfigFromString(std::string input) {
Status Config::parseConfigFromString(std::string input, int line_number) {
std::vector<std::string> kv;
Util::Split2KV(input, " \t", &kv);

Expand All @@ -462,10 +471,7 @@ Status Config::parseConfigFromString(std::string input) {
auto iter = fields_.find(kv[0]);
if (iter != fields_.end()) {
auto field = iter->second;
if (field->validate) {
auto s = field->validate(kv[0], kv[1]);
if (!s.IsOK()) return s;
}
field->line_number = line_number;
auto s = field->Set(kv[1]);
if (!s.IsOK()) return s;
}
Expand Down Expand Up @@ -504,7 +510,7 @@ Status Config::Load(const std::string &path) {
int line_num = 1;
while (!file.eof()) {
std::getline(file, line);
Status s = parseConfigFromString(line);
Status s = parseConfigFromString(line, line_num);
if (!s.IsOK()) {
file.close();
return Status(Status::NotOK, "at line: #L" + std::to_string(line_num) + ", err: " + s.Msg());
Expand All @@ -516,6 +522,19 @@ Status Config::Load(const std::string &path) {
std::cout << "Warn: no config file specified, using the default config. "
"In order to specify a config file use kvrocks -c /path/to/kvrocks.conf" << std::endl;
}

for (const auto &iter : fields_) {
caipengbo marked this conversation as resolved.
Show resolved Hide resolved
// line_number = 0 means the user didn't specify the field value
// on config file and would use default value, so won't validate here.
if (iter.second->line_number != 0 && iter.second->validate) {
caipengbo marked this conversation as resolved.
Show resolved Hide resolved
auto s = iter.second->validate(iter.first, iter.second->ToString());
if (!s.IsOK()) {
return Status(Status::NotOK, "at line: #L" + std::to_string(iter.second->line_number)
+ ", " + iter.first + " is invalid: " + s.Msg());
}
}
}

for (const auto &iter : fields_) {
if (iter.second->callback) {
auto s = iter.second->callback(nullptr, iter.first, iter.second->ToString());
Expand Down Expand Up @@ -644,6 +663,11 @@ Status Config::SetNamespace(const std::string &ns, const std::string &token) {
if (tokens.find(token) != tokens.end()) {
return Status(Status::NotOK, "the token has already exists");
}

if (token == requirepass || token == masterauth) {
return Status(Status::NotOK, "the token is duplicated with requirepass or masterauth");
}

for (const auto &iter : tokens) {
if (iter.second == ns) {
tokens.erase(iter.first);
Expand Down Expand Up @@ -672,6 +696,11 @@ Status Config::AddNamespace(const std::string &ns, const std::string &token) {
if (tokens.find(token) != tokens.end()) {
return Status(Status::NotOK, "the token has already exists");
}

if (token == requirepass || token == masterauth) {
return Status(Status::NotOK, "the token is duplicated with requirepass or masterauth");
}

for (const auto &iter : tokens) {
if (iter.second == ns) {
return Status(Status::NotOK, "the namespace has already exists");
Expand Down
2 changes: 1 addition & 1 deletion src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ struct Config{

void initFieldValidator();
void initFieldCallback();
Status parseConfigFromString(std::string input);
Status parseConfigFromString(std::string input, int line_number);
Status finish();
Status isNamespaceLegal(const std::string &ns);
};
1 change: 1 addition & 0 deletions src/config_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class ConfigField {
virtual Status ToBool(bool *b) { return Status(Status::NotOK, "not supported"); }

public:
int line_number = 0;
bool readonly = true;
validate_fn validate = nullptr;
callback_fn callback = nullptr;
Expand Down