Skip to content

Commit

Permalink
Authenticate for scan-part2 (#136)
Browse files Browse the repository at this point in the history
* fix auth for scan

* fix StorageClient

* default value for user and password
  • Loading branch information
caton-hpg authored Apr 26, 2024
1 parent a78a20f commit 03a5067
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 62 deletions.
53 changes: 16 additions & 37 deletions examples/StorageClientExample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,21 @@

#include "common/graph/Response.h"

void scanEdge(nebula::StorageClient& c,
bool auth = false,
const std::string& username = "",
const std::string& password = "") {
auto scanEdgeIter = c.scanEdgeWithPart("nba",
1,
"like",
std::vector<std::string>{"likeness"},
10,
0,
std::numeric_limits<int64_t>::max(),
"",
true,
true,
auth,
username,
password);
int main(int argc, char* argv[]) {
nebula::init(&argc, &argv);

nebula::StorageClient c({"127.0.0.1:9559"});

nebula::ScanEdgeIter scanEdgeIter = c.scanEdgeWithPart("nba",
1,
"like",
std::vector<std::string>{"likeness"},
10,
0,
std::numeric_limits<int64_t>::max(),
"",
true,
true);
std::cout << "scan edge..." << std::endl;
while (scanEdgeIter.hasNext()) {
std::cout << "-------------------------" << std::endl;
Expand All @@ -39,12 +37,7 @@ void scanEdge(nebula::StorageClient& c,
std::cout << res.second << std::endl;
std::cout << "+++++++++++++++++++++++++" << std::endl;
}
}

void scanVertex(nebula::StorageClient& c,
bool auth = false,
const std::string& username = "",
const std::string& password = "") {
nebula::ScanVertexIter scanVertexIter =
c.scanVertexWithPart("nba",
1,
Expand All @@ -54,10 +47,7 @@ void scanVertex(nebula::StorageClient& c,
std::numeric_limits<int64_t>::max(),
"",
true,
true,
auth,
username,
password);
true);
std::cout << "scan vertex..." << std::endl;
while (scanVertexIter.hasNext()) {
std::cout << "-------------------------" << std::endl;
Expand All @@ -66,17 +56,6 @@ void scanVertex(nebula::StorageClient& c,
std::cout << res.second << std::endl;
std::cout << "+++++++++++++++++++++++++" << std::endl;
}
}

int main(int argc, char* argv[]) {
nebula::init(&argc, &argv);
nebula::StorageClient c({"127.0.0.1:9559"});

scanVertex(c);
scanEdge(c);

scanVertex(c, true, "root", "nebula");
scanEdge(c, true, "root", "nebula");

return 0;
}
14 changes: 6 additions & 8 deletions include/nebula/sclient/StorageClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ class StorageClient {

public:
explicit StorageClient(const std::vector<std::string>& metaAddrs,
const std::string& user = "",
const std::string& password = "",
const MConfig& mConfig = MConfig{},
const SConfig& sConfig = SConfig{});

Expand All @@ -80,10 +82,7 @@ class StorageClient {
int64_t endTime = DEFAULT_END_TIME,
std::string filter = "",
bool onlyLatestVersion = false,
bool enableReadFromFollower = true,
bool needAuth = false,
const std::string& username = "",
const std::string& password = "");
bool enableReadFromFollower = true);

ScanVertexIter scanVertexWithPart(
std::string spaceName,
Expand All @@ -95,10 +94,7 @@ class StorageClient {
int64_t endTime = DEFAULT_END_TIME,
std::string filter = "",
bool onlyLatestVersion = false,
bool enableReadFromFollower = true,
bool needAuth = false,
const std::string& username = "",
const std::string& password = "");
bool enableReadFromFollower = true);

MetaClient* getMetaClient() {
return mClient_.get();
Expand All @@ -116,6 +112,8 @@ class StorageClient {
RemoteFunc&& remoteFunc,
folly::Promise<std::pair<::nebula::ErrorCode, Response>> pro);

std::string user_;
std::string password_;
std::unique_ptr<MetaClient> mClient_;
SConfig sConfig_;
std::shared_ptr<folly::IOThreadPoolExecutor> ioExecutor_;
Expand Down
27 changes: 12 additions & 15 deletions src/sclient/StorageClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@
namespace nebula {

StorageClient::StorageClient(const std::vector<std::string>& metaAddrs,
const std::string& user,
const std::string& password,
const MConfig& mConfig,
const SConfig& sConfig) {
const SConfig& sConfig)
: user_(user), password_(password) {
mClient_ = std::make_unique<MetaClient>(metaAddrs, mConfig);
sConfig_ = sConfig;
ioExecutor_ = std::make_shared<folly::IOThreadPoolExecutor>(std::thread::hardware_concurrency());
Expand Down Expand Up @@ -50,10 +53,7 @@ ScanEdgeIter StorageClient::scanEdgeWithPart(std::string spaceName,
int64_t endTime,
std::string filter,
bool onlyLatestVersion,
bool enableReadFromFollower,
bool needAuth,
const std::string& username,
const std::string& password) {
bool enableReadFromFollower) {
auto spaceIdResult = mClient_->getSpaceIdByNameFromCache(spaceName);
if (!spaceIdResult.first) {
return {nullptr, nullptr, false};
Expand Down Expand Up @@ -84,9 +84,9 @@ ScanEdgeIter StorageClient::scanEdgeWithPart(std::string spaceName,
req->set_filter(filter);
req->set_only_latest_version(onlyLatestVersion);
req->set_enable_read_from_follower(enableReadFromFollower);
req->set_need_authenticate(needAuth);
req->set_username(username);
req->set_password(password);
req->set_need_authenticate(true);
req->set_username(user_);
req->set_password(password_);

return {this, req};
}
Expand Down Expand Up @@ -124,10 +124,7 @@ ScanVertexIter StorageClient::scanVertexWithPart(
int64_t endTime,
std::string filter,
bool onlyLatestVersion,
bool enableReadFromFollower,
bool needAuth,
const std::string& username,
const std::string& password) {
bool enableReadFromFollower) {
auto spaceIdResult = mClient_->getSpaceIdByNameFromCache(spaceName);
if (!spaceIdResult.first) {
return {nullptr, nullptr, false};
Expand Down Expand Up @@ -164,9 +161,9 @@ ScanVertexIter StorageClient::scanVertexWithPart(
req->set_filter(filter);
req->set_only_latest_version(onlyLatestVersion);
req->set_enable_read_from_follower(enableReadFromFollower);
req->set_need_authenticate(needAuth);
req->set_username(username);
req->set_password(password);
req->set_need_authenticate(true);
req->set_username(user_);
req->set_password(password_);

return {this, req};
}
Expand Down
2 changes: 1 addition & 1 deletion src/sclient/tests/StorageClientSSLTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ TEST_F(StorageClientTest, SSL) {
prepare();
nebula::MConfig mConfig{1000, 60 * 1000, true, ""};
nebula::SConfig sConfig{1000, 60 * 1000, true, ""};
nebula::StorageClient c({kServerHost ":9559"}, mConfig, sConfig);
nebula::StorageClient c({kServerHost ":9559"}, "root", "nebula", mConfig, sConfig);
auto *m = c.getMetaClient();
LOG(INFO) << "Testing run once of meta client";
runOnce(*m);
Expand Down
2 changes: 1 addition & 1 deletion src/sclient/tests/StorageClientTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ class StorageClientTest : public SClientTest {
TEST_F(StorageClientTest, Basic) {
LOG(INFO) << "Prepare data.";
prepare();
nebula::StorageClient c({kServerHost ":9559"});
nebula::StorageClient c({kServerHost ":9559"}, "root", "nebula");
auto *m = c.getMetaClient();
LOG(INFO) << "Testing run once of meta client";
runOnce(*m);
Expand Down

0 comments on commit 03a5067

Please sign in to comment.