Skip to content

Commit

Permalink
check god role exist when meta init (vesoft-inc#978)
Browse files Browse the repository at this point in the history
<!--
Thanks for your contribution!
In order to review PR more efficiently, please add information according to the template.
-->

## What type of PR is this?
- [x] bug
- [ ] feature
- [ ] enhancement

## What problem(s) does this PR solve?
#### Issue(s) number: 
close vesoft-inc/nebula-ent#957

#### Description:
Before:
 it will check whether there is a root account,when meta is restarted

 After:
 it will check whether there is an account with the GOD role, when the meta is restarted

## How do you solve it?



## Special notes for your reviewer, ex. impact of this fix, design document, etc:



## Checklist:
Tests:
- [ ] Unit test(positive and negative cases)
- [ ] Function test
- [ ] Performance test
- [ ] N/A

Affects:
- [ ] Documentation affected (Please add the label if documentation needs to be modified.)
- [ ] Incompatibility (If it breaks the compatibility, please describe it and add the label.)
- [ ] If it's needed to cherry-pick (If cherry-pick to some branches is required, please label the destination version(s).)
- [ ] Performance impacted: Consumes more CPU/Memory


## Release notes:

Please confirm whether to be reflected in release notes and how to describe:
> ex. Fixed the bug .....


Migrated from vesoft-inc#4330

Co-authored-by: jimingquan <[email protected]>
  • Loading branch information
nebula-bots and nevermore3 authored Jun 17, 2022
1 parent 35d9839 commit 2e141cb
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 10 deletions.
15 changes: 10 additions & 5 deletions src/daemons/MetaDaemon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,11 +230,16 @@ int main(int argc, char* argv[]) {
}
if (nebula::value(ret) == localhost) {
LOG(INFO) << "Check and init root user";
if (!nebula::meta::RootUserMan::isUserExists(gKVStore.get())) {
if (!nebula::meta::RootUserMan::initRootUser(gKVStore.get())) {
LOG(ERROR) << "Init root user failed";
return EXIT_FAILURE;
}
auto checkRet = nebula::meta::RootUserMan::isGodExists(gKVStore.get());
if (!nebula::ok(checkRet)) {
auto retCode = nebula::error(checkRet);
LOG(ERROR) << "Parser God Role error:" << apache::thrift::util::enumNameSafe(retCode);
return EXIT_FAILURE;
}
auto existGod = nebula::value(checkRet);
if (!existGod && !nebula::meta::RootUserMan::initRootUser(gKVStore.get())) {
LOG(ERROR) << "Init root user failed";
return EXIT_FAILURE;
}
}
}
Expand Down
15 changes: 10 additions & 5 deletions src/daemons/StandAloneDaemon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -298,11 +298,16 @@ int main(int argc, char *argv[]) {
}
if (nebula::value(ret) == metaLocalhost) {
LOG(INFO) << "Check and init root user";
if (!nebula::meta::RootUserMan::isUserExists(gMetaKVStore.get())) {
if (!nebula::meta::RootUserMan::initRootUser(gMetaKVStore.get())) {
LOG(ERROR) << "Init root user failed";
return;
}
auto checkRet = nebula::meta::RootUserMan::isGodExists(gMetaKVStore.get());
if (!nebula::ok(checkRet)) {
auto retCode = nebula::error(checkRet);
LOG(ERROR) << "Parser God Role error:" << apache::thrift::util::enumNameSafe(retCode);
return;
}
auto existGod = nebula::value(checkRet);
if (!existGod && !nebula::meta::RootUserMan::initRootUser(gMetaKVStore.get())) {
LOG(ERROR) << "Init root user failed";
return;
}
}
}
Expand Down
20 changes: 20 additions & 0 deletions src/meta/RootUserMan.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,26 @@ namespace meta {
* */
class RootUserMan {
public:
static ErrorOr<nebula::cpp2::ErrorCode, bool> isGodExists(kvstore::KVStore* kv) {
auto rolePrefix = MetaKeyUtils::roleSpacePrefix(kDefaultSpaceId);
std::unique_ptr<kvstore::KVIterator> iter;
auto code = kv->prefix(kDefaultSpaceId, kDefaultPartId, rolePrefix, &iter, false);
if (code != nebula::cpp2::ErrorCode::SUCCEEDED) {
return code;
}
while (iter->valid()) {
auto val = iter->val();
auto type = *reinterpret_cast<const meta::cpp2::RoleType*>(val.begin());
if (type == meta::cpp2::RoleType::GOD) {
LOG(INFO) << "God user exists";
return true;
}
iter->next();
}
LOG(INFO) << "God user is not exists";
return false;
}

static bool isUserExists(kvstore::KVStore* kv) {
auto userKey = MetaKeyUtils::userKey("root");
std::string val;
Expand Down

0 comments on commit 2e141cb

Please sign in to comment.