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

User privilege manager in graph layer. #502

Closed
wants to merge 4 commits into from
Closed
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
22 changes: 22 additions & 0 deletions src/common/base/MD5Utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/* Copyright (c) 2019 vesoft inc. All rights reserved.
*
* This source code is licensed under Apache 2.0 License,
* attached with Common Clause Condition 1.0, found in the LICENSES directory.
*/

#ifndef COMMON_BASE_MD5UTILS_H
#define COMMON_BASE_MD5UTILS_H

#include "proxygen/lib/utils/CryptUtil.h"

namespace nebula {
class MD5Utils final {
public:
static std::string md5Encode(const std::string &str) {
return proxygen::md5Encode(folly::ByteRange(
reinterpret_cast<const unsigned char*>(str.c_str()),
str.length()));
}
};
}
#endif // COMMON_BASE_MD5UTILS_H
2 changes: 2 additions & 0 deletions src/common/base/Status.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ class Status final {
STATUS_GENERATOR(HostNotFound);
STATUS_GENERATOR(TagNotFound);
STATUS_GENERATOR(EdgeNotFound);
STATUS_GENERATOR(UserNotFound);

#undef STATUS_GENERATOR

Expand All @@ -128,6 +129,7 @@ class Status final {
kHostNotFound = 405,
kTagNotFound = 406,
kEdgeNotFound = 407,
kUserNotFound = 408,
};

Code code() const {
Expand Down
1 change: 1 addition & 0 deletions src/common/base/ThriftTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ using EdgeType = int32_t;
using EdgeRanking = int64_t;
using EdgeVersion = int64_t;
using SchemaVer = int64_t;
using UserID = int32_t;

} // namespace nebula
#endif // COMMON_BASE_THRIFTTYPES_H_
Expand Down
1 change: 1 addition & 0 deletions src/graph/AddHostsExecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Status AddHostsExecutor::prepare() {
if (hosts_.size() == 0) {
return Status::Error("Host address illegal");
}
ACL_CHECK();
return Status::OK();
}

Expand Down
7 changes: 6 additions & 1 deletion src/graph/AlterEdgeExecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ AlterEdgeExecutor::AlterEdgeExecutor(Sentence *sentence,


Status AlterEdgeExecutor::prepare() {
return checkIfGraphSpaceChosen();
auto status = checkIfGraphSpaceChosen();
if (!status.ok()) {
return status;
}
ACL_CHECK();
return Status::OK();
}


Expand Down
7 changes: 6 additions & 1 deletion src/graph/AlterTagExecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ AlterTagExecutor::AlterTagExecutor(Sentence *sentence,
}

Status AlterTagExecutor::prepare() {
return checkIfGraphSpaceChosen();
auto status = checkIfGraphSpaceChosen();
if (!status.ok()) {
return status;
}
ACL_CHECK();
return Status::OK();
}

void AlterTagExecutor::execute() {
Expand Down
2 changes: 2 additions & 0 deletions src/graph/AssignmentExecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ Status AssignmentExecutor::prepare() {
return status;
}

ACL_CHECK();

var_ = sentence_->var();
executor_ = TraverseExecutor::makeTraverseExecutor(sentence_->sentence(), ectx());
status = executor_->prepare();
Expand Down
2 changes: 2 additions & 0 deletions src/graph/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ add_library(
DescribeSpaceExecutor.cpp
ShowExecutor.cpp
YieldExecutor.cpp
UserExecutor.cpp
UserAccessControl.cpp
)
add_dependencies(
graph_obj
Expand Down
7 changes: 6 additions & 1 deletion src/graph/CreateEdgeExecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@ CreateEdgeExecutor::CreateEdgeExecutor(Sentence *sentence,


Status CreateEdgeExecutor::prepare() {
return checkIfGraphSpaceChosen();
auto status = checkIfGraphSpaceChosen();
if (!status.ok()) {
return status;
}
ACL_CHECK();
return Status::OK();
}


Expand Down
1 change: 1 addition & 0 deletions src/graph/CreateSpaceExecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Status CreateSpaceExecutor::prepare() {
if (replicaFactor_ <= 0) {
return Status::Error("Replica_factor value should be greater than zero");
}
ACL_CHECK();
return Status::OK();
}

Expand Down
7 changes: 6 additions & 1 deletion src/graph/CreateTagExecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@ CreateTagExecutor::CreateTagExecutor(Sentence *sentence,


Status CreateTagExecutor::prepare() {
return checkIfGraphSpaceChosen();
auto status = checkIfGraphSpaceChosen();
if (!status.ok()) {
return status;
}
ACL_CHECK();
return Status::OK();
}


Expand Down
7 changes: 6 additions & 1 deletion src/graph/DescribeEdgeExecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@ DescribeEdgeExecutor::DescribeEdgeExecutor(Sentence *sentence,


Status DescribeEdgeExecutor::prepare() {
return checkIfGraphSpaceChosen();
auto status = checkIfGraphSpaceChosen();
if (!status.ok()) {
return status;
}
ACL_CHECK();
return Status::OK();
}


Expand Down
6 changes: 6 additions & 0 deletions src/graph/DescribeSpaceExecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ DescribeSpaceExecutor::DescribeSpaceExecutor(Sentence *sentence,
}

Status DescribeSpaceExecutor::prepare() {
auto *name = sentence_->spaceName();
auto spaceRet = ectx()->getMetaClient()->getSpaceIdByNameFromCache(name->data());
if (!spaceRet.ok()) {
return Status::Error("Space not found : %s", name);
}
ACL_CHECK_SPACE(spaceRet.value());
return Status::OK();;
}

Expand Down
7 changes: 6 additions & 1 deletion src/graph/DescribeTagExecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@ DescribeTagExecutor::DescribeTagExecutor(Sentence *sentence,


Status DescribeTagExecutor::prepare() {
return checkIfGraphSpaceChosen();
auto status = checkIfGraphSpaceChosen();
if (!status.ok()) {
return status;
}
ACL_CHECK();
return Status::OK();
}


Expand Down
7 changes: 6 additions & 1 deletion src/graph/DropEdgeExecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ DropEdgeExecutor::DropEdgeExecutor(Sentence *sentence,
}

Status DropEdgeExecutor::prepare() {
return checkIfGraphSpaceChosen();
auto status = checkIfGraphSpaceChosen();
if (!status.ok()) {
return status;
}
ACL_CHECK();
return Status::OK();
}

void DropEdgeExecutor::execute() {
Expand Down
5 changes: 5 additions & 0 deletions src/graph/DropSpaceExecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ DropSpaceExecutor::DropSpaceExecutor(Sentence *sentence,

Status DropSpaceExecutor::prepare() {
spaceName_ = sentence_->spaceName();
auto spaceRet = ectx()->getMetaClient()->getSpaceIdByNameFromCache(spaceName_->data());
if (!spaceRet.ok()) {
return Status::Error("Space not found : %s", spaceName_);
}
ACL_CHECK_SPACE(spaceRet.value());
return Status::OK();
}

Expand Down
7 changes: 6 additions & 1 deletion src/graph/DropTagExecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ DropTagExecutor::DropTagExecutor(Sentence *sentence,
}

Status DropTagExecutor::prepare() {
return checkIfGraphSpaceChosen();
auto status = checkIfGraphSpaceChosen();
if (!status.ok()) {
return status;
}
ACL_CHECK();
return Status::OK();
}

void DropTagExecutor::execute() {
Expand Down
19 changes: 19 additions & 0 deletions src/graph/Executor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "graph/DescribeSpaceExecutor.h"
#include "graph/DropSpaceExecutor.h"
#include "graph/YieldExecutor.h"
#include "graph/UserExecutor.h"

namespace nebula {
namespace graph {
Expand Down Expand Up @@ -102,6 +103,24 @@ std::unique_ptr<Executor> Executor::makeExecutor(Sentence *sentence) {
case Sentence::Kind::kYield:
executor = std::make_unique<YieldExecutor>(sentence, ectx());
break;
case Sentence::Kind::kCreateUser:
executor = std::make_unique<CreateUserExecutor>(sentence, ectx());
break;
case Sentence::Kind::kDropUser:
executor = std::make_unique<DropUserExecutor>(sentence, ectx());
break;
case Sentence::Kind::kAlterUser:
executor = std::make_unique<AlterUserExecutor>(sentence, ectx());
break;
case Sentence::Kind::kGrant:
executor = std::make_unique<GrantExecutor>(sentence, ectx());
break;
case Sentence::Kind::kRevoke:
executor = std::make_unique<RevokeExecutor>(sentence, ectx());
break;
case Sentence::Kind::kChangePassword:
executor = std::make_unique<ChangePasswordExecutor>(sentence, ectx());
break;
case Sentence::Kind::kUnknown:
LOG(FATAL) << "Sentence kind unknown";
break;
Expand Down
68 changes: 68 additions & 0 deletions src/graph/Executor.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "cpp/helpers.h"
#include "graph/ExecutionContext.h"
#include "gen-cpp2/common_types.h"
#include "graph/UserAccessControl.h"


/**
Expand All @@ -21,6 +22,41 @@
namespace nebula {
namespace graph {

#define ACL_CHECK() \
do { \
auto spaceId = ectx()->rctx()->session()->space(); \
if (spaceId == -1) { \
spaceId = ectx()->getMetaClient()-> \
getMetaDefaultSpaceIdInCache(); \
} \
auto aclStatus = checkACL(spaceId, \
ectx()->rctx()->session()->user(), \
sentence_->kind()); \
if (!aclStatus.ok()) { \
return aclStatus; \
} \
} while (false)

#define ACL_CHECK_SPACE(spaceId) \
do { \
auto aclStatus = checkACL(spaceId, \
ectx()->rctx()->session()->user(), \
sentence_->kind()); \
if (!aclStatus.ok()) { \
return aclStatus; \
} \
} while (false)

#define ACL_CHECK_IS_GOD() \
do { \
const auto& userName = ectx()->rctx()->session()->user(); \
auto isGod = ectx()->getMetaClient()-> \
checkIsGodUserInCache(userName); \
if (FLAGS_security_authorization_enable && !isGod) { \
return Status::Error("God role requested"); \
} \
} while (false)

class Executor : public cpp::NonCopyable, public cpp::NonMovable {
public:
explicit Executor(ExecutionContext *ectx) {
Expand Down Expand Up @@ -85,6 +121,38 @@ class Executor : public cpp::NonCopyable, public cpp::NonMovable {
return Status::OK();
}

Status checkACL(GraphSpaceID spaceId, const std::string& user, Sentence::Kind op) {
if (!FLAGS_security_authorization_enable) {
return Status::OK();
}
auto userRet = ectx()->getMetaClient()->getUserIdByNameFromCache(user);
if (!userRet.ok()) {
return userRet.status();
}
auto ret = UserAccessControl::checkPerms(spaceId,
userRet.value(),
op,
ectx()->getMetaClient());
if (!ret.ok()) {
return ret;
}
return Status::OK();
}

meta::cpp2::RoleType toRole(RoleTypeClause::RoleType type) {
switch (type) {
case RoleTypeClause::RoleType::GOD:
return meta::cpp2::RoleType::GOD;
case RoleTypeClause::RoleType::ADMIN:
return meta::cpp2::RoleType::ADMIN;
case RoleTypeClause::RoleType::USER:
return meta::cpp2::RoleType::USER;
case RoleTypeClause::RoleType::GUEST:
return meta::cpp2::RoleType::GUEST;
}
return meta::cpp2::RoleType::UNKNOWN;
}

protected:
ExecutionContext *ectx_;
std::function<void()> onFinish_;
Expand Down
3 changes: 2 additions & 1 deletion src/graph/GoExecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ Status GoExecutor::prepare() {
return status;
}

return status;
ACL_CHECK();
return Status::OK();
}


Expand Down
2 changes: 2 additions & 0 deletions src/graph/GraphFlags.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,5 @@ DEFINE_string(stderr_log_file, "graphd-stderr.log", "Destination filename of std
DEFINE_bool(daemonize, true, "Whether run as a daemon process");
DEFINE_string(meta_server_addrs, "", "list of meta server addresses,"
"the format looks like ip1:port1, ip2:port2, ip3:port3");
DEFINE_bool(security_authorization_enable, false, "Whether to turn on security "
"authorization management");
2 changes: 1 addition & 1 deletion src/graph/GraphFlags.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ DECLARE_string(stdout_log_file);
DECLARE_string(stderr_log_file);
DECLARE_bool(daemonize);
DECLARE_string(meta_server_addrs);

DECLARE_bool(security_authorization_enable);

#endif // GRAPH_GRAPHFLAGS_H_
1 change: 1 addition & 0 deletions src/graph/InsertEdgeExecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ Status InsertEdgeExecutor::prepare() {
status = Status::Error("No schema found for '%s'", sentence_->edge()->c_str());
break;
}
ACL_CHECK_SPACE(spaceId);
} while (false);

return status;
Expand Down
1 change: 1 addition & 0 deletions src/graph/InsertVertexExecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ Status InsertVertexExecutor::prepare() {
status = Status::Error("No schema found for '%s'", tagName->c_str());
break;
}
ACL_CHECK_SPACE(spaceId);
} while (false);

return status;
Expand Down
2 changes: 2 additions & 0 deletions src/graph/PipeExecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ Status PipeExecutor::prepare() {
return status;
}

ACL_CHECK();

left_ = makeTraverseExecutor(sentence_->left());
right_ = makeTraverseExecutor(sentence_->right());
DCHECK(left_ != nullptr);
Expand Down
1 change: 1 addition & 0 deletions src/graph/RemoveHostsExecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Status RemoveHostsExecutor::prepare() {
if (host_.size() == 0) {
return Status::Error("Host address illegal");
}
ACL_CHECK();
return Status::OK();
}

Expand Down
Loading